diff --git a/src/api/l_physics_world.c b/src/api/l_physics_world.c index c959f76a..c7b343e2 100644 --- a/src/api/l_physics_world.c +++ b/src/api/l_physics_world.c @@ -112,17 +112,16 @@ static int l_lovrWorldNewSphereCollider(lua_State* L) { static int l_lovrWorldNewMeshCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); - lovrAssert(lua_istable(L, 2), "Vertices must be a table"); - lovrAssert(lua_istable(L, 3), "Indices must be a table"); + luaL_checktype(L, 2, LUA_TTABLE); + luaL_checktype(L, 3, LUA_TTABLE); int vertexCount = luax_len(L, 2); int indexCount = luax_len(L, 3); - // TODO: this never gets deallocated - float * vertices = malloc(sizeof(float) * vertexCount * 3); - unsigned * indices = malloc(sizeof(unsigned) * indexCount); + float* vertices = malloc(sizeof(float) * vertexCount * 3); + unsigned* indices = malloc(sizeof(unsigned) * indexCount); for (int i = 0; i < vertexCount; i++) { lua_rawgeti(L, 2, i + 1); - lovrAssert(lua_istable(L, -1), "Each verticle must be a table of coordinates"); + lovrAssert(lua_istable(L, -1), "Each vertex must be a table of coordinates"); lua_rawgeti(L, -1, 1); // x vertices[i * 3 + 0] = luaL_optnumber(L, -1, 0.); lua_pop(L, 1); @@ -136,9 +135,9 @@ static int l_lovrWorldNewMeshCollider(lua_State* L) { for (int i = 0; i < indexCount; i++) { lua_rawgeti(L, 3, i + 1); indices[i] = luaL_checkinteger(L, -1) - 1; + lua_pop(L, 1); } - lua_pop(L, indexCount); - Collider* collider = lovrColliderCreate(world, 0,0,0); + Collider* collider = lovrColliderCreate(world, 0, 0, 0); MeshShape* shape = lovrMeshShapeCreate(vertexCount, vertices, indexCount, indices); lovrColliderAddShape(collider, shape); lovrColliderInitInertia(collider, shape); diff --git a/src/modules/physics/physics.c b/src/modules/physics/physics.c index b14f4895..9ce48b67 100644 --- a/src/modules/physics/physics.c +++ b/src/modules/physics/physics.c @@ -698,6 +698,8 @@ void lovrShapeDestroyData(Shape* shape) { if (shape->type == SHAPE_MESH) { dTriMeshDataID dataID = dGeomTriMeshGetData(shape->id); dGeomTriMeshDataDestroy(dataID); + free(shape->vertices); + free(shape->indices); } dGeomDestroy(shape->id); shape->id = NULL; @@ -925,7 +927,7 @@ void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length) { dGeomCylinderSetParams(cylinder->id, lovrCylinderShapeGetRadius(cylinder), length); } -MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount, dTriIndex indices[]) { +MeshShape* lovrMeshShapeCreate(int vertexCount, float* vertices, int indexCount, dTriIndex* indices) { MeshShape* mesh = calloc(1, sizeof(MeshShape)); lovrAssert(mesh, "Out of memory"); mesh->ref = 1; @@ -934,6 +936,8 @@ MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount dGeomTriMeshDataPreprocess2(dataID, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL); mesh->id = dCreateTriMesh(0, dataID, 0, 0, 0); mesh->type = SHAPE_MESH; + mesh->vertices = vertices; + mesh->indices = indices; dGeomSetData(mesh->id, mesh); return mesh; } diff --git a/src/modules/physics/physics.h b/src/modules/physics/physics.h index 016b4bc4..ec9d4ed3 100644 --- a/src/modules/physics/physics.h +++ b/src/modules/physics/physics.h @@ -59,6 +59,8 @@ struct Shape { ShapeType type; dGeomID id; Collider* collider; + void* vertices; + void* indices; void* userdata; bool sensor; };