PlayRho  1.1.0
An Interactive Real-Time-Oriented C++ Physics Engine & Library
Body.cpp

This is the googletest based unit testing file for the interfaces to playrho::d2::Body.

/*
* Copyright (c) 2020 Louis Langholtz https://github.com/louis-langholtz/PlayRho
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "UnitTests.hpp"
#include <PlayRho/Dynamics/Body.hpp>
using namespace playrho;
using namespace playrho::d2;
TEST(Body, ByteSize)
{
// Check size at test runtime instead of compile-time via static_assert to avoid stopping
// builds and to report actual size rather than just reporting that expected size is wrong.
// architecture dependent...
switch (sizeof(Real)) {
case 4:
#if defined(_WIN64)
#if !defined(NDEBUG)
EXPECT_EQ(sizeof(Body), std::size_t(216));
#else
EXPECT_EQ(sizeof(Body), std::size_t(100));
#endif
#elif defined(_WIN32)
#if !defined(NDEBUG)
// Win32 debug
EXPECT_EQ(sizeof(Body), std::size_t(192));
#else
// Win32 release
EXPECT_EQ(sizeof(Body), std::size_t(100));
#endif
#else
EXPECT_EQ(sizeof(Body), std::size_t(100));
#endif
break;
case 8:
EXPECT_EQ(sizeof(Body), std::size_t(200));
break;
case 16:
EXPECT_EQ(sizeof(Body), std::size_t(400));
break;
default:
FAIL();
break;
}
}
TEST(Body, GetFlagsForBodyType)
{
EXPECT_EQ(Body::GetFlags(BodyType::Static), (Body::e_impenetrableFlag));
EXPECT_EQ(Body::GetFlags(BodyType::Kinematic),
}
TEST(Body, GetFlagsForBodyConf)
{
EXPECT_TRUE(
Body::GetFlags(BodyConf{}.UseAwake(false).UseAllowSleep(false).UseType(BodyType::Dynamic)) &
}
TEST(Body, LinearDampingOnConstruction)
{
EXPECT_EQ(Body(BodyConf{}.UseLinearDamping(0_Hz)).GetLinearDamping(), 0_Hz);
EXPECT_EQ(Body(BodyConf{}.UseLinearDamping(20_Hz)).GetLinearDamping(), 20_Hz);
EXPECT_EQ(Body(BodyConf{}.UseLinearDamping(30_Hz)).GetLinearDamping(), 30_Hz);
}
TEST(Body, AngularDampingOnConstruction)
{
EXPECT_EQ(Body(BodyConf{}.UseAngularDamping(0_Hz)).GetAngularDamping(), 0_Hz);
EXPECT_EQ(Body(BodyConf{}.UseAngularDamping(20_Hz)).GetAngularDamping(), 20_Hz);
EXPECT_EQ(Body(BodyConf{}.UseAngularDamping(30_Hz)).GetAngularDamping(), 30_Hz);
}
TEST(Body, InvMassOnConstruction)
{
EXPECT_EQ(Body(BodyConf{}.UseType(BodyType::Dynamic)).GetInvMass(), Real(1) / 1_kg);
EXPECT_EQ(Body(BodyConf{}.UseType(BodyType::Kinematic)).GetInvMass(), Real(0) / 1_kg);
EXPECT_EQ(Body(BodyConf{}.UseType(BodyType::Static)).GetInvMass(), Real(0) / 1_kg);
}
TEST(Body, TransformationOnConstruction)
{
EXPECT_EQ(
Body(BodyConf{}.UseLocation(Length2{10_m, 12_m}).UseAngle(90_deg)).GetTransformation(),
BodyConf{}.UseLocation(Length2{10_m, 12_m}).UseAngle(90_deg)));
EXPECT_EQ(
Body(BodyConf{}.UseLocation(Length2{4_m, -3_m}).UseAngle(-32_deg)).GetTransformation(),
BodyConf{}.UseLocation(Length2{4_m, -3_m}).UseAngle(-32_deg)));
}
TEST(Body, VelocityOnConstruction)
{
auto body = Body{};
body.SetVelocity(Velocity{LinearVelocity2{1_mps, 2_mps}, 3_rpm});
EXPECT_EQ(
Body(BodyConf{}.Use(Velocity{LinearVelocity2{1_mps, 2_mps}, 3_rpm})).GetVelocity().linear,
body.GetVelocity().linear);
EXPECT_EQ(
Body(BodyConf{}.Use(Velocity{LinearVelocity2{1_mps, 2_mps}, 3_rpm})).GetVelocity().angular,
body.GetVelocity().angular);
}
TEST(Body, AccelerationOnConstruction)
{
auto body = Body{};
EXPECT_EQ(Body(BodyConf{}
.UseAngularAcceleration(Real(4) * RadianPerSquareSecond))
body.GetLinearAcceleration());
EXPECT_EQ(Body(BodyConf{}
.UseAngularAcceleration(Real(4) * RadianPerSquareSecond))
body.GetAngularAcceleration());
}
TEST(Body, EqualsOperator)
{
EXPECT_TRUE(Body() == Body());
{
auto body = Body{};
EXPECT_TRUE(body == Body());
}
{
auto body1 = Body{};
auto body2 = Body{};
EXPECT_TRUE(body1 == body2);
}
{
auto body = Body{};
EXPECT_FALSE(body == Body());
}
{
auto body = Body{};
body.SetSweep(Sweep{Position{Length2{}, 2_deg}});
EXPECT_FALSE(body == Body());
}
{
auto body = Body{};
body.SetType(BodyType::Kinematic);
EXPECT_FALSE(body == Body());
}
{
auto body = Body{};
EXPECT_FALSE(body == Body());
}
{
auto body1 = Body{};
body1.SetAcceleration(LinearAcceleration2{}, Real(2) * RadianPerSquareSecond);
auto body2 = Body{};
EXPECT_FALSE(body1 == body2);
}
{
auto body = Body{};
SetMass(body, 3.2_kg);
EXPECT_FALSE(body == Body());
}
{
auto body = Body{};
body.SetInvRotInertia((Real(2) * SquareRadian) / (2_m2 * 1.2_kg));
EXPECT_FALSE(body == Body());
}
{
auto body = Body{};
SetLinearDamping(body, 2_Hz);
EXPECT_FALSE(body == Body());
}
{
auto body = Body{};
SetAngularDamping(body, 2_Hz);
EXPECT_FALSE(body == Body());
}
{
auto body1 = Body{};
body1.SetUnderActiveTime(2_s);
auto body2 = Body{};
EXPECT_FALSE(body1 == body2);
}
}
TEST(Body, NotEqualsOperator)
{
EXPECT_FALSE(Body() != Body());
{
auto body = Body{};
EXPECT_FALSE(body != Body());
}
{
auto body1 = Body{};
auto body2 = Body{};
EXPECT_FALSE(body1 != body2);
}
{
auto body = Body{};
EXPECT_TRUE(body != Body());
}
{
auto body = Body{};
body.SetSweep(Sweep{Position{Length2{}, 2_deg}});
EXPECT_TRUE(body != Body());
}
{
auto body = Body{};
body.SetType(BodyType::Kinematic);
EXPECT_TRUE(body != Body());
}
{
auto body = Body{};
EXPECT_TRUE(body != Body());
}
{
auto body1 = Body{};
body1.SetAcceleration(LinearAcceleration2{}, Real(2) * RadianPerSquareSecond);
auto body2 = Body{};
EXPECT_TRUE(body1 != body2);
}
{
auto body = Body{};
SetMass(body, 3.2_kg);
EXPECT_TRUE(body != Body());
}
{
auto body = Body{};
body.SetInvRotInertia((Real(2) * SquareRadian) / (2_m2 * 1.2_kg));
EXPECT_TRUE(body != Body());
}
{
auto body = Body{};
SetLinearDamping(body, 2_Hz);
EXPECT_TRUE(body != Body());
}
{
auto body = Body{};
SetAngularDamping(body, 2_Hz);
EXPECT_TRUE(body != Body());
}
{
auto body1 = Body{};
body1.SetUnderActiveTime(2_s);
auto body2 = Body{};
EXPECT_TRUE(body1 != body2);
}
}
playrho::BodyType::Static
@ Static
Static body type.
playrho::d2::Body::e_accelerationFlag
@ e_accelerationFlag
Acceleration flag.
Definition: Body.hpp:105
playrho::RadianPerSquareSecond
constexpr auto RadianPerSquareSecond
Radian per square second unit of angular acceleration.
Definition: Units.hpp:394
playrho::d2::BodyConf::UseType
constexpr BodyConf & UseType(BodyType t) noexcept
Use the given type.
Definition: BodyConf.hpp:166
playrho::d2
Name space for 2-dimensionally related PlayRho names.
Definition: AABB.cpp:34
playrho::d2::UnitVec
2-D unit vector.
Definition: UnitVec.hpp:50
playrho::d2::Body::e_impenetrableFlag
@ e_impenetrableFlag
Impenetrable flag.
Definition: Body.hpp:89
playrho::d2::Body::SetType
void SetType(BodyType value) noexcept
Sets the type of this body.
Definition: Body.cpp:124
playrho::d2::Body::SetInvRotInertia
void SetInvRotInertia(InvRotInertia v) noexcept
Sets the inverse rotational inertia.
Definition: Body.hpp:228
playrho::d2::Body::SetVelocity
void SetVelocity(const Velocity &velocity) noexcept
Sets the body's velocity (linear and angular velocity).
Definition: Body.cpp:165
playrho::d2::BodyConf::UseAwake
constexpr BodyConf & UseAwake(bool value) noexcept
Use the given awake value.
Definition: BodyConf.hpp:246
playrho
Name space for all PlayRho related names.
Definition: AABB.cpp:33
playrho::d2::Body::GetFlags
static FlagsType GetFlags(BodyType type) noexcept
Gets the flags for the given value.
Definition: Body.cpp:41
playrho::d2::BodyConf::UseFixedRotation
constexpr BodyConf & UseFixedRotation(bool value) noexcept
Use the given fixed rotation state.
Definition: BodyConf.hpp:252
playrho::d2::Velocity::angular
AngularVelocity angular
Angular velocity.
Definition: Velocity.hpp:40
playrho::d2::SetMass
void SetMass(Body &body, Mass mass)
Sets the mass of the given body.
Definition: Body.hpp:1090
playrho::d2::Body
Physical entity that exists within a World.
Definition: Body.hpp:71
playrho::d2::GetLinearDamping
Frequency GetLinearDamping(const Body &body) noexcept
Gets the linear damping of the body.
Definition: Body.hpp:987
playrho::d2::BodyConf::UseLocation
constexpr BodyConf & UseLocation(Length2 l) noexcept
Use the given location.
Definition: BodyConf.hpp:172
playrho::d2::BodyConf::UseLinearAcceleration
constexpr BodyConf & UseLinearAcceleration(LinearAcceleration2 v) noexcept
Use the given linear acceleration.
Definition: BodyConf.hpp:204
playrho::d2::GetAngularDamping
Frequency GetAngularDamping(const Body &body) noexcept
Gets the angular damping of the body.
Definition: Body.hpp:1003
playrho::d2::Body::SetAcceleration
void SetAcceleration(LinearAcceleration2 linear, AngularAcceleration angular) noexcept
Sets the linear and rotational accelerations on this body.
Definition: Body.cpp:177
playrho::d2::Velocity::linear
LinearVelocity2 linear
Linear velocity.
Definition: Velocity.hpp:39
playrho::d2::Sweep
Description of a "sweep" of motion in 2-D space.
Definition: Sweep.hpp:42
playrho::Real
float Real
Real-number type.
Definition: Real.hpp:69
playrho::d2::BodyConf
Configuration for a body.
Definition: BodyConf.hpp:50
playrho::d2::Velocity
2-D velocity related data structure.
Definition: Velocity.hpp:38
playrho::d2::BodyConf::UseAngularDamping
constexpr BodyConf & UseAngularDamping(NonNegative< Frequency > v) noexcept
Use the given angular damping.
Definition: BodyConf.hpp:228
playrho::d2::Body::e_velocityFlag
@ e_velocityFlag
Velocity flag.
Definition: Body.hpp:100
playrho::d2::Body::SetTransformation
void SetTransformation(const Transformation &value) noexcept
Sets the body's transformation.
Definition: Body.hpp:519
playrho::SquareRadian
constexpr auto SquareRadian
Square radian unit type.
Definition: Units.hpp:379
playrho::d2::Position
2-D positional data structure.
Definition: Position.hpp:37
playrho::Vector
Vector.
Definition: Vector.hpp:49
playrho::d2::Body::SetSweep
void SetSweep(const Sweep &value) noexcept
Sets the sweep value of the given body.
Definition: Body.hpp:389
playrho::d2::GetAngularAcceleration
AngularAcceleration GetAngularAcceleration(const Body &body) noexcept
Gets this body's angular acceleration.
Definition: Body.hpp:1048
playrho::d2::Transformation
Describes a geometric transformation.
Definition: Transformation.hpp:44
playrho::d2::BodyConf::Use
constexpr BodyConf & Use(Position v) noexcept
Use the given position for the linear and angular positions.
Definition: BodyConf.hpp:184
playrho::d2::GetTransformation
constexpr Transformation GetTransformation(const Length2 ctr, const UnitVec rot, const Length2 localCtr) noexcept
Gets the transformation for the given values.
Definition: Math.hpp:875
playrho::d2::GetInvMass
InvMass GetInvMass(const Body &body) noexcept
Gets the inverse total mass of the body.
Definition: Body.hpp:967
playrho::d2::BodyConf::UseLinearDamping
constexpr BodyConf & UseLinearDamping(NonNegative< Frequency > v) noexcept
Use the given linear damping.
Definition: BodyConf.hpp:222
playrho::d2::Body::e_fixedRotationFlag
@ e_fixedRotationFlag
Fixed rotation flag.
Definition: Body.hpp:92
playrho::d2::SetLinearDamping
void SetLinearDamping(Body &body, NonNegative< Frequency > value) noexcept
Sets the linear damping of the body.
Definition: Body.hpp:995
playrho::d2::GetVelocity
Velocity GetVelocity(const Body &body, Time h) noexcept
Gets the velocity of the body after the given time accounting for the body's acceleration and capped ...
Definition: Body.cpp:221
playrho::d2::Body::JustSetVelocity
void JustSetVelocity(Velocity value) noexcept
Sets the body's velocity.
Definition: Body.hpp:179
playrho::d2::GetLinearAcceleration
LinearAcceleration2 GetLinearAcceleration(const Body &body) noexcept
Gets this body's linear acceleration.
Definition: Body.hpp:1040
playrho::d2::Body::e_awakeFlag
@ e_awakeFlag
Awake flag.
Definition: Body.hpp:81
playrho::d2::SetAngularDamping
void SetAngularDamping(Body &body, NonNegative< Frequency > value) noexcept
Sets the angular damping of the body.
Definition: Body.hpp:1011