Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: add segment, polygon and chain collider types with debug draw

esenginef73b29b826ba
diff --git a/sdk/src/physics/PhysicsComponents.ts b/sdk/src/physics/PhysicsComponents.ts
index 10433f441..465e1324a 100644
--- a/sdk/src/physics/PhysicsComponents.ts
+++ b/sdk/src/physics/PhysicsComponents.ts
@@ -4,7 +4,7 @@
*/
import type { Vec2 } from '../types';
-import { defineBuiltin } from '../component';
+import { defineBuiltin, defineComponent } from '../component';
// =============================================================================
// Component Data Interfaces
@@ -23,6 +23,7 @@ export interface RigidBodyData {
export interface BoxColliderData {
halfExtents: Vec2;
offset: Vec2;
+ radius: number;
density: number;
friction: number;
restitution: number;
@@ -57,6 +58,40 @@ export interface CapsuleColliderData {
maskBits: number;
}
+export interface SegmentColliderData {
+ point1: Vec2;
+ point2: Vec2;
+ density: number;
+ friction: number;
+ restitution: number;
+ isSensor: boolean;
+ enabled: boolean;
+ categoryBits: number;
+ maskBits: number;
+}
+
+export interface PolygonColliderData {
+ vertices: Vec2[];
+ radius: number;
+ density: number;
+ friction: number;
+ restitution: number;
+ isSensor: boolean;
+ enabled: boolean;
+ categoryBits: number;
+ maskBits: number;
+}
+
+export interface ChainColliderData {
+ points: Vec2[];
+ isLoop: boolean;
+ friction: number;
+ restitution: number;
+ categoryBits: number;
+ maskBits: number;
+ enabled: boolean;
+}
+
// =============================================================================
// Builtin Component Instances
// =============================================================================
@@ -74,6 +109,7 @@ export const RigidBody = defineBuiltin<RigidBodyData>('RigidBody', {
export const BoxCollider = defineBuiltin<BoxColliderData>('BoxCollider', {
halfExtents: { x: 0.5, y: 0.5 },
offset: { x: 0, y: 0 },
+ radius: 0.05,
density: 1.0,
friction: 0.3,
restitution: 0.0,
@@ -108,6 +144,50 @@ export const CapsuleCollider = defineBuiltin<CapsuleColliderData>('CapsuleCollid
maskBits: 0xFFFF,
});
+export const SegmentCollider = defineBuiltin<SegmentColliderData>('SegmentCollider', {
+ point1: { x: -0.5, y: 0 },
+ point2: { x: 0.5, y: 0 },
+ density: 1.0,
+ friction: 0.3,
+ restitution: 0.0,
+ isSensor: false,
+ enabled: true,
+ categoryBits: 0x0001,
+ maskBits: 0xFFFF,
+});
+
+export const PolygonCollider = defineComponent<PolygonColliderData>('PolygonCollider', {
+ vertices: [
+ { x: -0.5, y: -0.5 },
+ { x: 0.5, y: -0.5 },
+ { x: 0.5, y: 0.5 },
+ { x: -0.5, y: 0.5 },
+ ],
+ radius: 0.0,
+ density: 1.0,
+ friction: 0.3,
+ restitution: 0.0,
+ isSensor: false,
+ enabled: true,
+ categoryBits: 0x0001,
+ maskBits: 0xFFFF,
+});
+
+export const ChainCollider = defineComponent<ChainColliderData>('ChainCollider', {
+ points: [
+ { x: -1, y: 0 },
+ { x: 0, y: 0.5 },
+ { x: 1, y: 0 },
+ { x: 0, y: -0.5 },
+ ],
+ isLoop: true,
+ friction: 0.6,
+ restitution: 0.0,
+ categoryBits: 0x0001,
+ maskBits: 0xFFFF,
+ enabled: true,
+});
+
// =============================================================================
// Body Type Enum (matches C++ BodyType)
// =============================================================================
diff --git a/sdk/src/physics/PhysicsDebugDraw.ts b/sdk/src/physics/PhysicsDebugDraw.ts
index 7068799e2..45b5a4d91 100644
--- a/sdk/src/physics/PhysicsDebugDraw.ts
+++ b/sdk/src/physics/PhysicsDebugDraw.ts
@@ -2,13 +2,19 @@ import type { App } from '../app';
import type { Color } from '../types';
import type { TransformData, CanvasData } from '../component';
import type { ResourceDef } from '../resource';
-import type { RigidBodyData, BoxColliderData, CircleColliderData, CapsuleColliderData } from './PhysicsComponents';
+import type {
+ RigidBodyData, BoxColliderData, CircleColliderData, CapsuleColliderData,
+ SegmentColliderData, PolygonColliderData, ChainColliderData,
+} from './PhysicsComponents';
import type { PhysicsEventsData } from './PhysicsPlugin';
import { Transform, Canvas } from '../component';
import { Draw } from '../draw';
import { defineResource } from '../resource';
import { registerDrawCallback } from '../customDraw';
-import { RigidBody, BoxCollider, CircleCollider, CapsuleCollider, BodyType } from './PhysicsComponents';
+import {
+ RigidBody, BoxCollider, CircleCollider, CapsuleCollider,
+ SegmentCollider, PolygonCollider, ChainCollider, BodyType,
+} from './PhysicsComponents';
export interface PhysicsDebugDrawConfig {
enabled: boolean;
@@ -228,6 +234,53 @@ export function drawPhysicsDebug(
const cy = wy + offsetX * sinA + offsetY * cosA;
drawCapsule(cx, cy, capsule.radius * ppu, capsule.halfHeight * ppu, angle, baseColor);
}
+
+ if (app.world.has(entity, SegmentCollider)) {
+ const seg = app.world.get(entity, SegmentCollider) as SegmentColliderData;
+ const baseColor = seg.isSensor ? SENSOR_COLOR : bodyTypeColor(rb.bodyType);
+ const cosA = Math.cos(angle);
+ const sinA = Math.sin(angle);
+ const p1x = wx + (seg.point1.x * ppu * cosA - seg.point1.y * ppu * sinA);
+ const p1y = wy + (seg.point1.x * ppu * sinA + seg.point1.y * ppu * cosA);
+ const p2x = wx + (seg.point2.x * ppu * cosA - seg.point2.y * ppu * sinA);
+ const p2y = wy + (seg.point2.x * ppu * sinA + seg.point2.y * ppu * cosA);
+ Draw.line({ x: p1x, y: p1y }, { x: p2x, y: p2y }, baseColor, DEBUG_LINE_THICKNESS);
+ }
+
+ if (app.world.has(entity, PolygonCollider)) {
+ const poly = app.world.get(entity, PolygonCollider) as PolygonColliderData;
+ const baseColor = poly.isSensor ? SENSOR_COLOR : bodyTypeColor(rb.bodyType);
+ const cosA = Math.cos(angle);
+ const sinA = Math.sin(angle);
+ const verts = poly.vertices;
+ for (let vi = 0; vi < verts.length; vi++) {
+ const v0 = verts[vi];
+ const v1 = verts[(vi + 1) % verts.length];
+ const x0 = wx + (v0.x * ppu * cosA - v0.y * ppu * sinA);
+ const y0 = wy + (v0.x * ppu * sinA + v0.y * ppu * cosA);
+ const x1 = wx + (v1.x * ppu * cosA - v1.y * ppu * sinA);
+ const y1 = wy + (v1.x * ppu * sinA + v1.y * ppu * cosA);
+ Draw.line({ x: x0, y: y0 }, { x: x1, y: y1 }, baseColor, DEBUG_LINE_THICKNESS);
+ }
+ }
+
+ if (app.world.has(entity, ChainCollider)) {
+ const chain = app.world.get(entity, ChainCollider) as ChainColliderData;
+ const baseColor = bodyTypeColor(rb.bodyType);
+ const cosA = Math.cos(angle);
+ const sinA = Math.sin(angle);
+ const pts = chain.points;
+ const end = chain.isLoop ? pts.length : pts.length - 1;
+ for (let pi = 0; pi < end; pi++) {
+ const p0 = pts[pi];
+ const p1 = pts[(pi + 1) % pts.length];
+ const x0 = wx + (p0.x * ppu * cosA - p0.y * ppu * sinA);
+ const y0 = wy + (p0.x * ppu * sinA + p0.y * ppu * cosA);
+ const x1 = wx + (p1.x * ppu * cosA - p1.y * ppu * sinA);
+ const y1 = wy + (p1.x * ppu * sinA + p1.y * ppu * cosA);
+ Draw.line({ x: x0, y: y0 }, { x: x1, y: y1 }, baseColor, DEBUG_LINE_THICKNESS);
+ }
+ }
}
if (config.showVelocity && physics && rb.bodyType === BodyType.Dynamic) {
diff --git a/sdk/src/physics/PhysicsModuleLoader.ts b/sdk/src/physics/PhysicsModuleLoader.ts
index a9a7c91ce..70d48aba9 100644
--- a/sdk/src/physics/PhysicsModuleLoader.ts
+++ b/sdk/src/physics/PhysicsModuleLoader.ts
@@ -15,7 +15,7 @@ export interface PhysicsWasmModule {
_physics_hasBody(entityId: number): number;
_physics_addBoxShape(entityId: number, halfW: number, halfH: number,
- offX: number, offY: number,
+ offX: number, offY: number, radius: number,
density: number, friction: number, restitution: number, isSensor: number,
categoryBits: number, maskBits: number): void;
_physics_addCircleShape(entityId: number, radius: number,
@@ -27,6 +27,16 @@ export interface PhysicsWasmModule {
density: number, friction: number, restitution: number, isSensor: number,
categoryBits: number, maskBits: number): void;
+ _physics_addSegmentShape(entityId: number, x1: number, y1: number, x2: number, y2: number,
+ density: number, friction: number, restitution: number, isSensor: number,
+ categoryBits: number, maskBits: number): void;
+ _physics_addPolygonShape(entityId: number, verticesPtr: number, vertexCount: number, radius: number,
+ density: number, friction: number, restitution: number, isSensor: number,
+ categoryBits: number, maskBits: number): void;
+ _physics_addChainShape(entityId: number, pointsPtr: number, pointCount: number, isLoop: number,
+ friction: number, restitution: number,
+ categoryBits: number, maskBits: number): void;
+
_physics_step(dt: number): void;
_physics_setBodyTransform(entityId: number, x: number, y: number, angle: number): void;
@@ -111,9 +121,12 @@ export async function loadPhysicsSideModule(
_physics_destroyBody: cwrap('physics_destroyBody', null, ['number']) as PhysicsWasmModule['_physics_destroyBody'],
_physics_hasBody: cwrap('physics_hasBody', 'number', ['number']) as PhysicsWasmModule['_physics_hasBody'],
- _physics_addBoxShape: cwrap('physics_addBoxShape', null, ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_addBoxShape'],
+ _physics_addBoxShape: cwrap('physics_addBoxShape', null, ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_addBoxShape'],
_physics_addCircleShape: cwrap('physics_addCircleShape', null, ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_addCircleShape'],
_physics_addCapsuleShape: cwrap('physics_addCapsuleShape', null, ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_addCapsuleShape'],
+ _physics_addSegmentShape: cwrap('physics_addSegmentShape', null, ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_addSegmentShape'],
+ _physics_addPolygonShape: cwrap('physics_addPolygonShape', null, ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_addPolygonShape'],
+ _physics_addChainShape: cwrap('physics_addChainShape', null, ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_addChainShape'],
_physics_step: cwrap('physics_step', null, ['number']) as PhysicsWasmModule['_physics_step'],
diff --git a/sdk/src/physics/PhysicsPlugin.ts b/sdk/src/physics/PhysicsPlugin.ts
index e584519bb..7dfdac9db 100644
--- a/sdk/src/physics/PhysicsPlugin.ts
+++ b/sdk/src/physics/PhysicsPlugin.ts
@@ -15,7 +15,14 @@ import {
type PhysicsWasmModule,
type PhysicsModuleFactory,
} from './PhysicsModuleLoader';
-import { RigidBody, BoxCollider, CircleCollider, CapsuleCollider, BodyType, type RigidBodyData, type BoxColliderData, type CircleColliderData, type CapsuleColliderData } from './PhysicsComponents';
+import {
+ RigidBody, BoxCollider, CircleCollider, CapsuleCollider,
+ SegmentCollider, PolygonCollider, ChainCollider,
+ BodyType,
+ type RigidBodyData, type BoxColliderData, type CircleColliderData,
+ type CapsuleColliderData, type SegmentColliderData, type PolygonColliderData,
+ type ChainColliderData,
+} from './PhysicsComponents';
import { setupPhysicsDebugDraw, PhysicsDebugDraw, type PhysicsDebugDrawConfig } from './PhysicsDebugDraw';
// =============================================================================
@@ -106,9 +113,9 @@ export class PhysicsPlugin implements Plugin {
gravity: config.gravity ?? { x: 0, y: -9.81 },
fixedTimestep: config.fixedTimestep ?? 1 / 60,
subStepCount: config.subStepCount ?? 4,
- contactHertz: config.contactHertz ?? 120,
+ contactHertz: config.contactHertz ?? 30,
contactDampingRatio: config.contactDampingRatio ?? 10,
- contactSpeed: config.contactSpeed ?? 10,
+ contactSpeed: config.contactSpeed ?? 3,
collisionLayerMasks: config.collisionLayerMasks,
};
}
@@ -273,7 +280,7 @@ function addShapeForEntity(app: App, module: PhysicsWasmModule, entity: Entity,
const mask = resolveCollisionMask(category, box.maskBits ?? 0xFFFF, layerMasks);
module._physics_addBoxShape(
entity, box.halfExtents.x, box.halfExtents.y,
- box.offset.x, box.offset.y,
+ box.offset.x, box.offset.y, box.radius ?? 0.05,
box.density, box.friction, box.restitution, box.isSensor ? 1 : 0,
category, mask
);
@@ -303,6 +310,60 @@ function addShapeForEntity(app: App, module: PhysicsWasmModule, entity: Entity,
capsule.density, capsule.friction, capsule.restitution, capsule.isSensor ? 1 : 0,
category, mask
);
+ return;
+ }
+
+ if (world.has(entity, SegmentCollider)) {
+ const seg = world.get(entity, SegmentCollider) as SegmentColliderData;
+ const category = seg.categoryBits ?? 0x0001;
+ const mask = resolveCollisionMask(category, seg.maskBits ?? 0xFFFF, layerMasks);
+ module._physics_addSegmentShape(
+ entity, seg.point1.x, seg.point1.y, seg.point2.x, seg.point2.y,
+ seg.density, seg.friction, seg.restitution, seg.isSensor ? 1 : 0,
+ category, mask
+ );
+ return;
+ }
+
+ if (world.has(entity, PolygonCollider)) {
+ const poly = world.get(entity, PolygonCollider) as PolygonColliderData;
+ const category = poly.categoryBits ?? 0x0001;
+ const mask = resolveCollisionMask(category, poly.maskBits ?? 0xFFFF, layerMasks);
+ const verts = poly.vertices;
+ const count = Math.min(verts.length, 8);
+ const byteSize = count * 2 * 4;
+ const ptr = module._malloc(byteSize);
+ const base = ptr >> 2;
+ for (let i = 0; i < count; i++) {
+ module.HEAPF32[base + i * 2] = verts[i].x;
+ module.HEAPF32[base + i * 2 + 1] = verts[i].y;
+ }
+ module._physics_addPolygonShape(
+ entity, ptr, count, poly.radius ?? 0,
+ poly.density, poly.friction, poly.restitution, poly.isSensor ? 1 : 0,
+ category, mask
+ );
+ module._free(ptr);
+ return;
+ }
+
+ if (world.has(entity, ChainCollider)) {
+ const chain = world.get(entity, ChainCollider) as ChainColliderData;
+ const pts = chain.points;
+ if (pts.length < 4) return;
+ const byteSize = pts.length * 2 * 4;
+ const ptr = module._malloc(byteSize);
+ const base = ptr >> 2;
+ for (let i = 0; i < pts.length; i++) {
+ module.HEAPF32[base + i * 2] = pts[i].x;
+ module.HEAPF32[base + i * 2 + 1] = pts[i].y;
+ }
+ module._physics_addChainShape(
+ entity, ptr, pts.length, chain.isLoop ? 1 : 0,
+ chain.friction, chain.restitution,
+ chain.categoryBits ?? 0x0001, chain.maskBits ?? 0xFFFF
+ );
+ module._free(ptr);
}
}
diff --git a/sdk/src/physics/index.ts b/sdk/src/physics/index.ts
index dce096243..e717f4a9d 100644
--- a/sdk/src/physics/index.ts
+++ b/sdk/src/physics/index.ts
@@ -33,11 +33,17 @@ export {
BoxCollider,
CircleCollider,
CapsuleCollider,
+ SegmentCollider,
+ PolygonCollider,
+ ChainCollider,
BodyType,
type RigidBodyData,
type BoxColliderData,
type CircleColliderData,
type CapsuleColliderData,
+ type SegmentColliderData,
+ type PolygonColliderData,
+ type ChainColliderData,
} from './PhysicsComponents';
export {
diff --git a/src/esengine/bindings/PhysicsModuleEntry.cpp b/src/esengine/bindings/PhysicsModuleEntry.cpp
index 92b3463f4..d53e49e34 100644
--- a/src/esengine/bindings/PhysicsModuleEntry.cpp
+++ b/src/esengine/bindings/PhysicsModuleEntry.cpp
@@ -26,6 +26,7 @@ static b2WorldId g_worldId = b2_nullWorldId;
static float g_fixedTimestep = 1.0f / 60.0f;
static int g_subStepCount = 4;
static float g_accumulator = 0.0f;
+static constexpr int MAX_PHYSICS_STEPS_PER_FRAME = 8;
static std::unordered_map<uint32_t, b2BodyId> g_entityToBody;
static std::unordered_map<uint32_t, b2ShapeId> g_entityToShape;
@@ -168,7 +169,7 @@ int physics_hasBody(uint32_t entityId) {
EMSCRIPTEN_KEEPALIVE
void physics_addBoxShape(uint32_t entityId, float halfW, float halfH,
- float offX, float offY,
+ float offX, float offY, float radius,
float density, float friction, float restitution, int isSensor,
uint32_t categoryBits, uint32_t maskBits) {
auto it = g_entityToBody.find(entityId);
@@ -184,7 +185,14 @@ void physics_addBoxShape(uint32_t entityId, float halfW, float halfH,
shapeDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
shapeDef.filter.maskBits = static_cast<uint64_t>(maskBits);
- b2Polygon polygon = b2MakeOffsetBox(halfW, halfH, {offX, offY}, b2MakeRot(0.0f));
+ b2Polygon polygon;
+ if (radius > 0.0f) {
+ float innerHalfW = halfW > radius ? halfW - radius : 0.0f;
+ float innerHalfH = halfH > radius ? halfH - radius : 0.0f;
+ polygon = b2MakeOffsetRoundedBox(innerHalfW, innerHalfH, {offX, offY}, b2MakeRot(0.0f), radius);
+ } else {
+ polygon = b2MakeOffsetBox(halfW, halfH, {offX, offY}, b2MakeRot(0.0f));
+ }
b2ShapeId shapeId = b2CreatePolygonShape(it->second, &shapeDef, &polygon);
g_entityToShape[entityId] = shapeId;
}
@@ -242,6 +250,95 @@ void physics_addCapsuleShape(uint32_t entityId, float radius, float halfHeight,
g_entityToShape[entityId] = shapeId;
}
+EMSCRIPTEN_KEEPALIVE
+void physics_addSegmentShape(uint32_t entityId, float x1, float y1, float x2, float y2,
+ float density, float friction, float restitution, int isSensor,
+ uint32_t categoryBits, uint32_t maskBits) {
+ auto it = g_entityToBody.find(entityId);
+ if (it == g_entityToBody.end()) return;
+
+ b2ShapeDef shapeDef = b2DefaultShapeDef();
+ shapeDef.density = density;
+ shapeDef.material.friction = friction;
+ shapeDef.material.restitution = restitution;
+ shapeDef.isSensor = isSensor != 0;
+ shapeDef.enableContactEvents = true;
+ shapeDef.enableSensorEvents = isSensor != 0;
+ shapeDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
+ shapeDef.filter.maskBits = static_cast<uint64_t>(maskBits);
+
+ b2Segment segment;
+ segment.point1 = {x1, y1};
+ segment.point2 = {x2, y2};
+
+ b2ShapeId shapeId = b2CreateSegmentShape(it->second, &shapeDef, &segment);
+ g_entityToShape[entityId] = shapeId;
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_addPolygonShape(uint32_t entityId, uintptr_t verticesPtr, int vertexCount, float radius,
+ float density, float friction, float restitution, int isSensor,
+ uint32_t categoryBits, uint32_t maskBits) {
+ auto it = g_entityToBody.find(entityId);
+ if (it == g_entityToBody.end()) return;
+ if (vertexCount < 3 || vertexCount > B2_MAX_POLYGON_VERTICES) return;
+
+ b2ShapeDef shapeDef = b2DefaultShapeDef();
+ shapeDef.density = density;
+ shapeDef.material.friction = friction;
+ shapeDef.material.restitution = restitution;
+ shapeDef.isSensor = isSensor != 0;
+ shapeDef.enableContactEvents = true;
+ shapeDef.enableSensorEvents = isSensor != 0;
+ shapeDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
+ shapeDef.filter.maskBits = static_cast<uint64_t>(maskBits);
+
+ auto* floats = reinterpret_cast<float*>(verticesPtr);
+ b2Vec2 points[B2_MAX_POLYGON_VERTICES];
+ for (int i = 0; i < vertexCount; i++) {
+ points[i] = {floats[i * 2], floats[i * 2 + 1]};
+ }
+
+ b2Hull hull = b2ComputeHull(points, vertexCount);
+ if (hull.count == 0) return;
+
+ b2Polygon polygon = (radius > 0.0f)
+ ? b2MakePolygon(&hull, radius)
+ : b2MakePolygon(&hull, 0.0f);
+ b2ShapeId shapeId = b2CreatePolygonShape(it->second, &shapeDef, &polygon);
+ g_entityToShape[entityId] = shapeId;
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_addChainShape(uint32_t entityId, uintptr_t pointsPtr, int pointCount, int isLoop,
+ float friction, float restitution,
+ uint32_t categoryBits, uint32_t maskBits) {
+ auto it = g_entityToBody.find(entityId);
+ if (it == g_entityToBody.end()) return;
+ if (pointCount < 4) return;
+
+ auto* floats = reinterpret_cast<float*>(pointsPtr);
+ std::vector<b2Vec2> points(pointCount);
+ for (int i = 0; i < pointCount; i++) {
+ points[i] = {floats[i * 2], floats[i * 2 + 1]};
+ }
+
+ b2SurfaceMaterial material = b2DefaultSurfaceMaterial();
+ material.friction = friction;
+ material.restitution = restitution;
+
+ b2ChainDef chainDef = b2DefaultChainDef();
+ chainDef.points = points.data();
+ chainDef.count = pointCount;
+ chainDef.isLoop = isLoop != 0;
+ chainDef.materials = &material;
+ chainDef.materialCount = 1;
+ chainDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
+ chainDef.filter.maskBits = static_cast<uint64_t>(maskBits);
+
+ b2CreateChain(it->second, &chainDef);
+}
+
// Simulation
EMSCRIPTEN_KEEPALIVE
@@ -250,9 +347,15 @@ void physics_step(float dt) {
g_accumulator += dt;
- while (g_accumulator >= g_fixedTimestep) {
+ int steps = 0;
+ while (g_accumulator >= g_fixedTimestep && steps < MAX_PHYSICS_STEPS_PER_FRAME) {
b2World_Step(g_worldId, g_fixedTimestep, g_subStepCount);
g_accumulator -= g_fixedTimestep;
+ ++steps;
+ }
+
+ if (g_accumulator > g_fixedTimestep) {
+ g_accumulator = g_fixedTimestep;
}
}
diff --git a/src/esengine/ecs/components/Collider.hpp b/src/esengine/ecs/components/Collider.hpp
index 86b4e9c85..8f931f1cf 100644
--- a/src/esengine/ecs/components/Collider.hpp
+++ b/src/esengine/ecs/components/Collider.hpp
@@ -105,4 +105,37 @@ struct CapsuleCollider {
CapsuleCollider() = default;
};
+ES_COMPONENT()
+struct SegmentCollider {
+ ES_PROPERTY()
+ glm::vec2 point1{-0.5f, 0.0f};
+
+ ES_PROPERTY()
+ glm::vec2 point2{0.5f, 0.0f};
+
+ ES_PROPERTY()
+ f32 density{1.0f};
+
+ ES_PROPERTY()
+ f32 friction{0.3f};
+
+ ES_PROPERTY()
+ f32 restitution{0.0f};
+
+ ES_PROPERTY()
+ bool isSensor{false};
+
+ ES_PROPERTY()
+ bool enabled{true};
+
+ ES_PROPERTY()
+ u32 categoryBits{0x0001};
+
+ ES_PROPERTY()
+ u32 maskBits{0xFFFF};
+
+ SegmentCollider() = default;
+};
+
+
} // namespace esengine::ecs