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
+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)