From 112a09ceb78bdd9eacd69b196a75c22275f7fc26 Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Sun, 8 Oct 2023 12:43:27 -0300 Subject: [PATCH] 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. --- src/modules/physics/box2d/Body.cpp | 5 +++++ src/modules/physics/box2d/Body.h | 4 ++++ src/modules/physics/box2d/Shape.cpp | 11 ++++++++++- src/modules/physics/box2d/wrap_Body.cpp | 8 ++++++++ src/modules/physics/box2d/wrap_Physics.cpp | 6 +----- src/scripts/nogame.lua | 1 - src/scripts/nogame.lua.h | 2 -- 7 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index a3e7ee110..a939de73b 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -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) diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index 1d0b2d770..1c5ae8a37 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -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; diff --git a/src/modules/physics/box2d/Shape.cpp b/src/modules/physics/box2d/Shape.cpp index c47c4c7ff..b7c0ddece 100644 --- a/src/modules/physics/box2d/Shape.cpp +++ b/src/modules/physics/box2d/Shape.cpp @@ -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) diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index f0d4dd865..44bda7c0e 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -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 }, diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index d54a65f54..34dfb81ff 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -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(); diff --git a/src/scripts/nogame.lua b/src/scripts/nogame.lua index cc4961499..39e473140 100644 --- a/src/scripts/nogame.lua +++ b/src/scripts/nogame.lua @@ -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 diff --git a/src/scripts/nogame.lua.h b/src/scripts/nogame.lua.h index b618cd035..b467c5eff 100644 --- a/src/scripts/nogame.lua.h +++ b/src/scripts/nogame.lua.h @@ -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,