mirror of https://github.com/bjornbytes/lovr.git
ConeJoint;
This commit is contained in:
parent
ea9e512d9a
commit
e9a3db6250
|
@ -8,6 +8,7 @@ void luax_pushjoint(lua_State* L, Joint* joint) {
|
|||
switch (lovrJointGetType(joint)) {
|
||||
case JOINT_WELD: luax_pushtype(L, WeldJoint, joint); break;
|
||||
case JOINT_BALL: luax_pushtype(L, BallJoint, joint); break;
|
||||
case JOINT_CONE: luax_pushtype(L, ConeJoint, joint); break;
|
||||
case JOINT_DISTANCE: luax_pushtype(L, DistanceJoint, joint); break;
|
||||
case JOINT_HINGE: luax_pushtype(L, HingeJoint, joint); break;
|
||||
case JOINT_SLIDER: luax_pushtype(L, SliderJoint, joint); break;
|
||||
|
@ -22,6 +23,7 @@ Joint* luax_checkjoint(lua_State* L, int index) {
|
|||
const uint64_t hashes[] = {
|
||||
hash64("WeldJoint", strlen("WeldJoint")),
|
||||
hash64("BallJoint", strlen("BallJoint")),
|
||||
hash64("ConeJoint", strlen("ConeJoint")),
|
||||
hash64("DistanceJoint", strlen("DistanceJoint")),
|
||||
hash64("HingeJoint", strlen("HingeJoint")),
|
||||
hash64("SliderJoint", strlen("SliderJoint"))
|
||||
|
@ -176,6 +178,52 @@ const luaL_Reg lovrBallJoint[] = {
|
|||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static int l_lovrConeJointGetAnchors(lua_State* L) {
|
||||
ConeJoint* joint = luax_checktype(L, 1, ConeJoint);
|
||||
float anchor1[3], anchor2[3];
|
||||
lovrConeJointGetAnchors(joint, anchor1, anchor2);
|
||||
lua_pushnumber(L, anchor1[0]);
|
||||
lua_pushnumber(L, anchor1[1]);
|
||||
lua_pushnumber(L, anchor1[2]);
|
||||
lua_pushnumber(L, anchor2[0]);
|
||||
lua_pushnumber(L, anchor2[1]);
|
||||
lua_pushnumber(L, anchor2[2]);
|
||||
return 6;
|
||||
}
|
||||
|
||||
static int l_lovrConeJointGetAxis(lua_State* L) {
|
||||
ConeJoint* joint = luax_checktype(L, 1, ConeJoint);
|
||||
float axis[3];
|
||||
lovrConeJointGetAxis(joint, axis);
|
||||
lua_pushnumber(L, axis[0]);
|
||||
lua_pushnumber(L, axis[1]);
|
||||
lua_pushnumber(L, axis[2]);
|
||||
return 3;
|
||||
}
|
||||
|
||||
static int l_lovrConeJointGetLimit(lua_State* L) {
|
||||
ConeJoint* joint = luax_checktype(L, 1, ConeJoint);
|
||||
float limit = lovrConeJointGetLimit(joint);
|
||||
lua_pushnumber(L, limit);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_lovrConeJointSetLimit(lua_State* L) {
|
||||
ConeJoint* joint = luax_checktype(L, 1, ConeJoint);
|
||||
float limit = luax_checkfloat(L, 2);
|
||||
lovrConeJointSetLimit(joint, limit);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrConeJoint[] = {
|
||||
lovrJoint,
|
||||
{ "getAnchors", l_lovrConeJointGetAnchors },
|
||||
{ "getAxis", l_lovrConeJointGetAxis },
|
||||
{ "getLimit", l_lovrConeJointGetLimit },
|
||||
{ "setLimit", l_lovrConeJointSetLimit },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static int l_lovrDistanceJointGetAnchors(lua_State* L) {
|
||||
DistanceJoint* joint = luax_checktype(L, 1, DistanceJoint);
|
||||
float anchor1[3], anchor2[3];
|
||||
|
|
|
@ -24,6 +24,7 @@ typedef Shape CompoundShape;
|
|||
|
||||
typedef Joint WeldJoint;
|
||||
typedef Joint BallJoint;
|
||||
typedef Joint ConeJoint;
|
||||
typedef Joint DistanceJoint;
|
||||
typedef Joint HingeJoint;
|
||||
typedef Joint SliderJoint;
|
||||
|
@ -240,6 +241,7 @@ void lovrCompoundShapeSetChildOffset(CompoundShape* shape, uint32_t index, float
|
|||
typedef enum {
|
||||
JOINT_WELD,
|
||||
JOINT_BALL,
|
||||
JOINT_CONE,
|
||||
JOINT_DISTANCE,
|
||||
JOINT_HINGE,
|
||||
JOINT_SLIDER
|
||||
|
@ -271,6 +273,12 @@ void lovrWeldJointGetAnchors(WeldJoint* joint, float anchor1[3], float anchor2[3
|
|||
BallJoint* lovrBallJointCreate(Collider* a, Collider* b, float anchor[3]);
|
||||
void lovrBallJointGetAnchors(BallJoint* joint, float anchor1[3], float anchor2[3]);
|
||||
|
||||
ConeJoint* lovrConeJointCreate(Collider* a, Collider* b, float anchor[3], float axis[3]);
|
||||
void lovrConeJointGetAnchors(ConeJoint* joint, float anchor1[3], float anchor2[3]);
|
||||
void lovrConeJointGetAxis(ConeJoint* joint, float axis[3]);
|
||||
float lovrConeJointGetLimit(ConeJoint* joint);
|
||||
void lovrConeJointSetLimit(ConeJoint* joint, float angle);
|
||||
|
||||
DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float anchor1[3], float anchor2[3]);
|
||||
void lovrDistanceJointGetAnchors(DistanceJoint* joint, float anchor1[3], float anchor2[3]);
|
||||
void lovrDistanceJointGetLimits(DistanceJoint* joint, float* min, float* max);
|
||||
|
|
|
@ -1362,6 +1362,62 @@ void lovrBallJointGetAnchors(BallJoint* joint, float anchor1[3], float anchor2[3
|
|||
lovrJointGetAnchors((Joint*) joint, anchor1, anchor2);
|
||||
}
|
||||
|
||||
// ConeJoint
|
||||
|
||||
ConeJoint* lovrConeJointCreate(Collider* a, Collider* b, float anchor[3], float axis[3]) {
|
||||
lovrCheck(a || b, "Joint requires at least one Collider");
|
||||
lovrCheck(!a || !b || a->world == b->world, "Joint bodies must exist in same World");
|
||||
JPH_Body* bodyA = a ? a->body : NULL;
|
||||
JPH_Body* bodyB = b ? b->body : NULL;
|
||||
World* world = (a ? a : b)->world;
|
||||
|
||||
ConeJoint* joint = lovrCalloc(sizeof(ConeJoint));
|
||||
joint->ref = 1;
|
||||
joint->type = JOINT_CONE;
|
||||
|
||||
JPH_ConeConstraintSettings* settings = JPH_ConeConstraintSettings_Create();
|
||||
JPH_ConeConstraintSettings_SetPoint1(settings, vec3_toJolt(anchor));
|
||||
JPH_ConeConstraintSettings_SetPoint2(settings, vec3_toJolt(anchor));
|
||||
JPH_ConeConstraintSettings_SetTwistAxis1(settings, vec3_toJolt(axis));
|
||||
JPH_ConeConstraintSettings_SetTwistAxis2(settings, vec3_toJolt(axis));
|
||||
joint->constraint = (JPH_Constraint*) JPH_ConeConstraintSettings_CreateConstraint(settings, bodyA, bodyB);
|
||||
JPH_PhysicsSystem_AddConstraint((a ? a : b)->world->system, joint->constraint);
|
||||
lovrJointInit(joint, a, b);
|
||||
lovrRetain(joint);
|
||||
return joint;
|
||||
}
|
||||
|
||||
void lovrConeJointGetAnchors(ConeJoint* joint, float anchor1[3], float anchor2[3]) {
|
||||
lovrJointGetAnchors((Joint*) joint, anchor1, anchor2);
|
||||
}
|
||||
|
||||
void lovrConeJointGetAxis(ConeJoint* joint, float axis[3]) {
|
||||
JPH_Vec3 resultAxis;
|
||||
JPH_ConeConstraintSettings* settings = (JPH_ConeConstraintSettings*) JPH_Constraint_GetConstraintSettings((JPH_Constraint*) joint->constraint);
|
||||
JPH_ConeConstraintSettings_GetTwistAxis1(settings, &resultAxis);
|
||||
JPH_Body* body1 = JPH_TwoBodyConstraint_GetBody1((JPH_TwoBodyConstraint*) joint->constraint);
|
||||
JPH_RMatrix4x4 centerOfMassTransform;
|
||||
JPH_Body_GetCenterOfMassTransform(body1, ¢erOfMassTransform);
|
||||
JPH_Matrix4x4 constraintToBody;
|
||||
JPH_TwoBodyConstraint_GetConstraintToBody1Matrix((JPH_TwoBodyConstraint*) joint->constraint, &constraintToBody);
|
||||
float translation[4] = {
|
||||
resultAxis.x,
|
||||
resultAxis.y,
|
||||
resultAxis.z,
|
||||
0.f
|
||||
};
|
||||
mat4_mulVec4(¢erOfMassTransform.m11, translation);
|
||||
vec3_init(axis, translation);
|
||||
}
|
||||
|
||||
float lovrConeJointGetLimit(ConeJoint* joint) {
|
||||
return acosf(JPH_ConeConstraint_GetCosHalfConeAngle((JPH_ConeConstraint*) joint->constraint));
|
||||
}
|
||||
|
||||
void lovrConeJointSetLimit(ConeJoint* joint, float angle) {
|
||||
JPH_ConeConstraint_SetHalfConeAngle((JPH_ConeConstraint*) joint->constraint, CLAMP(angle, 0.f, (float) M_PI));
|
||||
}
|
||||
|
||||
// DistanceJoint
|
||||
|
||||
DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float anchor1[3], float anchor2[3]) {
|
||||
|
@ -1445,7 +1501,7 @@ void lovrHingeJointGetAnchors(HingeJoint* joint, float anchor1[3], float anchor2
|
|||
|
||||
void lovrHingeJointGetAxis(HingeJoint* joint, float axis[3]) {
|
||||
JPH_Vec3 resultAxis;
|
||||
JPH_HingeConstraintSettings* settings = JPH_HingeConstraint_GetSettings((JPH_HingeConstraint*) joint->constraint);
|
||||
JPH_HingeConstraintSettings* settings = (JPH_HingeConstraintSettings*) JPH_Constraint_GetConstraintSettings((JPH_Constraint*) joint->constraint);
|
||||
JPH_HingeConstraintSettings_GetHingeAxis1(settings, &resultAxis);
|
||||
JPH_Body* body1 = JPH_TwoBodyConstraint_GetBody1((JPH_TwoBodyConstraint*) joint->constraint);
|
||||
JPH_RMatrix4x4 centerOfMassTransform;
|
||||
|
@ -1459,9 +1515,7 @@ void lovrHingeJointGetAxis(HingeJoint* joint, float axis[3]) {
|
|||
0.f
|
||||
};
|
||||
mat4_mulVec4(¢erOfMassTransform.m11, translation);
|
||||
axis[0] = translation[0];
|
||||
axis[1] = translation[1];
|
||||
axis[2] = translation[2];
|
||||
vec3_init(axis, translation);
|
||||
}
|
||||
|
||||
float lovrHingeJointGetAngle(HingeJoint* joint) {
|
||||
|
@ -1590,7 +1644,7 @@ void lovrSliderJointGetAnchors(SliderJoint* joint, float anchor1[3], float ancho
|
|||
|
||||
void lovrSliderJointGetAxis(SliderJoint* joint, float axis[3]) {
|
||||
JPH_Vec3 resultAxis;
|
||||
JPH_SliderConstraintSettings* settings = JPH_SliderConstraint_GetSettings((JPH_SliderConstraint*) joint->constraint);
|
||||
JPH_SliderConstraintSettings* settings = (JPH_SliderConstraintSettings*) JPH_Constraint_GetConstraintSettings((JPH_Constraint*) joint->constraint);
|
||||
JPH_SliderConstraintSettings_GetSliderAxis(settings, &resultAxis);
|
||||
JPH_Body* body1 = JPH_TwoBodyConstraint_GetBody1((JPH_TwoBodyConstraint*) joint->constraint);
|
||||
JPH_RMatrix4x4 centerOfMassTransform;
|
||||
|
|
|
@ -1171,6 +1171,26 @@ void lovrBallJointGetAnchors(BallJoint* joint, float anchor1[3], float anchor2[3
|
|||
anchor2[2] = anchor[2];
|
||||
}
|
||||
|
||||
ConeJoint* lovrConeJointCreate(Collider* a, Collider* b, float anchor[3], float axis[3]) {
|
||||
lovrThrow("NYI");
|
||||
}
|
||||
|
||||
void lovrConeJointGetAnchors(ConeJoint* joint, float anchor1[3], float anchor2[3]) {
|
||||
//
|
||||
}
|
||||
|
||||
void lovrConeJointGetAxis(ConeJoint* joint, float axis[3]) {
|
||||
//
|
||||
}
|
||||
|
||||
float lovrConeJointGetLimit(ConeJoint* joint) {
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
void lovrConeJointSetLimit(ConeJoint* joint, float angle) {
|
||||
//
|
||||
}
|
||||
|
||||
DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float anchor1[3], float anchor2[3]) {
|
||||
lovrCheck(a->world == b->world, "Joint bodies must exist in same World");
|
||||
DistanceJoint* joint = lovrCalloc(sizeof(DistanceJoint));
|
||||
|
|
Loading…
Reference in New Issue