Added backwards compatible frequency/dampingRatio functions. Added new stiffness/damping functions.

This commit is contained in:
Garrett Brown
2020-08-23 02:38:52 -04:00
parent 2d6efd8b8d
commit a16640c89c
14 changed files with 473 additions and 56 deletions
+30 -2
View File
@@ -56,9 +56,37 @@ float DistanceJoint::getLength() const
return Physics::scaleUp(joint->GetLength());
}
void DistanceJoint::setStiffness(float hz)
void DistanceJoint::setFrequency(float hz)
{
joint->SetStiffness(hz);
float stiffness, damping;
b2LinearStiffness(stiffness, damping, hz, getDampingRatio(), joint->GetBodyA(), joint->GetBodyB());
joint->SetStiffness(stiffness);
}
float DistanceJoint::getFrequency() const
{
float frequency, ratio;
Physics::b2LinearFrequency(frequency, ratio, joint->GetStiffness(), joint->GetDamping(), joint->GetBodyA(), joint->GetBodyB());
return frequency;
}
void DistanceJoint::setDampingRatio(float ratio)
{
float stiffness, damping;
b2LinearStiffness(stiffness, damping, getFrequency(), ratio, joint->GetBodyA(), joint->GetBodyB());
joint->SetDamping(damping);
}
float DistanceJoint::getDampingRatio() const
{
float frequency, ratio;
Physics::b2LinearFrequency(frequency, ratio, joint->GetStiffness(), joint->GetDamping(), joint->GetBodyA(), joint->GetBodyB());
return ratio;
}
void DistanceJoint::setStiffness(float k)
{
joint->SetStiffness(k);
}
float DistanceJoint::getStiffness() const
+26 -8
View File
@@ -57,24 +57,42 @@ public:
float getLength() const;
/**
* Sets the response speed.
* Sets the response speed. Independent of mass
**/
void setStiffness(float hz);
void setFrequency(float hz);
/**
* Gets the response speed.
* Gets the response speed. Independent of mass
**/
float getFrequency() const;
/**
* Set the spring damping ratio. Independent of mass
**/
void setDampingRatio(float ratio);
/**
* Get the spring damping ratio. Independent of mass
**/
float getDampingRatio() const;
/**
* Sets the response speed. Dependent of mass
**/
void setStiffness(float k);
/**
* Gets the response speed. Dependent of mass
**/
float getStiffness() const;
/**
* Sets the damping ratio.
* 0 = no damping, 1 = critical damping.
* Set the spring damping. Dependent of mass
**/
void setDamping(float d);
void setDamping(float ratio);
/**
* Gets the damping ratio.
* 0 = no damping, 1 = critical damping.
* Get the spring damping. Dependent of mass
**/
float getDamping() const;
+36 -2
View File
@@ -78,7 +78,7 @@ float MouseJoint::getMaxForce() const
return Physics::scaleUp(joint->GetMaxForce());
}
void MouseJoint::setStiffness(float hz)
void MouseJoint::setFrequency(float hz)
{
// This is kind of a crappy check. The Stiffness is used in an internal
// box2d calculation whose result must be > FLT_EPSILON, but other variables
@@ -86,7 +86,41 @@ void MouseJoint::setStiffness(float hz)
if (hz <= FLT_EPSILON * 2)
throw love::Exception("MouseJoint Stiffness must be a positive number.");
joint->SetStiffness(hz);
float stiffness, damping;
b2LinearStiffness(stiffness, damping, hz, getDampingRatio(), joint->GetBodyA(), joint->GetBodyB());
joint->SetStiffness(stiffness);
}
float MouseJoint::getFrequency() const
{
float frequency, ratio;
Physics::b2LinearFrequency(frequency, ratio, joint->GetStiffness(), joint->GetDamping(), joint->GetBodyA(), joint->GetBodyB());
return frequency;
}
void MouseJoint::setDampingRatio(float ratio)
{
float stiffness, damping;
b2LinearStiffness(stiffness, damping, getFrequency(), ratio, joint->GetBodyA(), joint->GetBodyB());
joint->SetDamping(damping);
}
float MouseJoint::getDampingRatio() const
{
float frequency, ratio;
Physics::b2LinearFrequency(frequency, ratio, joint->GetStiffness(), joint->GetDamping(), joint->GetBodyA(), joint->GetBodyB());
return ratio;
}
void MouseJoint::setStiffness(float k)
{
// This is kind of a crappy check. The Stiffness is used in an internal
// box2d calculation whose result must be > FLT_EPSILON, but other variables
// go into that calculation...
if (k <= FLT_EPSILON * 2)
throw love::Exception("MouseJoint Stiffness must be a positive number.");
joint->SetStiffness(k);
}
float MouseJoint::getStiffness() const
+26 -8
View File
@@ -76,24 +76,42 @@ public:
float getMaxForce() const;
/**
* Sets the response speed.
* Sets the response speed. Independent of mass
**/
void setStiffness(float hz);
void setFrequency(float hz);
/**
* Gets the response speed.
* Gets the response speed. Independent of mass
**/
float getFrequency() const;
/**
* Set the spring damping ratio. Independent of mass
**/
void setDampingRatio(float ratio);
/**
* Get the spring damping ratio. Independent of mass
**/
float getDampingRatio() const;
/**
* Sets the response speed. Dependent of mass
**/
void setStiffness(float k);
/**
* Gets the response speed. Dependent of mass
**/
float getStiffness() const;
/**
* Sets the damping ratio.
* 0 = no damping, 1 = critical damping.
* Set the spring damping. Dependent of mass
**/
void setDamping(float d);
void setDamping(float ratio);
/**
* Gets the damping ratio.
* 0 = no damping, 1 = critical damping.
* Get the spring damping. Dependent of mass
**/
float getDamping() const;
+60
View File
@@ -395,6 +395,66 @@ b2AABB Physics::scaleUp(const b2AABB &aabb)
return t;
}
void Physics::b2LinearFrequency(float& frequency, float& ratio, float stiffness, float damping, b2Body* bodyA, b2Body* bodyB)
{
float massA = bodyA->GetMass();
float massB = bodyB->GetMass();
float mass;
if (massA > 0.0f && massB > 0.0f)
{
mass = massA * massB / (massA + massB);
}
else if (massA > 0.0f)
{
mass = massA;
}
else
{
mass = massB;
}
if (mass == 0.0f || stiffness <= 0.0f)
{
frequency = 0.0f;
ratio = 0.0f;
return;
};
float omega = b2Sqrt(stiffness / mass);
frequency = omega / (2.0f * b2_pi);
ratio = damping / (mass * 2.0f * omega);
}
void Physics::b2AngularFrequency(float& frequency, float& ratio, float stiffness, float damping, b2Body* bodyA, b2Body* bodyB)
{
float IA = bodyA->GetInertia();
float IB = bodyB->GetInertia();
float I;
if (IA > 0.0f && IB > 0.0f)
{
I = IA * IB / (IA + IB);
}
else if (IA > 0.0f)
{
I = IA;
}
else
{
I = IB;
}
if (I == 0.0f || stiffness <= 0.0f)
{
frequency = 0.0f;
ratio = 0.0f;
return;
};
float omega = b2Sqrt(stiffness / I);
frequency = omega / (2.0f * b2_pi);
ratio = damping / (I * 2.0f * omega);
}
} // box2d
} // physics
} // love
+23 -1
View File
@@ -354,7 +354,29 @@ public:
* @param aabb The unscaled input AABB.
* @return The scaled AABB.
**/
static b2AABB scaleUp(const b2AABB &aabb);
static b2AABB scaleUp(const b2AABB& aabb);
/**
* Calculates linear frequency and damping radio from stiffness and damping
* @param frequency The output frequency
* @param ratio The output damping ratio
* @param stiffness The joint stiffness
* @param damping The joint damping
* @param bodyA The bodyA of the joint
* @param bodyB The bodyB of the joint
**/
static void b2LinearFrequency(float& frequency, float& ratio, float stiffness, float damping, b2Body* bodyA, b2Body* bodyB);
/**
* Calculates angular frequency and damping radio from stiffness and damping
* @param frequency The output frequency
* @param ratio The output damping ratio
* @param stiffness The joint stiffness
* @param damping The joint damping
* @param bodyA The bodyA of the joint
* @param bodyB The bodyB of the joint
**/
static void b2AngularFrequency(float& frequency, float& ratio, float stiffness, float damping, b2Body* bodyA, b2Body* bodyB);
private:
+30 -2
View File
@@ -66,9 +66,37 @@ void WeldJoint::init(b2WeldJointDef &def, Body *body1, Body *body2, float xA, fl
def.collideConnected = collideConnected;
}
void WeldJoint::setStiffness(float hz)
void WeldJoint::setFrequency(float hz)
{
joint->SetStiffness(hz);
float stiffness, damping;
b2LinearStiffness(stiffness, damping, hz, getDampingRatio(), joint->GetBodyA(), joint->GetBodyB());
joint->SetStiffness(stiffness);
}
float WeldJoint::getFrequency() const
{
float frequency, ratio;
Physics::b2LinearFrequency(frequency, ratio, joint->GetStiffness(), joint->GetDamping(), joint->GetBodyA(), joint->GetBodyB());
return frequency;
}
void WeldJoint::setDampingRatio(float ratio)
{
float stiffness, damping;
b2LinearStiffness(stiffness, damping, getFrequency(), ratio, joint->GetBodyA(), joint->GetBodyB());
joint->SetDamping(damping);
}
float WeldJoint::getDampingRatio() const
{
float frequency, ratio;
Physics::b2LinearFrequency(frequency, ratio, joint->GetStiffness(), joint->GetDamping(), joint->GetBodyA(), joint->GetBodyB());
return ratio;
}
void WeldJoint::setStiffness(float k)
{
joint->SetStiffness(k);
}
float WeldJoint::getStiffness() const
+26 -8
View File
@@ -50,24 +50,42 @@ public:
virtual ~WeldJoint();
/**
* Sets the response speed.
* Sets the response speed. Independent of mass
**/
void setStiffness(float hz);
void setFrequency(float hz);
/**
* Gets the response speed.
* Gets the response speed. Independent of mass
**/
float getFrequency() const;
/**
* Set the spring damping ratio. Independent of mass
**/
void setDampingRatio(float ratio);
/**
* Get the spring damping ratio. Independent of mass
**/
float getDampingRatio() const;
/**
* Sets the response speed. Dependent of mass
**/
void setStiffness(float k);
/**
* Gets the response speed. Dependent of mass
**/
float getStiffness() const;
/**
* Sets the damping ratio.
* 0 = no damping, 1 = critical damping.
* Set the spring damping. Dependent of mass
**/
void setDamping(float d);
void setDamping(float ratio);
/**
* Gets the damping ratio.
* 0 = no damping, 1 = critical damping.
* Get the spring damping. Dependent of mass
**/
float getDamping() const;
+30 -2
View File
@@ -95,9 +95,37 @@ float WheelJoint::getMotorTorque(float inv_dt) const
return Physics::scaleUp(Physics::scaleUp(joint->GetMotorTorque(inv_dt)));
}
void WheelJoint::setStiffness(float hz)
void WheelJoint::setFrequency(float hz)
{
joint->SetStiffness(hz);
float stiffness, damping;
b2AngularStiffness(stiffness, damping, hz, getDampingRatio(), joint->GetBodyA(), joint->GetBodyB());
joint->SetStiffness(stiffness);
}
float WheelJoint::getFrequency() const
{
float frequency, ratio;
Physics::b2AngularFrequency(frequency, ratio, joint->GetStiffness(), joint->GetDamping(), joint->GetBodyA(), joint->GetBodyB());
return frequency;
}
void WheelJoint::setDampingRatio(float ratio)
{
float stiffness, damping;
b2AngularStiffness(stiffness, damping, getFrequency(), ratio, joint->GetBodyA(), joint->GetBodyB());
joint->SetDamping(damping);
}
float WheelJoint::getDampingRatio() const
{
float frequency, ratio;
Physics::b2AngularFrequency(frequency, ratio, joint->GetStiffness(), joint->GetDamping(), joint->GetBodyA(), joint->GetBodyB());
return ratio;
}
void WheelJoint::setStiffness(float k)
{
joint->SetStiffness(k);
}
float WheelJoint::getStiffness() const
+25 -6
View File
@@ -96,23 +96,42 @@ public:
float getMotorTorque(float inv_dt) const;
/**
* Set the spring frequency, in hertz. Setting the frequency to 0
* disables the spring.
* Sets the response speed. Independent of mass
**/
void setStiffness(float hz);
void setFrequency(float hz);
/**
* Get the spring frequency, in hertz.
* Gets the response speed. Independent of mass
**/
float getFrequency() const;
/**
* Set the spring damping ratio. Independent of mass
**/
void setDampingRatio(float ratio);
/**
* Get the spring damping ratio. Independent of mass
**/
float getDampingRatio() const;
/**
* Sets the response speed. Dependent of mass
**/
void setStiffness(float k);
/**
* Gets the response speed. Dependent of mass
**/
float getStiffness() const;
/**
* Set the spring damping ratio.
* Set the spring damping. Dependent of mass
**/
void setDamping(float ratio);
/**
* Get the spring damping ratio.
* Get the spring damping. Dependent of mass
**/
float getDamping() const;
@@ -54,14 +54,14 @@ int w_DistanceJoint_setFrequency(lua_State *L)
{
DistanceJoint *t = luax_checkdistancejoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setStiffness(arg1);
t->setFrequency(arg1);
return 0;
}
int w_DistanceJoint_getFrequency(lua_State *L)
{
DistanceJoint *t = luax_checkdistancejoint(L, 1);
lua_pushnumber(L, t->getStiffness());
lua_pushnumber(L, t->getFrequency());
return 1;
}
@@ -69,11 +69,41 @@ int w_DistanceJoint_setDampingRatio(lua_State *L)
{
DistanceJoint *t = luax_checkdistancejoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDamping(arg1);
t->setDampingRatio(arg1);
return 0;
}
int w_DistanceJoint_getDampingRatio(lua_State *L)
{
DistanceJoint *t = luax_checkdistancejoint(L, 1);
lua_pushnumber(L, t->getDampingRatio());
return 1;
}
int w_DistanceJoint_setStiffness(lua_State *L)
{
DistanceJoint *t = luax_checkdistancejoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setStiffness(arg1);
return 0;
}
int w_DistanceJoint_getStiffness(lua_State *L)
{
DistanceJoint *t = luax_checkdistancejoint(L, 1);
lua_pushnumber(L, t->getStiffness());
return 1;
}
int w_DistanceJoint_setDamping(lua_State *L)
{
DistanceJoint *t = luax_checkdistancejoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDamping(arg1);
return 0;
}
int w_DistanceJoint_getDamping(lua_State *L)
{
DistanceJoint *t = luax_checkdistancejoint(L, 1);
lua_pushnumber(L, t->getDamping());
@@ -88,6 +118,10 @@ static const luaL_Reg w_DistanceJoint_functions[] =
{ "getFrequency", w_DistanceJoint_getFrequency },
{ "setDampingRatio", w_DistanceJoint_setDampingRatio },
{ "getDampingRatio", w_DistanceJoint_getDampingRatio },
{ "setStiffness", w_DistanceJoint_setStiffness },
{ "getStiffness", w_DistanceJoint_getStiffness },
{ "setDamping", w_DistanceJoint_setDamping },
{ "getDamping", w_DistanceJoint_getDamping },
{ 0, 0 }
};
+37 -3
View File
@@ -70,14 +70,14 @@ int w_MouseJoint_setFrequency(lua_State *L)
{
MouseJoint *t = luax_checkmousejoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
luax_catchexcept(L, [&]() { t->setStiffness(arg1); });
luax_catchexcept(L, [&]() { t->setFrequency(arg1); });
return 0;
}
int w_MouseJoint_getFrequency(lua_State *L)
{
MouseJoint *t = luax_checkmousejoint(L, 1);
lua_pushnumber(L, t->getStiffness());
lua_pushnumber(L, t->getFrequency());
return 1;
}
@@ -85,11 +85,41 @@ int w_MouseJoint_setDampingRatio(lua_State *L)
{
MouseJoint *t = luax_checkmousejoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDamping(arg1);
t->setDampingRatio(arg1);
return 0;
}
int w_MouseJoint_getDampingRatio(lua_State *L)
{
MouseJoint *t = luax_checkmousejoint(L, 1);
lua_pushnumber(L, t->getDampingRatio());
return 1;
}
int w_MouseJoint_setStiffness(lua_State *L)
{
MouseJoint *t = luax_checkmousejoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
luax_catchexcept(L, [&]() { t->setStiffness(arg1); });
return 0;
}
int w_MouseJoint_getStiffness(lua_State *L)
{
MouseJoint *t = luax_checkmousejoint(L, 1);
lua_pushnumber(L, t->getStiffness());
return 1;
}
int w_MouseJoint_setDamping(lua_State *L)
{
MouseJoint *t = luax_checkmousejoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDamping(arg1);
return 0;
}
int w_MouseJoint_getDamping(lua_State *L)
{
MouseJoint *t = luax_checkmousejoint(L, 1);
lua_pushnumber(L, t->getDamping());
@@ -106,6 +136,10 @@ static const luaL_Reg w_MouseJoint_functions[] =
{ "getFrequency", w_MouseJoint_getFrequency },
{ "setDampingRatio", w_MouseJoint_setDampingRatio },
{ "getDampingRatio", w_MouseJoint_getDampingRatio },
{ "setStiffness", w_MouseJoint_setStiffness },
{ "getStiffness", w_MouseJoint_getStiffness },
{ "setDamping", w_MouseJoint_setDamping },
{ "getDamping", w_MouseJoint_getDamping },
{ 0, 0 }
};
+37 -3
View File
@@ -39,14 +39,14 @@ int w_WeldJoint_setFrequency(lua_State *L)
{
WeldJoint *t = luax_checkweldjoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setStiffness(arg1);
t->setFrequency(arg1);
return 0;
}
int w_WeldJoint_getFrequency(lua_State *L)
{
WeldJoint *t = luax_checkweldjoint(L, 1);
lua_pushnumber(L, t->getStiffness());
lua_pushnumber(L, t->getFrequency());
return 1;
}
@@ -54,11 +54,41 @@ int w_WeldJoint_setDampingRatio(lua_State *L)
{
WeldJoint *t = luax_checkweldjoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDamping(arg1);
t->setDampingRatio(arg1);
return 0;
}
int w_WeldJoint_getDampingRatio(lua_State *L)
{
WeldJoint *t = luax_checkweldjoint(L, 1);
lua_pushnumber(L, t->getDampingRatio());
return 1;
}
int w_WeldJoint_setStiffness(lua_State *L)
{
WeldJoint *t = luax_checkweldjoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setStiffness(arg1);
return 0;
}
int w_WeldJoint_getStiffness(lua_State *L)
{
WeldJoint *t = luax_checkweldjoint(L, 1);
lua_pushnumber(L, t->getStiffness());
return 1;
}
int w_WeldJoint_setDamping(lua_State *L)
{
WeldJoint *t = luax_checkweldjoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDamping(arg1);
return 0;
}
int w_WeldJoint_getDamping(lua_State *L)
{
WeldJoint *t = luax_checkweldjoint(L, 1);
lua_pushnumber(L, t->getDamping());
@@ -78,6 +108,10 @@ static const luaL_Reg w_WeldJoint_functions[] =
{ "getFrequency", w_WeldJoint_getFrequency },
{ "setDampingRatio", w_WeldJoint_setDampingRatio },
{ "getDampingRatio", w_WeldJoint_getDampingRatio },
{ "setStiffness", w_WeldJoint_setStiffness },
{ "getStiffness", w_WeldJoint_getStiffness },
{ "setDamping", w_WeldJoint_setDamping },
{ "getDamping", w_WeldJoint_getDamping },
{ "getReferenceAngle", w_WeldJoint_getReferenceAngle },
{ 0, 0 }
};
+50 -8
View File
@@ -102,7 +102,37 @@ int w_WheelJoint_getMotorTorque(lua_State *L)
return 1;
}
int w_WheelJoint_setSpringFrequency(lua_State *L)
int w_WheelJoint_setFrequency(lua_State *L)
{
WheelJoint *t = luax_checkwheeljoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setFrequency(arg1);
return 0;
}
int w_WheelJoint_getFrequency(lua_State *L)
{
WheelJoint *t = luax_checkwheeljoint(L, 1);
lua_pushnumber(L, t->getFrequency());
return 1;
}
int w_WheelJoint_setDampingRatio(lua_State *L)
{
WheelJoint *t = luax_checkwheeljoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDampingRatio(arg1);
return 0;
}
int w_WheelJoint_getDampingRatio(lua_State *L)
{
WheelJoint *t = luax_checkwheeljoint(L, 1);
lua_pushnumber(L, t->getDampingRatio());
return 1;
}
int w_WheelJoint_setStiffness(lua_State *L)
{
WheelJoint *t = luax_checkwheeljoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
@@ -110,14 +140,14 @@ int w_WheelJoint_setSpringFrequency(lua_State *L)
return 0;
}
int w_WheelJoint_getSpringFrequency(lua_State *L)
int w_WheelJoint_getStiffness(lua_State *L)
{
WheelJoint *t = luax_checkwheeljoint(L, 1);
lua_pushnumber(L, t->getStiffness());
return 1;
}
int w_WheelJoint_setSpringDampingRatio(lua_State *L)
int w_WheelJoint_setDamping(lua_State *L)
{
WheelJoint *t = luax_checkwheeljoint(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
@@ -125,7 +155,7 @@ int w_WheelJoint_setSpringDampingRatio(lua_State *L)
return 0;
}
int w_WheelJoint_getSpringDampingRatio(lua_State *L)
int w_WheelJoint_getDamping(lua_State *L)
{
WheelJoint *t = luax_checkwheeljoint(L, 1);
lua_pushnumber(L, t->getDamping());
@@ -150,10 +180,22 @@ static const luaL_Reg w_WheelJoint_functions[] =
{ "setMaxMotorTorque", w_WheelJoint_setMaxMotorTorque },
{ "getMaxMotorTorque", w_WheelJoint_getMaxMotorTorque },
{ "getMotorTorque", w_WheelJoint_getMotorTorque },
{ "setSpringFrequency", w_WheelJoint_setSpringFrequency },
{ "getSpringFrequency", w_WheelJoint_getSpringFrequency },
{ "setSpringDampingRatio", w_WheelJoint_setSpringDampingRatio },
{ "getSpringDampingRatio", w_WheelJoint_getSpringDampingRatio },
{ "setSpringFrequency", w_WheelJoint_setFrequency },
{ "getSpringFrequency", w_WheelJoint_getFrequency },
{ "setSpringDampingRatio", w_WheelJoint_setDampingRatio },
{ "getSpringDampingRatio", w_WheelJoint_getDampingRatio },
{ "setSpringStiffness", w_WheelJoint_setStiffness },
{ "getSpringStiffness", w_WheelJoint_getStiffness },
{ "setSpringDamping", w_WheelJoint_setDamping },
{ "getSpringDamping", w_WheelJoint_getDamping },
{ "setFrequency", w_WheelJoint_setFrequency },
{ "getFrequency", w_WheelJoint_getFrequency },
{ "setDampingRatio", w_WheelJoint_setDampingRatio },
{ "getDampingRatio", w_WheelJoint_getDampingRatio },
{ "setStiffness", w_WheelJoint_setStiffness },
{ "getStiffness", w_WheelJoint_getStiffness },
{ "setDamping", w_WheelJoint_setDamping },
{ "getDamping", w_WheelJoint_getDamping },
{ "getAxis", w_WheelJoint_getAxis },
{ 0, 0 }
};