PlayRho
1.1.0
An Interactive Real-Time-Oriented C++ Physics Engine & Library
|
|
This is the googletest
based unit testing file for the free function interfaces to playrho::d2::World
body member functions and additional functionality.
#include "UnitTests.hpp"
#include <PlayRho/Dynamics/WorldBody.hpp>
#include <PlayRho/Dynamics/World.hpp>
#include <PlayRho/Dynamics/BodyConf.hpp>
#include <PlayRho/Dynamics/WorldFixture.hpp>
#include <PlayRho/Dynamics/WorldMisc.hpp>
#include <PlayRho/Dynamics/StepConf.hpp>
#include <PlayRho/Dynamics/Joints/Joint.hpp>
#include <PlayRho/Collision/Shapes/DiskShapeConf.hpp>
#include <PlayRho/Collision/Shapes/PolygonShapeConf.hpp>
#include <chrono>
TEST(WorldBody, WorldCreated)
{
EXPECT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
Awaken(world, body));
{
auto i = 0;
{
EXPECT_EQ(
GetBody(world, fixture), body);
++i;
}
EXPECT_EQ(i, 0);
}
{
auto i = 0;
{
++i;
}
EXPECT_EQ(i, 0);
}
{
auto i = 0;
{
++i;
}
EXPECT_EQ(i, 0);
}
}
TEST(WorldBody, SetVelocityDoesNothingToStatic)
{
};
ASSERT_FALSE(
IsAwake(world, body));
};
}
{
}
{
EXPECT_NO_THROW(
Destroy(world, bodyID));
EXPECT_TRUE(
Destroy(world, fixtureA));
}
TEST(WorldBody, SetEnabledCausesIsEnabled)
{
auto value = true;
for (auto i = 0; i < 4; ++i) {
value = !value;
}
}
{
}
TEST(WorldBody, CreateAndDestroyFixture)
{
conf.location =
Vec2{1.912f, -77.31f} * 1_m;
conf.density = 1_kgpm2;
const auto shape =
Shape(conf);
{
const auto fshape =
GetShape(world, fixture);
EXPECT_EQ(TypeCast<DiskShapeConf>(fshape).
GetLocation(), conf.GetLocation());
{
auto i = 0;
{
EXPECT_EQ(f, fixture);
++i;
}
EXPECT_EQ(i, 1);
}
EXPECT_TRUE(world.Destroy(fixture));
}
{
const auto fshape =
GetShape(world, fixture);
EXPECT_EQ(TypeCast<DiskShapeConf>(fshape).
GetLocation(), conf.GetLocation());
{
auto i = 0;
{
EXPECT_EQ(f, fixture);
++i;
}
EXPECT_EQ(i, 1);
}
}
}
{
SetType(world, body, BodyType::Static);
EXPECT_EQ(
GetType(world, body), BodyType::Static);
SetType(world, body, BodyType::Kinematic);
EXPECT_EQ(
GetType(world, body), BodyType::Kinematic);
}
TEST(WorldBody, StaticIsExpected)
{
}
TEST(WorldBody, KinematicIsExpected)
{
}
TEST(WorldBody, DynamicIsExpected)
{
}
{
const auto center =
Length2{0_m, 0_m};
const auto mass = 32_kg;
const auto rotInertia = 3 * rotInertiaUnits;
const auto massData =
MassData{center, mass, rotInertia};
{
EXPECT_EQ(
GetMass(world, body), 1_kg);
EXPECT_EQ(
GetRotInertia(world, body), std::numeric_limits<Real>::infinity() * rotInertiaUnits);
EXPECT_EQ(
GetMass(world, body), mass);
}
{
EXPECT_EQ(
GetMass(world, body), 1_kg);
EXPECT_EQ(
GetRotInertia(world, body), std::numeric_limits<Real>::infinity() * rotInertiaUnits);
EXPECT_EQ(
GetMass(world, body), mass);
EXPECT_EQ(
GetRotInertia(world, body), std::numeric_limits<Real>::infinity() * rotInertiaUnits);
}
{
EXPECT_EQ(
GetMass(world, body), 0_kg);
EXPECT_EQ(
GetRotInertia(world, body), std::numeric_limits<Real>::infinity() * rotInertiaUnits);
EXPECT_EQ(
GetMass(world, body), 0_kg);
EXPECT_EQ(
GetRotInertia(world, body), std::numeric_limits<Real>::infinity() * rotInertiaUnits);
}
}
{
static_cast<double>(
GetX(xfm2.q)),
0.001);
static_cast<double>(
GetY(xfm2.q)),
0.001);
world.Destroy(body);
}
{
{
ASSERT_FALSE(
IsAwake(world, body));
ASSERT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
}
{
ASSERT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
}
{
ASSERT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
ASSERT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
SetAcceleration(world, body, someLinearAccel * 0.5f, someAngularAccel * 0.9f);
EXPECT_FALSE(
IsAwake(world, body));
SetAcceleration(world, body, someLinearAccel * 1.5f, someAngularAccel * 1.9f);
ASSERT_FALSE(
IsAwake(world, body));
SetAcceleration(world, body, someLinearAccel * 1.5f, someAngularAccel * 2.0f);
ASSERT_FALSE(
IsAwake(world, body));
SetAcceleration(world, body, someLinearAccel * 2.0f, someAngularAccel * 2.0f);
ASSERT_FALSE(
IsAwake(world, body));
SetAcceleration(world, body, someLinearAccel * -1.0f, someAngularAccel * 2.0f);
}
}
TEST(WorldBody, SetAngularAcceleration)
{
}
TEST(WorldBody, SetAngularVelocity)
{
}
{
ASSERT_EQ(
GetMass(world, body), 4_kg);
}
{
ASSERT_EQ(
GetMass(world, body), 4_kg);
}
{
ASSERT_EQ(
GetMass(world, body), 4_kg);
}
{
ASSERT_EQ(
GetMass(world, body), 4_kg);
}
TEST(WorldBody, CreateLotsOfFixtures)
{
conf.location =
Vec2{1.912f, -77.31f} * 1_m;
conf.density = 1.3_kgpm2;
const auto shape =
Shape(conf);
const auto num = 5000;
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
{
for (auto i = decltype(num){0}; i < num; ++i)
{
}
{
int i = decltype(num){0};
{
++i;
}
EXPECT_EQ(i, num);
}
}
end = std::chrono::system_clock::now();
const std::chrono::duration<double> elapsed_secs_resetting_at_end = end - start;
start = std::chrono::system_clock::now();
{
for (auto i = decltype(num){0}; i < num; ++i)
{
}
{
auto i = decltype(num){0};
{
++i;
}
EXPECT_EQ(i, num);
}
}
end = std::chrono::system_clock::now();
const std::chrono::duration<double> elapsed_secs_resetting_in_create = end - start;
EXPECT_LT(elapsed_secs_resetting_at_end.count(), elapsed_secs_resetting_in_create.count());
}
{
}
TEST(WorldBody, ApplyLinearAccelDoesNothingToStatic)
{
ASSERT_FALSE(
IsAwake(world, body));
};
};
}
TEST(WorldBody, GetAccelerationFF)
{
}
TEST(WorldBody, SetAccelerationFF)
{
};
}
{
const auto l1 =
Length2{-8_m, 0_m};
const auto l2 =
Length2{+8_m, 0_m};
const auto l3 =
Length2{+16_m, 0_m};
0.032761313021183014, 0.032761313021183014/100);
}
TEST(WorldBody, RotateAboutWorldPointFF)
{
ASSERT_EQ(locationA,
Length2(0_m, 0_m));
EXPECT_NEAR(
static_cast<double>(
Real(
GetX(locationB)/
Meter)), +2.0, 0.001);
EXPECT_NEAR(
static_cast<double>(
Real(
GetY(locationB)/
Meter)), -2.0, 0.001);
}
TEST(WorldBody, RotateAboutLocalPointFF)
{
ASSERT_EQ(locationA,
Length2(0_m, 0_m));
EXPECT_NEAR(
static_cast<double>(
Real(
GetX(locationB)/
Meter)), +2.0, 0.001);
EXPECT_NEAR(
static_cast<double>(
Real(
GetY(locationB)/
Meter)), -2.0, 0.001);
}
{
const auto l1 =
Length2{-8_m, 0_m};
EXPECT_NEAR(
static_cast<double>(
Real(
GetX(force)/
Newton)), 8.1230141222476959, 0.01);
EXPECT_NEAR(
static_cast<double>(
Real(
GetY(force)/
Newton)), 9.0255714952945709, 0.01);
}
TEST(WorldBody, GetPositionFF)
{
SetAngle(world, body, position.angular);
static_cast<double>(
Real(position.angular /
Degree)),
0.0001);
}
TEST(WorldBody, GetSetTransformationFF)
{
EXPECT_EQ(xfm1.p, xfm0.p);
EXPECT_NEAR(
static_cast<double>(
GetX(xfm1.q)),
static_cast<double>(
GetX(xfm0.q)), 0.0001);
EXPECT_NEAR(
static_cast<double>(
GetY(xfm1.q)),
static_cast<double>(
GetY(xfm0.q)), 0.0001);
}
{
{
EXPECT_FALSE(
IsAwake(world, body));
}
{
EXPECT_FALSE(
IsAwake(world, body));
EXPECT_FALSE(
IsAwake(world, body));
}
}
{
for (auto i = 1; i < 10; ++i) {
}
ASSERT_NO_THROW(
Destroy(world, body));
ASSERT_NO_THROW(
Clear(world));
ASSERT_NO_THROW(
Destroy(world, body));
}
const Shape & GetShape(const FixtureConf &conf) noexcept
Gets the shape of the given configuration.
Definition: FixtureConf.hpp:126
void SetAcceleration(Body &body, Acceleration value) noexcept
Sets the accelerations on the given body.
Definition: Body.hpp:1032
RotInertia GetRotInertia(const Body &body) noexcept
Gets the rotational inertia of the body.
Definition: Body.hpp:1130
@ Static
Static body type.
constexpr auto RadianPerSquareSecond
Radian per square second unit of angular acceleration.
Definition: Units.hpp:394
constexpr auto MeterPerSquareSecond
Meter per square second unit of linear acceleration.
Definition: Units.hpp:345
BodyID CreateBody(World &world, const BodyConf &def)
Creates a rigid body with the given configuration.
Definition: WorldBody.cpp:58
constexpr BodyConf & UseType(BodyType t) noexcept
Use the given type.
Definition: BodyConf.hpp:166
PLAYRHO_QUANTITY(boost::units::si::torque) Torque
Torque quantity.
Definition: Units.hpp:255
Name space for 2-dimensionally related PlayRho names.
Definition: AABB.cpp:34
void NOT_USED(T &&...)
"Not used" annotator.
Definition: Templates.hpp:98
void RotateAboutWorldPoint(World &world, BodyID body, Angle amount, Length2 worldPoint)
Rotates a body a given amount around a point in world coordinates.
Definition: WorldBody.cpp:148
constexpr auto Kilogram
Kilogram unit of mass.
Definition: Units.hpp:352
static constexpr UnitVec GetRight() noexcept
Gets the right-ward oriented unit vector.
Definition: UnitVec.hpp:74
bool IsFixedRotation(const Body &body) noexcept
Does this body have fixed rotation?
Definition: Body.hpp:938
Force2 GetCentripetalForce(const World &world, BodyID id, Length2 axis)
Gets the centripetal force necessary to put the body into an orbit having the given radius.
Definition: WorldBody.cpp:483
SizedRange< std::vector< std::pair< BodyID, JointID > >::const_iterator > GetJoints(const World &world, BodyID id)
Gets the range of all joints attached to the identified body.
Definition: WorldBody.cpp:407
Mass GetMass(const Body &body) noexcept
Gets the mass of the body.
Definition: Body.hpp:1082
constexpr Position GetPosition(const Position pos0, const Position pos1, const Real beta) noexcept
Gets the position between two positions at a given unit interval.
Definition: Position.hpp:137
void SetAngle(World &world, BodyID body, Angle value)
Sets the body's angular orientation.
Definition: WorldBody.cpp:143
bool IsAwake(const Body &body) noexcept
Gets the awake/asleep state of this body.
Definition: Body.hpp:808
void Clear(Island &island) noexcept
Clears the island containers.
Definition: Island.cpp:145
SizedRange< std::vector< KeyedContactPtr >::const_iterator > GetContacts(const World &world, BodyID id)
Gets the container of all contacts attached to the identified body.
Definition: WorldBody.cpp:478
constexpr auto Newton
Newton unit of force.
Definition: Units.hpp:403
constexpr ConcreteConf & UseDensity(NonNegative< AreaDensity > value) noexcept
Uses the given density.
Definition: ShapeConf.hpp:111
Length GetMaxVertexRadius(const WorldImpl &world) noexcept
Gets the maximum vertex radius that shapes in this world can be.
Definition: WorldImplMisc.cpp:142
constexpr auto & GetX(T &value)
Gets the "X" element of the given value - i.e. the first element.
Definition: Math.hpp:66
Name space for all PlayRho related names.
Definition: AABB.cpp:33
BodyCounter GetBodyRange(const World &world) noexcept
Gets the extent of the currently valid body range.
Definition: WorldBody.cpp:42
void SetType(Body &body, BodyType value) noexcept
Sets the type of this body.
Definition: Body.hpp:699
BodyType type
Type of the body: static, kinematic, or dynamic.
Definition: BodyConf.hpp:108
Vector2< Real > Vec2
Vector with 2 Real elements.
Definition: Vector2.hpp:39
AngularVelocity GetAngularVelocity(const Body &body) noexcept
Gets the angular velocity.
Definition: Body.hpp:1178
std::remove_const< decltype(MaxBodies)>::type BodyCounter
Count type for bodies.
Definition: Settings.hpp:217
constexpr auto RadianPerSecond
Radian per second unit of angular velocity.
Definition: Units.hpp:384
Definition of an independent and simulatable "world".
Definition: World.hpp:129
constexpr auto InvalidFixtureID
Invalid fixture ID value.
Definition: FixtureID.hpp:33
Vector2< LinearVelocity > LinearVelocity2
2-element vector of linear velocity (LinearVelocity) quantities.
Definition: Vector2.hpp:47
constexpr auto NewtonMeterSecond
Newton meter second unit of angular momentum.
Definition: Units.hpp:418
void DestroyFixtures(World &world, BodyID id, bool resetMassData)
Destroys fixtures of the identified body.
Definition: WorldBody.cpp:266
SizedRange< std::vector< FixtureID >::const_iterator > GetFixtures(const World &world, BodyID id)
Gets the range of all constant fixtures attached to the given body.
Definition: WorldBody.cpp:79
Fixture definition.
Definition: FixtureConf.hpp:46
bool IsMassDataDirty(const Body &body) noexcept
Gets whether the mass data for this body is "dirty".
Definition: Body.hpp:954
Mass data.
Definition: MassData.hpp:41
std::size_t size(const DynamicTree &tree) noexcept
Gets the "size" of the given tree.
Definition: DynamicTree.hpp:773
void SetMassData(World &world, BodyID id, const MassData &massData)
Sets the mass properties to override the mass properties of the fixtures.
Definition: WorldBody.cpp:365
bool Awaken(Body &body) noexcept
Awakens the body if it's asleep.
Definition: Body.hpp:1056
constexpr auto Degree
Degree unit of angle quantity.
Definition: Units.hpp:374
void SetFixedRotation(Body &body, bool value)
Sets this body to have fixed rotation.
Definition: Body.hpp:947
void UnsetAwake(Body &body) noexcept
Sets this body to asleep if sleeping is allowed.
Definition: Body.hpp:837
bool IsEnabled(const Body &body) noexcept
Gets the enabled/disabled state of the body.
Definition: Body.hpp:771
PLAYRHO_QUANTITY(boost::units::si::angular_acceleration) AngularAcceleration
Angular acceleration quantity.
Definition: Units.hpp:235
Vector2< LinearAcceleration > LinearAcceleration2
2-element vector of linear acceleration (LinearAcceleration) quantities.
Definition: Vector2.hpp:51
Polygon shape configuration.
Definition: PolygonShapeConf.hpp:42
constexpr auto Meter
Meter unit of Length.
Definition: Units.hpp:337
constexpr auto SquareMeter
Square meter unit of area.
Definition: Units.hpp:356
constexpr bool empty(IndexPair3 pairs) noexcept
Checks whether the given collection of index pairs is empty.
Definition: IndexPair.hpp:69
static constexpr UnitVec GetTopRight() noexcept
Gets the 45 degree unit vector.
Definition: UnitVec.hpp:99
SizedRange< std::vector< BodyID >::const_iterator > GetBodiesForProxies(const World &world) noexcept
Gets the bodies-for-proxies range for the given world.
Definition: WorldBody.cpp:53
void RotateAboutLocalPoint(World &world, BodyID body, Angle amount, Length2 localPoint)
Rotates a body a given amount around a point in body local coordinates.
Definition: WorldBody.cpp:161
void Destroy(World &world, BodyID id)
Destroys the identified body.
Definition: WorldBody.cpp:73
Angle GetAngle(const UnitVec value)
Gets the angle of the given unit vector.
Definition: Math.hpp:718
void SetEnabled(Body &body) noexcept
Sets the enabled state.
Definition: Body.hpp:779
void SetLocation(World &world, BodyID body, Length2 value)
Sets the body's location.
Definition: WorldBody.cpp:138
constexpr auto InvalidBodyID
Invalid body ID value.
Definition: BodyID.hpp:33
static constexpr UnitVec GetLeft() noexcept
Gets the left-ward oriented unit vector.
Definition: UnitVec.hpp:86
PLAYRHO_QUANTITY(boost::units::si::angular_momentum) AngularMomentum
Angular momentum quantity.
Definition: Units.hpp:304
void SetTransform(World &world, BodyID id, Length2 location, Angle angle)
Sets the position of the body's origin and rotation.
Definition: WorldBody.hpp:213
BodyCounter GetBodyCount(const World &world) noexcept
Gets the body count in the given world.
Definition: WorldBody.hpp:808
NonNegative< Length > GetVertexRadius(const DistanceProxy &arg) noexcept
Gets the vertex radius property of a given distance proxy.
Definition: DistanceProxy.hpp:205
Invalid argument logic error.
Definition: InvalidArgument.hpp:33
FixtureID CreateFixture(World &world, FixtureConf def, bool resetMassData)
Creates a fixture within the specified world.
Definition: WorldFixture.cpp:48
float Real
Real-number type.
Definition: Real.hpp:69
constexpr DiskShapeConf & UseRadius(NonNegative< Length > r) noexcept
Uses the given value as the radius.
Definition: DiskShapeConf.hpp:65
Configuration for a body.
Definition: BodyConf.hpp:50
constexpr Length2 GetLocation(const Transformation &value) noexcept
Gets the location information from the given transformation.
Definition: Transformation.hpp:69
Acceleration CalcGravitationalAcceleration(const World &world, BodyID body)
Calculates the gravitationally associated acceleration for the given body within its world.
Definition: WorldBody.cpp:166
2-D velocity related data structure.
Definition: Velocity.hpp:38
constexpr auto SquareSecond
Square second unit.
Definition: Units.hpp:324
void SetVelocity(Body &body, const Velocity &value) noexcept
Sets the body's velocity (linear and angular velocity).
Definition: Body.hpp:1158
void ApplyLinearImpulse(Body &body, Momentum2 impulse, Length2 point) noexcept
Applies an impulse at a point.
Definition: Body.cpp:242
Length GetMinVertexRadius(const WorldImpl &world) noexcept
Gets the minimum vertex radius that shapes in this world can be.
Definition: WorldImplMisc.cpp:137
constexpr auto SquareRadian
Square radian unit type.
Definition: Units.hpp:379
Acceleration GetAcceleration(const Body &body) noexcept
Gets the given body's acceleration.
Definition: Body.hpp:1020
2-D positional data structure.
Definition: Position.hpp:37
void ResetMassData(World &world, BodyID id)
Resets the mass data properties.
Definition: WorldBody.hpp:611
Vector.
Definition: Vector.hpp:49
Length2 linear
Linear position.
Definition: Position.hpp:38
Length2 GetWorldCenter(const Body &body) noexcept
Get the world position of the center of mass.
Definition: Body.hpp:908
FixtureCounter GetFixtureCount(const World &world, BodyID id)
Gets the count of fixtures associated with the identified body.
Definition: WorldBody.hpp:129
void SetAwake(Body &body) noexcept
Awakens this body.
Definition: Body.hpp:822
void ApplyForce(World &world, BodyID id, Force2 force, Length2 point)
Apply a force at a world point.
Definition: WorldBody.cpp:499
TypeID GetType(const Shape &shape) noexcept
Gets the type info of the use of the given shape.
Definition: Shape.hpp:329
AngularAcceleration GetAngularAcceleration(const Body &body) noexcept
Gets this body's angular acceleration.
Definition: Body.hpp:1048
BodyID GetBody(const FixtureConf &conf) noexcept
Gets the body of the given configuration.
Definition: FixtureConf.hpp:119
2-D acceleration related data structure.
Definition: Acceleration.hpp:33
SizedRange< std::vector< BodyID >::const_iterator > GetBodies(const World &world) noexcept
Gets the bodies of the specified world.
Definition: WorldBody.cpp:47
constexpr Transformation GetTransformation(const Length2 ctr, const UnitVec rot, const Length2 localCtr) noexcept
Gets the transformation for the given values.
Definition: Math.hpp:875
void ApplyAngularImpulse(Body &body, AngularMomentum impulse) noexcept
Applies an angular impulse.
Definition: Body.cpp:252
NonNegative< Length > vertexRadius
Vertex radius.
Definition: DiskShapeConf.hpp:102
Shape.
Definition: Shape.hpp:183
LinearVelocity2 GetLinearVelocity(const Body &body) noexcept
Gets the linear velocity of the center of mass.
Definition: Body.hpp:1168
constexpr auto & GetY(T &value)
Gets the "Y" element of the given value - i.e. the second element.
Definition: Math.hpp:73
PLAYRHO_QUANTITY(boost::units::si::acceleration) LinearAcceleration
Linear acceleration quantity.
Definition: Units.hpp:176
Vector2< Length > Length2
2-element vector of Length quantities.
Definition: Vector2.hpp:43
constexpr auto Radian
Radian unit of angle.
Definition: Units.hpp:369
BodyCounter GetWorldIndex(const World &world, BodyID id) noexcept
Gets the world index for the given body.
Definition: WorldBody.cpp:202
bool IsSpeedable(const Body &body) noexcept
Is "speedable".
Definition: Body.hpp:708
bool IsAccelerable(const Body &body) noexcept
Is "accelerable".
Definition: Body.hpp:718
bool IsImpenetrable(const Body &body) noexcept
Is this body treated like a bullet for continuous collision detection?
Definition: Body.hpp:726
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
LinearAcceleration2 GetLinearAcceleration(const Body &body) noexcept
Gets this body's linear acceleration.
Definition: Body.hpp:1040
PLAYRHO_QUANTITY(boost::units::si::velocity) LinearVelocity
Linear velocity quantity.
Definition: Units.hpp:167
void ApplyTorque(World &world, BodyID id, Torque torque)
Applies a torque.
Definition: WorldBody.cpp:514
PLAYRHO_QUANTITY(boost::units::si::angular_velocity) AngularVelocity
Angular velocity quantity.
Definition: Units.hpp:225
Disk shape configuration.
Definition: DiskShapeConf.hpp:42
void SetTransformation(Body &body, Transformation value) noexcept
Sets the body's transformation.
Definition: Body.hpp:854