Compare commits

...

2 Commits

Author SHA1 Message Date
bjorn c37980b4d9 Collider shape is required I think;
Jolt doesn't really support bodies without shapes.
2024-04-04 21:52:51 -07:00
bjorn 615c8ce340 Add Collider:get/setShapeOffset; 2024-04-04 21:41:12 -07:00
3 changed files with 62 additions and 11 deletions

View File

@ -27,22 +27,39 @@ static int l_lovrColliderGetWorld(lua_State* L) {
static int l_lovrColliderGetShape(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
Shape* shape = lovrColliderGetShape(collider);
if (shape) {
luax_pushshape(L, shape);
} else {
lua_pushnil(L);
}
luax_pushshape(L, shape);
return 1;
}
static int l_lovrColliderSetShape(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
if (lua_isnoneornil(L, 2)) {
lovrColliderSetShape(collider, NULL);
} else {
Shape* shape = luax_checkshape(L, 2);
lovrColliderSetShape(collider, shape);
}
Shape* shape = luax_checkshape(L, 2);
lovrColliderSetShape(collider, shape);
return 0;
}
static int l_lovrColliderGetShapeOffset(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
float position[3], orientation[4], angle, ax, ay, az;
lovrColliderGetShapeOffset(collider, position, orientation);
quat_getAngleAxis(orientation, &angle, &ax, &ay, &az);
lua_pushnumber(L, position[0]);
lua_pushnumber(L, position[1]);
lua_pushnumber(L, position[2]);
lua_pushnumber(L, angle);
lua_pushnumber(L, ax);
lua_pushnumber(L, ay);
lua_pushnumber(L, az);
return 7;
}
static int l_lovrColliderSetShapeOffset(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
int index = 2;
float position[3], orientation[4];
index = luax_readvec3(L, index, position, NULL);
index = luax_readquat(L, index, orientation, NULL);
lovrColliderSetShapeOffset(collider, position, orientation);
return 0;
}
@ -509,6 +526,8 @@ const luaL_Reg lovrCollider[] = {
{ "getWorld", l_lovrColliderGetWorld },
{ "getShape", l_lovrColliderGetShape },
{ "setShape", l_lovrColliderSetShape },
{ "getShapeOffset", l_lovrColliderGetShapeOffset },
{ "setShapeOffset", l_lovrColliderSetShapeOffset },
{ "getJoints", l_lovrColliderGetJoints },
{ "getUserData", l_lovrColliderGetUserData },
{ "setUserData", l_lovrColliderSetUserData },

View File

@ -83,6 +83,8 @@ World* lovrColliderGetWorld(Collider* collider);
Collider* lovrColliderGetNext(Collider* collider);
Shape* lovrColliderGetShape(Collider* collider);
void lovrColliderSetShape(Collider* collider, Shape* shape);
void lovrColliderGetShapeOffset(Collider* collider, float* position, float* orientation);
void lovrColliderSetShapeOffset(Collider* collider, float* position, float* orientation);
Joint** lovrColliderGetJoints(Collider* collider, size_t* count);
const char* lovrColliderGetTag(Collider* collider);
bool lovrColliderSetTag(Collider* collider, const char* tag);

View File

@ -473,6 +473,36 @@ void lovrColliderSetShape(Collider* collider, Shape* shape) {
}
}
void lovrColliderGetShapeOffset(Collider* collider, float* position, float* orientation) {
const JPH_Shape* shape = JPH_BodyInterface_GetShape(collider->world->body_interface, collider->id);
if (JPH_Shape_GetSubType(shape) == JPH_ShapeSubType_RotatedTranslated) {
JPH_Vec3 jposition;
JPH_Quat jrotation;
JPH_RotatedTranslatedShape_GetPosition((JPH_RotatedTranslatedShape*) shape, &jposition);
JPH_RotatedTranslatedShape_GetRotation((JPH_RotatedTranslatedShape*) shape, &jrotation);
vec3_init(position, &jposition.x);
quat_init(orientation, &jrotation.x);
} else {
vec3_set(position, 0.f, 0.f, 0.f);
quat_identity(orientation);
}
}
void lovrColliderSetShapeOffset(Collider* collider, float* position, float* orientation) {
const JPH_Shape* shape = JPH_BodyInterface_GetShape(collider->world->body_interface, collider->id);
if (JPH_Shape_GetSubType(shape) == JPH_ShapeSubType_RotatedTranslated) {
JPH_Shape_Destroy((JPH_Shape*) shape);
}
JPH_Vec3 jposition = { position[0], position[1], position[2] };
JPH_Quat jrotation = { orientation[0], orientation[1], orientation[2], orientation[3] };
shape = (JPH_Shape*) JPH_RotatedTranslatedShape_Create(&jposition, &jrotation, collider->shape->shape);
bool updateMass = collider->shape->type == SHAPE_MESH || collider->shape->type == SHAPE_TERRAIN;
JPH_BodyInterface_SetShape(collider->world->body_interface, collider->id, shape, updateMass, JPH_Activation_Activate);
}
Joint** lovrColliderGetJoints(Collider* collider, size_t* count) {
*count = collider->joints.length;
return collider->joints.data;