Skip to content

Commit

Permalink
Shape:setDensity no longer needs Body:resetMassData unless the Body a…
Browse files Browse the repository at this point in the history
…lready 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.
  • Loading branch information
slime73 committed Oct 8, 2023
1 parent 1396e26 commit 112a09c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/modules/physics/box2d/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/modules/physics/box2d/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class Body : public love::physics::Body
**/
int getMassData(lua_State *L);

bool hasCustomMassData() const { return hasCustomMass; }

/**
* Gets the Body's angular damping.
**/
Expand Down Expand Up @@ -433,6 +435,8 @@ class Body : public love::physics::Body
// unowned?
World *world;

bool hasCustomMass;

// Reference to arbitrary data.
Reference* ref = nullptr;

Expand Down
11 changes: 10 additions & 1 deletion src/modules/physics/box2d/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -188,6 +195,8 @@ void Shape::setDensity(float density)
{
throwIfFixtureNotValid();
fixture->SetDensity(density);
if (!body->hasCustomMassData())
body->resetMassData();
}

void Shape::setSensor(bool sensor)
Expand Down
8 changes: 8 additions & 0 deletions src/modules/physics/box2d/wrap_Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 },
Expand Down
6 changes: 1 addition & 5 deletions src/modules/physics/box2d/wrap_Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/scripts/nogame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3042,7 +3042,6 @@ function love.nogame()
link.body:setAngularDamping(0.5)
link.shape = love.physics.newCircleShape(link.body, link.radius)
link.shape:setDensity(0.1 / i)
link.body:resetMassData()
link.state = State(link.body)

-- Note: every link must also be attached to the Duckloon. Otherwise the
Expand Down
2 changes: 0 additions & 2 deletions src/scripts/nogame.lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -11620,8 +11620,6 @@ const unsigned char nogame_lua[] =
0x20, 0x6c, 0x69, 0x6e, 0x6b, 0x2e, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x6c, 0x69, 0x6e, 0x6b, 0x2e, 0x73, 0x68, 0x61, 0x70, 0x65, 0x3a, 0x73, 0x65, 0x74, 0x44,
0x65, 0x6e, 0x73, 0x69, 0x74, 0x79, 0x28, 0x30, 0x2e, 0x31, 0x20, 0x2f, 0x20, 0x69, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x6c, 0x69, 0x6e, 0x6b, 0x2e, 0x62, 0x6f, 0x64, 0x79, 0x3a, 0x72, 0x65, 0x73, 0x65, 0x74,
0x4d, 0x61, 0x73, 0x73, 0x44, 0x61, 0x74, 0x61, 0x28, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x6c, 0x69, 0x6e, 0x6b, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x65, 0x20, 0x3d, 0x20, 0x53, 0x74,
0x61, 0x74, 0x65, 0x28, 0x6c, 0x69, 0x6e, 0x6b, 0x2e, 0x62, 0x6f, 0x64, 0x79, 0x29, 0x0a,
0x0a,
Expand Down

0 comments on commit 112a09c

Please sign in to comment.