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 physics shape cast, AABB overlap, mass data, and joint state APIs

esengine54ac877ef78f
diff --git a/sdk/src/physics/PhysicsModuleLoader.ts b/sdk/src/physics/PhysicsModuleLoader.ts
index c31c7536c..46f35ce7b 100644
--- a/sdk/src/physics/PhysicsModuleLoader.ts
+++ b/sdk/src/physics/PhysicsModuleLoader.ts
@@ -120,6 +120,49 @@ export interface PhysicsWasmModule {
_physics_setAwake(entityId: number, awake: number): void;
_physics_isAwake(entityId: number): number;
+ _physics_shapeCastCircle(centerX: number, centerY: number, radius: number,
+ translationX: number, translationY: number, maskBits: number): number;
+ _physics_shapeCastBox(centerX: number, centerY: number, halfW: number, halfH: number, angle: number,
+ translationX: number, translationY: number, maskBits: number): number;
+ _physics_shapeCastCapsule(center1X: number, center1Y: number, center2X: number, center2Y: number,
+ radius: number, translationX: number, translationY: number, maskBits: number): number;
+ _physics_getShapeCastBuffer(): number;
+
+ _physics_overlapAABB(minX: number, minY: number, maxX: number, maxY: number, maskBits: number): number;
+
+ _physics_getBodyMass(entityId: number): number;
+ _physics_getBodyInertia(entityId: number): number;
+ _physics_getBodyCenterOfMass(entityId: number): number;
+
+ _physics_getDistanceJointLength(entityId: number): number;
+ _physics_getDistanceJointCurrentLength(entityId: number): number;
+ _physics_setDistanceJointLength(entityId: number, length: number): void;
+ _physics_enableDistanceJointSpring(entityId: number, enable: number): void;
+ _physics_enableDistanceJointLimit(entityId: number, enable: number): void;
+ _physics_setDistanceJointLimits(entityId: number, minLength: number, maxLength: number): void;
+ _physics_enableDistanceJointMotor(entityId: number, enable: number): void;
+ _physics_setDistanceJointMotorSpeed(entityId: number, speed: number): void;
+ _physics_setDistanceJointMaxMotorForce(entityId: number, force: number): void;
+ _physics_getDistanceJointMotorForce(entityId: number): number;
+
+ _physics_getPrismaticJointTranslation(entityId: number): number;
+ _physics_getPrismaticJointSpeed(entityId: number): number;
+ _physics_enablePrismaticJointSpring(entityId: number, enable: number): void;
+ _physics_enablePrismaticJointLimit(entityId: number, enable: number): void;
+ _physics_setPrismaticJointLimits(entityId: number, lower: number, upper: number): void;
+ _physics_enablePrismaticJointMotor(entityId: number, enable: number): void;
+ _physics_setPrismaticJointMotorSpeed(entityId: number, speed: number): void;
+ _physics_setPrismaticJointMaxMotorForce(entityId: number, force: number): void;
+ _physics_getPrismaticJointMotorForce(entityId: number): number;
+
+ _physics_enableWheelJointSpring(entityId: number, enable: number): void;
+ _physics_enableWheelJointLimit(entityId: number, enable: number): void;
+ _physics_setWheelJointLimits(entityId: number, lower: number, upper: number): void;
+ _physics_enableWheelJointMotor(entityId: number, enable: number): void;
+ _physics_setWheelJointMotorSpeed(entityId: number, speed: number): void;
+ _physics_setWheelJointMaxMotorTorque(entityId: number, torque: number): void;
+ _physics_getWheelJointMotorTorque(entityId: number): number;
+
HEAPF32: Float32Array;
HEAPU8: Uint8Array;
HEAPU32: Uint32Array;
@@ -233,6 +276,46 @@ export async function loadPhysicsSideModule(
_physics_setAwake: cwrap('physics_setAwake', null, ['number', 'number']) as PhysicsWasmModule['_physics_setAwake'],
_physics_isAwake: cwrap('physics_isAwake', 'number', ['number']) as PhysicsWasmModule['_physics_isAwake'],
+ _physics_shapeCastCircle: cwrap('physics_shapeCastCircle', 'number', ['number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_shapeCastCircle'],
+ _physics_shapeCastBox: cwrap('physics_shapeCastBox', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_shapeCastBox'],
+ _physics_shapeCastCapsule: cwrap('physics_shapeCastCapsule', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_shapeCastCapsule'],
+ _physics_getShapeCastBuffer: cwrap('physics_getShapeCastBuffer', 'number', []) as PhysicsWasmModule['_physics_getShapeCastBuffer'],
+
+ _physics_overlapAABB: cwrap('physics_overlapAABB', 'number', ['number', 'number', 'number', 'number', 'number']) as PhysicsWasmModule['_physics_overlapAABB'],
+
+ _physics_getBodyMass: cwrap('physics_getBodyMass', 'number', ['number']) as PhysicsWasmModule['_physics_getBodyMass'],
+ _physics_getBodyInertia: cwrap('physics_getBodyInertia', 'number', ['number']) as PhysicsWasmModule['_physics_getBodyInertia'],
+ _physics_getBodyCenterOfMass: cwrap('physics_getBodyCenterOfMass', 'number', ['number']) as PhysicsWasmModule['_physics_getBodyCenterOfMass'],
+
+ _physics_getDistanceJointLength: cwrap('physics_getDistanceJointLength', 'number', ['number']) as PhysicsWasmModule['_physics_getDistanceJointLength'],
+ _physics_getDistanceJointCurrentLength: cwrap('physics_getDistanceJointCurrentLength', 'number', ['number']) as PhysicsWasmModule['_physics_getDistanceJointCurrentLength'],
+ _physics_setDistanceJointLength: cwrap('physics_setDistanceJointLength', null, ['number', 'number']) as PhysicsWasmModule['_physics_setDistanceJointLength'],
+ _physics_enableDistanceJointSpring: cwrap('physics_enableDistanceJointSpring', null, ['number', 'number']) as PhysicsWasmModule['_physics_enableDistanceJointSpring'],
+ _physics_enableDistanceJointLimit: cwrap('physics_enableDistanceJointLimit', null, ['number', 'number']) as PhysicsWasmModule['_physics_enableDistanceJointLimit'],
+ _physics_setDistanceJointLimits: cwrap('physics_setDistanceJointLimits', null, ['number', 'number', 'number']) as PhysicsWasmModule['_physics_setDistanceJointLimits'],
+ _physics_enableDistanceJointMotor: cwrap('physics_enableDistanceJointMotor', null, ['number', 'number']) as PhysicsWasmModule['_physics_enableDistanceJointMotor'],
+ _physics_setDistanceJointMotorSpeed: cwrap('physics_setDistanceJointMotorSpeed', null, ['number', 'number']) as PhysicsWasmModule['_physics_setDistanceJointMotorSpeed'],
+ _physics_setDistanceJointMaxMotorForce: cwrap('physics_setDistanceJointMaxMotorForce', null, ['number', 'number']) as PhysicsWasmModule['_physics_setDistanceJointMaxMotorForce'],
+ _physics_getDistanceJointMotorForce: cwrap('physics_getDistanceJointMotorForce', 'number', ['number']) as PhysicsWasmModule['_physics_getDistanceJointMotorForce'],
+
+ _physics_getPrismaticJointTranslation: cwrap('physics_getPrismaticJointTranslation', 'number', ['number']) as PhysicsWasmModule['_physics_getPrismaticJointTranslation'],
+ _physics_getPrismaticJointSpeed: cwrap('physics_getPrismaticJointSpeed', 'number', ['number']) as PhysicsWasmModule['_physics_getPrismaticJointSpeed'],
+ _physics_enablePrismaticJointSpring: cwrap('physics_enablePrismaticJointSpring', null, ['number', 'number']) as PhysicsWasmModule['_physics_enablePrismaticJointSpring'],
+ _physics_enablePrismaticJointLimit: cwrap('physics_enablePrismaticJointLimit', null, ['number', 'number']) as PhysicsWasmModule['_physics_enablePrismaticJointLimit'],
+ _physics_setPrismaticJointLimits: cwrap('physics_setPrismaticJointLimits', null, ['number', 'number', 'number']) as PhysicsWasmModule['_physics_setPrismaticJointLimits'],
+ _physics_enablePrismaticJointMotor: cwrap('physics_enablePrismaticJointMotor', null, ['number', 'number']) as PhysicsWasmModule['_physics_enablePrismaticJointMotor'],
+ _physics_setPrismaticJointMotorSpeed: cwrap('physics_setPrismaticJointMotorSpeed', null, ['number', 'number']) as PhysicsWasmModule['_physics_setPrismaticJointMotorSpeed'],
+ _physics_setPrismaticJointMaxMotorForce: cwrap('physics_setPrismaticJointMaxMotorForce', null, ['number', 'number']) as PhysicsWasmModule['_physics_setPrismaticJointMaxMotorForce'],
+ _physics_getPrismaticJointMotorForce: cwrap('physics_getPrismaticJointMotorForce', 'number', ['number']) as PhysicsWasmModule['_physics_getPrismaticJointMotorForce'],
+
+ _physics_enableWheelJointSpring: cwrap('physics_enableWheelJointSpring', null, ['number', 'number']) as PhysicsWasmModule['_physics_enableWheelJointSpring'],
+ _physics_enableWheelJointLimit: cwrap('physics_enableWheelJointLimit', null, ['number', 'number']) as PhysicsWasmModule['_physics_enableWheelJointLimit'],
+ _physics_setWheelJointLimits: cwrap('physics_setWheelJointLimits', null, ['number', 'number', 'number']) as PhysicsWasmModule['_physics_setWheelJointLimits'],
+ _physics_enableWheelJointMotor: cwrap('physics_enableWheelJointMotor', null, ['number', 'number']) as PhysicsWasmModule['_physics_enableWheelJointMotor'],
+ _physics_setWheelJointMotorSpeed: cwrap('physics_setWheelJointMotorSpeed', null, ['number', 'number']) as PhysicsWasmModule['_physics_setWheelJointMotorSpeed'],
+ _physics_setWheelJointMaxMotorTorque: cwrap('physics_setWheelJointMaxMotorTorque', null, ['number', 'number']) as PhysicsWasmModule['_physics_setWheelJointMaxMotorTorque'],
+ _physics_getWheelJointMotorTorque: cwrap('physics_getWheelJointMotorTorque', 'number', ['number']) as PhysicsWasmModule['_physics_getWheelJointMotorTorque'],
+
get HEAPF32() { return mainModule.HEAPF32; },
get HEAPU8() { return mainModule.HEAPU8; },
get HEAPU32() { return mainModule.HEAPU32; },
diff --git a/sdk/src/physics/PhysicsPlugin.ts b/sdk/src/physics/PhysicsPlugin.ts
index 01555147d..847d4e29e 100644
--- a/sdk/src/physics/PhysicsPlugin.ts
+++ b/sdk/src/physics/PhysicsPlugin.ts
@@ -80,6 +80,17 @@ export interface RaycastHit {
fraction: number;
}
+export type ShapeCastHit = RaycastHit;
+
+export interface MassData {
+ mass: number;
+ inertia: number;
+ centerOfMass: Vec2;
+}
+
+const COLLISION_EVENT_STRIDE = 6;
+const CAST_HIT_STRIDE = 6;
+
export const PhysicsAPI = defineResource<Physics>(null!, 'PhysicsAPI');
// =============================================================================
@@ -638,7 +649,7 @@ function collectEvents(app: App, module: PhysicsWasmModule, ppu: number): void {
if (enterCount > 0) {
const enterPtr = module._physics_getCollisionEnterBuffer() >> 2;
for (let i = 0; i < enterCount; i++) {
- const base = enterPtr + i * 6;
+ const base = enterPtr + i * COLLISION_EVENT_STRIDE;
collisionEnters.push({
entityA: module.HEAPU32[base] as Entity,
entityB: module.HEAPU32[base + 1] as Entity,
@@ -828,7 +839,7 @@ export class Physics {
const ptr = this.module_._physics_getRaycastBuffer() >> 2;
const results: RaycastHit[] = [];
for (let i = 0; i < count; i++) {
- const base = ptr + i * 6;
+ const base = ptr + i * CAST_HIT_STRIDE;
results.push({
entity: this.module_.HEAPU32[base] as Entity,
point: { x: this.module_.HEAPF32[base + 1] * ppu, y: this.module_.HEAPF32[base + 2] * ppu },
@@ -846,8 +857,196 @@ export class Physics {
center.x * invPpu, center.y * invPpu,
radius * invPpu, maskBits,
);
- if (count === 0) return [];
+ return this.readUniqueEntityBuffer_(count);
+ }
+
+ setAwake(entity: Entity, awake: boolean): void {
+ this.module_._physics_setAwake(entity, awake ? 1 : 0);
+ }
+
+ isAwake(entity: Entity): boolean {
+ return this.module_._physics_isAwake(entity) !== 0;
+ }
+
+ shapeCastCircle(
+ center: Vec2, radius: number, translation: Vec2,
+ maskBits = 0xFFFF, ppu = 100,
+ ): ShapeCastHit[] {
+ const invPpu = 1 / ppu;
+ const count = this.module_._physics_shapeCastCircle(
+ center.x * invPpu, center.y * invPpu, radius * invPpu,
+ translation.x * invPpu, translation.y * invPpu, maskBits,
+ );
+ return this.readShapeCastBuffer_(count, ppu);
+ }
+
+ shapeCastBox(
+ center: Vec2, halfExtents: Vec2, angle: number, translation: Vec2,
+ maskBits = 0xFFFF, ppu = 100,
+ ): ShapeCastHit[] {
+ const invPpu = 1 / ppu;
+ const count = this.module_._physics_shapeCastBox(
+ center.x * invPpu, center.y * invPpu,
+ halfExtents.x * invPpu, halfExtents.y * invPpu, angle,
+ translation.x * invPpu, translation.y * invPpu, maskBits,
+ );
+ return this.readShapeCastBuffer_(count, ppu);
+ }
+
+ shapeCastCapsule(
+ center1: Vec2, center2: Vec2, radius: number, translation: Vec2,
+ maskBits = 0xFFFF, ppu = 100,
+ ): ShapeCastHit[] {
+ const invPpu = 1 / ppu;
+ const count = this.module_._physics_shapeCastCapsule(
+ center1.x * invPpu, center1.y * invPpu,
+ center2.x * invPpu, center2.y * invPpu,
+ radius * invPpu,
+ translation.x * invPpu, translation.y * invPpu, maskBits,
+ );
+ return this.readShapeCastBuffer_(count, ppu);
+ }
+
+ overlapAABB(min: Vec2, max: Vec2, maskBits = 0xFFFF, ppu = 100): Entity[] {
+ const invPpu = 1 / ppu;
+ const count = this.module_._physics_overlapAABB(
+ min.x * invPpu, min.y * invPpu,
+ max.x * invPpu, max.y * invPpu, maskBits,
+ );
+ return this.readUniqueEntityBuffer_(count);
+ }
+
+ getMass(entity: Entity): number {
+ return this.module_._physics_getBodyMass(entity);
+ }
+ getInertia(entity: Entity): number {
+ return this.module_._physics_getBodyInertia(entity);
+ }
+
+ getCenterOfMass(entity: Entity, ppu = 100): Vec2 {
+ const ptr = this.module_._physics_getBodyCenterOfMass(entity);
+ const base = ptr >> 2;
+ return {
+ x: this.module_.HEAPF32[base] * ppu,
+ y: this.module_.HEAPF32[base + 1] * ppu,
+ };
+ }
+
+ getMassData(entity: Entity, ppu = 100): MassData {
+ return {
+ mass: this.getMass(entity),
+ inertia: this.getInertia(entity),
+ centerOfMass: this.getCenterOfMass(entity, ppu),
+ };
+ }
+
+ getDistanceJointLength(entity: Entity, ppu = 100): number {
+ return this.module_._physics_getDistanceJointLength(entity) * ppu;
+ }
+
+ getDistanceJointCurrentLength(entity: Entity, ppu = 100): number {
+ return this.module_._physics_getDistanceJointCurrentLength(entity) * ppu;
+ }
+
+ setDistanceJointLength(entity: Entity, length: number, ppu = 100): void {
+ this.module_._physics_setDistanceJointLength(entity, length / ppu);
+ }
+
+ enableDistanceJointSpring(entity: Entity, enable: boolean): void {
+ this.module_._physics_enableDistanceJointSpring(entity, enable ? 1 : 0);
+ }
+
+ enableDistanceJointLimit(entity: Entity, enable: boolean): void {
+ this.module_._physics_enableDistanceJointLimit(entity, enable ? 1 : 0);
+ }
+
+ setDistanceJointLimits(entity: Entity, minLength: number, maxLength: number, ppu = 100): void {
+ this.module_._physics_setDistanceJointLimits(entity, minLength / ppu, maxLength / ppu);
+ }
+
+ enableDistanceJointMotor(entity: Entity, enable: boolean): void {
+ this.module_._physics_enableDistanceJointMotor(entity, enable ? 1 : 0);
+ }
+
+ setDistanceJointMotorSpeed(entity: Entity, speed: number): void {
+ this.module_._physics_setDistanceJointMotorSpeed(entity, speed);
+ }
+
+ setDistanceJointMaxMotorForce(entity: Entity, force: number): void {
+ this.module_._physics_setDistanceJointMaxMotorForce(entity, force);
+ }
+
+ getDistanceJointMotorForce(entity: Entity): number {
+ return this.module_._physics_getDistanceJointMotorForce(entity);
+ }
+
+ getPrismaticJointTranslation(entity: Entity, ppu = 100): number {
+ return this.module_._physics_getPrismaticJointTranslation(entity) * ppu;
+ }
+
+ getPrismaticJointSpeed(entity: Entity, ppu = 100): number {
+ return this.module_._physics_getPrismaticJointSpeed(entity) * ppu;
+ }
+
+ enablePrismaticJointSpring(entity: Entity, enable: boolean): void {
+ this.module_._physics_enablePrismaticJointSpring(entity, enable ? 1 : 0);
+ }
+
+ enablePrismaticJointLimit(entity: Entity, enable: boolean): void {
+ this.module_._physics_enablePrismaticJointLimit(entity, enable ? 1 : 0);
+ }
+
+ setPrismaticJointLimits(entity: Entity, lower: number, upper: number, ppu = 100): void {
+ this.module_._physics_setPrismaticJointLimits(entity, lower / ppu, upper / ppu);
+ }
+
+ enablePrismaticJointMotor(entity: Entity, enable: boolean): void {
+ this.module_._physics_enablePrismaticJointMotor(entity, enable ? 1 : 0);
+ }
+
+ setPrismaticJointMotorSpeed(entity: Entity, speed: number): void {
+ this.module_._physics_setPrismaticJointMotorSpeed(entity, speed);
+ }
+
+ setPrismaticJointMaxMotorForce(entity: Entity, force: number): void {
+ this.module_._physics_setPrismaticJointMaxMotorForce(entity, force);
+ }
+
+ getPrismaticJointMotorForce(entity: Entity): number {
+ return this.module_._physics_getPrismaticJointMotorForce(entity);
+ }
+
+ enableWheelJointSpring(entity: Entity, enable: boolean): void {
+ this.module_._physics_enableWheelJointSpring(entity, enable ? 1 : 0);
+ }
+
+ enableWheelJointLimit(entity: Entity, enable: boolean): void {
+ this.module_._physics_enableWheelJointLimit(entity, enable ? 1 : 0);
+ }
+
+ setWheelJointLimits(entity: Entity, lower: number, upper: number, ppu = 100): void {
+ this.module_._physics_setWheelJointLimits(entity, lower / ppu, upper / ppu);
+ }
+
+ enableWheelJointMotor(entity: Entity, enable: boolean): void {
+ this.module_._physics_enableWheelJointMotor(entity, enable ? 1 : 0);
+ }
+
+ setWheelJointMotorSpeed(entity: Entity, speed: number): void {
+ this.module_._physics_setWheelJointMotorSpeed(entity, speed);
+ }
+
+ setWheelJointMaxMotorTorque(entity: Entity, torque: number): void {
+ this.module_._physics_setWheelJointMaxMotorTorque(entity, torque);
+ }
+
+ getWheelJointMotorTorque(entity: Entity): number {
+ return this.module_._physics_getWheelJointMotorTorque(entity);
+ }
+
+ private readUniqueEntityBuffer_(count: number): Entity[] {
+ if (count === 0) return [];
const ptr = this.module_._physics_getOverlapBuffer() >> 2;
const results: Entity[] = [];
const seen = new Set<number>();
@@ -861,12 +1060,21 @@ export class Physics {
return results;
}
- setAwake(entity: Entity, awake: boolean): void {
- this.module_._physics_setAwake(entity, awake ? 1 : 0);
- }
-
- isAwake(entity: Entity): boolean {
- return this.module_._physics_isAwake(entity) !== 0;
+ private readShapeCastBuffer_(count: number, ppu: number): ShapeCastHit[] {
+ if (count === 0) return [];
+ const ptr = this.module_._physics_getShapeCastBuffer() >> 2;
+ const results: ShapeCastHit[] = [];
+ for (let i = 0; i < count; i++) {
+ const base = ptr + i * CAST_HIT_STRIDE;
+ results.push({
+ entity: this.module_.HEAPU32[base] as Entity,
+ point: { x: this.module_.HEAPF32[base + 1] * ppu, y: this.module_.HEAPF32[base + 2] * ppu },
+ normal: { x: this.module_.HEAPF32[base + 3], y: this.module_.HEAPF32[base + 4] },
+ fraction: this.module_.HEAPF32[base + 5],
+ });
+ }
+ results.sort((a, b) => a.fraction - b.fraction);
+ return results;
}
static setDebugDraw(app: App, enabled: boolean): void {
diff --git a/sdk/src/physics/index.ts b/sdk/src/physics/index.ts
index 6953b5500..caa4c9a31 100644
--- a/sdk/src/physics/index.ts
+++ b/sdk/src/physics/index.ts
@@ -19,6 +19,8 @@ export {
type CollisionEnterEvent,
type SensorEvent,
type RaycastHit,
+ type ShapeCastHit,
+ type MassData,
} from './PhysicsPlugin';
export {
diff --git a/src/esengine/bindings/PhysicsModuleEntry.cpp b/src/esengine/bindings/PhysicsModuleEntry.cpp
index 6fd17de17..542623103 100644
--- a/src/esengine/bindings/PhysicsModuleEntry.cpp
+++ b/src/esengine/bindings/PhysicsModuleEntry.cpp
@@ -73,6 +73,25 @@ static constexpr int MAX_RAYCAST_HITS = 64;
static std::vector<float> g_overlapBuffer;
static constexpr int MAX_OVERLAP_HITS = 64;
+static std::vector<float> g_shapeCastBuffer;
+static constexpr int SHAPECAST_STRIDE = 6;
+
+static float g_massDataBuffer[2] = {};
+
+static b2JointId findValidJoint(uint32_t entityId) {
+ auto it = g_ctx.entityToJoint.find(entityId);
+ if (it == g_ctx.entityToJoint.end()) return b2_nullJointId;
+ if (!b2Joint_IsValid(it->second)) return b2_nullJointId;
+ return it->second;
+}
+
+static b2BodyId findValidBody(uint32_t entityId) {
+ auto it = g_ctx.entityToBody.find(entityId);
+ if (it == g_ctx.entityToBody.end()) return b2_nullBodyId;
+ if (!b2Body_IsValid(it->second)) return b2_nullBodyId;
+ return it->second;
+}
+
// =============================================================================
// Helper: Entity ID from body user data
// =============================================================================
@@ -1040,4 +1059,299 @@ int physics_isAwake(uint32_t entityId) {
return b2Body_IsAwake(it->second) ? 1 : 0;
}
+// Shape Cast
+
+static float shapeCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void* context) {
+ if (g_shapeCastBuffer.size() / SHAPECAST_STRIDE >= MAX_RAYCAST_HITS) return 0.0f;
+
+ uint32_t entityId = entityFromShape(shapeId);
+ if (entityId == 0xFFFFFFFF) return 1.0f;
+
+ pushEntityBits(g_shapeCastBuffer, entityId);
+ g_shapeCastBuffer.push_back(point.x);
+ g_shapeCastBuffer.push_back(point.y);
+ g_shapeCastBuffer.push_back(normal.x);
+ g_shapeCastBuffer.push_back(normal.y);
+ g_shapeCastBuffer.push_back(fraction);
+
+ return 1.0f;
+}
+
+EMSCRIPTEN_KEEPALIVE
+int physics_shapeCastCircle(float centerX, float centerY, float radius,
+ float translationX, float translationY, uint32_t maskBits) {
+ if (!b2World_IsValid(g_ctx.worldId)) return 0;
+
+ g_shapeCastBuffer.clear();
+
+ b2Vec2 center = {centerX, centerY};
+ b2ShapeProxy proxy = b2MakeProxy(&center, 1, radius);
+ b2Vec2 translation = {translationX, translationY};
+
+ b2QueryFilter filter = b2DefaultQueryFilter();
+ filter.maskBits = static_cast<uint64_t>(maskBits);
+
+ b2World_CastShape(g_ctx.worldId, &proxy, translation, filter, shapeCastCallback, nullptr);
+
+ return static_cast<int>(g_shapeCastBuffer.size() / SHAPECAST_STRIDE);
+}
+
+EMSCRIPTEN_KEEPALIVE
+int physics_shapeCastBox(float centerX, float centerY, float halfW, float halfH, float angle,
+ float translationX, float translationY, uint32_t maskBits) {
+ if (!b2World_IsValid(g_ctx.worldId)) return 0;
+
+ g_shapeCastBuffer.clear();
+
+ b2Polygon box = b2MakeOffsetBox(halfW, halfH, {centerX, centerY}, b2MakeRot(angle));
+ b2ShapeProxy proxy = b2MakeProxy(box.vertices, box.count, box.radius);
+ b2Vec2 translation = {translationX, translationY};
+
+ b2QueryFilter filter = b2DefaultQueryFilter();
+ filter.maskBits = static_cast<uint64_t>(maskBits);
+
+ b2World_CastShape(g_ctx.worldId, &proxy, translation, filter, shapeCastCallback, nullptr);
+
+ return static_cast<int>(g_shapeCastBuffer.size() / SHAPECAST_STRIDE);
+}
+
+EMSCRIPTEN_KEEPALIVE
+int physics_shapeCastCapsule(float center1X, float center1Y, float center2X, float center2Y,
+ float radius, float translationX, float translationY, uint32_t maskBits) {
+ if (!b2World_IsValid(g_ctx.worldId)) return 0;
+
+ g_shapeCastBuffer.clear();
+
+ b2Vec2 centers[2] = {{center1X, center1Y}, {center2X, center2Y}};
+ b2ShapeProxy proxy = b2MakeProxy(centers, 2, radius);
+ b2Vec2 translation = {translationX, translationY};
+
+ b2QueryFilter filter = b2DefaultQueryFilter();
+ filter.maskBits = static_cast<uint64_t>(maskBits);
+
+ b2World_CastShape(g_ctx.worldId, &proxy, translation, filter, shapeCastCallback, nullptr);
+
+ return static_cast<int>(g_shapeCastBuffer.size() / SHAPECAST_STRIDE);
+}
+
+EMSCRIPTEN_KEEPALIVE
+uintptr_t physics_getShapeCastBuffer() {
+ return reinterpret_cast<uintptr_t>(g_shapeCastBuffer.data());
+}
+
+// AABB Overlap
+
+EMSCRIPTEN_KEEPALIVE
+int physics_overlapAABB(float minX, float minY, float maxX, float maxY, uint32_t maskBits) {
+ if (!b2World_IsValid(g_ctx.worldId)) return 0;
+
+ g_overlapBuffer.clear();
+
+ b2AABB aabb = {{minX, minY}, {maxX, maxY}};
+ b2QueryFilter filter = b2DefaultQueryFilter();
+ filter.maskBits = static_cast<uint64_t>(maskBits);
+
+ b2World_OverlapAABB(g_ctx.worldId, aabb, filter, overlapCallback, nullptr);
+
+ return static_cast<int>(g_overlapBuffer.size());
+}
+
+// Body Mass Queries
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getBodyMass(uint32_t entityId) {
+ auto body = findValidBody(entityId);
+ if (B2_IS_NULL(body)) return 0;
+ return b2Body_GetMass(body);
+}
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getBodyInertia(uint32_t entityId) {
+ auto body = findValidBody(entityId);
+ if (B2_IS_NULL(body)) return 0;
+ return b2Body_GetRotationalInertia(body);
+}
+
+EMSCRIPTEN_KEEPALIVE
+uintptr_t physics_getBodyCenterOfMass(uint32_t entityId) {
+ g_massDataBuffer[0] = 0;
+ g_massDataBuffer[1] = 0;
+ auto body = findValidBody(entityId);
+ if (!B2_IS_NULL(body)) {
+ b2Vec2 com = b2Body_GetLocalCenterOfMass(body);
+ g_massDataBuffer[0] = com.x;
+ g_massDataBuffer[1] = com.y;
+ }
+ return reinterpret_cast<uintptr_t>(g_massDataBuffer);
+}
+
+// Joint State Queries — Distance Joint
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getDistanceJointLength(uint32_t entityId) {
+ auto jid = findValidJoint(entityId);
+ if (B2_IS_NULL(jid)) return 0;
+ return b2DistanceJoint_GetLength(jid);
+}
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getDistanceJointCurrentLength(uint32_t entityId) {
+ auto jid = findValidJoint(entityId);
+ if (B2_IS_NULL(jid)) return 0;
+ return b2DistanceJoint_GetCurrentLength(jid);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setDistanceJointLength(uint32_t entityId, float length) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2DistanceJoint_SetLength(jid, length);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enableDistanceJointSpring(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableSpring(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enableDistanceJointLimit(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableLimit(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setDistanceJointLimits(uint32_t entityId, float minLength, float maxLength) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2DistanceJoint_SetLengthRange(jid, minLength, maxLength);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enableDistanceJointMotor(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableMotor(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setDistanceJointMotorSpeed(uint32_t entityId, float speed) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2DistanceJoint_SetMotorSpeed(jid, speed);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setDistanceJointMaxMotorForce(uint32_t entityId, float force) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2DistanceJoint_SetMaxMotorForce(jid, force);
+}
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getDistanceJointMotorForce(uint32_t entityId) {
+ auto jid = findValidJoint(entityId);
+ if (B2_IS_NULL(jid)) return 0;
+ return b2DistanceJoint_GetMotorForce(jid);
+}
+
+// Joint State Queries — Prismatic Joint
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getPrismaticJointTranslation(uint32_t entityId) {
+ auto jid = findValidJoint(entityId);
+ if (B2_IS_NULL(jid)) return 0;
+ return b2PrismaticJoint_GetTranslation(jid);
+}
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getPrismaticJointSpeed(uint32_t entityId) {
+ auto jid = findValidJoint(entityId);
+ if (B2_IS_NULL(jid)) return 0;
+ return b2PrismaticJoint_GetSpeed(jid);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enablePrismaticJointSpring(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableSpring(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enablePrismaticJointLimit(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableLimit(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setPrismaticJointLimits(uint32_t entityId, float lower, float upper) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetLimits(jid, lower, upper);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enablePrismaticJointMotor(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableMotor(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setPrismaticJointMotorSpeed(uint32_t entityId, float speed) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetMotorSpeed(jid, speed);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setPrismaticJointMaxMotorForce(uint32_t entityId, float force) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetMaxMotorForce(jid, force);
+}
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getPrismaticJointMotorForce(uint32_t entityId) {
+ auto jid = findValidJoint(entityId);
+ if (B2_IS_NULL(jid)) return 0;
+ return b2PrismaticJoint_GetMotorForce(jid);
+}
+
+// Joint State Queries — Wheel Joint
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enableWheelJointSpring(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2WheelJoint_EnableSpring(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enableWheelJointLimit(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2WheelJoint_EnableLimit(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setWheelJointLimits(uint32_t entityId, float lower, float upper) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2WheelJoint_SetLimits(jid, lower, upper);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_enableWheelJointMotor(uint32_t entityId, int enable) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2WheelJoint_EnableMotor(jid, enable != 0);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setWheelJointMotorSpeed(uint32_t entityId, float speed) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2WheelJoint_SetMotorSpeed(jid, speed);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void physics_setWheelJointMaxMotorTorque(uint32_t entityId, float torque) {
+ auto jid = findValidJoint(entityId);
+ if (!B2_IS_NULL(jid)) b2WheelJoint_SetMaxMotorTorque(jid, torque);
+}
+
+EMSCRIPTEN_KEEPALIVE
+float physics_getWheelJointMotorTorque(uint32_t entityId) {
+ auto jid = findValidJoint(entityId);
+ if (B2_IS_NULL(jid)) return 0;
+ return b2WheelJoint_GetMotorTorque(jid);
+}
+
} // extern "C"