From a32eb380266cf51de1bc8b8d99aef3fb5dd41003 Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 25 May 2017 00:48:02 -0700 Subject: [PATCH] SliderJoint; --- src/api/lovr.h | 2 ++ src/api/physics.c | 33 +++++++++++++++++++++++--------- src/api/types/joints.c | 32 +++++++++++++++++++++++++++++++ src/physics/physics.c | 43 +++++++++++++++++++++++++++++++++++++----- src/physics/physics.h | 9 ++++++++- 5 files changed, 104 insertions(+), 15 deletions(-) diff --git a/src/api/lovr.h b/src/api/lovr.h index 6bea80d8..58bdf5e9 100644 --- a/src/api/lovr.h +++ b/src/api/lovr.h @@ -27,6 +27,7 @@ extern const luaL_Reg lovrFilesystem[]; extern const luaL_Reg lovrFont[]; extern const luaL_Reg lovrGraphics[]; extern const luaL_Reg lovrHeadset[]; +extern const luaL_Reg lovrHingeJoint[]; extern const luaL_Reg lovrJoint[]; extern const luaL_Reg lovrMath[]; extern const luaL_Reg lovrMesh[]; @@ -35,6 +36,7 @@ extern const luaL_Reg lovrPhysics[]; extern const luaL_Reg lovrShader[]; extern const luaL_Reg lovrShape[]; extern const luaL_Reg lovrSkybox[]; +extern const luaL_Reg lovrSliderJoint[]; extern const luaL_Reg lovrSource[]; extern const luaL_Reg lovrSphereShape[]; extern const luaL_Reg lovrTexture[]; diff --git a/src/api/physics.c b/src/api/physics.c index c5a2d647..6fb22dd5 100644 --- a/src/api/physics.c +++ b/src/api/physics.c @@ -10,6 +10,8 @@ int l_lovrPhysicsInit(lua_State* L) { luax_registertype(L, "World", lovrWorld); luax_registertype(L, "Collider", lovrCollider); luax_extendtype(L, "Joint", "BallJoint", lovrJoint, lovrBallJoint); + luax_extendtype(L, "Joint", "HingeJoint", lovrJoint, lovrHingeJoint); + luax_extendtype(L, "Joint", "SliderJoint", lovrJoint, lovrSliderJoint); luax_extendtype(L, "Shape", "SphereShape", lovrShape, lovrSphereShape); luax_extendtype(L, "Shape", "BoxShape", lovrShape, lovrBoxShape); luax_extendtype(L, "Shape", "CapsuleShape", lovrShape, lovrCapsuleShape); @@ -18,6 +20,7 @@ int l_lovrPhysicsInit(lua_State* L) { map_init(&JointTypes); map_set(&JointTypes, "ball", JOINT_BALL); map_set(&JointTypes, "hinge", JOINT_HINGE); + map_set(&JointTypes, "slider", JOINT_SLIDER); map_init(&ShapeTypes); map_set(&ShapeTypes, "sphere", SHAPE_SPHERE); @@ -38,9 +41,9 @@ int l_lovrPhysicsNewWorld(lua_State* L) { int l_lovrPhysicsNewBallJoint(lua_State* L) { Collider* a = luax_checktype(L, 1, Collider); Collider* b = luax_checktype(L, 2, Collider); - float x = luaL_optnumber(L, 3, 0.f); - float y = luaL_optnumber(L, 4, 0.f); - float z = luaL_optnumber(L, 5, 0.f); + float x = luaL_checknumber(L, 3); + float y = luaL_checknumber(L, 4); + float z = luaL_checknumber(L, 5); Joint* joint = lovrBallJointCreate(a, b, x, y, z); luax_pushtype(L, BallJoint, joint); return 1; @@ -74,17 +77,28 @@ int l_lovrPhysicsNewCylinderShape(lua_State* L) { int l_lovrPhysicsNewHingeJoint(lua_State* L) { Collider* a = luax_checktype(L, 1, Collider); Collider* b = luax_checktype(L, 2, Collider); - float x = luaL_optnumber(L, 3, 0.f); - float y = luaL_optnumber(L, 4, 0.f); - float z = luaL_optnumber(L, 5, 0.f); - float ax = luaL_optnumber(L, 6, 0.f); - float ay = luaL_optnumber(L, 7, 0.f); - float az = luaL_optnumber(L, 8, 0.f); + float x = luaL_checknumber(L, 3); + float y = luaL_checknumber(L, 4); + float z = luaL_checknumber(L, 5); + float ax = luaL_checknumber(L, 6); + float ay = luaL_checknumber(L, 7); + float az = luaL_checknumber(L, 8); Joint* joint = lovrHingeJointCreate(a, b, x, y, z, ax, ay, az); luax_pushtype(L, HingeJoint, joint); return 1; } +int l_lovrPhysicsNewSliderJoint(lua_State* L) { + Collider* a = luax_checktype(L, 1, Collider); + Collider* b = luax_checktype(L, 2, Collider); + float ax = luaL_checknumber(L, 3); + float ay = luaL_checknumber(L, 4); + float az = luaL_checknumber(L, 5); + Joint* joint = lovrSliderJointCreate(a, b, ax, ay, az); + luax_pushtype(L, SliderJoint, joint); + return 1; +} + int l_lovrPhysicsNewSphereShape(lua_State* L) { float radius = luaL_optnumber(L, 1, 1.f); SphereShape* sphere = lovrSphereShapeCreate(radius); @@ -99,6 +113,7 @@ const luaL_Reg lovrPhysics[] = { { "newCapsuleShape", l_lovrPhysicsNewCapsuleShape }, { "newCylinderShape", l_lovrPhysicsNewCylinderShape }, { "newHingeJoint", l_lovrPhysicsNewHingeJoint }, + { "newSliderJoint", l_lovrPhysicsNewSliderJoint }, { "newSphereShape", l_lovrPhysicsNewSphereShape }, { NULL, NULL } }; diff --git a/src/api/types/joints.c b/src/api/types/joints.c index f453feb0..43342619 100644 --- a/src/api/types/joints.c +++ b/src/api/types/joints.c @@ -146,3 +146,35 @@ const luaL_Reg lovrHingeJoint[] = { { "getAngle", l_lovrHingeJointGetAngle }, { NULL, NULL } }; + +int l_lovrSliderJointGetAxis(lua_State* L) { + SliderJoint* slider = luax_checktype(L, 1, SliderJoint); + float x, y, z; + lovrSliderJointGetAxis(slider, &x, &y, &z); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, z); + return 3; +} + +int l_lovrSliderJointSetAxis(lua_State* L) { + SliderJoint* slider = luax_checktype(L, 1, SliderJoint); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float z = luaL_checknumber(L, 4); + lovrSliderJointSetAxis(slider, x, y, z); + return 0; +} + +int l_lovrSliderJointGetPosition(lua_State* L) { + SliderJoint* slider = luax_checktype(L, 1, SliderJoint); + lua_pushnumber(L, lovrSliderJointGetPosition(slider)); + return 1; +} + +const luaL_Reg lovrSliderJoint[] = { + { "getAxis", l_lovrSliderJointGetAxis }, + { "setAxis", l_lovrSliderJointSetAxis }, + { "getPosition", l_lovrHingeJointGetAngle }, + { NULL, NULL } +}; diff --git a/src/physics/physics.c b/src/physics/physics.c index 78e1c64a..ea4fc09a 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -850,11 +850,11 @@ void lovrHingeJointSetAnchor(HingeJoint* hinge, float x, float y, float z) { } void lovrHingeJointGetAxis(HingeJoint* hinge, float* x, float* y, float* z) { - float anchor[3]; - dJointGetHingeAxis(hinge->id, anchor); - *x = anchor[0]; - *y = anchor[1]; - *z = anchor[2]; + float axis[3]; + dJointGetHingeAxis(hinge->id, axis); + *x = axis[0]; + *y = axis[1]; + *z = axis[2]; } void lovrHingeJointSetAxis(HingeJoint* hinge, float x, float y, float z) { @@ -864,3 +864,36 @@ void lovrHingeJointSetAxis(HingeJoint* hinge, float x, float y, float z) { float lovrHingeJointGetAngle(HingeJoint* hinge) { return dJointGetHingeAngle(hinge->id); } + +SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float ax, float ay, float az) { + if (a->world != b->world) { + error("Joint bodies must exist in same World"); + } + + SliderJoint* joint = lovrAlloc(sizeof(SliderJoint), lovrJointDestroy); + if (!joint) return NULL; + + joint->type = JOINT_SLIDER; + joint->id = dJointCreateSlider(a->world->id, 0); + dJointSetData(joint->id, joint); + dJointAttach(joint->id, a->body, b->body); + dJointSetSliderAxis(joint->id, ax, ay, az); + + return joint; +} + +void lovrSliderJointGetAxis(SliderJoint* slider, float* x, float* y, float* z) { + float axis[3]; + dJointGetSliderAxis(slider->id, axis); + *x = axis[0]; + *y = axis[1]; + *z = axis[2]; +} + +void lovrSliderJointSetAxis(SliderJoint* slider, float x, float y, float z) { + dJointSetSliderAxis(slider->id, x, y, z); +} + +float lovrSliderJointGetPosition(SliderJoint* slider) { + return dJointGetSliderPosition(slider->id); +} diff --git a/src/physics/physics.h b/src/physics/physics.h index 833b69cd..e8fa9908 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -16,7 +16,8 @@ typedef enum { typedef enum { JOINT_BALL, - JOINT_HINGE + JOINT_HINGE, + JOINT_SLIDER } JointType; typedef struct { @@ -60,6 +61,7 @@ typedef struct { typedef Joint BallJoint; typedef Joint HingeJoint; +typedef Joint SliderJoint; typedef void (*CollisionResolver)(World* world, void* userdata); typedef void (*RaycastCallback)(Shape* shape, float x, float y, float z, float nx, float ny, float nz, void* userdata); @@ -191,3 +193,8 @@ void lovrHingeJointSetAnchor(HingeJoint* hinge, float x, float y, float z); void lovrHingeJointGetAxis(HingeJoint* hinge, float* x, float* y, float* z); void lovrHingeJointSetAxis(HingeJoint* hinge, float x, float y, float z); float lovrHingeJointGetAngle(HingeJoint* hinge); + +SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float ax, float ay, float az); +void lovrSliderJointGetAxis(SliderJoint* slider, float* x, float* y, float* z); +void lovrSliderJointSetAxis(SliderJoint* slider, float x, float y, float z); +float lovrSliderJointGetPosition(SliderJoint* slider);