41constexpr float MOTOR_DAMPING = 1.0e6f;
43bool finite(
const PxVec3& v) {
return v.isFinite(); }
45PxVec3 perpendicularTo(
const PxVec3& v) {
47 if (axis.normalizeSafe() <= 0.0f) axis = PxVec3(0.0f, 1.0f, 0.0f);
48 const PxVec3 other = std::abs(axis.y) < 0.9f ? PxVec3(0.0f, 1.0f, 0.0f) : PxVec3(1.0f, 0.0f, 0.0f);
49 PxVec3 out = axis.cross(other);
50 return out.normalizeSafe() > 0.0f ? out : PxVec3(1.0f, 0.0f, 0.0f);
53bool referenceFrame(
const PxVec3& x,
const PxVec3& y, PxVec3& outX, PxVec3& outY) {
54 if (!finite(x) || !finite(y) || x.magnitudeSquared() < 1.0e-12f || y.magnitudeSquared() < 1.0e-12f)
return false;
55 outX = x.getNormalized();
56 const PxVec3 normal = y - outX * outX.dot(y);
57 if (normal.magnitudeSquared() < 1.0e-10f * y.magnitudeSquared())
return false;
58 outY = normal.getNormalized();
63PxTransform worldFrame(
const PxVec3& at,
const PxVec3& x,
const PxVec3& y) {
64 return PxTransform(at, PxQuat(PxMat33(x, y, x.cross(y))).getNormalized());
68PxTransform localFrame(PxRigidActor* actor,
const PxTransform&
world) {
69 return actor->getGlobalPose().transformInv(
world);
72PxRigidActor* actorOf(uint32_t bodyId) {
73 Body* body = g().body(bodyId);
74 return body ? body->
actor :
nullptr;
77bool bothLive(uint32_t a, uint32_t b) {
78 return g().isValid() && actorOf(a) !=
nullptr && actorOf(b) !=
nullptr;
81void wake(PxRigidActor* actor) {
82 if (PxRigidDynamic* d = actor ? actor->is<PxRigidDynamic>() :
nullptr) {
83 if (!(d->getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC) && d->getScene()) d->wakeUp();
87void release(uint32_t entity) {
88 auto found = g().joints.find(entity);
89 if (found == g().joints.end())
return;
90 found->second.joint->release();
91 g().joints.erase(found);
94uint32_t adopt(uint32_t entity, PxJoint* joint,
JointKind kind,
int collideConnected,
float maxForce = 0.0f) {
95 if (joint ==
nullptr)
return 0;
96 joint->setConstraintFlag(PxConstraintFlag::eCOLLISION_ENABLED, collideConnected != 0);
102 g().joints[entity] = record;
108static void hingeRange(
float lower,
float upper,
float& outMin,
float& outMax) {
109 outMin = std::clamp(std::min(lower, 0.0f), -PxPi, 0.0f);
110 outMax = std::clamp(std::max(upper, 0.0f), 0.0f, PxPi);
114static void shapeDistance(PxDistanceJoint* joint,
float minLength,
float maxLength,
float frequency,
float damping) {
115 const float lo = std::max(minLength, 0.0f);
116 joint->setMinDistance(lo);
117 joint->setMaxDistance(std::max(maxLength, lo));
118 PxDistanceJointFlags flags = PxDistanceJointFlag::eMAX_DISTANCE_ENABLED;
119 if (lo > 0.0f) flags |= PxDistanceJointFlag::eMIN_DISTANCE_ENABLED;
120 if (frequency > 0.0f) {
122 PxRigidActor* a =
nullptr;
123 PxRigidActor* b =
nullptr;
124 joint->getActors(a, b);
125 float massA = 0.0f, massB = 0.0f;
126 if (a)
if (PxRigidDynamic* d = a->is<PxRigidDynamic>()) massA = d->getMass();
127 if (b)
if (PxRigidDynamic* d = b->is<PxRigidDynamic>()) massB = d->getMass();
128 const float mass = massA > 0.0f && massB > 0.0f ? massA * massB / (massA + massB) : std::max(massA, massB);
129 const float omega = 2.0f * PxPi * frequency;
130 joint->setStiffness(mass * omega * omega);
131 joint->setDamping(2.0f * mass * std::max(damping, 0.0f) * omega);
132 flags |= PxDistanceJointFlag::eSPRING_ENABLED;
134 joint->setDistanceJointFlags(flags);
138static void wakeEnds(PxJoint* joint) {
139 PxRigidActor* a =
nullptr;
140 PxRigidActor* b =
nullptr;
141 joint->getActors(a, b);
146PxD6JointDrive motor(
bool on,
float limit) {
147 return on ? PxD6JointDrive(0.0f, MOTOR_DAMPING, limit) : PxD6JointDrive();
151PxD6Joint* d6(uint32_t bodyA, uint32_t bodyB,
const PxTransform& frame) {
152 PxRigidActor* a = actorOf(bodyA);
153 PxRigidActor* b = actorOf(bodyB);
154 PxD6Joint* joint = PxD6JointCreate(
px3::physics(), a, localFrame(a, frame), b, localFrame(b, frame));
155 if (joint) joint->setAngularDriveConfig(PxD6AngularDriveConfig::eSWING_TWIST);
160PxD6Motion::Enum swingMotion(
float half) {
161 if (half <= 1.0e-4f)
return PxD6Motion::eLOCKED;
162 if (half >= PxPi - 1.0e-4f)
return PxD6Motion::eFREE;
163 return PxD6Motion::eLIMITED;
172 float px,
float py,
float pz,
float ax,
float ay,
float az,
173 float nx,
float ny,
float nz,
float normalHalf,
float planeHalf,
174 float twistMin,
float twistMax,
int collideConnected) {
175 if (bodyA == bodyB || !bothLive(bodyA, bodyB) || g().joints.count(entity) != 0)
return 0;
177 if (!referenceFrame(PxVec3(ax, ay, az), PxVec3(nx, ny, nz), x, y) || !finite(PxVec3(px, py, pz))
178 || !std::isfinite(normalHalf) || !std::isfinite(planeHalf)
179 || !std::isfinite(twistMin) || !std::isfinite(twistMax)
180 || normalHalf < 0 || normalHalf > PxPi || planeHalf < 0 || planeHalf > PxPi
181 || twistMin < -PxPi || twistMax > PxPi || twistMin > twistMax)
return 0;
182 PxD6Joint* joint = d6(bodyA, bodyB, worldFrame(PxVec3(px, py, pz), x, y));
183 if (joint ==
nullptr)
return 0;
186 const bool twistFree = twistMin <= -PxPi + 1.0e-4f && twistMax >= PxPi - 1.0e-4f;
187 joint->setMotion(PxD6Axis::eTWIST, twistFree ? PxD6Motion::eFREE
188 : (twistMax - twistMin <= 1.0e-4f ? PxD6Motion::eLOCKED : PxD6Motion::eLIMITED));
189 joint->setTwistLimit(PxJointAngularLimitPair(twistMin, twistMax));
190 joint->setMotion(PxD6Axis::eSWING1, swingMotion(planeHalf));
191 joint->setMotion(PxD6Axis::eSWING2, swingMotion(normalHalf));
192 joint->setSwingLimit(PxJointLimitCone(std::max(planeHalf, 1.0e-3f), std::max(normalHalf, 1.0e-3f)));
198 float px,
float py,
float pz,
float ax,
float ay,
float az,
float nx,
float ny,
float nz,
199 int translationMotion,
int rotationMotion,
200 float lx,
float ly,
float lz,
float hx,
float hy,
float hz,
201 float rx,
float ry,
float rz,
float sx,
float sy,
float sz,
int collideConnected) {
202 if (bodyA == bodyB || !bothLive(bodyA, bodyB) || g().joints.count(entity) != 0)
return 0;
204 if (!referenceFrame(PxVec3(ax, ay, az), PxVec3(nx, ny, nz), x, y) || !finite(PxVec3(px, py, pz))
205 || translationMotion < 0 || translationMotion > 63 || rotationMotion < 0 || rotationMotion > 63)
return 0;
206 const float lower[] = {lx, ly, lz, rx, ry, rz};
207 const float upper[] = {hx, hy, hz, sx, sy, sz};
208 PxD6Motion::Enum modes[6];
209 for (
int i = 0; i < 6; ++i) {
210 const int mode = ((i < 3 ? translationMotion : rotationMotion) >> (2 * (i % 3))) & 3;
211 if (!std::isfinite(lower[i]) || !std::isfinite(upper[i]) || mode == 3)
return 0;
212 if (mode == 1 && (lower[i] >= upper[i] || (i >= 3 && (lower[i] < -PxPi || upper[i] > PxPi))))
return 0;
213 modes[i] = mode == 0 ? PxD6Motion::eLOCKED : (mode == 2 ? PxD6Motion::eFREE : PxD6Motion::eLIMITED);
215 PxD6Joint* joint = d6(bodyA, bodyB, worldFrame(PxVec3(px, py, pz), x, y));
216 if (joint ==
nullptr)
return 0;
217 const PxD6Axis::Enum linear[] = {PxD6Axis::eX, PxD6Axis::eY, PxD6Axis::eZ};
218 for (
int i = 0; i < 3; ++i) {
219 joint->setMotion(linear[i], modes[i]);
220 if (modes[i] == PxD6Motion::eLIMITED) {
221 joint->setLinearLimit(linear[i], PxJointLinearLimitPair(lower[i], upper[i], PxSpring(0.0f, 0.0f)));
224 joint->setMotion(PxD6Axis::eTWIST, modes[3]);
225 joint->setMotion(PxD6Axis::eSWING1, modes[4]);
226 joint->setMotion(PxD6Axis::eSWING2, modes[5]);
227 if (modes[3] == PxD6Motion::eLIMITED) joint->setTwistLimit(PxJointAngularLimitPair(rx, sx));
229 if (modes[4] == PxD6Motion::eLIMITED || modes[5] == PxD6Motion::eLIMITED) {
230 joint->setPyramidSwingLimit(PxJointLimitPyramid(
231 modes[4] == PxD6Motion::eLIMITED ? ry : -PxPi + 1.0e-3f,
232 modes[4] == PxD6Motion::eLIMITED ? sy : PxPi - 1.0e-3f,
233 modes[5] == PxD6Motion::eLIMITED ? rz : -PxPi + 1.0e-3f,
234 modes[5] == PxD6Motion::eLIMITED ? sz : PxPi - 1.0e-3f));
241 float vx,
float vy,
float vz,
float wx,
float wy,
float wz,
float maxForce,
float maxTorque) {
242 auto found = g().joints.find(entity);
243 if (found == g().joints.end() || !finite(PxVec3(vx, vy, vz)) || !finite(PxVec3(wx, wy, wz))
244 || !std::isfinite(maxForce) || maxForce < 0 || !std::isfinite(maxTorque) || maxTorque < 0)
return;
246 PxD6Joint* joint =
static_cast<PxD6Joint*
>(found->second.joint);
247 const bool on = enable != 0;
249 joint->setDrive(PxD6Drive::eX, motor(on && joint->getMotion(PxD6Axis::eX) != PxD6Motion::eLOCKED, maxForce));
250 joint->setDrive(PxD6Drive::eY, motor(on && joint->getMotion(PxD6Axis::eY) != PxD6Motion::eLOCKED, maxForce));
251 joint->setDrive(PxD6Drive::eZ, motor(on && joint->getMotion(PxD6Axis::eZ) != PxD6Motion::eLOCKED, maxForce));
253 joint->setDrive(PxD6Drive::eTWIST, motor(on && joint->getMotion(PxD6Axis::eTWIST) != PxD6Motion::eLOCKED, maxTorque));
254 joint->setDrive(PxD6Drive::eSWING1, motor(on && joint->getMotion(PxD6Axis::eSWING1) != PxD6Motion::eLOCKED, maxTorque));
255 joint->setDrive(PxD6Drive::eSWING2, motor(on && joint->getMotion(PxD6Axis::eSWING2) != PxD6Motion::eLOCKED, maxTorque));
258 joint->setDriveVelocity(found->second.kind ==
JointKind::SixDOF ? PxVec3(vx, vy, vz) : PxVec3(0.0f),
259 -PxVec3(wx, wy, wz),
true);
260 PxRigidActor* a =
nullptr;
261 PxRigidActor* b =
nullptr;
262 joint->getActors(a, b);
269 float px,
float py,
float pz,
int collideConnected) {
270 if (!bothLive(bodyA, bodyB))
return 0;
271 PxRigidActor* a = actorOf(bodyA);
272 PxRigidActor* b = actorOf(bodyB);
273 const PxTransform frame(PxVec3(px, py, pz));
274 return adopt(entity, PxSphericalJointCreate(
px3::physics(), a, localFrame(a, frame), b, localFrame(b, frame)),
280 float px,
float py,
float pz,
float ax,
float ay,
float az,
281 int enableLimit,
float lowerAngle,
float upperAngle,
282 int enableMotor,
float motorSpeed,
float maxTorque,
283 int collideConnected) {
284 if (!bothLive(bodyA, bodyB))
return 0;
285 PxVec3 axis(ax, ay, az);
286 if (axis.normalizeSafe() <= 0.0f) axis = PxVec3(0.0f, 1.0f, 0.0f);
287 const PxTransform frame = worldFrame(PxVec3(px, py, pz), axis, perpendicularTo(axis));
288 PxRigidActor* a = actorOf(bodyA);
289 PxRigidActor* b = actorOf(bodyB);
290 PxRevoluteJoint* joint = PxRevoluteJointCreate(
px3::physics(), a, localFrame(a, frame), b, localFrame(b, frame));
291 if (joint ==
nullptr)
return 0;
292 if (enableLimit != 0) {
293 float lo = 0.0f, hi = 0.0f;
294 hingeRange(lowerAngle, upperAngle, lo, hi);
295 joint->setLimit(PxJointAngularLimitPair(lo, hi));
296 joint->setRevoluteJointFlag(PxRevoluteJointFlag::eLIMIT_ENABLED,
true);
298 joint->setDriveForceLimit(maxTorque);
299 if (enableMotor != 0) {
300 joint->setRevoluteJointFlag(PxRevoluteJointFlag::eDRIVE_ENABLED,
true);
301 joint->setDriveVelocity(motorSpeed);
308 float px,
float py,
float pz,
float ax,
float ay,
float az,
309 int enableLimit,
float lower,
float upper,
310 int enableMotor,
float motorSpeed,
float maxForce,
311 int collideConnected) {
312 if (!bothLive(bodyA, bodyB))
return 0;
313 PxVec3 axis(ax, ay, az);
314 if (axis.normalizeSafe() <= 0.0f) axis = PxVec3(0.0f, 1.0f, 0.0f);
315 PxD6Joint* joint = d6(bodyA, bodyB, worldFrame(PxVec3(px, py, pz), axis, perpendicularTo(axis)));
316 if (joint ==
nullptr)
return 0;
317 const bool limited = enableLimit != 0;
318 joint->setMotion(PxD6Axis::eX, limited ? PxD6Motion::eLIMITED : PxD6Motion::eFREE);
319 if (limited) joint->setLinearLimit(PxD6Axis::eX, PxJointLinearLimitPair(std::min(lower, 0.0f), std::max(upper, 0.0f), PxSpring(0.0f, 0.0f)));
320 if (enableMotor != 0) {
321 joint->setDrive(PxD6Drive::eX, motor(
true, maxForce));
322 joint->setDriveVelocity(PxVec3(motorSpeed, 0.0f, 0.0f), PxVec3(0.0f),
true);
329 float ax,
float ay,
float az,
float bx,
float by,
float bz,
330 float minLength,
float maxLength,
float frequency,
float damping,
331 int collideConnected) {
332 if (!bothLive(bodyA, bodyB))
return 0;
333 PxRigidActor* a = actorOf(bodyA);
334 PxRigidActor* b = actorOf(bodyB);
335 PxDistanceJoint* joint = PxDistanceJointCreate(
px3::physics(),
336 a, localFrame(a, PxTransform(PxVec3(ax, ay, az))), b, localFrame(b, PxTransform(PxVec3(bx, by, bz))));
337 if (joint ==
nullptr)
return 0;
338 shapeDistance(joint, minLength, maxLength, frequency, damping);
344 if (!bothLive(bodyA, bodyB))
return 0;
345 PxRigidActor* a = actorOf(bodyA);
346 PxRigidActor* b = actorOf(bodyB);
348 const PxTransform frame = a->getGlobalPose();
349 return adopt(entity, PxFixedJointCreate(
px3::physics(), a, PxTransform(PxIdentity), b, localFrame(b, frame)),
355 if (g().joints.count(entity) == 0)
return;
358 wake(actorOf(bodyA));
359 wake(actorOf(bodyB));
364 auto found = g().joints.find(entity);
365 if (found == g().joints.end())
return;
367 auto* hinge =
static_cast<PxRevoluteJoint*
>(found->second.joint);
368 hinge->setRevoluteJointFlag(PxRevoluteJointFlag::eDRIVE_ENABLED, enable != 0);
369 hinge->setDriveVelocity(speed,
true);
371 auto* slider =
static_cast<PxD6Joint*
>(found->second.joint);
372 slider->setDrive(PxD6Drive::eX, motor(enable != 0, found->second.maxForce));
373 slider->setDriveVelocity(PxVec3(speed, 0.0f, 0.0f), PxVec3(0.0f),
true);
379 auto found = g().joints.find(entity);
380 if (found == g().joints.end() || !std::isfinite(lower) || !std::isfinite(upper))
return;
382 auto* hinge =
static_cast<PxRevoluteJoint*
>(found->second.joint);
383 float lo = 0.0f, hi = 0.0f;
384 hingeRange(lower, upper, lo, hi);
385 hinge->setLimit(PxJointAngularLimitPair(lo, hi));
386 hinge->setRevoluteJointFlag(PxRevoluteJointFlag::eLIMIT_ENABLED, enable != 0);
388 auto* slider =
static_cast<PxD6Joint*
>(found->second.joint);
389 slider->setMotion(PxD6Axis::eX, enable != 0 ? PxD6Motion::eLIMITED : PxD6Motion::eFREE);
390 if (enable != 0) slider->setLinearLimit(PxD6Axis::eX, PxJointLinearLimitPair(std::min(lower, 0.0f), std::max(upper, 0.0f), PxSpring(0.0f, 0.0f)));
399 auto found = g().joints.find(entity);
400 if (found == g().joints.end() || !std::isfinite(limit) || limit < 0.0f)
return;
401 found->second.maxForce = limit;
403 static_cast<PxRevoluteJoint*
>(found->second.joint)->setDriveForceLimit(limit);
405 auto* slider =
static_cast<PxD6Joint*
>(found->second.joint);
407 if (slider->getDrive(PxD6Drive::eX).damping > 0.0f) slider->setDrive(PxD6Drive::eX, motor(
true, limit));
416 auto found = g().joints.find(entity);
418 if (!std::isfinite(minLength) || !std::isfinite(maxLength) || !std::isfinite(frequency) || !std::isfinite(damping))
return;
419 shapeDistance(
static_cast<PxDistanceJoint*
>(found->second.joint), minLength, maxLength, frequency, damping);
425 g().queryBuffer.clear();
426 auto found = g().joints.find(entity);
427 if (found == g().joints.end())
return 0;
428 PxVec3 linear(0.0f), angular(0.0f);
429 found->second.joint->getConstraint()->getForce(linear, angular);
430 g().queryBuffer = {linear.magnitude(), angular.magnitude()};
436 auto found = g().joints.find(entity);
437 if (found == g().joints.end())
return 0.0f;
439 return static_cast<PxRevoluteJoint*
>(found->second.joint)->getAngle();
442 return found->second.joint->getRelativeTransform().p.x;
#define EMSCRIPTEN_KEEPALIVE
int physics3d_jointForce(uint32_t entity)
uint32_t physics3d_addSixDOFJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB, float px, float py, float pz, float ax, float ay, float az, float nx, float ny, float nz, int translationMotion, int rotationMotion, float lx, float ly, float lz, float hx, float hy, float hz, float rx, float ry, float rz, float sx, float sy, float sz, int collideConnected)
Axis motion is packed as two bits per axis: 0 locked, 1 limited, 2 free.
uint32_t physics3d_addSliderJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB, float px, float py, float pz, float ax, float ay, float az, int enableLimit, float lower, float upper, int enableMotor, float motorSpeed, float maxForce, int collideConnected)
One axis of travel — a lift, a piston, a sliding door.
void physics3d_setAdvancedJointMotor(uint32_t entity, int enable, float vx, float vy, float vz, float wx, float wy, float wz, float maxForce, float maxTorque)
Live velocity/force changes do not rebuild the joint or reset its zero pose.
void physics3d_setDistanceJoint(uint32_t entity, float minLength, float maxLength, float frequency, float damping)
uint32_t physics3d_addDistanceJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB, float ax, float ay, float az, float bx, float by, float bz, float minLength, float maxLength, float frequency, float damping, int collideConnected)
void physics3d_setJointMotor(uint32_t entity, int enable, float speed)
Drive an existing hinge or slider from gameplay, without rebuilding the joint.
void physics3d_setJointMotorLimit(uint32_t entity, float limit)
void physics3d_removeJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB)
uint32_t physics3d_addHingeJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB, float px, float py, float pz, float ax, float ay, float az, int enableLimit, float lowerAngle, float upperAngle, int enableMotor, float motorSpeed, float maxTorque, int collideConnected)
uint32_t physics3d_addSwingTwistJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB, float px, float py, float pz, float ax, float ay, float az, float nx, float ny, float nz, float normalHalf, float planeHalf, float twistMin, float twistMax, int collideConnected)
Both frames coincide at creation, so limits are relative to the authored pose.
float physics3d_jointValue(uint32_t entity)
uint32_t physics3d_addPointJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB, float px, float py, float pz, int collideConnected)
A shared point, free to turn on all three axes — a chain link, a ragdoll limb.
void physics3d_setJointLimits(uint32_t entity, int enable, float lower, float upper)
uint32_t physics3d_addFixedJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB, int collideConnected)
No freedom at all: the two move as one, until something breaks them apart.
The one 3D world the PhysX backend steps.
The Jolt module's entry points, declared once for both platforms.
static void wakeEnds(Constraint *constraint)
A joint changed under two sleeping bodies does nothing until they wake: they are woken.
What the module knows about a body beyond what PhysX holds.
physx::PxRigidActor * actor
float maxForce
A slider's motor force, kept so turning the motor back on restores it.