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

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

/*
* 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/Joints/RopeJointConf.hpp>
#include <PlayRho/Dynamics/Joints/Joint.hpp>
#include <PlayRho/Dynamics/BodyConf.hpp>
#include <PlayRho/Dynamics/World.hpp>
#include <PlayRho/Dynamics/WorldJoint.hpp>
#include <PlayRho/Dynamics/WorldBody.hpp>
#include <PlayRho/Dynamics/WorldFixture.hpp>
#include <PlayRho/Dynamics/WorldMisc.hpp>
#include <PlayRho/Dynamics/StepConf.hpp>
#include <PlayRho/Collision/Shapes/DiskShapeConf.hpp>
#include <cstring> // for std::memcmp
using namespace playrho;
using namespace playrho::d2;
TEST(RopeJointConf, 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.
switch (sizeof(Real)) {
case 4:
EXPECT_EQ(sizeof(RopeJointConf), std::size_t(68));
break;
case 8:
EXPECT_EQ(sizeof(RopeJointConf), std::size_t(128));
break;
case 16:
EXPECT_EQ(sizeof(RopeJointConf), std::size_t(256));
break;
default:
FAIL();
break;
}
}
TEST(RopeJointConf, DefaultConstruction)
{
EXPECT_EQ(def.bodyA, InvalidBodyID);
EXPECT_EQ(def.bodyB, InvalidBodyID);
EXPECT_EQ(def.collideConnected, false);
EXPECT_EQ(def.localAnchorA, Length2(-1_m, 0_m));
EXPECT_EQ(def.localAnchorB, Length2(+1_m, 0_m));
EXPECT_EQ(def.maxLength, 0_m);
}
TEST(RopeJointConf, Construction)
{
World world;
const auto b0 = CreateBody(world);
const auto b1 = CreateBody(world);
auto def = RopeJointConf{b0, b1};
Joint joint{def};
EXPECT_EQ(GetType(joint), GetTypeID<RopeJointConf>());
EXPECT_EQ(GetBodyA(joint), def.bodyA);
EXPECT_EQ(GetBodyB(joint), def.bodyB);
EXPECT_EQ(GetCollideConnected(joint), def.collideConnected);
EXPECT_EQ(GetLinearReaction(joint), Momentum2{});
EXPECT_EQ(GetAngularReaction(joint), AngularMomentum{0});
const auto id = CreateJoint(world, joint);
EXPECT_EQ(GetLocalAnchorA(joint), def.localAnchorA);
EXPECT_EQ(GetLocalAnchorB(joint), def.localAnchorB);
EXPECT_EQ(GetAnchorA(world, id), Length2(-1_m, 0_m));
EXPECT_EQ(GetAnchorB(world, id), Length2(+1_m, 0_m));
EXPECT_EQ(GetLimitState(joint), LimitState::e_inactiveLimit);
const auto conf = TypeCast<RopeJointConf>(GetJoint(world, id));
EXPECT_EQ(GetMaxLength(conf), def.maxLength);
}
{
auto world = World{};
const auto bodyA = CreateBody(world);
const auto bodyB = CreateBody(world);
auto def = RopeJointConf{bodyA, bodyB};
const auto localAnchorA = Length2{-2_m, 0_m};
const auto localAnchorB = Length2{+2_m, 0_m};
def.localAnchorA = localAnchorA;
def.localAnchorB = localAnchorB;
const auto joint = Joint{def};
ASSERT_EQ(GetType(joint), GetTypeID<RopeJointConf>());
ASSERT_EQ(GetBodyA(joint), def.bodyA);
ASSERT_EQ(GetBodyB(joint), def.bodyB);
ASSERT_EQ(GetCollideConnected(joint), def.collideConnected);
ASSERT_EQ(GetLocalAnchorA(joint), def.localAnchorA);
ASSERT_EQ(GetLocalAnchorB(joint), def.localAnchorB);
const auto conf = TypeCast<RopeJointConf>(joint);
ASSERT_EQ(GetMaxLength(conf), def.maxLength);
const auto cdef = GetRopeJointConf(joint);
EXPECT_EQ(cdef.bodyA, bodyA);
EXPECT_EQ(cdef.bodyB, bodyB);
EXPECT_EQ(cdef.collideConnected, false);
EXPECT_EQ(cdef.localAnchorA, localAnchorA);
EXPECT_EQ(cdef.localAnchorB, localAnchorB);
EXPECT_EQ(cdef.maxLength, 0_m);
}
TEST(RopeJointConf, WithDynamicCircles)
{
const auto circle = Shape{DiskShapeConf{}.UseRadius(0.2_m)};
auto world = World{};
const auto p1 = Length2{-1_m, 0_m};
const auto p2 = Length2{+1_m, 0_m};
const auto b1 = CreateBody(world, BodyConf{}.UseType(BodyType::Dynamic).UseLocation(p1));
const auto b2 = CreateBody(world, BodyConf{}.UseType(BodyType::Dynamic).UseLocation(p2));
CreateFixture(world, b1, circle);
CreateFixture(world, b2, circle);
const auto jd = RopeJointConf{b1, b2};
ASSERT_NE(CreateJoint(world, jd), InvalidJointID);
auto stepConf = StepConf{};
stepConf.doWarmStart = true;
Step(world, stepConf);
EXPECT_GT(GetX(GetLocation(world, b1)), -1_m);
EXPECT_EQ(GetY(GetLocation(world, b1)), 0_m);
EXPECT_LT(GetX(GetLocation(world, b2)), +1_m);
EXPECT_EQ(GetY(GetLocation(world, b2)), 0_m);
EXPECT_EQ(GetAngle(world, b1), 0_deg);
EXPECT_EQ(GetAngle(world, b2), 0_deg);
stepConf.doWarmStart = false;
Step(world, stepConf);
EXPECT_GT(GetX(GetLocation(world, b1)), -1_m);
EXPECT_EQ(GetY(GetLocation(world, b1)), 0_m);
EXPECT_LT(GetX(GetLocation(world, b2)), +1_m);
EXPECT_EQ(GetY(GetLocation(world, b2)), 0_m);
EXPECT_EQ(GetAngle(world, b1), 0_deg);
EXPECT_EQ(GetAngle(world, b2), 0_deg);
stepConf.doWarmStart = true;
stepConf.linearSlop = 10_m;
Step(world, stepConf);
EXPECT_GT(GetX(GetLocation(world, b1)), -1_m);
EXPECT_EQ(GetY(GetLocation(world, b1)), 0_m);
EXPECT_LT(GetX(GetLocation(world, b2)), +1_m);
EXPECT_EQ(GetY(GetLocation(world, b2)), 0_m);
EXPECT_EQ(GetAngle(world, b1), 0_deg);
EXPECT_EQ(GetAngle(world, b2), 0_deg);
}
{
auto jd = RopeJointConf{BodyID(0u), BodyID(1u)};
auto copy = RopeJointConf{};
// Do copy = jd without missing padding so memcmp works
std::memcpy(&copy, &jd, sizeof(RopeJointConf));
EXPECT_FALSE(ShiftOrigin(jd, Length2{0_m, 0_m}));
// Use memcmp since easier than writing full == suport pre C++20.
EXPECT_TRUE(std::memcmp(&jd, &copy, sizeof(RopeJointConf)) == 0);
}
TEST(RopeJointConf, EqualsOperator)
{
EXPECT_TRUE(RopeJointConf() == RopeJointConf());
{
auto conf = RopeJointConf{};
conf.localAnchorA = Length2{1.2_m, -3_m};
EXPECT_TRUE(conf == conf);
EXPECT_FALSE(RopeJointConf() == conf);
}
{
auto conf = RopeJointConf{};
conf.localAnchorB = Length2{1.2_m, -3_m};
EXPECT_TRUE(conf == conf);
EXPECT_FALSE(RopeJointConf() == conf);
}
{
auto conf = RopeJointConf{};
conf.maxLength = 12_m;
EXPECT_TRUE(conf == conf);
EXPECT_FALSE(RopeJointConf() == conf);
}
// TODO: test remaining fields.
}
TEST(RopeJointConf, NotEqualsOperator)
{
EXPECT_FALSE(RopeJointConf() != RopeJointConf());
{
auto conf = RopeJointConf{};
conf.mass = 13_kg;
EXPECT_FALSE(conf != conf);
EXPECT_TRUE(RopeJointConf() != conf);
}
// TODO: test remaining fields.
}
{
EXPECT_STREQ(GetName(GetTypeID<RopeJointConf>()), "d2::RopeJointConf");
}
playrho::BodyType::Static
@ Static
Static body type.
playrho::d2::CreateBody
BodyID CreateBody(World &world, const BodyConf &def)
Creates a rigid body with the given configuration.
Definition: WorldBody.cpp:58
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::RopeJointConf::localAnchorB
Length2 localAnchorB
The local anchor point relative to body B's origin.
Definition: RopeJointConf.hpp:82
playrho::InvalidJointID
constexpr auto InvalidJointID
Invalid joint ID value.
Definition: JointID.hpp:33
playrho::d2::GetName
const char * GetName(Manifold::Type type) noexcept
Gets a unique name for the given manifold type.
Definition: Manifold.cpp:788
playrho::GetX
constexpr auto & GetX(T &value)
Gets the "X" element of the given value - i.e. the first element.
Definition: Math.hpp:66
playrho
Name space for all PlayRho related names.
Definition: AABB.cpp:33
playrho::d2::GetJoint
const Joint & GetJoint(const WorldImpl &world, JointID id)
Gets the identified joint's value.
Definition: WorldImplJoint.cpp:57
playrho::d2::GetLocalAnchorA
Length2 GetLocalAnchorA(const Joint &object)
Get the anchor point on body-A in local coordinates.
Definition: Joint.cpp:62
playrho::d2::World
Definition of an independent and simulatable "world".
Definition: World.hpp:129
playrho::d2::RopeJointConf::mass
Mass mass
Mass.
Definition: RopeJointConf.hpp:94
playrho::d2::GetAnchorB
Length2 GetAnchorB(const World &world, JointID id)
Get the anchor point on body-B in world coordinates.
Definition: WorldJoint.cpp:216
playrho::d2::ShiftOrigin
constexpr bool ShiftOrigin(DistanceJointConf &, Length2) noexcept
Shifts the origin notion of the given configuration.
Definition: DistanceJointConf.hpp:177
playrho::d2::GetRopeJointConf
RopeJointConf GetRopeJointConf(const Joint &joint) noexcept
Gets the definition data for the given joint.
Definition: RopeJointConf.cpp:46
playrho::d2::GetBodyB
BodyID GetBodyB(const Contact &contact) noexcept
Gets the body B ID of the given contact.
Definition: Contact.hpp:588
playrho::d2::GetCollideConnected
bool GetCollideConnected(const Joint &object) noexcept
Gets collide connected.
Definition: Joint.hpp:293
playrho::d2::GetBodyA
BodyID GetBodyA(const Contact &contact) noexcept
Gets the body A ID of the given contact.
Definition: Contact.hpp:581
playrho::d2::GetLimitState
LimitState GetLimitState(const Joint &object)
Definition: Joint.cpp:631
playrho::d2::CreateJoint
JointID CreateJoint(WorldImpl &world, const Joint &def)
Creates a new joint.
Definition: WorldImplJoint.cpp:47
playrho::d2::GetLocalAnchorB
Length2 GetLocalAnchorB(const Joint &object)
Get the anchor point on body-B in local coordinates.
Definition: Joint.cpp:96
playrho::d2::GetAngle
Angle GetAngle(const UnitVec value)
Gets the angle of the given unit vector.
Definition: Math.hpp:718
playrho::StepConf::doWarmStart
bool doWarmStart
Do warm start.
Definition: StepConf.hpp:269
playrho::InvalidBodyID
constexpr auto InvalidBodyID
Invalid body ID value.
Definition: BodyID.hpp:33
playrho::AngularMomentum
PLAYRHO_QUANTITY(boost::units::si::angular_momentum) AngularMomentum
Angular momentum quantity.
Definition: Units.hpp:304
playrho::StepConf
Step configuration.
Definition: StepConf.hpp:42
playrho::d2::GetAngularReaction
constexpr AngularMomentum GetAngularReaction(const DistanceJointConf &) noexcept
Gets the current angular reaction for the given configuration.
Definition: DistanceJointConf.hpp:170
playrho::d2::CreateFixture
FixtureID CreateFixture(World &world, FixtureConf def, bool resetMassData)
Creates a fixture within the specified world.
Definition: WorldFixture.cpp:48
playrho::Real
float Real
Real-number type.
Definition: Real.hpp:69
playrho::d2::DiskShapeConf::UseRadius
constexpr DiskShapeConf & UseRadius(NonNegative< Length > r) noexcept
Uses the given value as the radius.
Definition: DiskShapeConf.hpp:65
playrho::d2::BodyConf
Configuration for a body.
Definition: BodyConf.hpp:50
playrho::d2::GetLocation
constexpr Length2 GetLocation(const Transformation &value) noexcept
Gets the location information from the given transformation.
Definition: Transformation.hpp:69
playrho::d2::RopeJointConf
Rope joint definition.
Definition: RopeJointConf.hpp:57
playrho::BodyID
detail::IndexingNamedType< BodyCounter, struct BodyIdentifier > BodyID
Identifier for bodies.
Definition: BodyID.hpp:30
playrho::Vector
Vector.
Definition: Vector.hpp:49
playrho::d2::GetAnchorA
Length2 GetAnchorA(const World &world, JointID id)
Get the anchor point on body-A in world coordinates.
Definition: WorldJoint.cpp:208
playrho::d2::GetType
TypeID GetType(const Shape &shape) noexcept
Gets the type info of the use of the given shape.
Definition: Shape.hpp:329
playrho::d2::GetLinearReaction
constexpr Momentum2 GetLinearReaction(const DistanceJointConf &object) noexcept
Gets the current linear reaction for the given configuration.
Definition: DistanceJointConf.hpp:163
playrho::d2::RopeJointConf::localAnchorA
Length2 localAnchorA
The local anchor point relative to body A's origin.
Definition: RopeJointConf.hpp:79
playrho::d2::Joint
A joint-like constraint on one or more bodies.
Definition: Joint.hpp:144
playrho::d2::RopeJointConf::maxLength
Length maxLength
The maximum length of the rope.
Definition: RopeJointConf.hpp:85
playrho::d2::Shape
Shape.
Definition: Shape.hpp:183
playrho::GetY
constexpr auto & GetY(T &value)
Gets the "Y" element of the given value - i.e. the second element.
Definition: Math.hpp:73
playrho::Length2
Vector2< Length > Length2
2-element vector of Length quantities.
Definition: Vector2.hpp:43
playrho::d2::Step
StepStats Step(WorldImpl &world, const StepConf &conf)
Steps the given world the specified amount.
Definition: WorldImplMisc.cpp:85
playrho::d2::GetMaxLength
constexpr auto GetMaxLength(const RopeJointConf &object) noexcept
Free function for getting the maximum length value of the given configuration.
Definition: RopeJointConf.hpp:165
playrho::d2::DiskShapeConf
Disk shape configuration.
Definition: DiskShapeConf.hpp:42