From 04ede87b578763443a7620e4658848fe63524383 Mon Sep 17 00:00:00 2001 From: Louis Langholtz Date: Tue, 19 Dec 2023 14:50:37 -0700 Subject: [PATCH] Adds massDataDirty to BodyConf & updates related code --- Library/include/playrho/d2/BodyConf.hpp | 24 +++++++++++++++++++++--- Library/source/playrho/d2/Body.cpp | 6 ++++++ Library/source/playrho/d2/BodyConf.cpp | 1 + UnitTests/BodyConf.cpp | 8 ++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Library/include/playrho/d2/BodyConf.hpp b/Library/include/playrho/d2/BodyConf.hpp index b4c73f7d7..7286e1e06 100644 --- a/Library/include/playrho/d2/BodyConf.hpp +++ b/Library/include/playrho/d2/BodyConf.hpp @@ -103,6 +103,9 @@ struct BodyConf { /// @brief Default enabled value. static constexpr auto DefaultEnabled = true; + /// @brief Default mass data dirty value. + static constexpr auto DefaultMassDataDirty = true; + /// @brief Max associable shapes. static constexpr auto MaxShapes = std::size_t(128); @@ -183,6 +186,9 @@ struct BodyConf { /// @brief Use the given enabled state. constexpr BodyConf& UseEnabled(bool value) noexcept; + /// @brief Use the given mass data dirty state. + constexpr BodyConf& UseMassDataDirty(bool v) noexcept; + // Public member variables... /// @brief Type of the body: static, kinematic, or dynamic. @@ -252,6 +258,9 @@ struct BodyConf { /// Whether or not the body is enabled. bool enabled = DefaultEnabled; + + /// @brief Whether mass data is "dirty". + bool massDataDirty = DefaultMassDataDirty; }; constexpr BodyConf& BodyConf::UseType(BodyType t) noexcept @@ -271,13 +280,15 @@ constexpr BodyConf& BodyConf::Use(const Sweep& v) noexcept return *this; } -constexpr BodyConf& BodyConf::UseInvMass(const NonNegative& v) noexcept +constexpr BodyConf& BodyConf::UseInvMass( + const NonNegative& v) noexcept { invMass = v; return *this; } -constexpr BodyConf& BodyConf::UseInvRotI(const NonNegative& v) noexcept +constexpr BodyConf& BodyConf::UseInvRotI( + const NonNegative& v) noexcept { invRotI = v; return *this; @@ -392,6 +403,12 @@ constexpr BodyConf& BodyConf::UseEnabled(bool value) noexcept return *this; } +constexpr BodyConf& BodyConf::UseMassDataDirty(bool v) noexcept +{ + massDataDirty = v; + return *this; +} + // Asserts some basic traits... static_assert(std::is_default_constructible_v); static_assert(std::is_copy_constructible_v); @@ -448,7 +465,8 @@ constexpr bool operator==(const BodyConf& lhs, const BodyConf& rhs) noexcept lhs.awake == rhs.awake && // lhs.fixedRotation == rhs.fixedRotation && // lhs.bullet == rhs.bullet && // - lhs.enabled == rhs.enabled; + lhs.enabled == rhs.enabled && // + lhs.massDataDirty == rhs.massDataDirty; } /// @brief Operator not-equals. diff --git a/Library/source/playrho/d2/Body.cpp b/Library/source/playrho/d2/Body.cpp index eee6c6795..037101ab5 100644 --- a/Library/source/playrho/d2/Body.cpp +++ b/Library/source/playrho/d2/Body.cpp @@ -100,6 +100,12 @@ Body::FlagsType Body::GetFlags(const BodyConf& bd) noexcept if (bd.enabled) { flags |= e_enabledFlag; } + if (bd.massDataDirty && + (!bd.shapes.empty() || + (bd.invMass != BodyConf::DefaultInvMass) || + (bd.invRotI != BodyConf::DefaultInvRotI))) { + flags |= e_massDataDirtyFlag; + } return flags; } diff --git a/Library/source/playrho/d2/BodyConf.cpp b/Library/source/playrho/d2/BodyConf.cpp index ac346eea2..7f8d864d9 100644 --- a/Library/source/playrho/d2/BodyConf.cpp +++ b/Library/source/playrho/d2/BodyConf.cpp @@ -46,6 +46,7 @@ BodyConf GetBodyConf(const Body& body) def.fixedRotation = IsFixedRotation(body); def.bullet = IsAccelerable(body) && IsImpenetrable(body); def.enabled = IsEnabled(body); + def.massDataDirty = IsMassDataDirty(body); return def; } diff --git a/UnitTests/BodyConf.cpp b/UnitTests/BodyConf.cpp index ad9a9cb0c..ab027b01a 100644 --- a/UnitTests/BodyConf.cpp +++ b/UnitTests/BodyConf.cpp @@ -69,6 +69,7 @@ TEST(BodyConf, DefaultConstruction) EXPECT_EQ(BodyConf().fixedRotation, BodyConf::DefaultFixedRotation); EXPECT_EQ(BodyConf().bullet, BodyConf::DefaultBullet); EXPECT_EQ(BodyConf().enabled, BodyConf::DefaultEnabled); + EXPECT_EQ(BodyConf().massDataDirty, BodyConf::DefaultMassDataDirty); } TEST(BodyConf, UseType) @@ -113,6 +114,12 @@ TEST(BodyConf, UseShapes) EXPECT_THROW(BodyConf{}.Use(toomany), LengthError); } +TEST(BodyConf, UseMassDataDirty) +{ + EXPECT_EQ(BodyConf{}.UseMassDataDirty(true).massDataDirty, true); + EXPECT_EQ(BodyConf{}.UseMassDataDirty(false).massDataDirty, false); +} + TEST(BodyConf, GetBodyConf1) { auto conf = BodyConf{}; @@ -144,6 +151,7 @@ TEST(BodyConf, GetBodyConf2) conf.fixedRotation = true; conf.bullet = true; conf.enabled = false; + conf.massDataDirty = false; conf.invMass = InvMass{Real(1) / 2_kg}; conf.invRotI = InvRotInertia{Real(4) * SquareRadian / (SquareMeter * 1_kg)}; SCOPED_TRACE("checking for dynamic");