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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* @file PhysXModuleEntry.cpp
* @brief The PhysX backend's world, bodies, characters, queries and readback.
* @details Every entry point here answers what its Jolt twin answers, in the
* same buffer layouts (Physics3DBindings.hpp is included so a
* signature that drifts is a compile error). Where the two engines
* genuinely differ the choice follows Unity, which runs this same
* solver: trigger volumes report character controllers, and a
* character stops rising when it hits a ceiling.
*
* @author ESEngine Team
* @date 2026
*
* @copyright Copyright (c) 2026 ESEngine Team
* Licensed under the Apache License, Version 2.0.
*/
#include "./PhysXWorld.hpp"
#include "../physics3d/Physics3DBindings.hpp"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <limits>
using namespace physx;
using esengine::physics3d_physx::Body;
using esengine::physics3d_physx::Character;
using esengine::physics3d_physx::World;
namespace px3 = esengine::physics3d_physx;
namespace {
PxDefaultAllocator& allocator() { static PxDefaultAllocator a; return a; }
PxDefaultErrorCallback& errors() { static PxDefaultErrorCallback e; return e; }
PxFoundation* gFoundation = nullptr;
PxPhysics* gPhysics = nullptr;
World& g() { return px3::world(); }
// Unity's defaults for the same solver (Project Settings > Physics, Rigidbody).
constexpr float CONTACT_OFFSET = 0.01f;
constexpr float SLEEP_THRESHOLD = 0.005f;
/// Unity's Default Max Angular Speed since 2021.2 (it was 7 before).
constexpr float MAX_ANGULAR_VELOCITY = 50.0f;
constexpr float MAX_DEPENETRATION_VELOCITY = 10.0f;
constexpr uint32_t POSITION_ITERATIONS = 6;
constexpr uint32_t VELOCITY_ITERATIONS = 1;
/// What the project asked for (physics3d_setSolver), the constants above until it asks.
uint32_t gPositionIterations = POSITION_ITERATIONS;
uint32_t gVelocityIterations = VELOCITY_ITERATIONS;
bool gAllowSleep = true;
constexpr float BOUNCE_THRESHOLD = 2.0f;
/// The ABI names no mass, so a body weighs what its volume of water would: the
/// density the Jolt backend uses, so switching backend does not change a scene.
constexpr float DENSITY = 1000.0f;
constexpr PxU32 MAX_OVERLAP_HITS = 256;
PxMaterial* material(float friction, float restitution) {
uint32_t f = 0, r = 0;
std::memcpy(&f, &friction, 4);
std::memcpy(&r, &restitution, 4);
const uint64_t key = (static_cast<uint64_t>(f) << 32) | r;
auto found = g().materials.find(key);
if (found != g().materials.end()) return found->second;
// One friction for both, as the SDK authors one; Average is Unity's default
// combine for both friction and bounce.
PxMaterial* m = gPhysics->createMaterial(friction, friction, restitution);
m->setFrictionCombineMode(PxCombineMode::eAVERAGE);
m->setRestitutionCombineMode(PxCombineMode::eAVERAGE);
g().materials[key] = m;
return m;
}
PxFilterData filterData(uint32_t layer, uint32_t kind) {
PxFilterData d;
d.word0 = layer;
d.word1 = kind;
return d;
}
/**
* Who meets whom. Layers are read from the live matrix, as the Jolt backend
* reads its own. Characters push and are pushed but report no contacts — a
* Jolt character is not a body and reports none either — while a trigger does
* report them, which is what Unity's OnTriggerEnter on a player relies on.
*/
PxFilterFlags filterShader(PxFilterObjectAttributes a0, PxFilterData d0,
PxFilterObjectAttributes a1, PxFilterData d1,
PxPairFlags& flags, const void*, PxU32) {
if (!g().collide(d0.word0, d1.word0)) return PxFilterFlag::eSUPPRESS;
if (PxFilterObjectIsTrigger(a0) || PxFilterObjectIsTrigger(a1)) {
flags = PxPairFlag::eTRIGGER_DEFAULT;
return PxFilterFlag::eDEFAULT;
}
flags = PxPairFlag::eCONTACT_DEFAULT | PxPairFlag::eDETECT_CCD_CONTACT;
if (((d0.word1 | d1.word1) & px3::KIND_CHARACTER) == 0) {
flags |= PxPairFlag::eNOTIFY_TOUCH_FOUND | PxPairFlag::eNOTIFY_TOUCH_LOST
| PxPairFlag::eNOTIFY_CONTACT_POINTS | PxPairFlag::ePRE_SOLVER_VELOCITY;
}
return PxFilterFlag::eDEFAULT;
}
/** An actor's velocity at a world point before the solver, from the pair's own record of it; still things are 0. */
PxVec3 pointVelocity(const PxContactPairHeader& header, int side, const PxContactPairVelocity& v, const PxVec3& at) {
const PxContactPairHeaderFlag::Enum removed = side == 0 ? PxContactPairHeaderFlag::eREMOVED_ACTOR_0 : PxContactPairHeaderFlag::eREMOVED_ACTOR_1;
PxVec3 velocity = v.linearVelocity[side];
const PxActor* actor = header.actors[side];
if (!(header.flags & removed) && actor) {
if (const PxRigidBody* body = actor->is<PxRigidBody>()) {
const PxVec3 centre = (body->getGlobalPose() * body->getCMassLocalPose()).p;
velocity += v.angularVelocity[side].cross(at - centre);
}
}
return velocity;
}
/** What one side gives the meeting: 0 for a static or kinematic actor, or one that has been removed. */
float inverseMass(const PxContactPairHeader& header, int side) {
const PxContactPairHeaderFlag::Enum removed = side == 0 ? PxContactPairHeaderFlag::eREMOVED_ACTOR_0 : PxContactPairHeaderFlag::eREMOVED_ACTOR_1;
const PxActor* actor = header.actors[side];
if ((header.flags & removed) || !actor) return 0.0f;
const PxRigidDynamic* body = actor->is<PxRigidDynamic>();
if (!body || (body->getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC)) return 0.0f;
return body->getInvMass();
}
/** Records what touched what, in the layouts the Jolt module writes. */
class ContactRecorder final : public PxSimulationEventCallback {
public:
void onContact(const PxContactPairHeader& header, const PxContactPair* pairs, PxU32 count) override {
// A removed actor's pointer is not to be read; its side is nobody.
const uint32_t a = (header.flags & PxContactPairHeaderFlag::eREMOVED_ACTOR_0)
? 0u : px3::entityOf(header.actors[0]);
const uint32_t b = (header.flags & PxContactPairHeaderFlag::eREMOVED_ACTOR_1)
? 0u : px3::entityOf(header.actors[1]);
if (a == 0 && b == 0) return;
// This callback comes after the solver: the velocities the two had as they met are in the extra data, one set
// for each pair, asked for with ePRE_SOLVER_VELOCITY.
const PxContactPairVelocity* before[16] = {};
PxContactPairExtraDataIterator extra(header.extraDataStream, header.extraDataStreamSize);
while (extra.nextItemSet()) {
if (extra.preSolverVelocity && extra.contactPairIndex < 16) before[extra.contactPairIndex] = extra.preSolverVelocity;
}
for (PxU32 i = 0; i < count; ++i) {
const PxContactPair& pair = pairs[i];
if (pair.events & PxPairFlag::eNOTIFY_TOUCH_FOUND) {
PxContactPairPoint point;
PxVec3 normal(0.0f), position(0.0f);
if (pair.contactCount > 0 && pair.extractContacts(&point, 1) > 0) {
// PhysX points from the second shape to the first; the
// buffer, like Jolt's, points from A to B.
normal = -point.normal;
position = point.position;
}
g().contactEnterBuffer.insert(g().contactEnterBuffer.end(), {
static_cast<float>(a), static_cast<float>(b),
normal.x, normal.y, normal.z, position.x, position.y, position.z,
});
// How fast A closed on B at the point, along the normal from A to B; 0 where PhysX gave no velocities.
float closing = 0.0f;
if (i < 16 && before[i]) {
const PxVec3 at[2] = { pointVelocity(header, 0, *before[i], position), pointVelocity(header, 1, *before[i], position) };
closing = (at[0] - at[1]).dot(normal);
}
g().contactEnterSpeedBuffer.push_back(closing);
const float both = inverseMass(header, 0) + inverseMass(header, 1);
g().contactEnterMassBuffer.push_back(both > 0.0f ? 1.0f / both : std::numeric_limits<float>::infinity());
}
if (pair.events & PxPairFlag::eNOTIFY_TOUCH_LOST) {
g().contactExitBuffer.push_back(static_cast<float>(a));
g().contactExitBuffer.push_back(static_cast<float>(b));
}
}
}
void onTrigger(PxTriggerPair* pairs, PxU32 count) override {
for (PxU32 i = 0; i < count; ++i) {
const PxTriggerPair& pair = pairs[i];
const uint32_t sensor = (pair.flags & PxTriggerPairFlag::eREMOVED_SHAPE_TRIGGER)
? 0u : px3::entityOf(pair.triggerActor);
const uint32_t other = (pair.flags & PxTriggerPairFlag::eREMOVED_SHAPE_OTHER)
? 0u : px3::entityOf(pair.otherActor);
if (sensor == 0 && other == 0) continue;
// The sensor first, as the Jolt module writes it.
std::vector<float>& out = pair.status == PxPairFlag::eNOTIFY_TOUCH_FOUND
? g().sensorEnterBuffer : g().sensorExitBuffer;
if (pair.status != PxPairFlag::eNOTIFY_TOUCH_FOUND
&& pair.status != PxPairFlag::eNOTIFY_TOUCH_LOST) continue;
out.push_back(static_cast<float>(sensor));
out.push_back(static_cast<float>(other));
}
}
void onConstraintBreak(PxConstraintInfo*, PxU32) override {}
void onWake(PxActor**, PxU32) override {}
void onSleep(PxActor**, PxU32) override {}
void onAdvance(const PxRigidBody* const*, const PxTransform*, const PxU32) override {}
};
ContactRecorder& recorder() { static ContactRecorder r; return r; }
struct BodyMotion {
int motion = 0;
float gravityScale = 1.0f;
float linearDamping = 0.0f;
float angularDamping = 0.0f;
int fixedRotation = 0;
uint32_t layer = 0;
int continuous = 0;
};
PxQuat quat(float qx, float qy, float qz, float qw) {
PxQuat q(qx, qy, qz, qw);
return q.magnitudeSquared() > 0.0f ? q.getNormalized() : PxQuat(PxIdentity);
}
/// Register one shape as a body, and remember which entity it speaks for.
uint32_t addBody(uint32_t entity, const PxGeometry& geometry, const PxTransform& local,
float px, float py, float pz, float qx, float qy, float qz, float qw,
const BodyMotion& how, float friction, float restitution, int isSensor) {
if (!g().isValid()) return 0;
const PxTransform pose(PxVec3(px, py, pz), quat(qx, qy, qz, qw));
if (!pose.isValid()) return 0;
PxRigidActor* actor = nullptr;
PxRigidDynamic* dynamic = nullptr;
if (how.motion == 0) {
actor = gPhysics->createRigidStatic(pose);
} else {
dynamic = gPhysics->createRigidDynamic(pose);
actor = dynamic;
}
if (actor == nullptr) return 0;
// A trigger is still found by queries: Unity's queriesHitTriggers is on by default.
const PxShapeFlags shapeFlags = isSensor != 0
? PxShapeFlags(PxShapeFlag::eTRIGGER_SHAPE | PxShapeFlag::eSCENE_QUERY_SHAPE)
: PxShapeFlags(PxShapeFlag::eSIMULATION_SHAPE | PxShapeFlag::eSCENE_QUERY_SHAPE);
PxShape* shape = gPhysics->createShape(geometry, *material(friction, restitution), true, shapeFlags);
if (shape == nullptr) { actor->release(); return 0; }
shape->setLocalPose(local);
shape->setContactOffset(CONTACT_OFFSET);
shape->setRestOffset(0.0f);
shape->setSimulationFilterData(filterData(how.layer, 0));
shape->setQueryFilterData(filterData(how.layer, 0));
actor->attachShape(*shape);
shape->release();
auto record = std::make_unique<Body>();
record->entity = entity;
record->layer = how.layer;
record->actor = actor;
record->sensor = isSensor != 0;
record->gravityScale = how.gravityScale;
if (dynamic != nullptr) {
if (how.motion == 1) dynamic->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, true);
if (isSensor != 0) {
// A trigger has no volume to weigh; it needs a mass all the same.
dynamic->setMass(1.0f);
dynamic->setMassSpaceInertiaTensor(PxVec3(1.0f));
} else {
PxRigidBodyExt::updateMassAndInertia(*dynamic, DENSITY);
}
dynamic->setLinearDamping(how.linearDamping);
dynamic->setAngularDamping(how.angularDamping);
dynamic->setMaxAngularVelocity(MAX_ANGULAR_VELOCITY);
dynamic->setMaxDepenetrationVelocity(MAX_DEPENETRATION_VELOCITY);
// A threshold of 0 is energy no body falls below: it never sleeps.
dynamic->setSleepThreshold(gAllowSleep ? SLEEP_THRESHOLD : 0.0f);
dynamic->setSolverIterationCounts(gPositionIterations, gVelocityIterations);
if (how.fixedRotation != 0) {
dynamic->setRigidDynamicLockFlags(PxRigidDynamicLockFlag::eLOCK_ANGULAR_X
| PxRigidDynamicLockFlag::eLOCK_ANGULAR_Y | PxRigidDynamicLockFlag::eLOCK_ANGULAR_Z);
}
// A kinematic body follows its target exactly; sweeping it is an error in PhysX.
if (how.continuous != 0 && how.motion == 2) {
dynamic->setRigidBodyFlag(PxRigidBodyFlag::eENABLE_CCD, true);
}
if (how.gravityScale != 1.0f) actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, true);
}
const uint32_t id = g().nextBodyId++;
record->id = id;
actor->userData = record.get();
g().scene->addActor(*actor);
g().bodies[id] = std::move(record);
return id;
}
/// A collider's offset from the entity. A caller that leaves it off passes NaN,
/// which means none, as `> 0` reads it in the Jolt backend.
PxVec3 offset(float cx, float cy, float cz) {
const PxVec3 v(cx, cy, cz);
return v.isFinite() ? v : PxVec3(0.0f);
}
/// A capsule stands up: PhysX lays its axis along X, the SDK stands it along Y.
const PxQuat& capsuleUpright() {
static const PxQuat q(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f));
return q;
}
PxRigidDynamic* dynamicOf(uint32_t bodyId) {
Body* body = g().body(bodyId);
return body ? body->actor->is<PxRigidDynamic>() : nullptr;
}
bool isKinematic(const PxRigidDynamic* dynamic) {
return dynamic->getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC;
}
/// What a substep does before PhysX runs it: the gravity a factor stands in for,
/// and the targets kinematic bodies given a velocity move to.
void beforeSubstep(float h) {
const PxVec3 gravity = g().scene->getGravity();
for (auto& entry : g().bodies) {
Body& body = *entry.second;
PxRigidDynamic* dynamic = body.actor->is<PxRigidDynamic>();
if (dynamic == nullptr) continue;
if (isKinematic(dynamic)) {
if (!body.hasKinematicVelocity) continue;
PxTransform target = dynamic->getGlobalPose();
target.p += body.kinematicVelocity * h;
dynamic->setKinematicTarget(target);
} else if (body.gravityScale != 1.0f && !dynamic->isSleeping()) {
dynamic->addForce(gravity * body.gravityScale, PxForceMode::eACCELERATION, false);
}
}
}
} // namespace
namespace esengine::physics3d_physx {
World& world() { static World instance; return instance; }
PxPhysics& physics() { return *gPhysics; }
PxMaterial* defaultMaterial() { return material(0.6f, 0.0f); }
} // namespace esengine::physics3d_physx
extern "C" {
// World lifecycle
EMSCRIPTEN_KEEPALIVE
void physics3d_init(float gx, float gy, float gz, uint32_t) {
if (g().isValid()) return;
// Foundation and SDK are process-wide, and PhysX allows one foundation: a
// world built after a shutdown reuses them.
if (gFoundation == nullptr) {
gFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, allocator(), errors());
if (gFoundation == nullptr) return;
gPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, PxTolerancesScale(), false, nullptr);
if (gPhysics == nullptr) return;
// No PxInitExtensions: it registers the serializers (and with them the
// metadata of every PhysX type), and joints need neither without PVD.
}
// No worker threads: a wasm module has none, and the tasks run on the
// thread that steps.
g().dispatcher = PxDefaultCpuDispatcherCreate(0);
PxSceneDesc desc(gPhysics->getTolerancesScale());
desc.gravity = PxVec3(gx, gy, gz);
desc.cpuDispatcher = g().dispatcher;
desc.filterShader = filterShader;
desc.simulationEventCallback = &recorder();
desc.flags |= PxSceneFlag::eENABLE_ACTIVE_ACTORS | PxSceneFlag::eENABLE_CCD | PxSceneFlag::eENABLE_PCM;
desc.frictionType = PxFrictionType::ePATCH;
desc.solverType = PxSolverType::ePGS;
desc.bounceThresholdVelocity = BOUNCE_THRESHOLD;
g().scene = gPhysics->createScene(desc);
if (g().scene == nullptr) return;
g().controllers = PxCreateControllerManager(*g().scene);
}
EMSCRIPTEN_KEEPALIVE
void physics3d_shutdown() {
if (!g().isValid()) return;
for (auto& entry : g().joints) entry.second.joint->release();
g().joints.clear();
g().characters.clear();
if (g().controllers) g().controllers->release(); // releases every controller
g().controllers = nullptr;
for (auto& entry : g().bodies) entry.second->actor->release();
g().bodies.clear();
g().scene->release();
g().scene = nullptr;
g().dispatcher->release();
g().dispatcher = nullptr;
g().transformBuffer.clear();
g().queryBuffer.clear();
g().contactEnterBuffer.clear();
g().contactEnterSpeedBuffer.clear();
g().contactEnterMassBuffer.clear();
g().contactExitBuffer.clear();
g().sensorEnterBuffer.clear();
g().sensorExitBuffer.clear();
}
EMSCRIPTEN_KEEPALIVE
int physics3d_isReady() { return g().isValid() ? 1 : 0; }
EMSCRIPTEN_KEEPALIVE
void physics3d_setLayerMask(uint32_t layer, uint32_t mask) {
if (layer < px3::LAYER_COUNT) g().masks[layer] = mask;
}
EMSCRIPTEN_KEEPALIVE
void physics3d_setSolver(int velocityIterations, int positionIterations, int allowSleep,
float bounceThreshold) {
gVelocityIterations = velocityIterations > 0 ? static_cast<uint32_t>(velocityIterations) : VELOCITY_ITERATIONS;
gPositionIterations = positionIterations > 0 ? static_cast<uint32_t>(positionIterations) : POSITION_ITERATIONS;
gAllowSleep = allowSleep != 0;
if (g().isValid()) g().scene->setBounceThresholdVelocity(bounceThreshold > 0.0f ? bounceThreshold : BOUNCE_THRESHOLD);
}
EMSCRIPTEN_KEEPALIVE
void physics3d_step(float dt, int collisionSteps) {
if (!g().isValid() || dt <= 0.0f) return;
// No character moved this step: what the last one met has been read, and is not told again.
if (g().characterHitsStale) g().characterHitBuffer.clear();
g().contactEnterBuffer.clear();
g().contactEnterSpeedBuffer.clear();
g().contactEnterMassBuffer.clear();
g().contactExitBuffer.clear();
g().sensorEnterBuffer.clear();
g().sensorExitBuffer.clear();
const int steps = collisionSteps > 0 ? collisionSteps : 1;
const float h = dt / static_cast<float>(steps);
for (int i = 0; i < steps; ++i) {
beforeSubstep(h);
g().scene->simulate(h);
g().scene->fetchResults(true);
}
PxU32 count = 0;
PxActor** active = g().scene->getActiveActors(count);
std::vector<float>& out = g().transformBuffer;
out.clear();
out.reserve(count * 8);
for (PxU32 i = 0; i < count; ++i) {
const PxRigidActor* actor = active[i]->is<PxRigidActor>();
if (actor == nullptr || actor->userData == nullptr) continue;
const Body* body = static_cast<const Body*>(actor->userData);
if (body->character) continue; // its pose comes back from moveCharacter
const PxTransform pose = actor->getGlobalPose();
out.insert(out.end(), {
static_cast<float>(body->entity), pose.p.x, pose.p.y, pose.p.z,
pose.q.x, pose.q.y, pose.q.z, pose.q.w,
});
}
g().characterHitsStale = true;
}
EMSCRIPTEN_KEEPALIVE
void physics3d_optimize() {
if (g().isValid()) g().scene->forceRebuildDynamicTree(PX_SCENE_PRUNER_STATIC);
}
// Bodies
EMSCRIPTEN_KEEPALIVE
uint32_t physics3d_addCompound(uint32_t entity, uintptr_t partsPtr, uint32_t count,
float px, float py, float pz, float qx, float qy, float qz, float qw,
int motion, float gravityScale, float linearDamping, float angularDamping,
int fixedRotation, uint32_t layer, int continuous,
float friction, float restitution, int isSensor, float mass) {
if (!g().isValid() || partsPtr == 0 || count == 0 || count > 16384) return 0;
const float* parts = reinterpret_cast<const float*>(partsPtr);
for (uint32_t i = 0; i < count; ++i) {
const float* p = parts + i * 11;
for (uint32_t j = 0; j < 11; ++j) if (!std::isfinite(p[j])) return 0;
if (!(p[1] > 0) || (p[0] == 0 && !(p[2] > 0 && p[3] > 0))
|| (p[0] == 2 && p[2] < 0) || (p[0] != 0 && p[0] != 1 && p[0] != 2)
|| PxQuat(p[7], p[8], p[9], p[10]).magnitudeSquared() < 1.0e-12f) return 0;
}
uint32_t id = 0;
const PxShapeFlags flags = isSensor != 0
? PxShapeFlags(PxShapeFlag::eTRIGGER_SHAPE | PxShapeFlag::eSCENE_QUERY_SHAPE)
: PxShapeFlags(PxShapeFlag::eSIMULATION_SHAPE | PxShapeFlag::eSCENE_QUERY_SHAPE);
for (uint32_t i = 0; i < count; ++i) {
const float* p = parts + i * 11;
PxGeometryHolder geometry;
PxQuat rotation = quat(p[7], p[8], p[9], p[10]);
if (p[0] == 0) geometry.storeAny(PxBoxGeometry(p[1], p[2], p[3]));
else if (p[0] == 1 || p[2] == 0) geometry.storeAny(PxSphereGeometry(p[1]));
else { geometry.storeAny(PxCapsuleGeometry(p[1], p[2])); rotation = rotation * capsuleUpright(); }
const PxTransform local(PxVec3(p[4], p[5], p[6]), rotation);
if (i == 0) {
id = addBody(entity, geometry.any(), local, px, py, pz, qx, qy, qz, qw,
{motion, gravityScale, linearDamping, angularDamping, fixedRotation, layer, continuous},
friction, restitution, isSensor);
if (id == 0) return 0;
} else {
PxShape* shape = gPhysics->createShape(geometry.any(), *material(friction, restitution), true, flags);
if (!shape) { physics3d_removeBody(id); return 0; }
shape->setLocalPose(local);
shape->setContactOffset(CONTACT_OFFSET);
shape->setRestOffset(0.0f);
shape->setSimulationFilterData(filterData(layer, 0));
shape->setQueryFilterData(filterData(layer, 0));
const bool attached = g().body(id)->actor->attachShape(*shape);
shape->release();
if (!attached) { physics3d_removeBody(id); return 0; }
}
}
if (auto* dynamic = dynamicOf(id)) {
if (!isSensor) {
if (mass > 0 && std::isfinite(mass)) PxRigidBodyExt::setMassAndUpdateInertia(*dynamic, mass);
else PxRigidBodyExt::updateMassAndInertia(*dynamic, DENSITY);
}
}
return id;
}
EMSCRIPTEN_KEEPALIVE
uint32_t physics3d_addBox(uint32_t entity, float hx, float hy, float hz,
float px, float py, float pz,
float qx, float qy, float qz, float qw,
int motion, float gravityScale, float linearDamping,
float angularDamping, int fixedRotation, uint32_t layer,
int continuous, float friction, float restitution, int isSensor,
float cx, float cy, float cz) {
if (!(hx > 0.0f && hy > 0.0f && hz > 0.0f)) return 0;
return addBody(entity, PxBoxGeometry(hx, hy, hz), PxTransform(offset(cx, cy, cz)),
px, py, pz, qx, qy, qz, qw,
{motion, gravityScale, linearDamping, angularDamping, fixedRotation, layer, continuous},
friction, restitution, isSensor);
}
EMSCRIPTEN_KEEPALIVE
uint32_t physics3d_addSphere(uint32_t entity, float radius,
float px, float py, float pz,
float qx, float qy, float qz, float qw,
int motion, float gravityScale, float linearDamping,
float angularDamping, int fixedRotation, uint32_t layer,
int continuous, float friction, float restitution, int isSensor,
float cx, float cy, float cz) {
if (!(radius > 0.0f)) return 0;
return addBody(entity, PxSphereGeometry(radius), PxTransform(offset(cx, cy, cz)),
px, py, pz, qx, qy, qz, qw,
{motion, gravityScale, linearDamping, angularDamping, fixedRotation, layer, continuous},
friction, restitution, isSensor);
}
EMSCRIPTEN_KEEPALIVE
uint32_t physics3d_addCapsule(uint32_t entity, float radius, float halfHeight,
float px, float py, float pz,
float qx, float qy, float qz, float qw,
int motion, float gravityScale, float linearDamping,
float angularDamping, int fixedRotation, uint32_t layer,
int continuous, float friction, float restitution, int isSensor,
float cx, float cy, float cz) {
if (!(radius > 0.0f) || !(halfHeight >= 0.0f)) return 0;
return addBody(entity, PxCapsuleGeometry(radius, halfHeight),
PxTransform(offset(cx, cy, cz), capsuleUpright()),
px, py, pz, qx, qy, qz, qw,
{motion, gravityScale, linearDamping, angularDamping, fixedRotation, layer, continuous},
friction, restitution, isSensor);
}
EMSCRIPTEN_KEEPALIVE
uint32_t physics3d_addMeshBody(uint32_t entity, uintptr_t vertexPtr, uint32_t vertexCount,
uintptr_t indexPtr, uint32_t indexCount,
float px, float py, float pz,
float qx, float qy, float qz, float qw,
uint32_t layer, float friction, float restitution) {
if (!g().isValid() || vertexCount == 0 || indexCount < 3 || indexCount % 3 != 0) return 0;
const float* positions = reinterpret_cast<const float*>(vertexPtr);
const uint32_t* indices = reinterpret_cast<const uint32_t*>(indexPtr);
if (positions == nullptr || indices == nullptr) return 0;
for (uint32_t i = 0; i < indexCount; ++i) {
if (indices[i] >= vertexCount) return 0; // would read past the vertices
}
PxTriangleMeshDesc desc;
desc.points.count = vertexCount;
desc.points.stride = sizeof(float) * 3;
desc.points.data = positions;
desc.triangles.count = indexCount / 3;
desc.triangles.stride = sizeof(uint32_t) * 3;
desc.triangles.data = indices;
const PxCookingParams params(gPhysics->getTolerancesScale());
PxTriangleMesh* mesh = PxCreateTriangleMesh(params, desc, gPhysics->getPhysicsInsertionCallback());
if (mesh == nullptr) return 0;
// A triangle soup has no inside, so no mass: always static, as in the Jolt backend.
const uint32_t id = addBody(entity, PxTriangleMeshGeometry(mesh), PxTransform(PxIdentity),
px, py, pz, qx, qy, qz, qw, {0, 1.0f, 0.0f, 0.0f, 0, layer},
friction, restitution, 0);
mesh->release(); // the shape holds its own reference
return id;
}
EMSCRIPTEN_KEEPALIVE
uint32_t physics3d_addConvexBody(uint32_t entity, uintptr_t vertexPtr, uint32_t vertexCount,
float px, float py, float pz,
float qx, float qy, float qz, float qw,
int motion, float gravityScale, float linearDamping,
float angularDamping, int fixedRotation, uint32_t layer,
int continuous, float friction, float restitution, int isSensor) {
if (!g().isValid() || vertexCount < 4) return 0;
const float* positions = reinterpret_cast<const float*>(vertexPtr);
if (positions == nullptr) return 0;
PxConvexMeshDesc desc;
desc.points.count = vertexCount;
desc.points.stride = sizeof(float) * 3;
desc.points.data = positions;
// PhysX keeps at most 255 hull vertices, Unity's limit as well.
desc.flags = PxConvexFlag::eCOMPUTE_CONVEX;
const PxCookingParams params(gPhysics->getTolerancesScale());
PxConvexMesh* mesh = PxCreateConvexMesh(params, desc, gPhysics->getPhysicsInsertionCallback());
// Every point on a plane, or all the same, has no hull.
if (mesh == nullptr) return 0;
const uint32_t id = addBody(entity, PxConvexMeshGeometry(mesh), PxTransform(PxIdentity),
px, py, pz, qx, qy, qz, qw,
{motion, gravityScale, linearDamping, angularDamping, fixedRotation, layer, continuous},
friction, restitution, isSensor);
mesh->release();
return id;
}
EMSCRIPTEN_KEEPALIVE
void physics3d_removeBody(uint32_t bodyId) {
auto found = g().bodies.find(bodyId);
if (!g().isValid() || found == g().bodies.end()) return;
PxRigidActor* actor = found->second->actor;
actor->userData = nullptr;
actor->release(); // leaves the scene as it goes
g().bodies.erase(found);
}
EMSCRIPTEN_KEEPALIVE
void physics3d_setTransform(uint32_t bodyId, float px, float py, float pz,
float qx, float qy, float qz, float qw) {
Body* body = g().body(bodyId);
if (!g().isValid() || body == nullptr) return;
const PxTransform pose(PxVec3(px, py, pz), quat(qx, qy, qz, qw));
if (!pose.isValid()) return;
// A teleport, as SetPositionAndRotation is in Jolt; it wakes the body too.
body->actor->setGlobalPose(pose, true);
if (PxRigidDynamic* dynamic = body->actor->is<PxRigidDynamic>()) {
if (isKinematic(dynamic)) dynamic->setKinematicTarget(pose);
}
}
EMSCRIPTEN_KEEPALIVE
void physics3d_setMass(uint32_t bodyId, float mass) {
Body* body = g().body(bodyId);
if (!g().isValid() || body == nullptr || !(mass > 0.0f) || !std::isfinite(mass)) return;
PxRigidDynamic* dynamic = body->actor->is<PxRigidDynamic>();
if (dynamic == nullptr || isKinematic(dynamic)) return;
PxRigidBodyExt::setMassAndUpdateInertia(*dynamic, mass);
}
EMSCRIPTEN_KEEPALIVE
void physics3d_setLinearVelocity(uint32_t bodyId, float vx, float vy, float vz) {
Body* body = g().body(bodyId);
if (!g().isValid() || body == nullptr) return;
PxRigidDynamic* dynamic = body->actor->is<PxRigidDynamic>();
if (dynamic == nullptr) return;
const PxVec3 v(vx, vy, vz);
if (!v.isFinite()) return;
if (isKinematic(dynamic)) {
body->kinematicVelocity = v;
body->hasKinematicVelocity = !v.isZero();
} else {
dynamic->setLinearVelocity(v, true);
}
}
EMSCRIPTEN_KEEPALIVE
int physics3d_getBodyState(uint32_t bodyId) {
Body* body = g().body(bodyId);
if (!g().isValid() || body == nullptr) return 0;
const PxTransform pose = body->actor->getGlobalPose();
PxVec3 v(0.0f);
if (const PxRigidDynamic* dynamic = body->actor->is<PxRigidDynamic>()) {
v = isKinematic(dynamic) ? body->kinematicVelocity : dynamic->getLinearVelocity();
}
g().queryBuffer = {pose.p.x, pose.p.y, pose.p.z, pose.q.x, pose.q.y, pose.q.z, pose.q.w, v.x, v.y, v.z};
return 1;
}
EMSCRIPTEN_KEEPALIVE
int physics3d_getBodyDynamics(uint32_t bodyId) {
PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return 0;
const float mass = dynamic->getMass();
if (!(mass > 0.0f)) return 0;
const PxVec3 w = dynamic->getAngularVelocity();
g().queryBuffer = {mass, w.x, w.y, w.z};
return 1;
}
EMSCRIPTEN_KEEPALIVE
int physics3d_getBodyMassData(uint32_t bodyId) {
PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return 0;
const float mass = dynamic->getMass();
if (!(mass > 0.0f)) return 0;
const PxVec3 centre = (dynamic->getGlobalPose() * dynamic->getCMassLocalPose()).p;
// About the body's own axes, as the Jolt world answers: the mass frame may be turned from them, so each axis is
// taken into it, its inverse moments weighed along it, and the moment is the inverse of that.
const PxVec3 inertia = dynamic->getMassSpaceInertiaTensor();
const PxQuat frame = dynamic->getCMassLocalPose().q;
const PxRigidDynamicLockFlags locked = dynamic->getRigidDynamicLockFlags();
const float inf = std::numeric_limits<float>::infinity();
const auto moment = [&](const PxVec3& axis, bool isLocked) {
if (isLocked) return inf;
const PxVec3 a = frame.rotateInv(axis);
const auto inv = [](float i) { return i > 0.0f ? 1.0f / i : 0.0f; };
const float weighed = a.x * a.x * inv(inertia.x) + a.y * a.y * inv(inertia.y) + a.z * a.z * inv(inertia.z);
return weighed > 0.0f ? 1.0f / weighed : inf;
};
g().queryBuffer = {mass, centre.x, centre.y, centre.z,
moment(PxVec3(1, 0, 0), static_cast<bool>(locked & PxRigidDynamicLockFlag::eLOCK_ANGULAR_X)),
moment(PxVec3(0, 1, 0), static_cast<bool>(locked & PxRigidDynamicLockFlag::eLOCK_ANGULAR_Y)),
moment(PxVec3(0, 0, 1), static_cast<bool>(locked & PxRigidDynamicLockFlag::eLOCK_ANGULAR_Z))};
return 1;
}
EMSCRIPTEN_KEEPALIVE
void physics3d_addForce(uint32_t bodyId, float fx, float fy, float fz, float px, float py, float pz) {
PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return;
// A point that is not a number is its centre of mass: pushed there, it turns nothing.
if (std::isfinite(px) && std::isfinite(py) && std::isfinite(pz)) PxRigidBodyExt::addForceAtPos(*dynamic, PxVec3(fx, fy, fz), PxVec3(px, py, pz), PxForceMode::eFORCE, true);
else dynamic->addForce(PxVec3(fx, fy, fz), PxForceMode::eFORCE, true);
}
EMSCRIPTEN_KEEPALIVE
void physics3d_addImpulse(uint32_t bodyId, float ix, float iy, float iz, float px, float py, float pz) {
PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return;
const PxVec3 impulse(ix, iy, iz);
if (!impulse.isFinite()) return;
if (std::isfinite(px) && std::isfinite(py) && std::isfinite(pz)) PxRigidBodyExt::addForceAtPos(*dynamic, impulse, PxVec3(px, py, pz), PxForceMode::eIMPULSE, true);
else dynamic->addForce(impulse, PxForceMode::eIMPULSE, true);
}
EMSCRIPTEN_KEEPALIVE
void physics3d_addAngularImpulse(uint32_t bodyId, float ix, float iy, float iz) {
PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return;
dynamic->addTorque(PxVec3(ix, iy, iz), PxForceMode::eIMPULSE, true);
}
EMSCRIPTEN_KEEPALIVE
void physics3d_setAngularVelocity(uint32_t bodyId, float wx, float wy, float wz) {
PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return;
dynamic->setAngularVelocity(PxVec3(wx, wy, wz), true);
}
EMSCRIPTEN_KEEPALIVE
void physics3d_setGravity(float gx, float gy, float gz) {
if (!g().isValid()) return;
g().scene->setGravity(PxVec3(gx, gy, gz));
}
EMSCRIPTEN_KEEPALIVE
void physics3d_setAwake(uint32_t bodyId, int awake) {
PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return;
if (awake) dynamic->wakeUp(); else dynamic->putToSleep();
}
EMSCRIPTEN_KEEPALIVE
int physics3d_isAwake(uint32_t bodyId) {
const PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return 0;
return dynamic->isSleeping() ? 0 : 1;
}
EMSCRIPTEN_KEEPALIVE
void physics3d_addTorque(uint32_t bodyId, float tx, float ty, float tz) {
PxRigidDynamic* dynamic = dynamicOf(bodyId);
if (!g().isValid() || dynamic == nullptr || isKinematic(dynamic)) return;
dynamic->addTorque(PxVec3(tx, ty, tz), PxForceMode::eFORCE, true);
}
// Readback
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_transforms() { return reinterpret_cast<uintptr_t>(g().transformBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_transformsBytes() { return g().transformBuffer.size() * sizeof(float); }
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_characterHits() { return reinterpret_cast<uintptr_t>(g().characterHitBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_characterHitsBytes() { return g().characterHitBuffer.size() * sizeof(float); }
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_contactEnterMasses() { return reinterpret_cast<uintptr_t>(g().contactEnterMassBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_contactEnterMassesBytes() { return g().contactEnterMassBuffer.size() * sizeof(float); }
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_contactEnterSpeeds() { return reinterpret_cast<uintptr_t>(g().contactEnterSpeedBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_contactEnterSpeedsBytes() { return g().contactEnterSpeedBuffer.size() * sizeof(float); }
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_contactEnters() { return reinterpret_cast<uintptr_t>(g().contactEnterBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_contactEntersBytes() { return g().contactEnterBuffer.size() * sizeof(float); }
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_contactExits() { return reinterpret_cast<uintptr_t>(g().contactExitBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_contactExitsBytes() { return g().contactExitBuffer.size() * sizeof(float); }
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_sensorEnters() { return reinterpret_cast<uintptr_t>(g().sensorEnterBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_sensorEntersBytes() { return g().sensorEnterBuffer.size() * sizeof(float); }
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_sensorExits() { return reinterpret_cast<uintptr_t>(g().sensorExitBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_sensorExitsBytes() { return g().sensorExitBuffer.size() * sizeof(float); }
EMSCRIPTEN_KEEPALIVE
uintptr_t physics3d_queryResult() { return reinterpret_cast<uintptr_t>(g().queryBuffer.data()); }
EMSCRIPTEN_KEEPALIVE
size_t physics3d_queryResultBytes() { return g().queryBuffer.size() * sizeof(float); }
} // extern "C"