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

This is the googletest based unit testing file for the free function interfaces to playrho::d2::World fixture member functions and additional functionality.

/*
* 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/World.hpp>
#include <PlayRho/Dynamics/WorldImpl.hpp>
#include <PlayRho/Dynamics/WorldBody.hpp>
#include <PlayRho/Dynamics/WorldMisc.hpp>
#include <PlayRho/Dynamics/WorldFixture.hpp>
#include <PlayRho/Collision/Shapes/DiskShapeConf.hpp>
#include <PlayRho/Collision/Shapes/ChainShapeConf.hpp>
using namespace playrho;
using namespace playrho::d2;
TEST(WorldFixture, CreateDestroy)
{
auto world = World{};
EXPECT_EQ(GetFixtureRange(world), 0u);
EXPECT_THROW(CreateFixture(world, BodyID(0u), Shape{DiskShapeConf{}}), std::out_of_range);
EXPECT_THROW(Destroy(world, FixtureID(0u)), std::out_of_range);
const auto body = CreateBody(world);
auto fixture = FixtureID(0u);
EXPECT_NO_THROW(fixture = CreateFixture(world, body, Shape{DiskShapeConf{}}));
const auto fixtures = std::vector<FixtureID>{};
using ValueType = std::remove_cv_t<std::remove_reference_t<decltype(GetFixtures(world, body))>>;
auto fixturesRange = ValueType();
EXPECT_NO_THROW(fixturesRange = GetFixtures(world, body));
EXPECT_EQ(GetFixtureRange(world), 1u);
ASSERT_EQ(size(fixturesRange), 1u);
EXPECT_EQ(*begin(fixturesRange), fixture);
EXPECT_NO_THROW(Destroy(world, fixture));
EXPECT_NO_THROW(fixturesRange = GetFixtures(world, body));
EXPECT_EQ(size(fixturesRange), 0u);
}
TEST(WorldFixture, SetFilterData)
{
auto world = World{};
const auto body = CreateBody(world);
const auto fixture = CreateFixture(world, body, Shape{DiskShapeConf{}});
const auto origFilter = Filter{1u, 2u, 3u};
auto copyFilter = Filter{};
EXPECT_NO_THROW(SetFilterData(world, fixture, origFilter));
EXPECT_NO_THROW(copyFilter = GetFilterData(world, fixture));
EXPECT_EQ(origFilter.categoryBits, copyFilter.categoryBits);
EXPECT_EQ(origFilter.maskBits, copyFilter.maskBits);
EXPECT_EQ(origFilter.groupIndex, copyFilter.groupIndex);
}
TEST(WorldFixture, CreateMatchesConf)
{
const auto density = 2_kgpm2;
const auto friction = Real(0.5);
const auto restitution = Real(0.4);
const auto isSensor = true;
const auto conf = DiskShapeConf{}.UseFriction(friction).UseRestitution(restitution).UseDensity(density);
const auto shapeA = Shape(conf);
auto def = FixtureConf{};
def.isSensor = isSensor;
World world;
const auto body = CreateBody(world);
const auto fixture = CreateFixture(world, body, shapeA, def);
EXPECT_EQ(GetBody(world, fixture), body);
EXPECT_EQ(GetShape(world, fixture), shapeA);
EXPECT_EQ(GetDensity(world, fixture), density);
EXPECT_EQ(GetFriction(world, fixture), friction);
EXPECT_EQ(GetRestitution(world, fixture), restitution);
EXPECT_EQ(IsSensor(world, fixture), isSensor);
}
TEST(WorldFixture, SetSensor)
{
const auto shapeA = Shape{DiskShapeConf{}};
const auto bodyCtrPos = Length2(1_m, 2_m);
World world;
const auto body = CreateBody(world, BodyConf{}.UseLocation(bodyCtrPos));
const auto fixture = CreateFixture(world, body, shapeA);
SetSensor(world, fixture, true);
EXPECT_TRUE(IsSensor(world, fixture));
SetSensor(world, fixture, true);
EXPECT_TRUE(IsSensor(world, fixture));
SetSensor(world, fixture, false);
EXPECT_FALSE(IsSensor(world, fixture));
}
TEST(WorldFixture, TestPointFreeFunction)
{
const auto shapeA = Shape{DiskShapeConf{}};
const auto bodyCtrPos = Length2(1_m, 2_m);
World world;
const auto body = CreateBody(world, BodyConf{}.UseLocation(bodyCtrPos));
const auto fixture = CreateFixture(world, body, shapeA);
EXPECT_TRUE(TestPoint(world, fixture, bodyCtrPos));
EXPECT_FALSE(TestPoint(world, fixture, Length2{}));
}
playrho::d2::GetShape
const Shape & GetShape(const FixtureConf &conf) noexcept
Gets the shape of the given configuration.
Definition: FixtureConf.hpp:126
playrho::d2::CreateBody
BodyID CreateBody(World &world, const BodyConf &def)
Creates a rigid body with the given configuration.
Definition: WorldBody.cpp:58
playrho::d2
Name space for 2-dimensionally related PlayRho names.
Definition: AABB.cpp:34
playrho::d2::GetDensity
NonNegative< AreaDensity > GetDensity(const Shape &shape) noexcept
Gets the density of the given shape.
Definition: Shape.hpp:304
playrho::d2::IsSensor
bool IsSensor(const Contact &contact) noexcept
Gets whether the given contact is for sensors or not.
Definition: Contact.hpp:665
playrho::d2::ShapeBuilder::UseRestitution
constexpr ConcreteConf & UseRestitution(Finite< Real > value) noexcept
Uses the given restitution.
Definition: ShapeConf.hpp:103
playrho::d2::FixtureConf::isSensor
bool isSensor
A sensor shape collects contact information but never generates a collision response.
Definition: FixtureConf.hpp:97
playrho::d2::SetFilterData
void SetFilterData(FixtureConf &conf, Filter value) noexcept
Sets the filter-data of the given configuration.
Definition: FixtureConf.hpp:175
playrho::d2::ShapeBuilder::UseDensity
constexpr ConcreteConf & UseDensity(NonNegative< AreaDensity > value) noexcept
Uses the given density.
Definition: ShapeConf.hpp:111
playrho
Name space for all PlayRho related names.
Definition: AABB.cpp:33
playrho::d2::GetFixtureRange
FixtureCounter GetFixtureRange(const World &world) noexcept
Gets the extent of the currently valid fixture range.
Definition: WorldFixture.cpp:33
playrho::d2::TestPoint
bool TestPoint(const DistanceProxy &proxy, Length2 point) noexcept
Tests a point for containment in the given distance proxy.
Definition: DistanceProxy.cpp:114
playrho::d2::World
Definition of an independent and simulatable "world".
Definition: World.hpp:129
playrho::d2::GetFixtures
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
playrho::d2::FixtureConf
Fixture definition.
Definition: FixtureConf.hpp:46
playrho::d2::GetFriction
Real GetFriction(const Shape &shape) noexcept
Gets the coefficient of friction.
Definition: Shape.hpp:294
playrho::d2::size
std::size_t size(const DynamicTree &tree) noexcept
Gets the "size" of the given tree.
Definition: DynamicTree.hpp:773
playrho::d2::BodyConf::UseLocation
constexpr BodyConf & UseLocation(Length2 l) noexcept
Use the given location.
Definition: BodyConf.hpp:172
playrho::d2::ShapeBuilder::UseFriction
constexpr ConcreteConf & UseFriction(NonNegative< Real > value) noexcept
Uses the given friction.
Definition: ShapeConf.hpp:95
playrho::d2::GetFilterData
Filter GetFilterData(const FixtureConf &conf) noexcept
Gets the filter-data of the given configuration.
Definition: FixtureConf.hpp:168
playrho::FixtureID
detail::IndexingNamedType< FixtureCounter, struct FixtureIdentifier > FixtureID
Fixture identifier.
Definition: FixtureID.hpp:30
playrho::d2::Destroy
void Destroy(World &world, BodyID id)
Destroys the identified body.
Definition: WorldBody.cpp:73
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::BodyConf
Configuration for a body.
Definition: BodyConf.hpp:50
playrho::BodyID
detail::IndexingNamedType< BodyCounter, struct BodyIdentifier > BodyID
Identifier for bodies.
Definition: BodyID.hpp:30
playrho::d2::GetRestitution
Real GetRestitution(const Shape &shape) noexcept
Gets the coefficient of restitution value of the given shape.
Definition: Shape.hpp:299
playrho::Vector
Vector.
Definition: Vector.hpp:49
playrho::d2::GetBody
BodyID GetBody(const FixtureConf &conf) noexcept
Gets the body of the given configuration.
Definition: FixtureConf.hpp:119
playrho::d2::SetSensor
void SetSensor(FixtureConf &conf, bool value) noexcept
Sets whether or not the given configuration is a sensor.
Definition: FixtureConf.hpp:161
playrho::d2::Shape
Shape.
Definition: Shape.hpp:183
playrho::Filter
A holder for contact filtering data.
Definition: Filter.hpp:33
playrho::Length2
Vector2< Length > Length2
2-element vector of Length quantities.
Definition: Vector2.hpp:43
playrho::d2::DiskShapeConf
Disk shape configuration.
Definition: DiskShapeConf.hpp:42