Skip to content

Commit

Permalink
Shape:get/setDensity;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed Apr 30, 2024
1 parent a8f8f15 commit ce217dd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/api/l_physics_shapes.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ static int l_lovrShapeGetType(lua_State* L) {
}

static int l_lovrShapeGetUserData(lua_State* L) {
luax_checktype(L, 1, Shape);
luax_checkshape(L, 1);
luax_pushstash(L, "lovr.shape.userdata");
lua_pushvalue(L, 1);
lua_rawget(L, -2);
return 1;
}

static int l_lovrShapeSetUserData(lua_State* L) {
luax_checktype(L, 1, Shape);
luax_checkshape(L, 1);
luax_pushstash(L, "lovr.shape.userdata");
lua_pushvalue(L, 1);
lua_pushvalue(L, 2);
Expand All @@ -252,12 +252,26 @@ static int l_lovrShapeSetUserData(lua_State* L) {
}

static int l_lovrShapeGetVolume(lua_State* L) {
Shape* shape = luax_checktype(L, 1, Shape);
Shape* shape = luax_checkshape(L, 1);
float volume = lovrShapeGetVolume(shape);
lua_pushnumber(L, volume);
return 1;
}

static int l_lovrShapeGetDensity(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float density = lovrShapeGetDensity(shape);
lua_pushnumber(L, density);
return 1;
}

static int l_lovrShapeSetDensity(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float density = luax_checkfloat(L, 2);
lovrShapeSetDensity(shape, density);
return 0;
}

static int l_lovrShapeGetMass(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float density = luax_checkfloat(L, 2);
Expand Down Expand Up @@ -296,7 +310,9 @@ static int l_lovrShapeGetAABB(lua_State* L) {
{ "getType", l_lovrShapeGetType }, \
{ "getUserData", l_lovrShapeGetUserData }, \
{ "setUserData", l_lovrShapeSetUserData }, \
{ "setVolume", l_lovrShapeGetVolume }, \
{ "getVolume", l_lovrShapeGetVolume }, \
{ "getDensity", l_lovrShapeGetDensity }, \
{ "setDensity", l_lovrShapeSetDensity }, \
{ "getMass", l_lovrShapeGetMass }, \
{ "getAABB", l_lovrShapeGetAABB }

Expand Down
2 changes: 2 additions & 0 deletions src/modules/physics/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ typedef enum {
void lovrShapeDestroy(void* ref);
ShapeType lovrShapeGetType(Shape* shape);
float lovrShapeGetVolume(Shape* shape);
float lovrShapeGetDensity(Shape* shape);
void lovrShapeSetDensity(Shape* shape, float density);
void lovrShapeGetMass(Shape* shape, float density, float centerOfMass[3], float* mass, float inertia[6]);
void lovrShapeGetAABB(Shape* shape, float position[3], float orientation[4], float aabb[6]);

Expand Down
14 changes: 14 additions & 0 deletions src/modules/physics/physics_jolt.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,20 @@ float lovrShapeGetVolume(Shape* shape) {
}
}

float lovrShapeGetDensity(Shape* shape) {
if (shape->type == SHAPE_MESH || shape->type == SHAPE_TERRAIN) {
return 0.f;
} else {
return JPH_ConvexShape_GetDensity((JPH_ConvexShape*) shape->handle);
}
}

void lovrShapeSetDensity(Shape* shape, float density) {
if (shape->type != SHAPE_MESH && shape->type != SHAPE_TERRAIN) {
JPH_ConvexShape_SetDensity((JPH_ConvexShape*) shape->handle, density);
}
}

void lovrShapeGetMass(Shape* shape, float density, float centerOfMass[3], float* mass, float inertia[6]) {
//
}
Expand Down
8 changes: 8 additions & 0 deletions src/modules/physics/physics_ode.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,14 @@ float lovrShapeGetVolume(Shape* shape) {
return 0.f;
}

float lovrShapeGetDensity(Shape* shape) {
return 0.f;
}

void lovrShapeSetDensity(Shape* shape, float density) {
//
}

Collider* lovrShapeGetCollider(Shape* shape) {
return shape->collider;
}
Expand Down

0 comments on commit ce217dd

Please sign in to comment.