Skip to content

Commit 9940ec5

Browse files
Merge pull request #392 from louis-langholtz/updates-20210402
Updates 20210402
2 parents 01d4aab + 2ed4dad commit 9940ec5

9 files changed

Lines changed: 359 additions & 80 deletions

File tree

PlayRho/Common/Sweep.hpp

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,43 @@ namespace d2 {
4141
class Sweep
4242
{
4343
public:
44-
4544
/// @brief Default constructor.
4645
Sweep() = default;
47-
46+
4847
/// @brief Copy constructor.
4948
constexpr Sweep(const Sweep& copy) = default;
50-
49+
5150
/// @brief Initializing constructor.
52-
constexpr Sweep(const Position p0, const Position p1,
53-
const Length2 lc = Length2{0_m, 0_m},
54-
Real a0 = 0) noexcept:
55-
pos0{p0}, pos1{p1}, localCenter{lc}, alpha0{a0}
51+
constexpr Sweep(const Position p0, const Position p1, const Length2 lc = Length2{0_m, 0_m},
52+
Real a0 = 0) noexcept
53+
: pos0{p0}, pos1{p1}, localCenter{lc}, alpha0{a0}
5654
{
5755
assert(a0 >= 0);
5856
assert(a0 < 1);
5957
}
60-
58+
6159
/// @brief Initializing constructor.
62-
constexpr explicit Sweep(const Position p,
63-
const Length2 lc = Length2{0_m, 0_m}):
64-
Sweep{p, p, lc, 0}
60+
constexpr explicit Sweep(const Position p, const Length2 lc = Length2{0_m, 0_m})
61+
: Sweep{p, p, lc, 0}
6562
{
6663
// Intentionally empty.
6764
}
68-
65+
6966
/// @brief Gets the local center of mass position.
7067
/// @note This value can only be set via a sweep constructed using an initializing
7168
/// constructor.
72-
Length2 GetLocalCenter() const noexcept { return localCenter; }
73-
69+
Length2 GetLocalCenter() const noexcept
70+
{
71+
return localCenter;
72+
}
73+
7474
/// @brief Gets the alpha 0 for this sweep.
7575
/// @return Value between 0 and less than 1.
76-
Real GetAlpha0() const noexcept { return alpha0; }
77-
76+
Real GetAlpha0() const noexcept
77+
{
78+
return alpha0;
79+
}
80+
7881
/// @brief Advances the sweep by a factor of the difference between the given time alpha
7982
/// and the sweep's alpha 0.
8083
/// @details This advances position 0 (<code>pos0</code>) of the sweep towards position
@@ -85,22 +88,22 @@ class Sweep
8588
/// undefined if value is invalid.
8689
///
8790
void Advance0(Real alpha) noexcept;
88-
91+
8992
/// @brief Resets the alpha 0 value back to zero.
9093
/// @post Getting the alpha 0 value after calling this method will return zero.
9194
void ResetAlpha0() noexcept;
92-
95+
9396
/// @brief Center world position and world angle at time "0".
9497
Position pos0;
9598

9699
/// @brief Center world position and world angle at time "1".
97100
Position pos1;
98-
101+
99102
private:
100103
/// @brief Local center of mass position.
101104
/// @note 8-bytes.
102105
Length2 localCenter = Length2{0_m, 0_m};
103-
106+
104107
/// @brief Fraction of the current time step in the range [0,1]
105108
/// @note <code>pos0.linear</code> and <code>pos0.angular</code> are the positions at
106109
/// <code>alpha0</code>.
@@ -114,7 +117,7 @@ inline void Sweep::Advance0(const Real alpha) noexcept
114117
assert(alpha >= 0);
115118
assert(alpha < 1);
116119
assert(alpha0 < 1);
117-
120+
118121
const auto beta = (alpha - alpha0) / (1 - alpha0);
119122
pos0 = GetPosition(pos0, pos1, beta);
120123
alpha0 = alpha;
@@ -127,15 +130,32 @@ inline void Sweep::ResetAlpha0() noexcept
127130

128131
// Free functions...
129132

133+
/// @brief Equals operator.
134+
/// @relatedalso Sweep
135+
constexpr bool operator==(const Sweep& lhs, const Sweep& rhs)
136+
{
137+
return lhs.pos0 == rhs.pos0 && //
138+
lhs.pos1 == rhs.pos1 && //
139+
lhs.GetLocalCenter() == rhs.GetLocalCenter() && //
140+
lhs.GetAlpha0() == rhs.GetAlpha0();
141+
}
142+
143+
/// @brief Not-equals operator.
144+
/// @relatedalso Sweep
145+
constexpr bool operator!=(const Sweep& lhs, const Sweep& rhs)
146+
{
147+
return !(lhs == rhs);
148+
}
149+
130150
} // namespace d2
131151

132152
/// @brief Determines if the given value is valid.
133-
/// @relatedalso d2::Transformation
153+
/// @relatedalso d2::Sweep
134154
template <>
135155
constexpr bool IsValid(const d2::Sweep& value) noexcept
136156
{
137-
return IsValid(value.pos0) && IsValid(value.pos1)
138-
&& IsValid(value.GetLocalCenter()) && IsValid(value.GetAlpha0());
157+
return IsValid(value.pos0) && IsValid(value.pos1) && IsValid(value.GetLocalCenter()) &&
158+
IsValid(value.GetAlpha0());
139159
}
140160

141161
} // namespace playrho

PlayRho/Dynamics/Body.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,19 @@ void ApplyAngularImpulse(Body& body, AngularMomentum impulse) noexcept
257257
body.SetVelocity(velocity);
258258
}
259259

260+
bool operator==(const Body& lhs, const Body& rhs)
261+
{
262+
return GetTransformation(lhs) == GetTransformation(rhs) && //
263+
GetSweep(lhs) == GetSweep(rhs) && //
264+
GetType(lhs) == GetType(rhs) && //
265+
GetVelocity(lhs) == GetVelocity(rhs) && //
266+
GetAcceleration(lhs) == GetAcceleration(rhs) && //
267+
GetInvMass(lhs) == GetInvMass(rhs) && //
268+
GetInvRotInertia(lhs) == GetInvRotInertia(rhs) && //
269+
GetLinearDamping(lhs) == GetLinearDamping(rhs) && //
270+
GetAngularDamping(lhs) == GetAngularDamping(rhs) && //
271+
GetUnderActiveTime(lhs) == GetUnderActiveTime(rhs);
272+
}
273+
260274
} // namespace d2
261275
} // namespace playrho

PlayRho/Dynamics/Body.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,13 @@ inline Mass GetMass(const Body& body) noexcept
10851085
return (invMass != InvMass{0}) ? Mass{Real{1} / invMass} : 0_kg;
10861086
}
10871087

1088+
/// @brief Sets the mass of the given body.
1089+
/// @relatedalso Body
1090+
inline void SetMass(Body& body, Mass mass)
1091+
{
1092+
body.SetInvMass(InvMass{Real(1) / mass});
1093+
}
1094+
10881095
/// @brief Sets the linear and rotational accelerations on this body.
10891096
/// @note This has no effect on non-accelerable bodies.
10901097
/// @note A non-zero acceleration will also awaken the body.
@@ -1305,6 +1312,17 @@ void ApplyLinearImpulse(Body& body, Momentum2 impulse, Length2 point) noexcept;
13051312
/// @relatedalso Body
13061313
void ApplyAngularImpulse(Body& body, AngularMomentum impulse) noexcept;
13071314

1315+
/// @brief Equals operator.
1316+
/// @relatedalso Body
1317+
bool operator==(const Body& lhs, const Body& rhs);
1318+
1319+
/// @brief Not-equals operator.
1320+
/// @relatedalso Body
1321+
inline bool operator!=(const Body& lhs, const Body& rhs)
1322+
{
1323+
return !(lhs == rhs);
1324+
}
1325+
13081326
} // namespace d2
13091327
} // namespace playrho
13101328

PlayRho/Dynamics/BodyConf.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ constexpr Angle GetAngle(const BodyConf& conf) noexcept
291291
}
292292

293293
/// @brief Operator equals.
294+
/// @relatedalso BodyConf
294295
constexpr bool operator==(const BodyConf& lhs, const BodyConf& rhs) noexcept
295296
{
296297
return lhs.type == rhs.type && //
@@ -311,6 +312,7 @@ constexpr bool operator==(const BodyConf& lhs, const BodyConf& rhs) noexcept
311312
}
312313

313314
/// @brief Operator not-equals.
315+
/// @relatedalso BodyConf
314316
constexpr bool operator!=(const BodyConf& lhs, const BodyConf& rhs) noexcept
315317
{
316318
return !(lhs == rhs);

PlayRho/Dynamics/FixtureConf.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ struct FixtureConf {
9797
bool isSensor = false;
9898
};
9999

100+
/// @brief Operator equals.
101+
/// @relatedalso FixtureConf
102+
inline bool operator==(const FixtureConf& lhs, const FixtureConf& rhs)
103+
{
104+
return lhs.shape == rhs.shape && //
105+
lhs.filter == rhs.filter && //
106+
lhs.body == rhs.body && //
107+
lhs.isSensor == rhs.isSensor;
108+
}
109+
110+
/// @brief Operator not-equals.
111+
/// @relatedalso FixtureConf
112+
inline bool operator!=(const FixtureConf& lhs, const FixtureConf& rhs)
113+
{
114+
return !(lhs == rhs);
115+
}
116+
100117
/// @brief Gets the body of the given configuration.
101118
/// @relatedalso FixtureConf
102119
inline BodyID GetBody(const FixtureConf& conf) noexcept

UnitTests/Acceleration.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,22 @@ TEST(Acceleration, Subtraction)
5353
(Acceleration{LinearAcceleration2{0_mps2, 0_mps2}, 0 * RadianPerSquareSecond}));
5454
}
5555

56+
TEST(Acceleration, EqualsOperator)
57+
{
58+
const auto zero = Acceleration{};
59+
EXPECT_TRUE(zero == zero);
60+
EXPECT_TRUE((Acceleration{LinearAcceleration2{}, AngularAcceleration{}} == zero));
61+
EXPECT_FALSE((Acceleration{LinearAcceleration2{2_mps2, 0_mps2}, AngularAcceleration{}} == zero));
62+
EXPECT_FALSE((Acceleration{LinearAcceleration2{0_mps2, 2_mps2}, AngularAcceleration{}} == zero));
63+
EXPECT_FALSE((Acceleration{LinearAcceleration2{}, Real(2) * RadianPerSquareSecond} == zero));
64+
}
65+
66+
TEST(Acceleration, NotEqualsOperator)
67+
{
68+
const auto zero = Acceleration{};
69+
EXPECT_FALSE(zero != zero);
70+
EXPECT_FALSE((Acceleration{LinearAcceleration2{}, AngularAcceleration{}} != zero));
71+
EXPECT_TRUE((Acceleration{LinearAcceleration2{2_mps2, 0_mps2}, AngularAcceleration{}} != zero));
72+
EXPECT_TRUE((Acceleration{LinearAcceleration2{0_mps2, 2_mps2}, AngularAcceleration{}} != zero));
73+
EXPECT_TRUE((Acceleration{LinearAcceleration2{}, Real(2) * RadianPerSquareSecond} != zero));
74+
}

UnitTests/Body.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,147 @@ TEST(Body, AccelerationOnConstruction)
139139
.GetAngularAcceleration(),
140140
body.GetAngularAcceleration());
141141
}
142+
143+
TEST(Body, EqualsOperator)
144+
{
145+
EXPECT_TRUE(Body() == Body());
146+
{
147+
auto body = Body{};
148+
EXPECT_TRUE(body == Body());
149+
}
150+
{
151+
auto body1 = Body{};
152+
body1.SetType(BodyType::Dynamic);
153+
auto body2 = Body{};
154+
body2.SetType(BodyType::Dynamic);
155+
EXPECT_TRUE(body1 == body2);
156+
}
157+
{
158+
auto body = Body{};
159+
body.SetTransformation(Transformation{Length2{2_m, 0_m}, UnitVec{}});
160+
EXPECT_FALSE(body == Body());
161+
}
162+
{
163+
auto body = Body{};
164+
body.SetSweep(Sweep{Position{Length2{}, 2_deg}});
165+
EXPECT_FALSE(body == Body());
166+
}
167+
{
168+
auto body = Body{};
169+
body.SetType(BodyType::Kinematic);
170+
EXPECT_FALSE(body == Body());
171+
}
172+
{
173+
auto body = Body{};
174+
body.JustSetVelocity(Velocity{LinearVelocity2{}, 2_rpm});
175+
EXPECT_FALSE(body == Body());
176+
}
177+
{
178+
auto body1 = Body{};
179+
body1.SetType(BodyType::Dynamic);
180+
body1.SetAcceleration(LinearAcceleration2{}, Real(2) * RadianPerSquareSecond);
181+
auto body2 = Body{};
182+
body2.SetType(BodyType::Dynamic);
183+
EXPECT_FALSE(body1 == body2);
184+
}
185+
{
186+
auto body = Body{};
187+
SetMass(body, 3.2_kg);
188+
EXPECT_FALSE(body == Body());
189+
}
190+
{
191+
auto body = Body{};
192+
body.SetInvRotInertia((Real(2) * SquareRadian) / (2_m2 * 1.2_kg));
193+
EXPECT_FALSE(body == Body());
194+
}
195+
{
196+
auto body = Body{};
197+
SetLinearDamping(body, 2_Hz);
198+
EXPECT_FALSE(body == Body());
199+
}
200+
{
201+
auto body = Body{};
202+
SetAngularDamping(body, 2_Hz);
203+
EXPECT_FALSE(body == Body());
204+
}
205+
{
206+
auto body1 = Body{};
207+
body1.SetType(BodyType::Dynamic);
208+
body1.SetUnderActiveTime(2_s);
209+
auto body2 = Body{};
210+
body2.SetType(BodyType::Dynamic);
211+
EXPECT_FALSE(body1 == body2);
212+
}
213+
}
214+
215+
TEST(Body, NotEqualsOperator)
216+
{
217+
EXPECT_FALSE(Body() != Body());
218+
{
219+
auto body = Body{};
220+
EXPECT_FALSE(body != Body());
221+
}
222+
{
223+
auto body1 = Body{};
224+
body1.SetType(BodyType::Dynamic);
225+
auto body2 = Body{};
226+
body2.SetType(BodyType::Dynamic);
227+
EXPECT_FALSE(body1 != body2);
228+
}
229+
{
230+
auto body = Body{};
231+
body.SetTransformation(Transformation{Length2{2_m, 0_m}, UnitVec{}});
232+
EXPECT_TRUE(body != Body());
233+
}
234+
{
235+
auto body = Body{};
236+
body.SetSweep(Sweep{Position{Length2{}, 2_deg}});
237+
EXPECT_TRUE(body != Body());
238+
}
239+
{
240+
auto body = Body{};
241+
body.SetType(BodyType::Kinematic);
242+
EXPECT_TRUE(body != Body());
243+
}
244+
{
245+
auto body = Body{};
246+
body.JustSetVelocity(Velocity{LinearVelocity2{}, 2_rpm});
247+
EXPECT_TRUE(body != Body());
248+
}
249+
{
250+
auto body1 = Body{};
251+
body1.SetType(BodyType::Dynamic);
252+
body1.SetAcceleration(LinearAcceleration2{}, Real(2) * RadianPerSquareSecond);
253+
auto body2 = Body{};
254+
body2.SetType(BodyType::Dynamic);
255+
EXPECT_TRUE(body1 != body2);
256+
}
257+
{
258+
auto body = Body{};
259+
SetMass(body, 3.2_kg);
260+
EXPECT_TRUE(body != Body());
261+
}
262+
{
263+
auto body = Body{};
264+
body.SetInvRotInertia((Real(2) * SquareRadian) / (2_m2 * 1.2_kg));
265+
EXPECT_TRUE(body != Body());
266+
}
267+
{
268+
auto body = Body{};
269+
SetLinearDamping(body, 2_Hz);
270+
EXPECT_TRUE(body != Body());
271+
}
272+
{
273+
auto body = Body{};
274+
SetAngularDamping(body, 2_Hz);
275+
EXPECT_TRUE(body != Body());
276+
}
277+
{
278+
auto body1 = Body{};
279+
body1.SetType(BodyType::Dynamic);
280+
body1.SetUnderActiveTime(2_s);
281+
auto body2 = Body{};
282+
body2.SetType(BodyType::Dynamic);
283+
EXPECT_TRUE(body1 != body2);
284+
}
285+
}

0 commit comments

Comments
 (0)