Skip to content

Commit

Permalink
Promote :getAnchors methods to superclass;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed May 26, 2024
1 parent 7178ab0 commit 1a5113c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 135 deletions.
98 changes: 14 additions & 84 deletions src/api/l_physics_joints.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ static int l_lovrJointGetColliders(lua_State* L) {
return 2;
}

static int l_lovrJointGetAnchors(lua_State* L) {
Joint* joint = luax_checkjoint(L, 1);
float anchor1[3], anchor2[3];
lovrJointGetAnchors(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_lovrJointGetUserData(lua_State* L) {
luax_checkjoint(L, 1);
luax_pushstash(L, "lovr.joint.userdata");
Expand Down Expand Up @@ -143,6 +156,7 @@ static int l_lovrJointGetTorque(lua_State* L) {
{ "isDestroyed", l_lovrJointIsDestroyed }, \
{ "getType", l_lovrJointGetType }, \
{ "getColliders", l_lovrJointGetColliders }, \
{ "getAnchors", l_lovrJointGetAnchors }, \
{ "getUserData", l_lovrJointGetUserData }, \
{ "setUserData", l_lovrJointSetUserData }, \
{ "getPriority", l_lovrJointGetPriority }, \
Expand All @@ -152,57 +166,16 @@ static int l_lovrJointGetTorque(lua_State* L) {
{ "getForce", l_lovrJointGetForce }, \
{ "getTorque", l_lovrJointGetTorque }

static int l_lovrWeldJointGetAnchors(lua_State* L) {
WeldJoint* joint = luax_checktype(L, 1, WeldJoint);
float anchor1[3], anchor2[3];
lovrWeldJointGetAnchors(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;
}

const luaL_Reg lovrWeldJoint[] = {
lovrJoint,
{ "getAnchors", l_lovrWeldJointGetAnchors },
{ NULL, NULL }
};

static int l_lovrBallJointGetAnchors(lua_State* L) {
BallJoint* joint = luax_checktype(L, 1, BallJoint);
float anchor1[3], anchor2[3];
lovrBallJointGetAnchors(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;
}

const luaL_Reg lovrBallJoint[] = {
lovrJoint,
{ "getAnchors", l_lovrBallJointGetAnchors },
{ 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];
Expand All @@ -229,26 +202,12 @@ static int l_lovrConeJointSetLimit(lua_State* L) {

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];
lovrDistanceJointGetAnchors(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_lovrDistanceJointGetLimits(lua_State* L) {
DistanceJoint* joint = luax_checktype(L, 1, DistanceJoint);
float min, max;
Expand Down Expand Up @@ -285,27 +244,13 @@ static int l_lovrDistanceJointSetSpring(lua_State* L) {

const luaL_Reg lovrDistanceJoint[] = {
lovrJoint,
{ "getAnchors", l_lovrDistanceJointGetAnchors },
{ "getLimits", l_lovrDistanceJointGetLimits },
{ "setLimits", l_lovrDistanceJointSetLimits },
{ "getSpring", l_lovrDistanceJointGetSpring },
{ "setSpring", l_lovrDistanceJointSetSpring },
{ NULL, NULL }
};

static int l_lovrHingeJointGetAnchors(lua_State* L) {
HingeJoint* joint = luax_checktype(L, 1, HingeJoint);
float anchor1[3], anchor2[3];
lovrHingeJointGetAnchors(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_lovrHingeJointGetAxis(lua_State* L) {
HingeJoint* joint = luax_checktype(L, 1, HingeJoint);
float axis[3];
Expand Down Expand Up @@ -448,7 +393,6 @@ static int l_lovrHingeJointSetSpring(lua_State* L) {

const luaL_Reg lovrHingeJoint[] = {
lovrJoint,
{ "getAnchors", l_lovrHingeJointGetAnchors },
{ "getAxis", l_lovrHingeJointGetAxis },
{ "getAngle", l_lovrHingeJointGetAngle },
{ "getLimits", l_lovrHingeJointGetLimits },
Expand All @@ -467,19 +411,6 @@ const luaL_Reg lovrHingeJoint[] = {
{ NULL, NULL }
};

static int l_lovrSliderJointGetAnchors(lua_State* L) {
SliderJoint* joint = luax_checktype(L, 1, SliderJoint);
float anchor1[3], anchor2[3];
lovrSliderJointGetAnchors(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_lovrSliderJointGetAxis(lua_State* L) {
SliderJoint* joint = luax_checktype(L, 1, SliderJoint);
float axis[3];
Expand Down Expand Up @@ -622,7 +553,6 @@ static int l_lovrSliderJointSetSpring(lua_State* L) {

const luaL_Reg lovrSliderJoint[] = {
lovrJoint,
{ "getAnchors", l_lovrSliderJointGetAnchors },
{ "getAxis", l_lovrSliderJointGetAxis },
{ "getPosition", l_lovrSliderJointGetPosition },
{ "getLimits", l_lovrSliderJointGetLimits },
Expand Down
66 changes: 21 additions & 45 deletions src/modules/physics/physics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1992,27 +1992,6 @@ TerrainShape* lovrTerrainShapeCreate(float* vertices, uint32_t n, float scaleXZ,

// Joints

static void lovrJointGetAnchors(Joint* joint, float anchor1[3], float anchor2[3]) {
JPH_Body* body1 = JPH_TwoBodyConstraint_GetBody1((JPH_TwoBodyConstraint*) joint->constraint);
JPH_Body* body2 = JPH_TwoBodyConstraint_GetBody2((JPH_TwoBodyConstraint*) joint->constraint);
JPH_RMatrix4x4 centerOfMassTransform1;
JPH_RMatrix4x4 centerOfMassTransform2;
JPH_Body_GetCenterOfMassTransform(body1, &centerOfMassTransform1);
JPH_Body_GetCenterOfMassTransform(body2, &centerOfMassTransform2);
JPH_Matrix4x4 constraintToBody1;
JPH_Matrix4x4 constraintToBody2;
JPH_TwoBodyConstraint_GetConstraintToBody1Matrix((JPH_TwoBodyConstraint*) joint->constraint, &constraintToBody1);
JPH_TwoBodyConstraint_GetConstraintToBody2Matrix((JPH_TwoBodyConstraint*) joint->constraint, &constraintToBody2);
mat4_mulVec4(&centerOfMassTransform1.m11, &constraintToBody1.m41);
mat4_mulVec4(&centerOfMassTransform2.m11, &constraintToBody2.m41);
anchor1[0] = constraintToBody1.m41;
anchor1[1] = constraintToBody1.m42;
anchor1[2] = constraintToBody1.m43;
anchor2[0] = constraintToBody2.m41;
anchor2[1] = constraintToBody2.m42;
anchor2[2] = constraintToBody2.m43;
}

static JointNode* lovrJointGetNode(Joint* joint, Collider* collider) {
return collider == lovrJointGetColliderA(joint) ? &joint->a : &joint->b;
}
Expand Down Expand Up @@ -2113,6 +2092,27 @@ Joint* lovrJointGetNext(Joint* joint, Collider* collider) {
return lovrJointGetNode(joint, collider)->next;
}

void lovrJointGetAnchors(Joint* joint, float anchor1[3], float anchor2[3]) {
JPH_Body* body1 = JPH_TwoBodyConstraint_GetBody1((JPH_TwoBodyConstraint*) joint->constraint);
JPH_Body* body2 = JPH_TwoBodyConstraint_GetBody2((JPH_TwoBodyConstraint*) joint->constraint);
JPH_RMatrix4x4 centerOfMassTransform1;
JPH_RMatrix4x4 centerOfMassTransform2;
JPH_Body_GetCenterOfMassTransform(body1, &centerOfMassTransform1);
JPH_Body_GetCenterOfMassTransform(body2, &centerOfMassTransform2);
JPH_Matrix4x4 constraintToBody1;
JPH_Matrix4x4 constraintToBody2;
JPH_TwoBodyConstraint_GetConstraintToBody1Matrix((JPH_TwoBodyConstraint*) joint->constraint, &constraintToBody1);
JPH_TwoBodyConstraint_GetConstraintToBody2Matrix((JPH_TwoBodyConstraint*) joint->constraint, &constraintToBody2);
mat4_mulVec4(&centerOfMassTransform1.m11, &constraintToBody1.m41);
mat4_mulVec4(&centerOfMassTransform2.m11, &constraintToBody2.m41);
anchor1[0] = constraintToBody1.m41;
anchor1[1] = constraintToBody1.m42;
anchor1[2] = constraintToBody1.m43;
anchor2[0] = constraintToBody2.m41;
anchor2[1] = constraintToBody2.m42;
anchor2[2] = constraintToBody2.m43;
}

uint32_t lovrJointGetPriority(Joint* joint) {
return JPH_Constraint_GetConstraintPriority(joint->constraint);
}
Expand Down Expand Up @@ -2196,10 +2196,6 @@ WeldJoint* lovrWeldJointCreate(Collider* a, Collider* b, float anchor[3]) {
return joint;
}

void lovrWeldJointGetAnchors(WeldJoint* joint, float anchor1[3], float anchor2[3]) {
lovrJointGetAnchors((Joint*) joint, anchor1, anchor2);
}

// BallJoint

BallJoint* lovrBallJointCreate(Collider* a, Collider* b, float anchor[3]) {
Expand All @@ -2224,10 +2220,6 @@ BallJoint* lovrBallJointCreate(Collider* a, Collider* b, float anchor[3]) {
return joint;
}

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]) {
Expand All @@ -2253,10 +2245,6 @@ ConeJoint* lovrConeJointCreate(Collider* a, Collider* b, float anchor[3], float
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);
Expand Down Expand Up @@ -2309,10 +2297,6 @@ DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float anchor1[3
return joint;
}

void lovrDistanceJointGetAnchors(DistanceJoint* joint, float anchor1[3], float anchor2[3]) {
lovrJointGetAnchors((Joint*) joint, anchor1, anchor2);
}

void lovrDistanceJointGetLimits(DistanceJoint* joint, float* min, float* max) {
*min = JPH_DistanceConstraint_GetMinDistance((JPH_DistanceConstraint*) joint->constraint);
*max = JPH_DistanceConstraint_GetMaxDistance((JPH_DistanceConstraint*) joint->constraint);
Expand Down Expand Up @@ -2364,10 +2348,6 @@ HingeJoint* lovrHingeJointCreate(Collider* a, Collider* b, float anchor[3], floa
return joint;
}

void lovrHingeJointGetAnchors(HingeJoint* joint, float anchor1[3], float anchor2[3]) {
lovrJointGetAnchors(joint, anchor1, anchor2);
}

void lovrHingeJointGetAxis(HingeJoint* joint, float axis[3]) {
JPH_Vec3 resultAxis;
JPH_HingeConstraintSettings* settings = (JPH_HingeConstraintSettings*) JPH_Constraint_GetConstraintSettings((JPH_Constraint*) joint->constraint);
Expand Down Expand Up @@ -2509,10 +2489,6 @@ SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float axis[3]) {
return joint;
}

void lovrSliderJointGetAnchors(SliderJoint* joint, float anchor1[3], float anchor2[3]) {
lovrJointGetAnchors(joint, anchor1, anchor2);
}

void lovrSliderJointGetAxis(SliderJoint* joint, float axis[3]) {
JPH_Vec3 resultAxis;
JPH_SliderConstraintSettings* settings = (JPH_SliderConstraintSettings*) JPH_Constraint_GetConstraintSettings((JPH_Constraint*) joint->constraint);
Expand Down
7 changes: 1 addition & 6 deletions src/modules/physics/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ JointType lovrJointGetType(Joint* joint);
Collider* lovrJointGetColliderA(Joint* joint);
Collider* lovrJointGetColliderB(Joint* joint);
Joint* lovrJointGetNext(Joint* joint, Collider* collider);
void lovrJointGetAnchors(Joint* joint, float anchor1[3], float anchor2[3]);
uint32_t lovrJointGetPriority(Joint* joint);
void lovrJointSetPriority(Joint* joint, uint32_t priority);
bool lovrJointIsEnabled(Joint* joint);
Expand All @@ -300,26 +301,21 @@ float lovrJointGetForce(Joint* joint);
float lovrJointGetTorque(Joint* joint);

WeldJoint* lovrWeldJointCreate(Collider* a, Collider* b, float anchor[3]);
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);
void lovrDistanceJointSetLimits(DistanceJoint* joint, float min, float max);
void lovrDistanceJointGetSpring(DistanceJoint* joint, float* frequency, float* damping);
void lovrDistanceJointSetSpring(DistanceJoint* joint, float frequency, float damping);

HingeJoint* lovrHingeJointCreate(Collider* a, Collider* b, float anchor[3], float axis[3]);
void lovrHingeJointGetAnchors(HingeJoint* joint, float anchor1[3], float anchor2[3]);
void lovrHingeJointGetAxis(HingeJoint* joint, float axis[3]);
float lovrHingeJointGetAngle(HingeJoint* joint);
void lovrHingeJointGetLimits(HingeJoint* joint, float* min, float* max);
Expand All @@ -337,7 +333,6 @@ void lovrHingeJointGetSpring(HingeJoint* joint, float* frequency, float* damping
void lovrHingeJointSetSpring(HingeJoint* joint, float frequency, float damping);

SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float axis[3]);
void lovrSliderJointGetAnchors(SliderJoint* joint, float anchor1[3], float anchor2[3]);
void lovrSliderJointGetAxis(SliderJoint* joint, float axis[3]);
float lovrSliderJointGetPosition(SliderJoint* joint);
void lovrSliderJointGetLimits(SliderJoint* joint, float* min, float* max);
Expand Down

0 comments on commit 1a5113c

Please sign in to comment.