SliderJoint;

This commit is contained in:
bjorn 2017-05-25 00:48:02 -07:00
parent c828eaa338
commit a32eb38026
5 changed files with 104 additions and 15 deletions

View File

@ -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[];

View File

@ -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 }
};

View File

@ -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 }
};

View File

@ -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);
}

View File

@ -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);