Shape:setDensity no longer needs Body:resetMassData unless the Body already has custom mass data.

Add Body:hasCustomMassData.
If the Body has custom mass data, attaching a Shape to it doesn't automatically reset its mass data.
This commit is contained in:
Sasha Szpakowski
2023-10-08 12:43:27 -03:00
parent 1396e26626
commit 112a09ceb7
7 changed files with 28 additions and 9 deletions
+5
View File
@@ -39,6 +39,7 @@ namespace box2d
Body::Body(World *world, b2Vec2 p, Body::Type type)
: world(world)
, hasCustomMass(false)
{
b2BodyDef def;
def.position = Physics::scaleDown(p);
@@ -249,6 +250,7 @@ void Body::setLinearDamping(float d)
void Body::resetMassData()
{
body->ResetMassData();
hasCustomMass = false;
}
void Body::setMassData(float x, float y, float m, float i)
@@ -258,6 +260,7 @@ void Body::setMassData(float x, float y, float m, float i)
massData.mass = m;
massData.I = Physics::scaleDown(Physics::scaleDown(i));
body->SetMassData(&massData);
hasCustomMass = true;
}
void Body::setMass(float m)
@@ -266,6 +269,7 @@ void Body::setMass(float m)
body->GetMassData(&data);
data.mass = m;
body->SetMassData(&data);
hasCustomMass = true;
}
void Body::setInertia(float i)
@@ -275,6 +279,7 @@ void Body::setInertia(float i)
massData.mass = body->GetMass();
massData.I = Physics::scaleDown(Physics::scaleDown(i));
body->SetMassData(&massData);
hasCustomMass = true;
}
void Body::setGravityScale(float scale)
+4
View File
@@ -137,6 +137,8 @@ public:
**/
int getMassData(lua_State *L);
bool hasCustomMassData() const { return hasCustomMass; }
/**
* Gets the Body's angular damping.
**/
@@ -433,6 +435,8 @@ private:
// unowned?
World *world;
bool hasCustomMass;
// Reference to arbitrary data.
Reference* ref = nullptr;
+10 -1
View File
@@ -47,9 +47,16 @@ Shape::Shape(Body *body, const b2Shape &shape)
b2FixtureDef def;
def.shape = &shape;
def.userData.pointer = (uintptr_t)this;
def.density = 1.0f;
// 0 density stops CreateFixture from calling b2Body::ResetMassData().
def.density = body->hasCustomMassData() ? 0.0f : 1.0f;
fixture = body->body->CreateFixture(&def);
this->shape = fixture->GetShape();
if (body->hasCustomMassData())
setDensity(1.0f);
retain(); // Shape::destroy does the release().
}
else
@@ -188,6 +195,8 @@ void Shape::setDensity(float density)
{
throwIfFixtureNotValid();
fixture->SetDensity(density);
if (!body->hasCustomMassData())
body->resetMassData();
}
void Shape::setSensor(bool sensor)
+8
View File
@@ -162,6 +162,13 @@ int w_Body_getMassData(lua_State *L)
return t->getMassData(L);
}
int w_Body_hasCustomMassData(lua_State *L)
{
Body *t = luax_checkbody(L, 1);
luax_pushboolean(L, t->hasCustomMassData());
return 1;
}
int w_Body_getAngularDamping(lua_State *L)
{
Body *t = luax_checkbody(L, 1);
@@ -688,6 +695,7 @@ static const luaL_Reg w_Body_functions[] =
{ "getMass", w_Body_getMass },
{ "getInertia", w_Body_getInertia },
{ "getMassData", w_Body_getMassData },
{ "hasCustomMassData", w_Body_hasCustomMassData },
{ "getAngularDamping", w_Body_getAngularDamping },
{ "getLinearDamping", w_Body_getLinearDamping },
{ "getGravityScale", w_Body_getGravityScale },
+1 -5
View File
@@ -262,11 +262,7 @@ int w_newFixture(lua_State *L)
float density = (float)luaL_optnumber(L, 3, 1.0f);
Shape *newShape;
luax_catchexcept(L, [&]() {
newShape = instance()->newAttachedShape(body, shape, density);
newShape->setDensity(density);
body->resetMassData();
});
luax_catchexcept(L, [&]() { newShape = instance()->newAttachedShape(body, shape, density); });
luax_pushshape(L, newShape);
newShape->release();