12 float anchorAx,
float anchorAy,
13 float anchorBx,
float anchorBy,
14 int enableMotor,
float motorSpeed,
float maxMotorTorque,
15 int enableLimit,
float lowerAngle,
float upperAngle,
16 int collideConnected) {
17 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
19 auto itA =
g_ctx.entityToBody.find(entityIdA);
20 auto itB =
g_ctx.entityToBody.find(entityIdB);
21 if (itA ==
g_ctx.entityToBody.end() || itB ==
g_ctx.entityToBody.end())
return 0;
22 if (!b2Body_IsValid(itA->second) || !b2Body_IsValid(itB->second))
return 0;
24 b2RevoluteJointDef jointDef = b2DefaultRevoluteJointDef();
25 jointDef.base.bodyIdA = itA->second;
26 jointDef.base.bodyIdB = itB->second;
27 jointDef.base.localFrameA.p = {anchorAx, anchorAy};
28 jointDef.base.localFrameB.p = {anchorBx, anchorBy};
29 jointDef.enableMotor = enableMotor != 0;
30 jointDef.motorSpeed = motorSpeed;
31 jointDef.maxMotorTorque = maxMotorTorque;
32 jointDef.enableLimit = enableLimit != 0;
33 jointDef.lowerAngle = lowerAngle;
34 jointDef.upperAngle = upperAngle;
35 jointDef.base.collideConnected = collideConnected != 0;
37 b2JointId jointId = b2CreateRevoluteJoint(
g_ctx.worldId, &jointDef);
38 g_ctx.entityToJoint[entityIdB] = jointId;
44 auto it =
g_ctx.entityToJoint.find(entityId);
45 if (it ==
g_ctx.entityToJoint.end())
return;
47 if (b2Joint_IsValid(it->second)) {
48 b2DestroyJoint(it->second,
true);
50 g_ctx.entityToJoint.erase(it);
55 auto it =
g_ctx.entityToJoint.find(entityId);
56 if (it ==
g_ctx.entityToJoint.end())
return;
57 if (!b2Joint_IsValid(it->second))
return;
58 b2RevoluteJoint_SetMotorSpeed(it->second, speed);
63 auto it =
g_ctx.entityToJoint.find(entityId);
64 if (it ==
g_ctx.entityToJoint.end())
return;
65 if (!b2Joint_IsValid(it->second))
return;
66 b2RevoluteJoint_SetMaxMotorTorque(it->second, torque);
71 auto it =
g_ctx.entityToJoint.find(entityId);
72 if (it ==
g_ctx.entityToJoint.end())
return;
73 if (!b2Joint_IsValid(it->second))
return;
74 b2RevoluteJoint_EnableMotor(it->second, enable != 0);
79 auto it =
g_ctx.entityToJoint.find(entityId);
80 if (it ==
g_ctx.entityToJoint.end())
return;
81 if (!b2Joint_IsValid(it->second))
return;
82 b2RevoluteJoint_EnableLimit(it->second, enable != 0);
87 auto it =
g_ctx.entityToJoint.find(entityId);
88 if (it ==
g_ctx.entityToJoint.end())
return;
89 if (!b2Joint_IsValid(it->second))
return;
90 b2RevoluteJoint_SetLimits(it->second, lower, upper);
95 auto it =
g_ctx.entityToJoint.find(entityId);
96 if (it ==
g_ctx.entityToJoint.end())
return 0;
97 if (!b2Joint_IsValid(it->second))
return 0;
98 return b2RevoluteJoint_GetAngle(it->second);
103 auto it =
g_ctx.entityToJoint.find(entityId);
104 if (it ==
g_ctx.entityToJoint.end())
return 0;
105 if (!b2Joint_IsValid(it->second))
return 0;
106 return b2RevoluteJoint_GetMotorTorque(it->second);
111 return g_ctx.entityToJoint.contains(entityId) ? 1 : 0;
116 float anchorAx,
float anchorAy,
117 float anchorBx,
float anchorBy,
118 float length,
int enableSpring,
float hertz,
float dampingRatio,
119 int enableLimit,
float minLength,
float maxLength,
120 int enableMotor,
float maxMotorForce,
float motorSpeed,
121 int collideConnected) {
122 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
124 auto itA =
g_ctx.entityToBody.find(entityIdA);
125 auto itB =
g_ctx.entityToBody.find(entityIdB);
126 if (itA ==
g_ctx.entityToBody.end() || itB ==
g_ctx.entityToBody.end())
return 0;
127 if (!b2Body_IsValid(itA->second) || !b2Body_IsValid(itB->second))
return 0;
129 b2DistanceJointDef jointDef = b2DefaultDistanceJointDef();
130 jointDef.base.bodyIdA = itA->second;
131 jointDef.base.bodyIdB = itB->second;
132 jointDef.base.localFrameA.p = {anchorAx, anchorAy};
133 jointDef.base.localFrameB.p = {anchorBx, anchorBy};
134 jointDef.length = length;
135 jointDef.enableSpring = enableSpring != 0;
136 jointDef.hertz = hertz;
137 jointDef.dampingRatio = dampingRatio;
138 jointDef.enableLimit = enableLimit != 0;
139 jointDef.minLength = minLength;
140 jointDef.maxLength = maxLength;
141 jointDef.enableMotor = enableMotor != 0;
142 jointDef.maxMotorForce = maxMotorForce;
143 jointDef.motorSpeed = motorSpeed;
144 jointDef.base.collideConnected = collideConnected != 0;
146 b2JointId jointId = b2CreateDistanceJoint(
g_ctx.worldId, &jointDef);
147 g_ctx.entityToJoint[entityIdB] = jointId;
153 float anchorAx,
float anchorAy,
154 float anchorBx,
float anchorBy,
155 float axisX,
float axisY,
156 int enableSpring,
float hertz,
float dampingRatio,
157 int enableLimit,
float lowerTranslation,
float upperTranslation,
158 int enableMotor,
float maxMotorForce,
float motorSpeed,
159 int collideConnected) {
160 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
162 auto itA =
g_ctx.entityToBody.find(entityIdA);
163 auto itB =
g_ctx.entityToBody.find(entityIdB);
164 if (itA ==
g_ctx.entityToBody.end() || itB ==
g_ctx.entityToBody.end())
return 0;
165 if (!b2Body_IsValid(itA->second) || !b2Body_IsValid(itB->second))
return 0;
167 b2PrismaticJointDef jointDef = b2DefaultPrismaticJointDef();
168 jointDef.base.bodyIdA = itA->second;
169 jointDef.base.bodyIdB = itB->second;
170 jointDef.base.localFrameA.p = {anchorAx, anchorAy};
171 jointDef.base.localFrameB.p = {anchorBx, anchorBy};
173 float len = sqrtf(axisX * axisX + axisY * axisY);
175 float nx = axisX / len;
176 float ny = axisY / len;
177 jointDef.base.localFrameA.q = {nx, ny};
180 jointDef.enableSpring = enableSpring != 0;
181 jointDef.hertz = hertz;
182 jointDef.dampingRatio = dampingRatio;
183 jointDef.enableLimit = enableLimit != 0;
184 jointDef.lowerTranslation = lowerTranslation;
185 jointDef.upperTranslation = upperTranslation;
186 jointDef.enableMotor = enableMotor != 0;
187 jointDef.maxMotorForce = maxMotorForce;
188 jointDef.motorSpeed = motorSpeed;
189 jointDef.base.collideConnected = collideConnected != 0;
191 b2JointId jointId = b2CreatePrismaticJoint(
g_ctx.worldId, &jointDef);
192 g_ctx.entityToJoint[entityIdB] = jointId;
198 float anchorAx,
float anchorAy,
199 float anchorBx,
float anchorBy,
200 float linearHertz,
float angularHertz,
201 float linearDampingRatio,
float angularDampingRatio,
202 int collideConnected) {
203 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
205 auto itA =
g_ctx.entityToBody.find(entityIdA);
206 auto itB =
g_ctx.entityToBody.find(entityIdB);
207 if (itA ==
g_ctx.entityToBody.end() || itB ==
g_ctx.entityToBody.end())
return 0;
208 if (!b2Body_IsValid(itA->second) || !b2Body_IsValid(itB->second))
return 0;
210 b2WeldJointDef jointDef = b2DefaultWeldJointDef();
211 jointDef.base.bodyIdA = itA->second;
212 jointDef.base.bodyIdB = itB->second;
213 jointDef.base.localFrameA.p = {anchorAx, anchorAy};
214 jointDef.base.localFrameB.p = {anchorBx, anchorBy};
215 jointDef.linearHertz = linearHertz;
216 jointDef.angularHertz = angularHertz;
217 jointDef.linearDampingRatio = linearDampingRatio;
218 jointDef.angularDampingRatio = angularDampingRatio;
219 jointDef.base.collideConnected = collideConnected != 0;
221 b2JointId jointId = b2CreateWeldJoint(
g_ctx.worldId, &jointDef);
222 g_ctx.entityToJoint[entityIdB] = jointId;
228 float anchorAx,
float anchorAy,
229 float anchorBx,
float anchorBy,
230 float axisX,
float axisY,
231 int enableSpring,
float hertz,
float dampingRatio,
232 int enableLimit,
float lowerTranslation,
float upperTranslation,
233 int enableMotor,
float maxMotorTorque,
float motorSpeed,
234 int collideConnected) {
235 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
237 auto itA =
g_ctx.entityToBody.find(entityIdA);
238 auto itB =
g_ctx.entityToBody.find(entityIdB);
239 if (itA ==
g_ctx.entityToBody.end() || itB ==
g_ctx.entityToBody.end())
return 0;
240 if (!b2Body_IsValid(itA->second) || !b2Body_IsValid(itB->second))
return 0;
242 b2WheelJointDef jointDef = b2DefaultWheelJointDef();
243 jointDef.base.bodyIdA = itA->second;
244 jointDef.base.bodyIdB = itB->second;
245 jointDef.base.localFrameA.p = {anchorAx, anchorAy};
246 jointDef.base.localFrameB.p = {anchorBx, anchorBy};
248 float len = sqrtf(axisX * axisX + axisY * axisY);
250 float nx = axisX / len;
251 float ny = axisY / len;
252 jointDef.base.localFrameA.q = {nx, ny};
255 jointDef.enableSpring = enableSpring != 0;
256 jointDef.hertz = hertz;
257 jointDef.dampingRatio = dampingRatio;
258 jointDef.enableLimit = enableLimit != 0;
259 jointDef.lowerTranslation = lowerTranslation;
260 jointDef.upperTranslation = upperTranslation;
261 jointDef.enableMotor = enableMotor != 0;
262 jointDef.maxMotorTorque = maxMotorTorque;
263 jointDef.motorSpeed = motorSpeed;
264 jointDef.base.collideConnected = collideConnected != 0;
266 b2JointId jointId = b2CreateWheelJoint(
g_ctx.worldId, &jointDef);
267 g_ctx.entityToJoint[entityIdB] = jointId;
274 if (B2_IS_NULL(jid))
return 0;
275 return b2DistanceJoint_GetLength(jid);
281 if (B2_IS_NULL(jid))
return 0;
282 return b2DistanceJoint_GetCurrentLength(jid);
288 if (!B2_IS_NULL(jid)) b2DistanceJoint_SetLength(jid, length);
294 if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableSpring(jid, enable != 0);
300 if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableLimit(jid, enable != 0);
306 if (!B2_IS_NULL(jid)) b2DistanceJoint_SetLengthRange(jid, minLength, maxLength);
312 if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableMotor(jid, enable != 0);
318 if (!B2_IS_NULL(jid)) b2DistanceJoint_SetMotorSpeed(jid, speed);
324 if (!B2_IS_NULL(jid)) b2DistanceJoint_SetMaxMotorForce(jid, force);
330 if (B2_IS_NULL(jid))
return 0;
331 return b2DistanceJoint_GetMotorForce(jid);
337 if (B2_IS_NULL(jid))
return 0;
338 return b2PrismaticJoint_GetTranslation(jid);
344 if (B2_IS_NULL(jid))
return 0;
345 return b2PrismaticJoint_GetSpeed(jid);
351 if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableSpring(jid, enable != 0);
357 if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableLimit(jid, enable != 0);
363 if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetLimits(jid, lower, upper);
369 if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableMotor(jid, enable != 0);
375 if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetMotorSpeed(jid, speed);
381 if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetMaxMotorForce(jid, force);
387 if (B2_IS_NULL(jid))
return 0;
388 return b2PrismaticJoint_GetMotorForce(jid);
394 if (!B2_IS_NULL(jid)) b2WheelJoint_EnableSpring(jid, enable != 0);
400 if (!B2_IS_NULL(jid)) b2WheelJoint_EnableLimit(jid, enable != 0);
406 if (!B2_IS_NULL(jid)) b2WheelJoint_SetLimits(jid, lower, upper);
412 if (!B2_IS_NULL(jid)) b2WheelJoint_EnableMotor(jid, enable != 0);
418 if (!B2_IS_NULL(jid)) b2WheelJoint_SetMotorSpeed(jid, speed);
424 if (!B2_IS_NULL(jid)) b2WheelJoint_SetMaxMotorTorque(jid, torque);
430 if (B2_IS_NULL(jid))
return 0;
431 return b2WheelJoint_GetMotorTorque(jid);
442 float linearVelX,
float linearVelY,
float maxVelocityForce,
443 float angularVelocity,
float maxVelocityTorque,
444 float linearHertz,
float linearDampingRatio,
float maxSpringForce,
445 float angularHertz,
float angularDampingRatio,
float maxSpringTorque,
446 int collideConnected) {
447 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
449 auto itA =
g_ctx.entityToBody.find(entityIdA);
450 auto itB =
g_ctx.entityToBody.find(entityIdB);
451 if (itA ==
g_ctx.entityToBody.end() || itB ==
g_ctx.entityToBody.end())
return 0;
452 if (!b2Body_IsValid(itA->second) || !b2Body_IsValid(itB->second))
return 0;
454 b2MotorJointDef jointDef = b2DefaultMotorJointDef();
455 jointDef.base.bodyIdA = itA->second;
456 jointDef.base.bodyIdB = itB->second;
457 jointDef.linearVelocity = {linearVelX, linearVelY};
458 jointDef.maxVelocityForce = maxVelocityForce;
459 jointDef.angularVelocity = angularVelocity;
460 jointDef.maxVelocityTorque = maxVelocityTorque;
461 jointDef.linearHertz = linearHertz;
462 jointDef.linearDampingRatio = linearDampingRatio;
463 jointDef.maxSpringForce = maxSpringForce;
464 jointDef.angularHertz = angularHertz;
465 jointDef.angularDampingRatio = angularDampingRatio;
466 jointDef.maxSpringTorque = maxSpringTorque;
467 jointDef.base.collideConnected = collideConnected != 0;
469 b2JointId jointId = b2CreateMotorJoint(
g_ctx.worldId, &jointDef);
470 g_ctx.entityToJoint[entityIdB] = jointId;
477 if (!B2_IS_NULL(jid)) b2MotorJoint_SetLinearVelocity(jid, {vx, vy});
483 if (!B2_IS_NULL(jid)) b2MotorJoint_SetAngularVelocity(jid, omega);
489 if (!B2_IS_NULL(jid)) b2MotorJoint_SetMaxVelocityForce(jid, force);
495 if (!B2_IS_NULL(jid)) b2MotorJoint_SetMaxVelocityTorque(jid, torque);
507 float hertz,
float dampingRatio,
float maxForce) {
508 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
510 auto it =
g_ctx.entityToBody.find(entityId);
511 if (it ==
g_ctx.entityToBody.end() || !b2Body_IsValid(it->second))
return 0;
513 if (b2Joint_IsValid(
g_ctx.mouseJointId)) b2DestroyJoint(
g_ctx.mouseJointId,
false);
514 if (b2Body_IsValid(
g_ctx.mouseBodyId)) b2DestroyBody(
g_ctx.mouseBodyId);
516 b2BodyDef bodyDef = b2DefaultBodyDef();
517 bodyDef.type = b2_kinematicBody;
518 bodyDef.position = {targetX, targetY};
519 bodyDef.enableSleep =
false;
520 g_ctx.mouseBodyId = b2CreateBody(
g_ctx.worldId, &bodyDef);
522 b2MotorJointDef jointDef = b2DefaultMotorJointDef();
523 jointDef.base.bodyIdA =
g_ctx.mouseBodyId;
524 jointDef.base.bodyIdB = it->second;
525 jointDef.base.localFrameB.p = b2Body_GetLocalPoint(it->second, {targetX, targetY});
526 jointDef.linearHertz = hertz;
527 jointDef.linearDampingRatio = dampingRatio;
532 b2MassData massData = b2Body_GetMassData(it->second);
533 if (maxForce <= 0.0f) {
534 float g = b2Length(b2World_GetGravity(
g_ctx.worldId));
535 maxForce = 100.0f * massData.mass * (g > 0.0f ? g : 10.0f);
537 jointDef.maxSpringForce = maxForce;
538 if (massData.mass > 0.0f) {
539 float lever = sqrtf(massData.rotationalInertia / massData.mass);
540 jointDef.maxVelocityTorque = 0.25f * lever * (maxForce / 100.0f);
543 g_ctx.mouseJointId = b2CreateMotorJoint(
g_ctx.worldId, &jointDef);
545 b2Body_SetAwake(it->second,
true);
551 if (!b2Body_IsValid(
g_ctx.mouseBodyId))
return;
552 b2Body_SetTransform(
g_ctx.mouseBodyId, {targetX, targetY}, b2Rot_identity);
553 if (b2Joint_IsValid(
g_ctx.mouseJointId)) {
554 b2BodyId bodyB = b2Joint_GetBodyB(
g_ctx.mouseJointId);
555 if (b2Body_IsValid(bodyB)) b2Body_SetAwake(bodyB,
true);
561 if (b2Joint_IsValid(
g_ctx.mouseJointId)) b2DestroyJoint(
g_ctx.mouseJointId,
false);
562 if (b2Body_IsValid(
g_ctx.mouseBodyId)) b2DestroyBody(
g_ctx.mouseBodyId);
563 g_ctx.mouseJointId = b2_nullJointId;
564 g_ctx.mouseBodyId = b2_nullBodyId;
569 return b2Joint_IsValid(
g_ctx.mouseJointId) ? 1 : 0;
#define EMSCRIPTEN_KEEPALIVE
The Box2D module's entry points, declared once for both platforms.
b2JointId findValidJoint(uint32_t entityId)
void physics_enableWheelJointLimit(uint32_t entityId, int enable)
void physics_setRevoluteMotorSpeed(uint32_t entityId, float speed)
void physics_setMotorJointMaxVelocityTorque(uint32_t entityId, float torque)
void physics_setPrismaticJointMotorSpeed(uint32_t entityId, float speed)
int physics_hasMouseJoint()
void physics_destroyMouseJoint()
void physics_enableDistanceJointMotor(uint32_t entityId, int enable)
void physics_setWheelJointMotorSpeed(uint32_t entityId, float speed)
void physics_enablePrismaticJointMotor(uint32_t entityId, int enable)
void physics_setDistanceJointMaxMotorForce(uint32_t entityId, float force)
void physics_enablePrismaticJointSpring(uint32_t entityId, int enable)
int physics_hasJoint(uint32_t entityId)
void physics_setRevoluteMaxMotorTorque(uint32_t entityId, float torque)
void physics_setMouseTarget(float targetX, float targetY)
void physics_setRevoluteLimits(uint32_t entityId, float lower, float upper)
float physics_getDistanceJointLength(uint32_t entityId)
int physics_createWheelJoint(uint32_t entityIdA, uint32_t entityIdB, float anchorAx, float anchorAy, float anchorBx, float anchorBy, float axisX, float axisY, int enableSpring, float hertz, float dampingRatio, int enableLimit, float lowerTranslation, float upperTranslation, int enableMotor, float maxMotorTorque, float motorSpeed, int collideConnected)
void physics_setWheelJointMaxMotorTorque(uint32_t entityId, float torque)
void physics_enableWheelJointMotor(uint32_t entityId, int enable)
float physics_getPrismaticJointMotorForce(uint32_t entityId)
void physics_setDistanceJointLength(uint32_t entityId, float length)
void physics_setPrismaticJointMaxMotorForce(uint32_t entityId, float force)
float physics_getPrismaticJointSpeed(uint32_t entityId)
int physics_createMouseJoint(uint32_t entityId, float targetX, float targetY, float hertz, float dampingRatio, float maxForce)
int physics_createWeldJoint(uint32_t entityIdA, uint32_t entityIdB, float anchorAx, float anchorAy, float anchorBx, float anchorBy, float linearHertz, float angularHertz, float linearDampingRatio, float angularDampingRatio, int collideConnected)
void physics_enablePrismaticJointLimit(uint32_t entityId, int enable)
void physics_enableRevoluteLimit(uint32_t entityId, int enable)
float physics_getDistanceJointMotorForce(uint32_t entityId)
float physics_getWheelJointMotorTorque(uint32_t entityId)
void physics_enableRevoluteMotor(uint32_t entityId, int enable)
float physics_getPrismaticJointTranslation(uint32_t entityId)
float physics_getDistanceJointCurrentLength(uint32_t entityId)
float physics_getRevoluteMotorTorque(uint32_t entityId)
void physics_setDistanceJointLimits(uint32_t entityId, float minLength, float maxLength)
int physics_createDistanceJoint(uint32_t entityIdA, uint32_t entityIdB, float anchorAx, float anchorAy, float anchorBx, float anchorBy, float length, int enableSpring, float hertz, float dampingRatio, int enableLimit, float minLength, float maxLength, int enableMotor, float maxMotorForce, float motorSpeed, int collideConnected)
void physics_enableDistanceJointLimit(uint32_t entityId, int enable)
void physics_enableWheelJointSpring(uint32_t entityId, int enable)
void physics_enableDistanceJointSpring(uint32_t entityId, int enable)
void physics_setDistanceJointMotorSpeed(uint32_t entityId, float speed)
void physics_destroyJoint(uint32_t entityId)
int physics_createMotorJoint(uint32_t entityIdA, uint32_t entityIdB, float linearVelX, float linearVelY, float maxVelocityForce, float angularVelocity, float maxVelocityTorque, float linearHertz, float linearDampingRatio, float maxSpringForce, float angularHertz, float angularDampingRatio, float maxSpringTorque, int collideConnected)
float physics_getRevoluteAngle(uint32_t entityId)
void physics_setMotorJointLinearVelocity(uint32_t entityId, float vx, float vy)
void physics_setMotorJointMaxVelocityForce(uint32_t entityId, float force)
void physics_setWheelJointLimits(uint32_t entityId, float lower, float upper)
int physics_createPrismaticJoint(uint32_t entityIdA, uint32_t entityIdB, float anchorAx, float anchorAy, float anchorBx, float anchorBy, float axisX, float axisY, int enableSpring, float hertz, float dampingRatio, int enableLimit, float lowerTranslation, float upperTranslation, int enableMotor, float maxMotorForce, float motorSpeed, int collideConnected)
void physics_setPrismaticJointLimits(uint32_t entityId, float lower, float upper)
void physics_setMotorJointAngularVelocity(uint32_t entityId, float omega)
int physics_createRevoluteJoint(uint32_t entityIdA, uint32_t entityIdB, float anchorAx, float anchorAy, float anchorBx, float anchorBy, int enableMotor, float motorSpeed, float maxMotorTorque, int enableLimit, float lowerAngle, float upperAngle, int collideConnected)