Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Physics3DBindings.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
25#pragma once
26
27#include <cstddef>
28#include <cstdint>
29
30extern "C" {
31
32// World lifecycle
33void physics3d_init(float gx, float gy, float gz, uint32_t maxBodies);
36void physics3d_step(float dt, int collisionSteps);
38void physics3d_setLayerMask(uint32_t layer, uint32_t mask);
43int physics3d_raycastAll(float ox, float oy, float oz, float dx, float dy, float dz, uint32_t layerMask, uint32_t maxHits);
46int physics3d_raycastWith(float ox, float oy, float oz, float dx, float dy, float dz,
47 uint32_t layerMask, uint32_t flags);
50void physics3d_setSolver(int velocityIterations, int positionIterations, int allowSleep,
51 float bounceThreshold);
52
53// Bodies
54uint32_t physics3d_addCompound(uint32_t entity, uintptr_t partsPtr, uint32_t count,
55 float px, float py, float pz, float qx, float qy, float qz, float qw,
56 int motion, float gravityScale, float linearDamping, float angularDamping,
57 int fixedRotation, uint32_t layer, int continuous,
58 float friction, float restitution, int isSensor, float mass);
59uint32_t physics3d_addBox(uint32_t entity, float hx, float hy, float hz,
60 float px, float py, float pz,
61 float qx, float qy, float qz, float qw,
62 int motion, float gravityScale, float linearDamping,
63 float angularDamping, int fixedRotation, uint32_t layer,
64 int continuous, float friction, float restitution, int isSensor,
65 float cx, float cy, float cz);
66uint32_t physics3d_addSphere(uint32_t entity, float radius,
67 float px, float py, float pz,
68 float qx, float qy, float qz, float qw,
69 int motion, float gravityScale, float linearDamping,
70 float angularDamping, int fixedRotation, uint32_t layer,
71 int continuous, float friction, float restitution, int isSensor,
72 float cx, float cy, float cz);
73uint32_t physics3d_addCapsule(uint32_t entity, float radius, float halfHeight,
74 float px, float py, float pz,
75 float qx, float qy, float qz, float qw,
76 int motion, float gravityScale, float linearDamping,
77 float angularDamping, int fixedRotation, uint32_t layer,
78 int continuous, float friction, float restitution, int isSensor,
79 float cx, float cy, float cz);
80uint32_t physics3d_addMeshBody(uint32_t entity, uintptr_t vertexPtr, uint32_t vertexCount,
81 uintptr_t indexPtr, uint32_t indexCount,
82 float px, float py, float pz,
83 float qx, float qy, float qz, float qw,
84 uint32_t layer, float friction, float restitution);
85uint32_t physics3d_addConvexBody(uint32_t entity, uintptr_t vertexPtr, uint32_t vertexCount,
86 float px, float py, float pz,
87 float qx, float qy, float qz, float qw,
88 int motion, float gravityScale, float linearDamping,
89 float angularDamping, int fixedRotation, uint32_t layer,
90 int continuous, float friction, float restitution, int isSensor);
91void physics3d_removeBody(uint32_t bodyId);
92void physics3d_setTransform(uint32_t bodyId, float px, float py, float pz,
93 float qx, float qy, float qz, float qw);
94void physics3d_setLinearVelocity(uint32_t bodyId, float vx, float vy, float vz);
95// A moving body's mass, in kilograms, its inertia scaled with it so it turns as its shape says; nothing for a body
96// that does not move, or a mass that is not a positive number.
97void physics3d_setMass(uint32_t bodyId, float mass);
98// A blow of (ix, iy, iz) newton seconds at the world point (px, py, pz) - at its centre of mass where the point is not a
99// number - all at once: its speed changes by impulse /
100// mass, and it turns by as much as the point is off its centre of mass. Wakes it. Nothing for a body that does not move.
101void physics3d_addImpulse(uint32_t bodyId, float ix, float iy, float iz, float px, float py, float pz);
102// A twist of (ix, iy, iz) newton metre seconds, all at once. Wakes it.
103void physics3d_addAngularImpulse(uint32_t bodyId, float ix, float iy, float iz);
104// Its spin, in radians a second about each world axis. Wakes it.
105void physics3d_setAngularVelocity(uint32_t bodyId, float wx, float wy, float wz);
106// The world's gravity, in metres a second squared. A sleeping body stays asleep until something wakes it.
107void physics3d_setGravity(float gx, float gy, float gz);
108// Wake a body (awake != 0) or put it to sleep; and whether it is awake: 1 awake, 0 asleep or not a moving body.
109void physics3d_setAwake(uint32_t bodyId, int awake);
110int physics3d_isAwake(uint32_t bodyId);
111// Writes (mass, cx, cy, cz, ix, iy, iz): kilograms, its centre of mass in the world in metres, and how hard it is to
112// turn about each of its own axes in kg m^2 - the moment that answers a twist about that axis alone, its moment of
113// inertia there where the axis is a principal one, as a box's are; infinite about an axis it may not turn on.
114// 0 for a body that does not move.
115int physics3d_getBodyMassData(uint32_t bodyId);
116int physics3d_getBodyState(uint32_t bodyId);
117// What a force needs to know of a body: its mass and how it is turning.
118int physics3d_getBodyDynamics(uint32_t bodyId);
119// Forces, held until the next step and applied over it: at a world point (so it turns the body as well as pushing it), and as a torque.
120void physics3d_addForce(uint32_t bodyId, float fx, float fy, float fz, float px, float py, float pz);
121void physics3d_addTorque(uint32_t bodyId, float tx, float ty, float tz);
122
123// Characters — swept rather than solved, and not bodies.
124uint32_t physics3d_addCharacter(uint32_t entity, float radius, float halfHeight,
125 float px, float py, float pz, float maxSlope, float mass,
126 uint32_t layer, float pushForce);
127void physics3d_removeCharacter(uint32_t characterId);
128void physics3d_moveCharacter(uint32_t characterId, float vx, float vy, float vz,
129 float dt, float stepUp, float stepDown);
130void physics3d_setCharacterPosition(uint32_t characterId, float px, float py, float pz);
131
132// Joints
133uint32_t physics3d_addSwingTwistJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB,
134 float px, float py, float pz, float ax, float ay, float az,
135 float nx, float ny, float nz, float normalHalf, float planeHalf,
136 float twistMin, float twistMax, int collideConnected);
137uint32_t physics3d_addSixDOFJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB,
138 float px, float py, float pz, float ax, float ay, float az, float nx, float ny, float nz,
139 int translationMotion, int rotationMotion,
140 float lx, float ly, float lz, float hx, float hy, float hz,
141 float rx, float ry, float rz, float sx, float sy, float sz, int collideConnected);
142void physics3d_setAdvancedJointMotor(uint32_t entity, int enable,
143 float vx, float vy, float vz, float wx, float wy, float wz, float maxForce, float maxTorque);
144uint32_t physics3d_addPointJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB,
145 float px, float py, float pz, int collideConnected);
146uint32_t physics3d_addHingeJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB,
147 float px, float py, float pz,
148 float ax, float ay, float az,
149 int enableLimit, float lowerAngle, float upperAngle,
150 int enableMotor, float motorSpeed, float maxTorque,
151 int collideConnected);
152uint32_t physics3d_addSliderJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB,
153 float px, float py, float pz,
154 float ax, float ay, float az,
155 int enableLimit, float lower, float upper,
156 int enableMotor, float motorSpeed, float maxForce,
157 int collideConnected);
158uint32_t physics3d_addDistanceJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB,
159 float ax, float ay, float az,
160 float bx, float by, float bz,
161 float minLength, float maxLength,
162 float frequency, float damping, int collideConnected);
163uint32_t physics3d_addFixedJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB,
164 int collideConnected);
165void physics3d_removeJoint(uint32_t entity, uint32_t bodyA, uint32_t bodyB);
166void physics3d_setJointMotor(uint32_t entity, int enable, float speed);
167// What the joint held its two bodies together with over the last step: writes (force in newtons, torque in newton
168// metres) into the query buffer, its limits' share included and its motor's not. 1 for a joint, 0 for none.
169int physics3d_jointForce(uint32_t entity);
170// A hinge's angles (radians) or a slider's travel (metres) it may move between, or none (enable 0). A range is
171// measured from the pose the joint was made in and always holds it: one that does not reach 0 is widened to 0.
172void physics3d_setJointLimits(uint32_t entity, int enable, float lower, float upper);
173// The most a hinge's motor may twist (newton metres) or a slider's push (newtons).
174void physics3d_setJointMotorLimit(uint32_t entity, float limit);
175// A distance joint's shortest and longest (metres), and its spring (frequency 0 is rigid).
176void physics3d_setDistanceJoint(uint32_t entity, float minLength, float maxLength, float frequency, float damping);
177float physics3d_jointValue(uint32_t entity);
178
179// Queries. Each answers a hit count and leaves the hits in the query buffer.
180int physics3d_raycast(float ox, float oy, float oz, float dx, float dy, float dz,
181 uint32_t layerMask);
182// A box of half-extents (hx, hy, hz), turned by (qx, qy, qz, qw), swept from (px, py, pz) along (dx, dy, dz): the
183// first body it would meet, written as sphereCast writes it.
184int physics3d_boxCast(float px, float py, float pz, float hx, float hy, float hz,
185 float qx, float qy, float qz, float qw, float dx, float dy, float dz, uint32_t layerMask);
186// An upright capsule (its length along its own y, as a character stands), turned by (qx, qy, qz, qw), swept the same way.
187int physics3d_capsuleCast(float px, float py, float pz, float radius, float halfHeight,
188 float qx, float qy, float qz, float qw, float dx, float dy, float dz, uint32_t layerMask);
189int physics3d_sphereCast(float px, float py, float pz, float radius,
190 float dx, float dy, float dz, uint32_t layerMask);
191int physics3d_overlapSphere(float px, float py, float pz, float radius, uint32_t layerMask);
192int physics3d_overlapBox(float px, float py, float pz, float hx, float hy, float hz,
193 uint32_t layerMask);
194
195// Readback. A pointer crosses as an integer, the same way the 2D module's
196// getters do: the caller reads `*Bytes()` from the module and copies exactly
197// that much out of the heap it names.
198
199// @heapreturn physics3d_transformsBytes()
200uintptr_t physics3d_transforms();
202// @heapreturn physics3d_contactEntersBytes()
203uintptr_t physics3d_contactEnters();
205// How fast the two came together at each contact begun this step, along its normal, in the world's own units a
206// second: one float for each entry of physics3d_contactEnters, in its order.
207// @heapreturn physics3d_contactEnterSpeedsBytes()
210// The mass the two met with at each contact begun this step, in kilograms: 1 / (1/a + 1/b), a body that does not move
211// counting as one of no inverse mass, so a stone on a floor meets it with the stone's own mass. Infinite where neither
212// moves of itself (a kinematic body into a static one). One float for each entry of physics3d_contactEnters.
213// @heapreturn physics3d_contactEnterMassesBytes()
216// What characters ran into this step, each meeting once until they part: (character entity, other entity, px,py,pz,
217// nx,ny,nz with the normal toward the character, closing speed, meeting mass in kilograms) for each, the speed and mass
218// as a contact between bodies gives them. Read after the step; the next step's first move starts it again.
219// @heapreturn physics3d_characterHitsBytes()
220uintptr_t physics3d_characterHits();
222// @heapreturn physics3d_contactExitsBytes()
223uintptr_t physics3d_contactExits();
225// @heapreturn physics3d_sensorEntersBytes()
226uintptr_t physics3d_sensorEnters();
228// @heapreturn physics3d_sensorExitsBytes()
229uintptr_t physics3d_sensorExits();
231// @heapreturn physics3d_queryResultBytes()
232uintptr_t physics3d_queryResult();
234
235} // extern "C"
void physics3d_setSolver(int velocityIterations, int positionIterations, int allowSleep, float bounceThreshold)
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)
int physics3d_overlapBox(float px, float py, float pz, float hx, float hy, float hz, uint32_t layerMask)
The same for a box, given its half-extents.
uintptr_t physics3d_sensorEnters()
int physics3d_raycast(float ox, float oy, float oz, float dx, float dy, float dz, uint32_t layerMask)
size_t physics3d_queryResultBytes()
size_t physics3d_sensorEntersBytes()
void physics3d_addImpulse(uint32_t bodyId, float ix, float iy, float iz, float px, float py, float pz)
void physics3d_removeCharacter(uint32_t characterId)
uintptr_t physics3d_queryResult()
void physics3d_setAwake(uint32_t bodyId, int awake)
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)
int physics3d_raycastAll(float ox, float oy, float oz, float dx, float dy, float dz, uint32_t layerMask, uint32_t maxHits)
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)
Registers imported geometry as a collider.
size_t physics3d_sensorExitsBytes()
size_t physics3d_contactEnterMassesBytes()
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)
Registers the convex hull of imported geometry as a collider.
uintptr_t physics3d_characterHits()
void physics3d_setTransform(uint32_t bodyId, float px, float py, float pz, float qx, float qy, float qz, float qw)
void physics3d_setMass(uint32_t bodyId, float mass)
void physics3d_init(float gx, float gy, float gz, uint32_t maxBodies)
int physics3d_jointForce(uint32_t entity)
size_t physics3d_contactExitsBytes()
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)
void physics3d_addForce(uint32_t bodyId, float fx, float fy, float fz, float px, float py, float pz)
void physics3d_shutdown()
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.
size_t physics3d_contactEnterSpeedsBytes()
int physics3d_boxCast(float px, float py, float pz, float hx, float hy, float hz, float qx, float qy, float qz, float qw, float dx, float dy, float dz, uint32_t layerMask)
void physics3d_addTorque(uint32_t bodyId, float tx, float ty, float tz)
A torque of (tx, ty, tz) newton metres, over the next step.
void physics3d_step(float dt, int collisionSteps)
Advance the world, then refill the transform buffer with every active body.
size_t physics3d_contactEntersBytes()
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_removeBody(uint32_t bodyId)
int physics3d_isReady()
void physics3d_setDistanceJoint(uint32_t entity, float minLength, float maxLength, float frequency, float damping)
int physics3d_isAwake(uint32_t bodyId)
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)
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_setCharacterPosition(uint32_t characterId, float px, float py, float pz)
Puts a character somewhere, rather than moving it there.
int physics3d_getBodyDynamics(uint32_t bodyId)
void physics3d_setJointMotor(uint32_t entity, int enable, float speed)
Drive an existing hinge or slider from gameplay, without rebuilding the joint.
void physics3d_moveCharacter(uint32_t characterId, float vx, float vy, float vz, float dt, float stepUp, float stepDown)
uintptr_t physics3d_contactEnters()
uintptr_t physics3d_transforms()
void physics3d_setJointMotorLimit(uint32_t entity, float limit)
int physics3d_getBodyMassData(uint32_t bodyId)
void physics3d_setAngularVelocity(uint32_t bodyId, float wx, float wy, float wz)
int physics3d_overlapSphere(float px, float py, float pz, float radius, uint32_t layerMask)
Everything a sphere at this position overlaps.
uintptr_t physics3d_sensorExits()
size_t physics3d_characterHitsBytes()
int physics3d_getBodyState(uint32_t bodyId)
uintptr_t physics3d_contactEnterSpeeds()
void physics3d_setLinearVelocity(uint32_t bodyId, float vx, float vy, float vz)
int physics3d_raycastWith(float ox, float oy, float oz, float dx, float dy, float dz, uint32_t layerMask, uint32_t flags)
The world's nearest hit, or a character's when one is nearer and flags bit 0 asks for them.
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)
int physics3d_sphereCast(float px, float py, float pz, float radius, float dx, float dy, float dz, uint32_t layerMask)
void physics3d_addAngularImpulse(uint32_t bodyId, float ix, float iy, float iz)
void physics3d_optimize()
void physics3d_setGravity(float gx, float gy, float gz)
uintptr_t physics3d_contactExits()
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.
int physics3d_capsuleCast(float px, float py, float pz, float radius, float halfHeight, float qx, float qy, float qz, float qw, float dx, float dy, float dz, uint32_t layerMask)
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_setLayerMask(uint32_t layer, uint32_t mask)
Declares which layers layer layer collides with.
uint32_t physics3d_addCharacter(uint32_t entity, float radius, float halfHeight, float px, float py, float pz, float maxSlope, float mass, uint32_t layer, float pushForce)
Registers a character: an upright capsule swept against the world.
void physics3d_setJointLimits(uint32_t entity, int enable, float lower, float upper)
uintptr_t physics3d_contactEnterMasses()
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.
size_t physics3d_transformsBytes()