1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-08 15:13:35 +00:00

Giant Shape rework!;

This commit is contained in:
bjorn 2024-05-12 15:10:06 -07:00
parent cd290f4407
commit f70d98714f
9 changed files with 833 additions and 591 deletions

View file

@ -41,7 +41,6 @@ dev
- Add `Collider:get/setCenterOfMass`.
- Add `Collider:resetMassData`.
- Add `Collider:is/setEnabled`.
- Add `CompoundShape`.
- Add `ConvexShape`.
- Add `WeldJoint`.
- Add `ConeJoint`.

@ -1 +1 @@
Subproject commit 75c1f26235af213e7f1d3a30f5e10ca9749bf798
Subproject commit 1fead37b3a6d73ff5a560c4c1a3d39067abfda60

View file

@ -11,7 +11,6 @@ StringEntry lovrShapeType[] = {
[SHAPE_CONVEX] = ENTRY("convex"),
[SHAPE_MESH] = ENTRY("mesh"),
[SHAPE_TERRAIN] = ENTRY("terrain"),
[SHAPE_COMPOUND] = ENTRY("compound"),
{ 0 }
};
@ -212,13 +211,6 @@ static int l_lovrPhysicsNewTerrainShape(lua_State* L) {
return 1;
}
static int l_lovrPhysicsNewCompoundShape(lua_State* L) {
CompoundShape* shape = luax_newcompoundshape(L, 1);
luax_pushtype(L, CompoundShape, shape);
lovrRelease(shape, lovrShapeDestroy);
return 1;
}
static int l_lovrPhysicsNewWeldJoint(lua_State* L) {
Collider* a = luax_totype(L, 1, Collider);
Collider* b = luax_totype(L, 2, Collider);
@ -297,7 +289,6 @@ static const luaL_Reg lovrPhysics[] = {
{ "newConvexShape", l_lovrPhysicsNewConvexShape },
{ "newMeshShape", l_lovrPhysicsNewMeshShape },
{ "newTerrainShape", l_lovrPhysicsNewTerrainShape },
{ "newCompoundShape", l_lovrPhysicsNewCompoundShape },
{ "newWeldJoint", l_lovrPhysicsNewWeldJoint },
{ "newBallJoint", l_lovrPhysicsNewBallJoint },
{ "newConeJoint", l_lovrPhysicsNewConeJoint },
@ -317,7 +308,6 @@ extern const luaL_Reg lovrCylinderShape[];
extern const luaL_Reg lovrConvexShape[];
extern const luaL_Reg lovrMeshShape[];
extern const luaL_Reg lovrTerrainShape[];
extern const luaL_Reg lovrCompoundShape[];
extern const luaL_Reg lovrWeldJoint[];
extern const luaL_Reg lovrBallJoint[];
extern const luaL_Reg lovrConeJoint[];
@ -338,7 +328,6 @@ int luaopen_lovr_physics(lua_State* L) {
luax_registertype(L, ConvexShape);
luax_registertype(L, MeshShape);
luax_registertype(L, TerrainShape);
luax_registertype(L, CompoundShape);
luax_registertype(L, WeldJoint);
luax_registertype(L, BallJoint);
luax_registertype(L, ConeJoint);

View file

@ -38,24 +38,6 @@ static int l_lovrColliderGetWorld(lua_State* L) {
return 1;
}
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);
}
return 1;
}
static int l_lovrColliderSetShape(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
Shape* shape = lua_isnoneornil(L, 2) ? NULL : luax_checkshape(L, 2);
lovrColliderSetShape(collider, shape);
return 0;
}
static int l_lovrColliderGetJoints(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
lua_newtable(L);
@ -68,6 +50,44 @@ static int l_lovrColliderGetJoints(lua_State* L) {
return 1;
}
static int l_lovrColliderGetShapes(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
lua_newtable(L);
int index = 1;
Shape* shape = NULL;
while ((shape = lovrColliderGetShapes(collider, shape)) != NULL) {
luax_pushshape(L, shape);
lua_rawseti(L, -2, index++);
}
return 1;
}
static int l_lovrColliderGetShape(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
Shape* shape = lovrColliderGetShapes(collider, NULL);
if (shape) {
luax_pushshape(L, shape);
} else {
lua_pushnil(L);
}
return 1;
}
static int l_lovrColliderAddShape(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
Shape* shape = luax_checkshape(L, 2);
lovrColliderAddShape(collider, shape);
lua_settop(L, 2);
return 1;
}
static int l_lovrColliderRemoveShape(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
Shape* shape = luax_checkshape(L, 2);
lovrColliderRemoveShape(collider, shape);
return 0;
}
static int l_lovrColliderGetUserData(lua_State* L) {
luax_checktype(L, 1, Collider);
luax_pushstash(L, "lovr.collider.userdata");
@ -174,12 +194,8 @@ static int l_lovrColliderGetMass(lua_State* L) {
static int l_lovrColliderSetMass(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
if (lua_isnoneornil(L, 2)) {
lovrColliderSetMass(collider, NULL);
} else {
float mass = luax_checkfloat(L, 2);
lovrColliderSetMass(collider, &mass);
}
float mass = luax_checkfloat(L, 2);
lovrColliderSetMass(collider, mass);
return 0;
}
@ -199,14 +215,10 @@ static int l_lovrColliderGetInertia(lua_State* L) {
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);
}
float diagonal[3], rotation[4];
int index = luax_readvec3(L, 2, diagonal, NULL);
luax_readquat(L, index, rotation, NULL);
lovrColliderSetInertia(collider, diagonal, rotation);
return 0;
}
@ -222,13 +234,23 @@ static int l_lovrColliderGetCenterOfMass(lua_State* L) {
static int l_lovrColliderSetCenterOfMass(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
if (lua_isnoneornil(L, 2)) {
lovrColliderSetCenterOfMass(collider, NULL);
} else {
float center[3];
luax_readvec3(L, 2, center, NULL);
lovrColliderSetCenterOfMass(collider, center);
}
float center[3];
luax_readvec3(L, 2, center, NULL);
lovrColliderSetCenterOfMass(collider, center);
return 0;
}
static int l_lovrColliderGetAutomaticMass(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
bool enabled = lovrColliderGetAutomaticMass(collider);
lua_pushboolean(L, enabled);
return 1;
}
static int l_lovrColliderSetAutomaticMass(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
bool enable = lua_toboolean(L, 2);
lovrColliderSetAutomaticMass(collider, enable);
return 0;
}
@ -607,16 +629,6 @@ static int l_lovrColliderSetTag(lua_State* L) {
return 0;
}
// Deprecated
static int l_lovrColliderGetShapes(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
Shape* shape = lovrColliderGetShape(collider);
lua_createtable(L, 1, 0);
luax_pushshape(L, shape);
lua_rawseti(L, -2, 1);
return 1;
}
// Deprecated
static int l_lovrColliderIsGravityIgnored(lua_State* L) {
Collider* collider = luax_checktype(L, 1, Collider);
@ -638,9 +650,11 @@ const luaL_Reg lovrCollider[] = {
{ "isEnabled", l_lovrColliderIsEnabled },
{ "setEnabled", l_lovrColliderSetEnabled },
{ "getWorld", l_lovrColliderGetWorld },
{ "getShape", l_lovrColliderGetShape },
{ "setShape", l_lovrColliderSetShape },
{ "getJoints", l_lovrColliderGetJoints },
{ "getShapes", l_lovrColliderGetShapes },
{ "getShape", l_lovrColliderGetShape },
{ "addShape", l_lovrColliderAddShape },
{ "removeShape", l_lovrColliderRemoveShape },
{ "getUserData", l_lovrColliderGetUserData },
{ "setUserData", l_lovrColliderSetUserData },
{ "isKinematic", l_lovrColliderIsKinematic },
@ -661,6 +675,8 @@ const luaL_Reg lovrCollider[] = {
{ "setInertia", l_lovrColliderSetInertia },
{ "getCenterOfMass", l_lovrColliderGetCenterOfMass },
{ "setCenterOfMass", l_lovrColliderSetCenterOfMass },
{ "getAutomaticMass", l_lovrColliderGetAutomaticMass },
{ "setAutomaticMass", l_lovrColliderSetAutomaticMass },
{ "resetMassData", l_lovrColliderResetMassData },
{ "getEnabledAxes", l_lovrColliderGetEnabledAxes },
{ "setEnabledAxes", l_lovrColliderSetEnabledAxes },
@ -699,7 +715,6 @@ const luaL_Reg lovrCollider[] = {
{ "setTag", l_lovrColliderSetTag },
// Deprecated
{ "getShapes", l_lovrColliderGetShapes },
{ "isGravityIgnored", l_lovrColliderIsGravityIgnored },
{ "setGravityIgnored", l_lovrColliderSetGravityIgnored },

View file

@ -15,7 +15,6 @@ void luax_pushshape(lua_State* L, Shape* shape) {
case SHAPE_CONVEX: luax_pushtype(L, ConvexShape, shape); break;
case SHAPE_MESH: luax_pushtype(L, MeshShape, shape); break;
case SHAPE_TERRAIN: luax_pushtype(L, TerrainShape, shape); break;
case SHAPE_COMPOUND: luax_pushtype(L, CompoundShape, shape); break;
default: lovrUnreachable();
}
}
@ -31,8 +30,7 @@ Shape* luax_checkshape(lua_State* L, int index) {
hash64("CylinderShape", strlen("CylinderShape")),
hash64("ConvexShape", strlen("ConvexShape")),
hash64("MeshShape", strlen("MeshShape")),
hash64("TerrainShape", strlen("TerrainShape")),
hash64("CompoundShape", strlen("CompoundShape"))
hash64("TerrainShape", strlen("TerrainShape"))
};
for (size_t i = 0; i < COUNTOF(hashes); i++) {
@ -70,6 +68,9 @@ Shape* luax_newcylindershape(lua_State* L, int index) {
}
Shape* luax_newconvexshape(lua_State* L, int index) {
ConvexShape* parent = luax_totype(L, index, ConvexShape);
if (parent) return lovrConvexShapeClone(parent);
float* points;
uint32_t count;
bool shouldFree;
@ -80,6 +81,9 @@ Shape* luax_newconvexshape(lua_State* L, int index) {
}
Shape* luax_newmeshshape(lua_State* L, int index) {
MeshShape* parent = luax_totype(L, index, MeshShape);
if (parent) return lovrMeshShapeClone(parent);
float* vertices;
uint32_t* indices;
uint32_t vertexCount;
@ -149,83 +153,17 @@ Shape* luax_newterrainshape(lua_State* L, int index) {
}
}
Shape* luax_newcompoundshape(lua_State* L, int index) {
if (lua_isnoneornil(L, index)) {
return lovrCompoundShapeCreate(NULL, NULL, NULL, 0, false);
}
static int l_lovrShapeDestroy(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
lovrShapeDestroyData(shape);
return 0;
}
luaL_checktype(L, index, LUA_TTABLE);
int length = luax_len(L, index);
uint32_t defer = lovrDeferPush();
Shape** shapes = lovrMalloc(length * sizeof(Shape*));
float* positions = lovrMalloc(length * 3 * sizeof(float));
float* orientations = lovrMalloc(length * 4 * sizeof(float));
lovrDefer(lovrFree, shapes);
lovrDefer(lovrFree, positions);
lovrDefer(lovrFree, orientations);
for (int i = 0; i < length; i++) {
lua_rawgeti(L, index, i + 1);
lovrCheck(lua_istable(L, -1), "Expected table of tables for compound shape");
lua_rawgeti(L, -1, 1);
shapes[i] = luax_checkshape(L, -1);
lua_pop(L, 1);
int index = 2;
lua_rawgeti(L, -1, index);
switch (lua_type(L, -1)) {
case LUA_TNIL:
vec3_set(&positions[3 * i], 0.f, 0.f, 0.f);
lua_pop(L, 1);
break;
case LUA_TNUMBER:
lua_rawgeti(L, -2, index + 1);
lua_rawgeti(L, -3, index + 2);
vec3_set(&positions[3 * i], luax_tofloat(L, -3), luax_tofloat(L, -2), luax_tofloat(L, -1));
lua_pop(L, 3);
index += 3;
break;
default: {
float* v = luax_checkvector(L, -1, V_VEC3, "nil, number, or vec3");
vec3_init(&positions[3 * i], v);
lua_pop(L, 1);
break;
}
}
lua_rawgeti(L, -1, index);
switch (lua_type(L, -1)) {
case LUA_TNIL:
quat_identity(&orientations[4 * i]);
lua_pop(L, 1);
break;
case LUA_TNUMBER:
lua_rawgeti(L, -2, index);
lua_rawgeti(L, -3, index);
lua_rawgeti(L, -4, index);
quat_set(&orientations[4 * i], luax_tofloat(L, -4), luax_tofloat(L, -3), luax_tofloat(L, -2), luax_tofloat(L, -1));
lua_pop(L, 4);
break;
default: {
float* q = luax_checkvector(L, -1, V_QUAT, "nil, number, or quat");
quat_init(&positions[4 * i], q);
lua_pop(L, 1);
break;
}
}
lua_pop(L, 1);
}
lua_getfield(L, index, "freeze");
bool freeze = lua_toboolean(L, -1);
lua_pop(L, 1);
CompoundShape* shape = lovrCompoundShapeCreate(shapes, positions, orientations, length, freeze);
lovrDeferPop(defer);
return shape;
static int l_lovrShapeIsDestroyed(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
bool destroyed = lovrShapeIsDestroyed(shape);
lua_pushboolean(L, destroyed);
return 1;
}
static int l_lovrShapeGetType(lua_State* L) {
@ -234,6 +172,13 @@ static int l_lovrShapeGetType(lua_State* L) {
return 1;
}
static int l_lovrShapeGetCollider(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
Collider* collider = lovrShapeGetCollider(shape);
luax_pushtype(L, Collider, collider);
return 1;
}
static int l_lovrShapeGetUserData(lua_State* L) {
luax_checkshape(L, 1);
luax_pushstash(L, "lovr.shape.userdata");
@ -304,25 +249,81 @@ static int l_lovrShapeGetCenterOfMass(lua_State* L) {
return 3;
}
static int l_lovrShapeGetOffset(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float position[3], orientation[4], angle, ax, ay, az;
lovrShapeGetOffset(shape, 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_lovrShapeSetOffset(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float position[3], orientation[4];
int index = 2;
index = luax_readvec3(L, index, position, NULL);
index = luax_readquat(L, index, orientation, NULL);
lovrShapeSetOffset(shape, position, orientation);
return 0;
}
static int l_lovrShapeGetPosition(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float position[3];
lovrShapeGetPose(shape, position, NULL);
lua_pushnumber(L, position[0]);
lua_pushnumber(L, position[1]);
lua_pushnumber(L, position[2]);
return 3;
}
static int l_lovrShapeGetOrientation(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float orientation[4], angle, x, y, z;
lovrShapeGetPose(shape, NULL, orientation);
quat_getAngleAxis(orientation, &angle, &x, &y, &z);
lua_pushnumber(L, angle);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 4;
}
static int l_lovrShapeGetPose(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float position[3], orientation[4], angle, ax, ay, az;
lovrShapeGetPose(shape, 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_lovrShapeGetAABB(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float position[3], orientation[4], aabb[6];
if (lua_gettop(L) >= 2) {
int index = 2;
index = luax_readvec3(L, index, position, NULL);
index = luax_readquat(L, index, orientation, NULL);
lovrShapeGetAABB(shape, position, orientation, aabb);
} else {
lovrShapeGetAABB(shape, NULL, NULL, aabb);
}
for (int i = 0; i < 6; i++) {
lua_pushnumber(L, aabb[i]);
}
float aabb[6];
lovrShapeGetAABB(shape, aabb);
for (int i = 0; i < 6; i++) lua_pushnumber(L, aabb[i]);
return 6;
}
#define lovrShape \
{ "destroy", l_lovrShapeDestroy }, \
{ "isDestroyed", l_lovrShapeIsDestroyed }, \
{ "getType", l_lovrShapeGetType }, \
{ "getCollider", l_lovrShapeGetCollider }, \
{ "getUserData", l_lovrShapeGetUserData }, \
{ "setUserData", l_lovrShapeSetUserData }, \
{ "getVolume", l_lovrShapeGetVolume }, \
@ -331,6 +332,11 @@ static int l_lovrShapeGetAABB(lua_State* L) {
{ "getMass", l_lovrShapeGetMass }, \
{ "getInertia", l_lovrShapeGetInertia }, \
{ "getCenterOfMass", l_lovrShapeGetCenterOfMass }, \
{ "getOffset", l_lovrShapeGetOffset }, \
{ "setOffset", l_lovrShapeSetOffset }, \
{ "getPosition", l_lovrShapeGetPosition }, \
{ "getOrientation", l_lovrShapeGetOrientation }, \
{ "getPose", l_lovrShapeGetPose }, \
{ "getAABB", l_lovrShapeGetAABB }
static int l_lovrBoxShapeGetDimensions(lua_State* L) {
@ -343,9 +349,18 @@ static int l_lovrBoxShapeGetDimensions(lua_State* L) {
return 3;
}
static int l_lovrBoxShapeSetDimensions(lua_State* L) {
BoxShape* box = luax_checktype(L, 1, BoxShape);
float dimensions[3];
luax_readvec3(L, 2, dimensions, NULL);
lovrBoxShapeSetDimensions(box, dimensions);
return 0;
}
const luaL_Reg lovrBoxShape[] = {
lovrShape,
{ "getDimensions", l_lovrBoxShapeGetDimensions },
{ "setDimensions", l_lovrBoxShapeSetDimensions },
{ NULL, NULL }
};
@ -355,9 +370,17 @@ static int l_lovrSphereShapeGetRadius(lua_State* L) {
return 1;
}
static int l_lovrSphereShapeSetRadius(lua_State* L) {
SphereShape* sphere = luax_checktype(L, 1, SphereShape);
float radius = luax_checkfloat(L, 2);
lovrSphereShapeSetRadius(sphere, radius);
return 0;
}
const luaL_Reg lovrSphereShape[] = {
lovrShape,
{ "getRadius", l_lovrSphereShapeGetRadius },
{ "setRadius", l_lovrSphereShapeSetRadius },
{ NULL, NULL }
};
@ -367,16 +390,32 @@ static int l_lovrCapsuleShapeGetRadius(lua_State* L) {
return 1;
}
static int l_lovrCapsuleShapeSetRadius(lua_State* L) {
CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape);
float radius = luax_checkfloat(L, 2);
lovrCapsuleShapeSetRadius(capsule, radius);
return 0;
}
static int l_lovrCapsuleShapeGetLength(lua_State* L) {
CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape);
lua_pushnumber(L, lovrCapsuleShapeGetLength(capsule));
return 1;
}
static int l_lovrCapsuleShapeSetLength(lua_State* L) {
CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape);
float length = luax_checkfloat(L, 2);
lovrCapsuleShapeSetLength(capsule, length);
return 0;
}
const luaL_Reg lovrCapsuleShape[] = {
lovrShape,
{ "getRadius", l_lovrCapsuleShapeGetRadius },
{ "setRadius", l_lovrCapsuleShapeSetRadius },
{ "getLength", l_lovrCapsuleShapeGetLength },
{ "setLength", l_lovrCapsuleShapeSetLength },
{ NULL, NULL }
};
@ -386,16 +425,32 @@ static int l_lovrCylinderShapeGetRadius(lua_State* L) {
return 1;
}
static int l_lovrCylinderShapeSetRadius(lua_State* L) {
CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape);
float radius = luax_checkfloat(L, 2);
lovrCylinderShapeSetRadius(cylinder, radius);
return 0;
}
static int l_lovrCylinderShapeGetLength(lua_State* L) {
CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape);
lua_pushnumber(L, lovrCylinderShapeGetLength(cylinder));
return 1;
}
static int l_lovrCylinderShapeSetLength(lua_State* L) {
CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape);
float length = luax_checkfloat(L, 2);
lovrCylinderShapeSetLength(cylinder, length);
return 0;
}
const luaL_Reg lovrCylinderShape[] = {
lovrShape,
{ "getRadius", l_lovrCylinderShapeGetRadius },
{ "setRadius", l_lovrCylinderShapeSetRadius },
{ "getLength", l_lovrCylinderShapeGetLength },
{ "setLength", l_lovrCylinderShapeSetLength },
{ NULL, NULL }
};
@ -460,109 +515,3 @@ const luaL_Reg lovrTerrainShape[] = {
lovrShape,
{ NULL, NULL }
};
static int l_lovrCompoundShapeIsFrozen(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
bool frozen = lovrCompoundShapeIsFrozen(shape);
lua_pushboolean(L, frozen);
return 1;
}
static int l_lovrCompoundShapeAddChild(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
Shape* child = luax_checkshape(L, 2);
float position[3], orientation[4];
int index = 3;
index = luax_readvec3(L, index, position, NULL);
index = luax_readquat(L, index, orientation, NULL);
lovrCompoundShapeAddChild(shape, child, position, orientation);
return 0;
}
static int l_lovrCompoundShapeReplaceChild(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
uint32_t index = luax_checku32(L, 2) - 1;
Shape* child = luax_checkshape(L, 3);
float position[3], orientation[4];
int i = 4;
i = luax_readvec3(L, i, position, NULL);
i = luax_readquat(L, i, orientation, NULL);
lovrCompoundShapeReplaceChild(shape, index, child, position, orientation);
return 0;
}
static int l_lovrCompoundShapeRemoveChild(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
uint32_t index = luax_checku32(L, 2) - 1;
lovrCompoundShapeRemoveChild(shape, index);
return 0;
}
static int l_lovrCompoundShapeGetChild(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
uint32_t index = luax_checku32(L, 2) - 1;
Shape* child = lovrCompoundShapeGetChild(shape, index);
luax_pushshape(L, child);
return 1;
}
static int l_lovrCompoundShapeGetChildren(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
int count = (int) lovrCompoundShapeGetChildCount(shape);
lua_createtable(L, count, 0);
for (int i = 0; i < count; i++) {
Shape* child = lovrCompoundShapeGetChild(shape, (uint32_t) i);
luax_pushshape(L, child);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
static int l_lovrCompoundShapeGetChildCount(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
uint32_t count = lovrCompoundShapeGetChildCount(shape);
lua_pushinteger(L, count);
return 1;
}
static int l_lovrCompoundShapeGetChildOffset(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
uint32_t index = luax_checku32(L, 2) - 1;
float position[3], orientation[4], angle, ax, ay, az;
lovrCompoundShapeGetChildOffset(shape, index, 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_lovrCompoundShapeSetChildOffset(lua_State* L) {
CompoundShape* shape = luax_checktype(L, 1, CompoundShape);
uint32_t index = luax_checku32(L, 2) - 1;
float position[3], orientation[4];
int i = 3;
i = luax_readvec3(L, i, position, NULL);
i = luax_readquat(L, i, orientation, NULL);
lovrCompoundShapeSetChildOffset(shape, index, position, orientation);
return 0;
}
const luaL_Reg lovrCompoundShape[] = {
lovrShape,
{ "isFrozen", l_lovrCompoundShapeIsFrozen },
{ "addChild", l_lovrCompoundShapeAddChild },
{ "replaceChild", l_lovrCompoundShapeReplaceChild },
{ "removeChild", l_lovrCompoundShapeRemoveChild },
{ "getChild", l_lovrCompoundShapeGetChild },
{ "getChildren", l_lovrCompoundShapeGetChildren },
{ "getChildCount", l_lovrCompoundShapeGetChildCount },
{ "getChildOffset", l_lovrCompoundShapeGetChildOffset },
{ "setChildOffset", l_lovrCompoundShapeSetChildOffset },
{ "__len", l_lovrCompoundShapeGetChildCount }, // :)
{ NULL, NULL }
};

View file

@ -133,9 +133,8 @@ static void contactCallback(void* userdata, World* world, Collider* a, Collider*
static int l_lovrWorldNewCollider(lua_State* L) {
World* world = luax_checktype(L, 1, World);
float position[3];
int index = luax_readvec3(L, 2, position, NULL);
Shape* shape = luax_checkshape(L, index);
Collider* collider = lovrColliderCreate(world, position, shape);
luax_readvec3(L, 2, position, NULL);
Collider* collider = lovrColliderCreate(world, position, NULL);
luax_pushtype(L, Collider, collider);
lovrRelease(collider, lovrColliderDestroy);
return 1;
@ -335,22 +334,21 @@ static int l_lovrWorldRaycast(lua_State* L) {
static int l_lovrWorldShapecast(lua_State* L) {
World* world = luax_checktype(L, 1, World);
int index = 2;
float pose[7], scale, end[3];
float pose[7], end[3];
Shape* shape = luax_checkshape(L, index++);
index = luax_readvec3(L, index, pose, NULL);
index = luax_readvec3(L, index, end, NULL);
scale = luax_optfloat(L, index++, 1.f);
index = luax_readquat(L, index, pose + 3, NULL);
uint32_t filter = luax_checktagmask(L, index++, world);
if (lua_isnoneornil(L, index)) {
CastResult hit;
if (lovrWorldShapecast(world, shape, pose, scale, end, filter, castClosestCallback, &hit)) {
if (lovrWorldShapecast(world, shape, pose, end, filter, castClosestCallback, &hit)) {
return luax_pushcastresult(L, &hit);
}
} else {
luaL_checktype(L, index, LUA_TFUNCTION);
lua_settop(L, index);
lovrWorldShapecast(world, shape, pose, scale, end, filter, castCallback, L);
lovrWorldShapecast(world, shape, pose, end, filter, castCallback, L);
}
return 0;
}
@ -358,31 +356,20 @@ static int l_lovrWorldShapecast(lua_State* L) {
static int l_lovrWorldCollideShape(lua_State* L) {
World* world = luax_checktype(L, 1, World);
int index;
Shape* shape;
float pose[7], scale;
Collider* collider = luax_totype(L, 2, Collider);
if (collider) {
shape = lovrColliderGetShape(collider);
lovrColliderGetPosition(collider, pose);
lovrColliderGetOrientation(collider, pose + 3);
scale = 1.f;
index = 3;
} else {
shape = luax_checkshape(L, 2);
index = luax_readvec3(L, 3, pose, NULL);
scale = luax_optfloat(L, index++, 1.f);
index = luax_readquat(L, index, pose + 3, NULL);
}
float pose[7];
Shape* shape = luax_checkshape(L, 2);
index = luax_readvec3(L, 3, pose, NULL);
index = luax_readquat(L, index, pose + 3, NULL);
uint32_t filter = luax_checktagmask(L, index++, world);
if (lua_isnoneornil(L, index)) {
CollideResult hit;
if (lovrWorldCollideShape(world, shape, pose, scale, filter, collideFirstCallback, &hit)) {
if (lovrWorldCollideShape(world, shape, pose, filter, collideFirstCallback, &hit)) {
return luax_pushcollideresult(L, &hit);
}
} else {
luaL_checktype(L, index, LUA_TFUNCTION);
lua_settop(L, index);
lovrWorldCollideShape(world, shape, pose, scale, filter, collideCallback, L);
lovrWorldCollideShape(world, shape, pose, filter, collideCallback, L);
}
return 0;
}

View file

@ -21,7 +21,6 @@ typedef Shape CylinderShape;
typedef Shape ConvexShape;
typedef Shape MeshShape;
typedef Shape TerrainShape;
typedef Shape CompoundShape;
typedef Joint WeldJoint;
typedef Joint BallJoint;
@ -86,8 +85,8 @@ void lovrWorldGetGravity(World* world, float gravity[3]);
void lovrWorldSetGravity(World* world, float gravity[3]);
void lovrWorldUpdate(World* world, float dt);
bool lovrWorldRaycast(World* world, float start[3], float end[3], uint32_t filter, CastCallback* callback, void* userdata);
bool lovrWorldShapecast(World* world, Shape* shape, float pose[7], float scale, float end[3], uint32_t filter, CastCallback* callback, void* userdata);
bool lovrWorldCollideShape(World* world, Shape* shape, float pose[7], float scale, uint32_t filter, CollideCallback* callback, void* userdata);
bool lovrWorldShapecast(World* world, Shape* shape, float pose[7], float end[3], uint32_t filter, CastCallback* callback, void* userdata);
bool lovrWorldCollideShape(World* world, Shape* shape, float pose[7], uint32_t filter, CollideCallback* callback, void* userdata);
bool lovrWorldQueryBox(World* world, float position[3], float size[3], uint32_t filter, QueryCallback* callback, void* userdata);
bool lovrWorldQuerySphere(World* world, float position[3], float radius, uint32_t filter, QueryCallback* callback, void* userdata);
void lovrWorldDisableCollisionBetween(World* world, const char* tag1, const char* tag2);
@ -119,8 +118,9 @@ bool lovrColliderIsEnabled(Collider* collider);
void lovrColliderSetEnabled(Collider* collider, bool enable);
World* lovrColliderGetWorld(Collider* collider);
Joint* lovrColliderGetJoints(Collider* collider, Joint* joint);
Shape* lovrColliderGetShape(Collider* collider);
void lovrColliderSetShape(Collider* collider, Shape* shape);
Shape* lovrColliderGetShapes(Collider* collider, Shape* shape);
void lovrColliderAddShape(Collider* collider, Shape* shape);
void lovrColliderRemoveShape(Collider* collider, Shape* shape);
const char* lovrColliderGetTag(Collider* collider);
void lovrColliderSetTag(Collider* collider, const char* tag);
float lovrColliderGetFriction(Collider* collider);
@ -140,11 +140,13 @@ 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 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]);
bool lovrColliderGetAutomaticMass(Collider* collider);
void lovrColliderSetAutomaticMass(Collider* collider, bool enable);
void lovrColliderResetMassData(Collider* collider);
void lovrColliderGetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]);
void lovrColliderSetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]);
@ -205,54 +207,57 @@ typedef enum {
SHAPE_CYLINDER,
SHAPE_CONVEX,
SHAPE_MESH,
SHAPE_TERRAIN,
SHAPE_COMPOUND
SHAPE_TERRAIN
} ShapeType;
void lovrShapeDestroy(void* ref);
void lovrShapeDestroyData(Shape* shape);
bool lovrShapeIsDestroyed(Shape* shape);
ShapeType lovrShapeGetType(Shape* shape);
Collider* lovrShapeGetCollider(Shape* shape);
float lovrShapeGetVolume(Shape* shape);
float lovrShapeGetDensity(Shape* shape);
void lovrShapeSetDensity(Shape* shape, float density);
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]);
void lovrShapeGetOffset(Shape* shape, float position[3], float orientation[4]);
void lovrShapeSetOffset(Shape* shape, float position[3], float orientation[4]);
void lovrShapeGetPose(Shape* shape, float position[3], float orientation[4]);
void lovrShapeGetAABB(Shape* shape, float aabb[6]);
BoxShape* lovrBoxShapeCreate(float dimensions[3]);
void lovrBoxShapeGetDimensions(BoxShape* shape, float dimensions[3]);
void lovrBoxShapeSetDimensions(BoxShape* shape, float dimensions[3]);
SphereShape* lovrSphereShapeCreate(float radius);
float lovrSphereShapeGetRadius(SphereShape* shape);
void lovrSphereShapeSetRadius(SphereShape* shape, float radius);
CapsuleShape* lovrCapsuleShapeCreate(float radius, float length);
float lovrCapsuleShapeGetRadius(CapsuleShape* shape);
void lovrCapsuleShapeSetRadius(CapsuleShape* shape, float radius);
float lovrCapsuleShapeGetLength(CapsuleShape* shape);
void lovrCapsuleShapeSetLength(CapsuleShape* shape, float length);
CylinderShape* lovrCylinderShapeCreate(float radius, float length);
float lovrCylinderShapeGetRadius(CylinderShape* shape);
void lovrCylinderShapeSetRadius(CylinderShape* shape, float radius);
float lovrCylinderShapeGetLength(CylinderShape* shape);
void lovrCylinderShapeSetLength(CylinderShape* shape, float length);
ConvexShape* lovrConvexShapeCreate(float points[], uint32_t count);
ConvexShape* lovrConvexShapeClone(ConvexShape* parent);
uint32_t lovrConvexShapeGetPointCount(ConvexShape* shape);
void lovrConvexShapeGetPoint(ConvexShape* shape, uint32_t index, float point[3]);
uint32_t lovrConvexShapeGetFaceCount(ConvexShape* shape);
uint32_t lovrConvexShapeGetFace(ConvexShape* shape, uint32_t index, uint32_t* pointIndices, uint32_t capacity);
MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount, uint32_t indices[]);
MeshShape* lovrMeshShapeClone(MeshShape* parent);
TerrainShape* lovrTerrainShapeCreate(float* vertices, uint32_t n, float scaleXZ, float scaleY);
CompoundShape* lovrCompoundShapeCreate(Shape** shapes, float* positions, float* orientations, uint32_t count, bool freeze);
bool lovrCompoundShapeIsFrozen(CompoundShape* shape);
void lovrCompoundShapeAddChild(CompoundShape* shape, Shape* child, float position[3], float orientation[4]);
void lovrCompoundShapeReplaceChild(CompoundShape* shape, uint32_t index, Shape* child, float position[3], float orientation[4]);
void lovrCompoundShapeRemoveChild(CompoundShape* shape, uint32_t index);
Shape* lovrCompoundShapeGetChild(CompoundShape* shape, uint32_t index);
uint32_t lovrCompoundShapeGetChildCount(CompoundShape* shape);
void lovrCompoundShapeGetChildOffset(CompoundShape* shape, uint32_t index, float position[3], float orientation[4]);
void lovrCompoundShapeSetChildOffset(CompoundShape* shape, uint32_t index, float position[3], float orientation[4]);
// These tokens need to exist for Lua bindings
#define lovrBoxShapeDestroy lovrShapeDestroy
#define lovrSphereShapeDestroy lovrShapeDestroy
@ -261,7 +266,6 @@ void lovrCompoundShapeSetChildOffset(CompoundShape* shape, uint32_t index, float
#define lovrConvexShapeDestroy lovrShapeDestroy
#define lovrMeshShapeDestroy lovrShapeDestroy
#define lovrTerrainShapeDestroy lovrShapeDestroy
#define lovrCompoundShapeDestroy lovrShapeDestroy
// Joints

File diff suppressed because it is too large Load diff

View file

@ -293,11 +293,11 @@ bool lovrWorldRaycast(World* world, float start[3], float end[3], uint32_t filte
return false;
}
bool lovrWorldShapecast(World* world, Shape* shape, float pose[7], float scale, float end[3], uint32_t filter, CastCallback* callback, void* userdata) {
bool lovrWorldShapecast(World* world, Shape* shape, float pose[7], float end[3], uint32_t filter, CastCallback* callback, void* userdata) {
return false;
}
bool lovrWorldCollideShape(World* world, Shape* shape, float pose[7], float scale, uint32_t filter, CollideCallback* callback, void* userdata) {
bool lovrWorldCollideShape(World* world, Shape* shape, float pose[7], uint32_t filter, CollideCallback* callback, void* userdata) {
return false;
}
@ -436,7 +436,7 @@ Collider* lovrColliderCreate(World* world, float position[3], Shape* shape) {
dBodySetData(collider->body, collider);
arr_init(&collider->joints);
lovrColliderSetShape(collider, shape);
lovrColliderAddShape(collider, shape);
lovrColliderSetPosition(collider, position);
// Adjust the world's collider list
@ -465,8 +465,6 @@ void lovrColliderDestroyData(Collider* collider) {
return;
}
lovrColliderSetShape(collider, NULL);
Joint** joints = collider->joints.data;
size_t count = collider->joints.length;
for (size_t i = 0; i < count; i++) {
@ -501,31 +499,12 @@ World* lovrColliderGetWorld(Collider* collider) {
return collider->world;
}
Shape* lovrColliderGetShape(Collider* collider) {
return collider->shape;
Shape* lovrColliderGetShapes(Collider* collider, Shape* shape) {
return NULL;
}
void lovrColliderSetShape(Collider* collider, Shape* shape) {
if (collider->shape) {
dSpaceRemove(collider->world->space, collider->shape->id);
dGeomSetBody(collider->shape->id, 0);
collider->shape->collider = NULL;
lovrRelease(collider->shape, lovrShapeDestroy);
}
collider->shape = shape;
if (shape) {
if (shape->collider) {
lovrColliderSetShape(shape->collider, NULL);
}
shape->collider = collider;
dGeomSetBody(shape->id, collider->body);
dSpaceID newSpace = collider->world->space;
dSpaceAdd(newSpace, shape->id);
lovrRetain(shape);
}
void lovrColliderAddShape(Collider* collider, Shape* shape) {
//
}
Joint* lovrColliderGetJoints(Collider* collider, Joint* joint) {
@ -633,10 +612,10 @@ float lovrColliderGetMass(Collider* collider) {
return m.mass;
}
void lovrColliderSetMass(Collider* collider, float* mass) {
void lovrColliderSetMass(Collider* collider, float mass) {
dMass m;
dBodyGetMass(collider->body, &m);
dMassAdjust(&m, *mass);
dMassAdjust(&m, mass);
dBodySetMass(collider->body, &m);
}
@ -656,6 +635,14 @@ void lovrColliderSetCenterOfMass(Collider* collider, float center[3]) {
//
}
bool lovrColliderGetAutomaticMass(Collider* collider) {
return false;
}
void lovrColliderSetAutomaticMass(Collider* collider, bool enable) {
//
}
void lovrColliderResetMassData(Collider* collider) {
//
}
@ -913,6 +900,14 @@ void lovrShapeDestroy(void* ref) {
lovrFree(shape);
}
void lovrShapeDestroyData(Shape* shape) {
//
}
bool lovrShapeIsDestroyed(Shape* shape) {
return false;
}
ShapeType lovrShapeGetType(Shape* shape) {
return shape->type;
}
@ -949,7 +944,12 @@ void lovrShapeGetMassData(Shape* shape, float* mass, float inertia[9], float cen
//
}
void lovrShapeGetAABB(Shape* shape, float position[3], float orientation[4], float aabb[6]) {
void lovrShapeGetOffset(Shape* shape, float position[3], float orientation[4]) {}
void lovrShapeSetOffset(Shape* shape, float position[3], float orientation[4]) {}
void lovrShapeGetPosition(Shape* shape, float position[3]) {}
void lovrShapeGetOrientation(Shape* shape, float orientation[4]) {}
void lovrShapeGetAABB(Shape* shape, float aabb[6]) {
dGeomGetAABB(shape->id, aabb);
}
@ -1035,6 +1035,10 @@ ConvexShape* lovrConvexShapeCreate(float positions[], uint32_t count) {
lovrThrow("ODE does not support ConvexShape");
}
ConvexShape* lovrConvexShapeClone(ConvexShape* parent) {
return NULL;
}
uint32_t lovrConvexShapeGetPointCount(ConvexShape* convex) {
return 0;
}
@ -1065,6 +1069,10 @@ MeshShape* lovrMeshShapeCreate(int vertexCount, float* vertices, int indexCount,
return mesh;
}
MeshShape* lovrMeshShapeClone(MeshShape* parent) {
return NULL;
}
TerrainShape* lovrTerrainShapeCreate(float* vertices, uint32_t n, float scaleXZ, float scaleY) {
const float thickness = 10.f;
TerrainShape* terrain = lovrCalloc(sizeof(TerrainShape));
@ -1077,42 +1085,6 @@ TerrainShape* lovrTerrainShapeCreate(float* vertices, uint32_t n, float scaleXZ,
return terrain;
}
CompoundShape* lovrCompoundShapeCreate(Shape** shapes, float* positions, float* orientations, uint32_t count, bool freeze) {
lovrThrow("ODE does not support CompoundShape");
}
bool lovrCompoundShapeIsFrozen(CompoundShape* shape) {
return false;
}
void lovrCompoundShapeAddChild(CompoundShape* shape, Shape* child, float* position, float* orientation) {
//
}
void lovrCompoundShapeReplaceChild(CompoundShape* shape, uint32_t index, Shape* child, float* position, float* orientation) {
//
}
void lovrCompoundShapeRemoveChild(CompoundShape* shape, uint32_t index) {
//
}
Shape* lovrCompoundShapeGetChild(CompoundShape* shape, uint32_t index) {
return NULL;
}
uint32_t lovrCompoundShapeGetChildCount(CompoundShape* shape) {
return 0;
}
void lovrCompoundShapeGetChildOffset(CompoundShape* shape, uint32_t index, float* position, float* orientation) {
//
}
void lovrCompoundShapeSetChildOffset(CompoundShape* shape, uint32_t index, float* position, float* orientation) {
//
}
void lovrJointDestroy(void* ref) {
Joint* joint = ref;
lovrJointDestroyData(joint);