Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
PhysicsJoints.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
5
6#include <cmath>
7
8extern "C" {
9
11int physics_createRevoluteJoint(uint32_t entityIdA, uint32_t entityIdB,
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;
18
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;
23
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;
36
37 b2JointId jointId = b2CreateRevoluteJoint(g_ctx.worldId, &jointDef);
38 g_ctx.entityToJoint[entityIdB] = jointId;
39 return 1;
40}
41
43void physics_destroyJoint(uint32_t entityId) {
44 auto it = g_ctx.entityToJoint.find(entityId);
45 if (it == g_ctx.entityToJoint.end()) return;
46
47 if (b2Joint_IsValid(it->second)) {
48 b2DestroyJoint(it->second, true);
49 }
50 g_ctx.entityToJoint.erase(it);
51}
52
54void physics_setRevoluteMotorSpeed(uint32_t entityId, float speed) {
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);
59}
60
62void physics_setRevoluteMaxMotorTorque(uint32_t entityId, float torque) {
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);
67}
68
70void physics_enableRevoluteMotor(uint32_t entityId, int enable) {
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);
75}
76
78void physics_enableRevoluteLimit(uint32_t entityId, int enable) {
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);
83}
84
86void physics_setRevoluteLimits(uint32_t entityId, float lower, float upper) {
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);
91}
92
94float physics_getRevoluteAngle(uint32_t entityId) {
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);
99}
100
102float physics_getRevoluteMotorTorque(uint32_t entityId) {
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);
107}
108
110int physics_hasJoint(uint32_t entityId) {
111 return g_ctx.entityToJoint.contains(entityId) ? 1 : 0;
112}
113
115int physics_createDistanceJoint(uint32_t entityIdA, uint32_t entityIdB,
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;
123
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;
128
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;
145
146 b2JointId jointId = b2CreateDistanceJoint(g_ctx.worldId, &jointDef);
147 g_ctx.entityToJoint[entityIdB] = jointId;
148 return 1;
149}
150
152int physics_createPrismaticJoint(uint32_t entityIdA, uint32_t entityIdB,
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;
161
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;
166
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};
172
173 float len = sqrtf(axisX * axisX + axisY * axisY);
174 if (len > 0.0f) {
175 float nx = axisX / len;
176 float ny = axisY / len;
177 jointDef.base.localFrameA.q = {nx, ny};
178 }
179
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;
190
191 b2JointId jointId = b2CreatePrismaticJoint(g_ctx.worldId, &jointDef);
192 g_ctx.entityToJoint[entityIdB] = jointId;
193 return 1;
194}
195
197int physics_createWeldJoint(uint32_t entityIdA, uint32_t entityIdB,
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;
204
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;
209
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;
220
221 b2JointId jointId = b2CreateWeldJoint(g_ctx.worldId, &jointDef);
222 g_ctx.entityToJoint[entityIdB] = jointId;
223 return 1;
224}
225
227int physics_createWheelJoint(uint32_t entityIdA, uint32_t entityIdB,
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;
236
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;
241
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};
247
248 float len = sqrtf(axisX * axisX + axisY * axisY);
249 if (len > 0.0f) {
250 float nx = axisX / len;
251 float ny = axisY / len;
252 jointDef.base.localFrameA.q = {nx, ny};
253 }
254
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;
265
266 b2JointId jointId = b2CreateWheelJoint(g_ctx.worldId, &jointDef);
267 g_ctx.entityToJoint[entityIdB] = jointId;
268 return 1;
269}
270
272float physics_getDistanceJointLength(uint32_t entityId) {
273 auto jid = findValidJoint(entityId);
274 if (B2_IS_NULL(jid)) return 0;
275 return b2DistanceJoint_GetLength(jid);
276}
277
280 auto jid = findValidJoint(entityId);
281 if (B2_IS_NULL(jid)) return 0;
282 return b2DistanceJoint_GetCurrentLength(jid);
283}
284
286void physics_setDistanceJointLength(uint32_t entityId, float length) {
287 auto jid = findValidJoint(entityId);
288 if (!B2_IS_NULL(jid)) b2DistanceJoint_SetLength(jid, length);
289}
290
292void physics_enableDistanceJointSpring(uint32_t entityId, int enable) {
293 auto jid = findValidJoint(entityId);
294 if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableSpring(jid, enable != 0);
295}
296
298void physics_enableDistanceJointLimit(uint32_t entityId, int enable) {
299 auto jid = findValidJoint(entityId);
300 if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableLimit(jid, enable != 0);
301}
302
304void physics_setDistanceJointLimits(uint32_t entityId, float minLength, float maxLength) {
305 auto jid = findValidJoint(entityId);
306 if (!B2_IS_NULL(jid)) b2DistanceJoint_SetLengthRange(jid, minLength, maxLength);
307}
308
310void physics_enableDistanceJointMotor(uint32_t entityId, int enable) {
311 auto jid = findValidJoint(entityId);
312 if (!B2_IS_NULL(jid)) b2DistanceJoint_EnableMotor(jid, enable != 0);
313}
314
316void physics_setDistanceJointMotorSpeed(uint32_t entityId, float speed) {
317 auto jid = findValidJoint(entityId);
318 if (!B2_IS_NULL(jid)) b2DistanceJoint_SetMotorSpeed(jid, speed);
319}
320
322void physics_setDistanceJointMaxMotorForce(uint32_t entityId, float force) {
323 auto jid = findValidJoint(entityId);
324 if (!B2_IS_NULL(jid)) b2DistanceJoint_SetMaxMotorForce(jid, force);
325}
326
328float physics_getDistanceJointMotorForce(uint32_t entityId) {
329 auto jid = findValidJoint(entityId);
330 if (B2_IS_NULL(jid)) return 0;
331 return b2DistanceJoint_GetMotorForce(jid);
332}
333
335float physics_getPrismaticJointTranslation(uint32_t entityId) {
336 auto jid = findValidJoint(entityId);
337 if (B2_IS_NULL(jid)) return 0;
338 return b2PrismaticJoint_GetTranslation(jid);
339}
340
342float physics_getPrismaticJointSpeed(uint32_t entityId) {
343 auto jid = findValidJoint(entityId);
344 if (B2_IS_NULL(jid)) return 0;
345 return b2PrismaticJoint_GetSpeed(jid);
346}
347
349void physics_enablePrismaticJointSpring(uint32_t entityId, int enable) {
350 auto jid = findValidJoint(entityId);
351 if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableSpring(jid, enable != 0);
352}
353
355void physics_enablePrismaticJointLimit(uint32_t entityId, int enable) {
356 auto jid = findValidJoint(entityId);
357 if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableLimit(jid, enable != 0);
358}
359
361void physics_setPrismaticJointLimits(uint32_t entityId, float lower, float upper) {
362 auto jid = findValidJoint(entityId);
363 if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetLimits(jid, lower, upper);
364}
365
367void physics_enablePrismaticJointMotor(uint32_t entityId, int enable) {
368 auto jid = findValidJoint(entityId);
369 if (!B2_IS_NULL(jid)) b2PrismaticJoint_EnableMotor(jid, enable != 0);
370}
371
373void physics_setPrismaticJointMotorSpeed(uint32_t entityId, float speed) {
374 auto jid = findValidJoint(entityId);
375 if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetMotorSpeed(jid, speed);
376}
377
379void physics_setPrismaticJointMaxMotorForce(uint32_t entityId, float force) {
380 auto jid = findValidJoint(entityId);
381 if (!B2_IS_NULL(jid)) b2PrismaticJoint_SetMaxMotorForce(jid, force);
382}
383
385float physics_getPrismaticJointMotorForce(uint32_t entityId) {
386 auto jid = findValidJoint(entityId);
387 if (B2_IS_NULL(jid)) return 0;
388 return b2PrismaticJoint_GetMotorForce(jid);
389}
390
392void physics_enableWheelJointSpring(uint32_t entityId, int enable) {
393 auto jid = findValidJoint(entityId);
394 if (!B2_IS_NULL(jid)) b2WheelJoint_EnableSpring(jid, enable != 0);
395}
396
398void physics_enableWheelJointLimit(uint32_t entityId, int enable) {
399 auto jid = findValidJoint(entityId);
400 if (!B2_IS_NULL(jid)) b2WheelJoint_EnableLimit(jid, enable != 0);
401}
402
404void physics_setWheelJointLimits(uint32_t entityId, float lower, float upper) {
405 auto jid = findValidJoint(entityId);
406 if (!B2_IS_NULL(jid)) b2WheelJoint_SetLimits(jid, lower, upper);
407}
408
410void physics_enableWheelJointMotor(uint32_t entityId, int enable) {
411 auto jid = findValidJoint(entityId);
412 if (!B2_IS_NULL(jid)) b2WheelJoint_EnableMotor(jid, enable != 0);
413}
414
416void physics_setWheelJointMotorSpeed(uint32_t entityId, float speed) {
417 auto jid = findValidJoint(entityId);
418 if (!B2_IS_NULL(jid)) b2WheelJoint_SetMotorSpeed(jid, speed);
419}
420
422void physics_setWheelJointMaxMotorTorque(uint32_t entityId, float torque) {
423 auto jid = findValidJoint(entityId);
424 if (!B2_IS_NULL(jid)) b2WheelJoint_SetMaxMotorTorque(jid, torque);
425}
426
428float physics_getWheelJointMotorTorque(uint32_t entityId) {
429 auto jid = findValidJoint(entityId);
430 if (B2_IS_NULL(jid)) return 0;
431 return b2WheelJoint_GetMotorTorque(jid);
432}
433
434// -----------------------------------------------------------------------------
435// Motor joint — drives bodyB toward a relative linear/angular velocity (or a
436// spring-held offset) against bodyA. Good for conveyors, driven platforms, and
437// the "move toward a target" behaviour the mouse-drag helper below reuses.
438// -----------------------------------------------------------------------------
439
441int physics_createMotorJoint(uint32_t entityIdA, uint32_t entityIdB,
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;
448
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;
453
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;
468
469 b2JointId jointId = b2CreateMotorJoint(g_ctx.worldId, &jointDef);
470 g_ctx.entityToJoint[entityIdB] = jointId;
471 return 1;
472}
473
475void physics_setMotorJointLinearVelocity(uint32_t entityId, float vx, float vy) {
476 auto jid = findValidJoint(entityId);
477 if (!B2_IS_NULL(jid)) b2MotorJoint_SetLinearVelocity(jid, {vx, vy});
478}
479
481void physics_setMotorJointAngularVelocity(uint32_t entityId, float omega) {
482 auto jid = findValidJoint(entityId);
483 if (!B2_IS_NULL(jid)) b2MotorJoint_SetAngularVelocity(jid, omega);
484}
485
487void physics_setMotorJointMaxVelocityForce(uint32_t entityId, float force) {
488 auto jid = findValidJoint(entityId);
489 if (!B2_IS_NULL(jid)) b2MotorJoint_SetMaxVelocityForce(jid, force);
490}
491
493void physics_setMotorJointMaxVelocityTorque(uint32_t entityId, float torque) {
494 auto jid = findValidJoint(entityId);
495 if (!B2_IS_NULL(jid)) b2MotorJoint_SetMaxVelocityTorque(jid, torque);
496}
497
498// -----------------------------------------------------------------------------
499// Mouse (drag) joint — Box2D v3 has no dedicated mouse joint, so this is the
500// canonical replacement from Box2D's own sample: a kinematic "mouse body" plus a
501// spring motor joint pulling the target toward the cursor. Only one is live at a
502// time (stored on the context); creating a new one tears down the previous.
503// -----------------------------------------------------------------------------
504
506int physics_createMouseJoint(uint32_t entityId, float targetX, float targetY,
507 float hertz, float dampingRatio, float maxForce) {
508 if (!b2World_IsValid(g_ctx.worldId)) return 0;
509
510 auto it = g_ctx.entityToBody.find(entityId);
511 if (it == g_ctx.entityToBody.end() || !b2Body_IsValid(it->second)) return 0;
512
513 if (b2Joint_IsValid(g_ctx.mouseJointId)) b2DestroyJoint(g_ctx.mouseJointId, false);
514 if (b2Body_IsValid(g_ctx.mouseBodyId)) b2DestroyBody(g_ctx.mouseBodyId);
515
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);
521
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;
528
529 // Auto-size the spring force from the body's weight (Box2D's sample formula) when
530 // the caller passes maxForce <= 0. Top-down worlds have no gravity, so fall back
531 // to a nominal g so the drag still has bite. Also cap angular drift like the sample.
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);
536 }
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);
541 }
542
543 g_ctx.mouseJointId = b2CreateMotorJoint(g_ctx.worldId, &jointDef);
544
545 b2Body_SetAwake(it->second, true);
546 return 1;
547}
548
550void physics_setMouseTarget(float targetX, float targetY) {
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);
556 }
557}
558
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;
565}
566
569 return b2Joint_IsValid(g_ctx.mouseJointId) ? 1 : 0;
570}
571
572} // extern "C"
#define EMSCRIPTEN_KEEPALIVE
The Box2D module's entry points, declared once for both platforms.
b2JointId findValidJoint(uint32_t entityId)
PhysicsContext g_ctx
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)