Skip to content

Commit

Permalink
Adds massDataDirty to BodyConf & updates related code
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-langholtz committed Dec 19, 2023
1 parent ec35f23 commit 04ede87
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
24 changes: 21 additions & 3 deletions Library/include/playrho/d2/BodyConf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -271,13 +280,15 @@ constexpr BodyConf& BodyConf::Use(const Sweep& v) noexcept
return *this;
}

constexpr BodyConf& BodyConf::UseInvMass(const NonNegative<InvMass>& v) noexcept
constexpr BodyConf& BodyConf::UseInvMass(
const NonNegative<InvMass>& v) noexcept
{
invMass = v;
return *this;
}

constexpr BodyConf& BodyConf::UseInvRotI(const NonNegative<InvRotInertia>& v) noexcept
constexpr BodyConf& BodyConf::UseInvRotI(
const NonNegative<InvRotInertia>& v) noexcept
{
invRotI = v;
return *this;
Expand Down Expand Up @@ -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<BodyConf>);
static_assert(std::is_copy_constructible_v<BodyConf>);
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions Library/source/playrho/d2/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions Library/source/playrho/d2/BodyConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions UnitTests/BodyConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{};
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 04ede87

Please sign in to comment.