Skip to content

Commit

Permalink
New mass API;
Browse files Browse the repository at this point in the history
- All of the mass properties have regular getters/setters
  - :get/setMass
  - :get/setInertia
  - :get/setCenterOfMass
- You can use nil for any of the setters to set the mass property to an
  "automatic" value, computed from the shape and/or other overridden
  mass properties.
- If you override the mass properties, we assume you know what you're
  doing and don't try to be sneaky and auto-update downstream values.
  So changing the mass or center of mass doesn't auto-scale the inertia.
- If you change the shape (or enabled axes), mass data gets reset.
- You can use :resetMass to reset all 3 mass properties at once.  It's
  also useful if something about the underlying shape changed.
  • Loading branch information
bjornbytes committed May 3, 2024
1 parent c23a0c0 commit 836189e
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 169 deletions.
2 changes: 1 addition & 1 deletion deps/jolt-physics-sharp
Submodule jolt-physics-sharp updated 52 files
+48 −0 .gitattributes
+16 −1 .github/workflows/build_native.yml
+0 −1,159 CMake/CPM.cmake
+25 −57 CMakeLists.txt
+1 −1 README.md
+0 −2 src/JoltPhysicsSharp/Body.cs
+10 −0 src/JoltPhysicsSharp/BroadPhaseCastResult.cs
+98 −0 src/JoltPhysicsSharp/BroadPhaseQuery.cs
+44 −44 src/JoltPhysicsSharp/Character/CharacterBaseSettings.cs
+10 −0 src/JoltPhysicsSharp/CollidePointResult.cs
+17 −0 src/JoltPhysicsSharp/CollideShapeResult.cs
+29 −0 src/JoltPhysicsSharp/ConstraintSubType.cs
+13 −0 src/JoltPhysicsSharp/ConstraintType.cs
+25 −0 src/JoltPhysicsSharp/Constraints/Constraint.cs
+44 −8 src/JoltPhysicsSharp/Constraints/DistanceConstraint.cs
+169 −0 src/JoltPhysicsSharp/Constraints/FixedConstraint.cs
+32 −14 src/JoltPhysicsSharp/Constraints/PointConstraint.cs
+22 −3 src/JoltPhysicsSharp/Constraints/TwoBodyConstraint.cs
+3 −3 src/JoltPhysicsSharp/DelegateProxies.cs
+26 −1 src/JoltPhysicsSharp/Foundation.cs
+606 −356 src/JoltPhysicsSharp/JoltApi.cs
+2 −2 src/JoltPhysicsSharp/JoltPhysicsSharp.csproj
+3 −4 src/JoltPhysicsSharp/MassProperties.cs
+5 −0 src/JoltPhysicsSharp/MotionProperties.cs
+93 −0 src/JoltPhysicsSharp/MotorSettings.cs
+194 −0 src/JoltPhysicsSharp/NarrowPhaseQuery.cs
+2 −0 src/JoltPhysicsSharp/PhysicsSystem.cs
+6 −0 src/JoltPhysicsSharp/Properties/AssemblyInfo.cs
+1 −1 src/JoltPhysicsSharp/RayCastResult.cs
+56 −1 src/JoltPhysicsSharp/Shape/ConvexHullShape.cs
+19 −0 src/JoltPhysicsSharp/ShapeCastResult.cs
+1 −1 src/JoltPhysicsSharp/ShapeSubType.cs
+1 −1 src/JoltPhysicsSharp/SoftBodyCreationSettings.cs
+19 −0 src/JoltPhysicsSharp/SpringMode.cs
+18 −0 src/JoltPhysicsSharp/SpringSettings.cs
+ src/JoltPhysicsSharp/runtimes/libs_linux/linux-x64/native/libjoltc.so
+ src/JoltPhysicsSharp/runtimes/libs_osx/osx/native/libjoltc.dylib
+ src/JoltPhysicsSharp/runtimes/libs_windows/win-arm64/native/joltc.dll
+ src/JoltPhysicsSharp/runtimes/libs_windows/win-arm64/native/joltc_double.dll
+ src/JoltPhysicsSharp/runtimes/libs_windows/win-x64/native/joltc.dll
+ src/JoltPhysicsSharp/runtimes/libs_windows/win-x64/native/joltc_double.dll
+ src/JoltPhysicsSharp/runtimes/linux-x64/native/libjoltc.so
+ src/JoltPhysicsSharp/runtimes/osx/native/libjoltc.dylib
+ src/JoltPhysicsSharp/runtimes/win-arm64/native/joltc.dll
+ src/JoltPhysicsSharp/runtimes/win-arm64/native/joltc_double.dll
+ src/JoltPhysicsSharp/runtimes/win-x64/native/joltc.dll
+ src/JoltPhysicsSharp/runtimes/win-x64/native/joltc_double.dll
+5 −9 src/joltc/CMakeLists.txt
+349 −64 src/joltc/joltc.cpp
+51 −22 src/joltc/joltc.h
+1 −1 src/samples/HelloWorld/HelloWorld.csproj
+1 −1 src/tests/JoltPhysicsSharp.Tests.csproj
115 changes: 55 additions & 60 deletions src/api/l_physics_collider.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,53 +173,67 @@ static int l_lovrColliderGetMass(lua_State* L) {

static int l_lovrColliderSetMass(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
float mass = luax_checkfloat(L, 2);
lovrColliderSetMass(collider, mass);
if (lua_isnoneornil(L, 2)) {
lovrColliderSetMass(collider, NULL);
} else {
float mass = luax_checkfloat(L, 2);
lovrColliderSetMass(collider, &mass);
}
return 0;
}

static int l_lovrColliderGetMassData(lua_State* L) {
static int l_lovrColliderGetInertia(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
float centerOfMass[3], mass, inertia[6];
lovrColliderGetMassData(collider, centerOfMass, &mass, inertia);
lua_pushnumber(L, centerOfMass[0]);
lua_pushnumber(L, centerOfMass[1]);
lua_pushnumber(L, centerOfMass[2]);
lua_pushnumber(L, mass);
lua_newtable(L);
for (int i = 0; i < 6; i++) {
lua_pushnumber(L, inertia[i]);
lua_rawseti(L, -2, i + 1);
float diagonal[3], rotation[4];
lovrColliderGetInertia(collider, diagonal, rotation);
lua_pushnumber(L, diagonal[0]);
lua_pushnumber(L, diagonal[1]);
lua_pushnumber(L, diagonal[2]);
lua_pushnumber(L, rotation[0]);
lua_pushnumber(L, rotation[1]);
lua_pushnumber(L, rotation[2]);
lua_pushnumber(L, rotation[3]);
return 7;
}

static int l_lovrColliderSetInertia(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
if (lua_isnoneornil(L, 2)) {
lovrColliderSetInertia(collider, NULL, NULL);
} else {
float diagonal[3], rotation[4];
int index = luax_readvec3(L, 2, diagonal, NULL);
luax_readquat(L, index, rotation, NULL);
lovrColliderSetInertia(collider, diagonal, rotation);
}
return 5;
return 0;
}

static int l_lovrColliderSetMassData(lua_State* L) {
static int l_lovrColliderGetCenterOfMass(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
float centerOfMass[3];
int index = luax_readvec3(L, 2, centerOfMass, NULL);
float mass = luax_checkfloat(L, index++);
float inertia[6];
if (lua_istable(L, index) && luax_len(L, index) >= 6) {
for (int i = 0; i < 6; i++) {
lua_rawgeti(L, index, i + 1);
if (!lua_isnumber(L, -1)) {
return luaL_argerror(L, 6, "Expected 6 numbers or a table with 6 numbers");
}
float center[3];
lovrColliderGetCenterOfMass(collider, center);
lua_pushnumber(L, center[0]);
lua_pushnumber(L, center[1]);
lua_pushnumber(L, center[2]);
return 3;
}

inertia[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
static int l_lovrColliderSetCenterOfMass(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
if (lua_isnoneornil(L, 2)) {
lovrColliderSetCenterOfMass(collider, NULL);
} else {
for (int i = index; i < index + 6; i++) {
if (lua_isnumber(L, i)) {
inertia[i - index] = lua_tonumber(L, i);
} else {
return luaL_argerror(L, i, "Expected 6 numbers or a table with 6 numbers");
}
}
float center[3];
luax_readvec3(L, 2, center, NULL);
lovrColliderSetCenterOfMass(collider, center);
}
lovrColliderSetMassData(collider, centerOfMass, mass, inertia);
return 0;
}

static int l_lovrColliderResetMassData(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
lovrColliderResetMassData(collider);
return 0;
}

Expand Down Expand Up @@ -478,26 +492,6 @@ static int l_lovrColliderApplyAngularImpulse(lua_State* L) {
return 0;
}

static int l_lovrColliderGetLocalCenter(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
float center[3];
lovrColliderGetLocalCenter(collider, center);
lua_pushnumber(L, center[0]);
lua_pushnumber(L, center[1]);
lua_pushnumber(L, center[2]);
return 3;
}

static int l_lovrColliderGetWorldCenter(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
float center[3];
lovrColliderGetWorldCenter(collider, center);
lua_pushnumber(L, center[0]);
lua_pushnumber(L, center[1]);
lua_pushnumber(L, center[2]);
return 3;
}

static int l_lovrColliderGetLocalPoint(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
float world[3], local[3];
Expand Down Expand Up @@ -662,8 +656,11 @@ const luaL_Reg lovrCollider[] = {
{ "setAwake", l_lovrColliderSetAwake },
{ "getMass", l_lovrColliderGetMass },
{ "setMass", l_lovrColliderSetMass },
{ "getMassData", l_lovrColliderGetMassData },
{ "setMassData", l_lovrColliderSetMassData },
{ "getInertia", l_lovrColliderGetInertia },
{ "setInertia", l_lovrColliderSetInertia },
{ "getCenterOfMass", l_lovrColliderGetCenterOfMass },
{ "setCenterOfMass", l_lovrColliderSetCenterOfMass },
{ "resetMassData", l_lovrColliderResetMassData },
{ "getEnabledAxes", l_lovrColliderGetEnabledAxes },
{ "setEnabledAxes", l_lovrColliderSetEnabledAxes },
{ "getPosition", l_lovrColliderGetPosition },
Expand All @@ -686,8 +683,6 @@ const luaL_Reg lovrCollider[] = {
{ "applyTorque", l_lovrColliderApplyTorque },
{ "applyLinearImpulse", l_lovrColliderApplyLinearImpulse },
{ "applyAngularImpulse", l_lovrColliderApplyAngularImpulse },
{ "getLocalCenter", l_lovrColliderGetLocalCenter },
{ "getWorldCenter", l_lovrColliderGetWorldCenter },
{ "getLocalPoint", l_lovrColliderGetLocalPoint },
{ "getWorldPoint", l_lovrColliderGetWorldPoint },
{ "getLocalVector", l_lovrColliderGetLocalVector },
Expand Down
42 changes: 14 additions & 28 deletions src/api/l_physics_shapes.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,44 +272,31 @@ static int l_lovrShapeSetDensity(lua_State* L) {
return 0;
}

static int l_lovrShapeGetMassData(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float mass, inertia[9], center[3];
lovrShapeGetMassData(shape, &mass, inertia, center);
lua_pushnumber(L, mass);
lua_createtable(L, 9, 0);
for (int i = 0; i < 9; i++) {
lua_pushnumber(L, inertia[i]);
lua_rawseti(L, -2, i + 1);
}
lua_pushnumber(L, center[0]);
lua_pushnumber(L, center[1]);
lua_pushnumber(L, center[2]);
return 5;
}

static int l_lovrShapeGetMass(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float mass;
lovrShapeGetMassData(shape, &mass, NULL, NULL);
float mass = lovrShapeGetMass(shape);
lua_pushnumber(L, mass);
return 1;
}

static int l_lovrShapeGetInertia(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float inertia[9];
lovrShapeGetMassData(shape, NULL, inertia, NULL);
for (int i = 0; i < 9; i++) {
lua_pushnumber(L, inertia[i]);
}
return 9;
float diagonal[3], rotation[4];
lovrShapeGetInertia(shape, diagonal, rotation);
lua_pushnumber(L, diagonal[0]);
lua_pushnumber(L, diagonal[1]);
lua_pushnumber(L, diagonal[2]);
lua_pushnumber(L, rotation[0]);
lua_pushnumber(L, rotation[1]);
lua_pushnumber(L, rotation[2]);
lua_pushnumber(L, rotation[3]);
return 7;
}

static int l_lovrShapeGetCenter(lua_State* L) {
static int l_lovrShapeGetCenterOfMass(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float center[3];
lovrShapeGetMassData(shape, NULL, NULL, center);
lovrShapeGetCenterOfMass(shape, center);
lua_pushnumber(L, center[0]);
lua_pushnumber(L, center[1]);
lua_pushnumber(L, center[2]);
Expand Down Expand Up @@ -340,10 +327,9 @@ static int l_lovrShapeGetAABB(lua_State* L) {
{ "getVolume", l_lovrShapeGetVolume }, \
{ "getDensity", l_lovrShapeGetDensity }, \
{ "setDensity", l_lovrShapeSetDensity }, \
{ "getMassData", l_lovrShapeGetMassData }, \
{ "getMass", l_lovrShapeGetMass }, \
{ "getInertia", l_lovrShapeGetInertia }, \
{ "getCenter", l_lovrShapeGetCenter }, \
{ "getCenterOfMass", l_lovrShapeGetCenterOfMass }, \
{ "getAABB", l_lovrShapeGetAABB }

static int l_lovrBoxShapeGetDimensions(lua_State* L) {
Expand Down
15 changes: 9 additions & 6 deletions src/modules/physics/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ void lovrColliderSetSleepingAllowed(Collider* collider, bool allowed);
bool lovrColliderIsAwake(Collider* collider);
void lovrColliderSetAwake(Collider* collider, bool awake);
float lovrColliderGetMass(Collider* collider);
void lovrColliderSetMass(Collider* collider, float mass);
void lovrColliderGetMassData(Collider* collider, float centerOfMass[3], float* mass, float inertia[6]);
void lovrColliderSetMassData(Collider* collider, float centerOfMass[3], float mass, float inertia[6]);
void lovrColliderSetMass(Collider* collider, float* mass);
void lovrColliderGetInertia(Collider* collider, float diagonal[3], float rotation[4]);
void lovrColliderSetInertia(Collider* collider, float diagonal[3], float rotation[4]);
void lovrColliderGetCenterOfMass(Collider* collider, float center[3]);
void lovrColliderSetCenterOfMass(Collider* collider, float center[3]);
void lovrColliderResetMassData(Collider* collider);
void lovrColliderGetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]);
void lovrColliderSetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]);
void lovrColliderGetPosition(Collider* collider, float position[3]);
Expand All @@ -156,8 +159,6 @@ void lovrColliderApplyTorque(Collider* collider, float torque[3]);
void lovrColliderApplyLinearImpulse(Collider* collider, float impulse[3]);
void lovrColliderApplyLinearImpulseAtPosition(Collider* collider, float impulse[3], float position[3]);
void lovrColliderApplyAngularImpulse(Collider* collider, float impulse[3]);
void lovrColliderGetLocalCenter(Collider* collider, float center[3]);
void lovrColliderGetWorldCenter(Collider* collider, float center[3]);
void lovrColliderGetLocalPoint(Collider* collider, float world[3], float local[3]);
void lovrColliderGetWorldPoint(Collider* collider, float local[3], float world[3]);
void lovrColliderGetLocalVector(Collider* collider, float world[3], float local[3]);
Expand All @@ -184,7 +185,9 @@ ShapeType lovrShapeGetType(Shape* shape);
float lovrShapeGetVolume(Shape* shape);
float lovrShapeGetDensity(Shape* shape);
void lovrShapeSetDensity(Shape* shape, float density);
void lovrShapeGetMassData(Shape* shape, float* mass, float inertia[9], float center[3]);
float lovrShapeGetMass(Shape* shape);
void lovrShapeGetInertia(Shape* shape, float diagonal[3], float rotation[4]);
void lovrShapeGetCenterOfMass(Shape* shape, float center[3]);
void lovrShapeGetAABB(Shape* shape, float position[3], float orientation[4], float aabb[6]);

BoxShape* lovrBoxShapeCreate(float dimensions[3]);
Expand Down
Loading

0 comments on commit 836189e

Please sign in to comment.