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

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

/*
* 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/MotorJointConf.hpp>
#include <PlayRho/Dynamics/Joints/Joint.hpp>
#include <PlayRho/Dynamics/BodyConf.hpp>
#include <PlayRho/Dynamics/World.hpp>
#include <PlayRho/Dynamics/WorldBody.hpp>
#include <PlayRho/Dynamics/WorldJoint.hpp>
#include <PlayRho/Dynamics/WorldFixture.hpp>
#include <PlayRho/Dynamics/WorldMisc.hpp>
#include <PlayRho/Dynamics/StepConf.hpp>
#include <PlayRho/Collision/Shapes/DiskShapeConf.hpp>
using namespace playrho;
using namespace playrho::d2;
TEST(MotorJointConf, 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(MotorJointConf), std::size_t(92));
break;
case 8:
EXPECT_EQ(sizeof(MotorJointConf), std::size_t(176));
break;
case 16:
EXPECT_EQ(sizeof(MotorJointConf), std::size_t(352));
break;
default:
FAIL();
break;
}
}
TEST(MotorJointConf, DefaultConstruction)
{
EXPECT_EQ(def.bodyA, InvalidBodyID);
EXPECT_EQ(def.bodyB, InvalidBodyID);
EXPECT_EQ(def.collideConnected, false);
EXPECT_EQ(def.linearOffset, (Length2{}));
EXPECT_EQ(def.angularOffset, 0_deg);
EXPECT_EQ(def.maxForce, 1_N);
EXPECT_EQ(def.maxTorque, 1_Nm);
EXPECT_EQ(def.correctionFactor, Real(0.3));
}
TEST(MotorJointConf, BuilderConstruction)
{
const auto bodyA = static_cast<BodyID>(0x1);
const auto bodyB = static_cast<BodyID>(0x2);
const auto collideConnected = true;
const auto linearOffset = Length2{2_m, 3_m};
const auto angularOffset = 33_rad;
const auto maxForce = 22_N;
const auto maxTorque = 31_Nm;
const auto correctionFactor = Real(0.44f);
const auto def = MotorJointConf{}
.UseBodyA(bodyA)
.UseBodyB(bodyB)
.UseCollideConnected(collideConnected)
.UseLinearOffset(linearOffset)
.UseAngularOffset(angularOffset)
.UseMaxForce(maxForce)
.UseMaxTorque(maxTorque)
.UseCorrectionFactor(correctionFactor);
EXPECT_EQ(def.bodyA, bodyA);
EXPECT_EQ(def.bodyB, bodyB);
EXPECT_EQ(def.collideConnected, collideConnected);
EXPECT_EQ(def.linearOffset, linearOffset);
EXPECT_EQ(def.angularOffset, angularOffset);
EXPECT_EQ(def.maxForce, maxForce);
EXPECT_EQ(def.maxTorque, maxTorque);
EXPECT_EQ(def.correctionFactor, correctionFactor);
}
TEST(MotorJoint, Construction)
{
auto world = World{};
const auto b0 = CreateBody(world);
const auto b1 = CreateBody(world);
auto def = GetMotorJointConf(world, b0, b1);
const auto jointID = CreateJoint(world, def);
EXPECT_EQ(GetType(world, jointID), GetTypeID<MotorJointConf>());
EXPECT_EQ(GetBodyA(world, jointID), def.bodyA);
EXPECT_EQ(GetBodyB(world, jointID), def.bodyB);
EXPECT_EQ(GetCollideConnected(world, jointID), def.collideConnected);
EXPECT_EQ(GetLinearReaction(world, jointID), Momentum2{});
EXPECT_EQ(GetAngularReaction(world, jointID), AngularMomentum{0});
EXPECT_EQ(GetLinearOffset(world, jointID), def.linearOffset);
EXPECT_EQ(GetAngularOffset(world, jointID), def.angularOffset);
const auto conf = TypeCast<MotorJointConf>(GetJoint(world, jointID));
EXPECT_EQ(GetMaxForce(conf), def.maxForce);
EXPECT_EQ(GetMaxTorque(conf), def.maxTorque);
EXPECT_EQ(GetCorrectionFactor(conf), def.correctionFactor);
EXPECT_EQ(GetMaxForce(Joint{conf}), def.maxForce);
EXPECT_EQ(GetMaxTorque(Joint{conf}), def.maxTorque);
}
TEST(MotorJoint, ShiftOrigin)
{
auto world = World{};
const auto b0 = CreateBody(world);
const auto b1 = CreateBody(world);
auto def = GetMotorJointConf(world, b0, b1);
const auto joint = CreateJoint(world, def);
const auto newOrigin = Length2{1_m, 1_m};
EXPECT_FALSE(ShiftOrigin(world, joint, newOrigin));
}
TEST(MotorJoint, SetCorrectionFactor)
{
auto world = World{};
const auto b0 = CreateBody(world);
const auto b1 = CreateBody(world);
auto def = GetMotorJointConf(world, b0, b1);
const auto jointID = CreateJoint(world, def);
auto conf = TypeCast<MotorJointConf>(GetJoint(world, jointID));
ASSERT_EQ(GetCorrectionFactor(conf), def.correctionFactor);
ASSERT_EQ(Real(0.3), def.correctionFactor);
EXPECT_NO_THROW(SetCorrectionFactor(conf, Real(0.9)));
EXPECT_EQ(GetCorrectionFactor(conf), Real(0.9));
EXPECT_NO_THROW(SetJoint(world, jointID, conf));
auto conf2 = TypeCast<MotorJointConf>(GetJoint(world, jointID));
EXPECT_EQ(GetCorrectionFactor(conf2), Real(0.9));
}
TEST(MotorJoint, GetMotorJointConf)
{
auto world = World{};
const auto b0 = CreateBody(world);
const auto b1 = CreateBody(world);
auto def = GetMotorJointConf(world, b0, b1);
const auto jointID = CreateJoint(world, def);
ASSERT_EQ(GetType(world, jointID), GetTypeID<MotorJointConf>());
ASSERT_EQ(GetBodyA(world, jointID), def.bodyA);
ASSERT_EQ(GetBodyB(world, jointID), def.bodyB);
ASSERT_EQ(GetCollideConnected(world, jointID), def.collideConnected);
ASSERT_EQ(GetLinearOffset(world, jointID), def.linearOffset);
ASSERT_EQ(GetAngularOffset(world, jointID), def.angularOffset);
const auto conf = TypeCast<MotorJointConf>(GetJoint(world, jointID));
ASSERT_EQ(GetMaxForce(conf), def.maxForce);
ASSERT_EQ(GetMaxTorque(conf), def.maxTorque);
ASSERT_EQ(GetCorrectionFactor(conf), def.correctionFactor);
const auto cdef = GetMotorJointConf(GetJoint(world, jointID));
EXPECT_EQ(cdef.bodyA, b0);
EXPECT_EQ(cdef.bodyB, b1);
EXPECT_EQ(cdef.collideConnected, false);
EXPECT_EQ(cdef.linearOffset, (Length2{}));
EXPECT_EQ(cdef.angularOffset, 0_deg);
EXPECT_EQ(cdef.maxForce, 1_N);
EXPECT_EQ(cdef.maxTorque, 1_Nm);
EXPECT_EQ(cdef.correctionFactor, Real(0.3));
}
TEST(MotorJoint, 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 anchor = Length2(2_m, 1_m);
const auto jd = GetMotorJointConf(world, b1, b2);
const auto joint = CreateJoint(world, jd);
ASSERT_NE(joint, InvalidJointID);
EXPECT_EQ(GetAnchorA(world, joint), p1);
EXPECT_EQ(GetAnchorB(world, joint), p2);
auto stepConf = StepConf{};
Step(world, stepConf);
EXPECT_NEAR(double(Real{GetX(GetLocation(world, b1)) / Meter}), -1.0, 0.001);
EXPECT_NEAR(double(Real{GetY(GetLocation(world, b1)) / Meter}), 0.0, 0.001);
EXPECT_NEAR(double(Real{GetX(GetLocation(world, b2)) / Meter}), +1.0, 0.01);
EXPECT_NEAR(double(Real{GetY(GetLocation(world, b2)) / Meter}), 0.0, 0.01);
EXPECT_EQ(GetAngle(world, b1), 0_deg);
EXPECT_EQ(GetAngle(world, b2), 0_deg);
stepConf.doWarmStart = false;
Step(world, stepConf);
EXPECT_NEAR(double(Real{GetX(GetLocation(world, b1)) / Meter}), -1.0, 0.001);
EXPECT_NEAR(double(Real{GetY(GetLocation(world, b1)) / Meter}), 0.0, 0.001);
EXPECT_NEAR(double(Real{GetX(GetLocation(world, b2)) / Meter}), +1.0, 0.01);
EXPECT_NEAR(double(Real{GetY(GetLocation(world, b2)) / Meter}), 0.0, 0.01);
EXPECT_EQ(GetAngle(world, b1), 0_deg);
EXPECT_EQ(GetAngle(world, b2), 0_deg);
}
TEST(MotorJoint, SetLinearOffset)
{
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 anchor = Length2(2_m, 1_m);
const auto jd = GetMotorJointConf(world, b1, b2);
const auto joint = CreateJoint(world, jd);
ASSERT_NE(joint, InvalidJointID);
EXPECT_EQ(GetAnchorA(world, joint), p1);
EXPECT_EQ(GetAnchorB(world, joint), p2);
const auto linearOffset = Length2{2_m, 1_m};
ASSERT_EQ(GetLinearOffset(world, joint), jd.linearOffset);
ASSERT_NE(jd.linearOffset, linearOffset);
SetLinearOffset(world, joint, linearOffset);
EXPECT_EQ(GetLinearOffset(world, joint), linearOffset);
}
TEST(MotorJoint, SetAngularOffset)
{
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 anchor = Length2(2_m, 1_m);
const auto jd = GetMotorJointConf(world, b1, b2);
const auto joint = CreateJoint(world, jd);
ASSERT_NE(joint, InvalidJointID);
EXPECT_EQ(GetAnchorA(world, joint), p1);
EXPECT_EQ(GetAnchorB(world, joint), p2);
ASSERT_EQ(GetAngularOffset(world, joint), 0_deg);
SetAngularOffset(world, joint, 45_deg);
EXPECT_EQ(GetAngularOffset(world, joint), 45_deg);
}
{
auto conf = MotorJointConf{};
conf.angularMass = RotInertia{2_m2 * 3_kg / SquareRadian}; // L^2 M QP^-2
auto rotInertia = RotInertia{};
EXPECT_NO_THROW(rotInertia = GetAngularMass(Joint{conf}));
EXPECT_EQ(conf.angularMass, rotInertia);
}
TEST(MotorJointConf, EqualsOperator)
{
EXPECT_TRUE(MotorJointConf() == MotorJointConf());
{
auto conf = MotorJointConf{};
conf.linearOffset = Length2{1.2_m, -3_m};
EXPECT_TRUE(conf == conf);
EXPECT_FALSE(MotorJointConf() == conf);
}
{
auto conf = MotorJointConf{};
conf.angularOffset = 33_deg;
EXPECT_TRUE(conf == conf);
EXPECT_FALSE(MotorJointConf() == conf);
}
{
auto conf = MotorJointConf{};
conf.correctionFactor = Real(3.4);
EXPECT_TRUE(conf == conf);
EXPECT_FALSE(MotorJointConf() == conf);
}
{
auto conf = MotorJointConf{};
conf.angularError = 19_deg;
EXPECT_TRUE(conf == conf);
EXPECT_FALSE(MotorJointConf() == conf);
}
// TODO: test remaining fields.
}
TEST(MotorJointConf, NotEqualsOperator)
{
EXPECT_FALSE(MotorJointConf() != MotorJointConf());
{
auto conf = MotorJointConf{};
conf.maxForce = 2.5_N;
EXPECT_FALSE(conf != conf);
EXPECT_TRUE(MotorJointConf() != conf);
}
// TODO: test remaining fields.
}
{
EXPECT_STREQ(GetName(GetTypeID<MotorJointConf>()), "d2::MotorJointConf");
}
playrho::d2::GetMaxForce
constexpr auto GetMaxForce(const FrictionJointConf &object) noexcept
Free function for getting the max force value of the given configuration.
Definition: FrictionJointConf.hpp:177
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::JointBuilder::UseBodyA
constexpr reference UseBodyA(BodyID b) noexcept
Use value for body A setting.
Definition: JointConf.hpp:80
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::JointBuilder::UseCollideConnected
constexpr reference UseCollideConnected(bool v) noexcept
Use value for collide connected setting.
Definition: JointConf.hpp:94
playrho::d2::MotorJointConf::correctionFactor
Real correctionFactor
Position correction factor in the range [0,1].
Definition: MotorJointConf.hpp:110
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::d2::SetAngularOffset
void SetAngularOffset(Joint &object, Angle value)
Sets the angular offset property of the specified joint if its type has one.
Definition: Joint.cpp:621
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::SetLinearOffset
void SetLinearOffset(Joint &object, Length2 value)
Sets the linear offset property of the specified joint if its type has one.
Definition: Joint.cpp:602
playrho::d2::MotorJointConf::angularError
Angle angularError
Angular error.
Definition: MotorJointConf.hpp:116
playrho::d2::World
Definition of an independent and simulatable "world".
Definition: World.hpp:129
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::MotorJointConf::angularOffset
Angle angularOffset
Angle of body-B minus angle of body-A.
Definition: MotorJointConf.hpp:98
playrho::d2::MotorJointConf::maxForce
NonNegative< Force > maxForce
Maximum motor force.
Definition: MotorJointConf.hpp:104
playrho::d2::ShiftOrigin
constexpr bool ShiftOrigin(DistanceJointConf &, Length2) noexcept
Shifts the origin notion of the given configuration.
Definition: DistanceJointConf.hpp:177
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::Meter
constexpr auto Meter
Meter unit of Length.
Definition: Units.hpp:337
playrho::d2::MotorJointConf::linearOffset
Length2 linearOffset
Position of body-B minus the position of body-A, in body-A's frame.
Definition: MotorJointConf.hpp:95
playrho::d2::CreateJoint
JointID CreateJoint(WorldImpl &world, const Joint &def)
Creates a new joint.
Definition: WorldImplJoint.cpp:47
playrho::d2::SetJoint
void SetJoint(WorldImpl &world, JointID id, const Joint &def)
Sets the identified joint's new value.
Definition: WorldImplJoint.cpp:62
playrho::d2::SetCorrectionFactor
constexpr auto SetCorrectionFactor(MotorJointConf &object, Real value) noexcept
Free function for setting the correction factor value of the given configuration.
Definition: MotorJointConf.hpp:276
playrho::d2::GetAngle
Angle GetAngle(const UnitVec value)
Gets the angle of the given unit vector.
Definition: Math.hpp:718
playrho::d2::GetCorrectionFactor
constexpr auto GetCorrectionFactor(const MotorJointConf &object) noexcept
Free function for getting the correction factor value of the given configuration.
Definition: MotorJointConf.hpp:269
playrho::d2::GetLinearOffset
Length2 GetLinearOffset(const Joint &object)
Gets the linear offset property of the specified joint if its type has one.
Definition: Joint.cpp:593
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::GetAngularOffset
Angle GetAngularOffset(const Joint &object)
Gets the angular offset property of the specified joint if its type has one.
Definition: Joint.cpp:612
playrho::d2::GetAngularMass
RotInertia GetAngularMass(const Joint &object)
Gets the given joint's angular mass.
Definition: Joint.cpp:287
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::d2::MotorJointConf::angularMass
RotInertia angularMass
Angular mass.
Definition: MotorJointConf.hpp:118
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::SquareRadian
constexpr auto SquareRadian
Square radian unit type.
Definition: Units.hpp:379
playrho::d2::GetMaxTorque
constexpr auto GetMaxTorque(const FrictionJointConf &object) noexcept
Free function for getting the max torque value of the given configuration.
Definition: FrictionJointConf.hpp:191
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::RotInertia
PLAYRHO_QUANTITY(boost::units::si::moment_of_inertia) RotInertia
Rotational inertia quantity.
Definition: Units.hpp:274
playrho::detail::IndexingNamedType< BodyCounter, struct BodyIdentifier >
playrho::d2::JointBuilder::UseBodyB
constexpr reference UseBodyB(BodyID b) noexcept
Use value for body B setting.
Definition: JointConf.hpp:87
playrho::d2::GetMotorJointConf
MotorJointConf GetMotorJointConf(const Joint &joint) noexcept
Gets the definition data for the given joint.
Definition: MotorJointConf.cpp:67
playrho::d2::Joint
A joint-like constraint on one or more bodies.
Definition: Joint.hpp:144
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::d2::MotorJointConf::UseLinearOffset
constexpr auto & UseLinearOffset(Length2 v) noexcept
Uses the given linear offset value.
Definition: MotorJointConf.hpp:60
playrho::d2::Step
StepStats Step(WorldImpl &world, const StepConf &conf)
Steps the given world the specified amount.
Definition: WorldImplMisc.cpp:85
playrho::d2::MotorJointConf
Motor joint definition.
Definition: MotorJointConf.hpp:49
playrho::d2::DiskShapeConf
Disk shape configuration.
Definition: DiskShapeConf.hpp:42