Skip to content

Commit

Permalink
Replaces location & angle members of BodyConf with sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-langholtz committed Dec 19, 2023
1 parent 758dbf8 commit a709055
Show file tree
Hide file tree
Showing 54 changed files with 422 additions and 493 deletions.
5 changes: 3 additions & 2 deletions Benchmark/BenchmarkMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include <playrho/d2/DynamicTree.hpp>
#include <playrho/d2/Joint.hpp>
#include <playrho/d2/Manifold.hpp>
#include <playrho/d2/Position.hpp>
#include <playrho/d2/ShapeSeparation.hpp>
#include <playrho/d2/World.hpp>
#include <playrho/d2/WorldBody.hpp> // for GetAwakeCount
Expand Down Expand Up @@ -1669,8 +1670,8 @@ static playrho::d2::Position GetPositionFloorNormal(playrho::d2::Position pos0,
const auto da = pos1.angular - pos0.angular;
const auto na =
pos0.angular + (da - twoPi * floor((da + playrho::Pi * playrho::Radian) * rTwoPi)) * beta;
return {pos0.linear + (pos1.linear - pos0.linear) * beta,
na - twoPi * floor((na + playrho::Pi * playrho::Radian) * rTwoPi)};
return playrho::d2::Position{pos0.linear + (pos1.linear - pos0.linear) * beta,
na - twoPi * floor((na + playrho::Pi * playrho::Radian) * rTwoPi)};
}

static void GetPositionNaively(benchmark::State& state)
Expand Down
50 changes: 31 additions & 19 deletions Library/include/playrho/d2/BodyConf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <playrho/Vector2.hpp>

#include <playrho/d2/Position.hpp>
#include <playrho/d2/Sweep.hpp>
#include <playrho/d2/Transformation.hpp>
#include <playrho/d2/Velocity.hpp>

Expand All @@ -53,11 +54,8 @@ struct BodyConf {
/// @brief Default body type.
static constexpr auto DefaultBodyType = BodyType::Static;

/// @brief Default location.
static constexpr auto DefaultLocation = Length2{};

/// @brief Default angle.
static constexpr auto DefaultAngle = 0_deg;
/// @brief Default sweep.
static constexpr auto DefaultSweep = Sweep{};

/// @brief Default linear velocity.
static constexpr auto DefaultLinearVelocity = LinearVelocity2{};
Expand Down Expand Up @@ -106,6 +104,9 @@ struct BodyConf {
/// @brief Use the given type.
constexpr BodyConf& Use(BodyType t) noexcept;

/// @brief Use the given sweep.
constexpr BodyConf& Use(const Sweep& v) noexcept;

/// @brief Use the given location.
constexpr BodyConf& UseLocation(const Length2& l) noexcept;

Expand Down Expand Up @@ -172,12 +173,10 @@ struct BodyConf {
/// @note If a dynamic body would have zero mass, the mass is set to one.
BodyType type = DefaultBodyType;

/// The world location of the body. Avoid creating bodies at the origin
/// since this can lead to many overlapping shapes.
Length2 location = DefaultLocation;

/// The world angle of the body.
Angle angle = DefaultAngle;
/// @brief The sweep of the body.
/// @details This establishes a body's location and angle.
/// @note Avoid creating bodies at the origin since this can lead to many overlapping shapes.
Sweep sweep = DefaultSweep;

/// The linear velocity of the body's origin in world co-ordinates (in m/s).
LinearVelocity2 linearVelocity = DefaultLinearVelocity;
Expand Down Expand Up @@ -242,22 +241,27 @@ constexpr BodyConf& BodyConf::Use(BodyType t) noexcept
return *this;
}

constexpr BodyConf& BodyConf::Use(const Sweep& v) noexcept
{
sweep = v;
return *this;
}

constexpr BodyConf& BodyConf::UseLocation(const Length2& l) noexcept
{
location = l;
sweep = Sweep{Position{l, sweep.pos0.angular}};
return *this;
}

constexpr BodyConf& BodyConf::UseAngle(Angle a) noexcept
{
angle = a;
sweep = Sweep{Position{sweep.pos0.linear, a}};
return *this;
}

constexpr BodyConf& BodyConf::Use(const Position& v) noexcept
{
location = v.linear;
angle = v.angular;
sweep = Sweep{v};
return *this;
}

Expand Down Expand Up @@ -372,20 +376,28 @@ BodyConf GetBodyConf(const Body& body);
/// @relatedalso BodyConf
Transformation GetTransformation(const BodyConf& conf) noexcept;

/// @brief Gets the location of the given configuration.
/// @relatedalso BodyConf
constexpr auto GetLocation(const BodyConf& conf) noexcept
-> Length2
{
return conf.sweep.pos0.linear;
}

/// @brief Gets the angle of the given configuration.
/// @relatedalso BodyConf
constexpr Angle GetAngle(const BodyConf& conf) noexcept
constexpr auto GetAngle(const BodyConf& conf) noexcept
-> Angle
{
return conf.angle;
return conf.sweep.pos0.angular;
}

/// @brief Operator equals.
/// @relatedalso BodyConf
constexpr bool operator==(const BodyConf& lhs, const BodyConf& rhs) noexcept
{
return lhs.type == rhs.type && //
lhs.location == rhs.location && //
lhs.angle == rhs.angle && //
lhs.sweep == rhs.sweep && //
lhs.linearVelocity == rhs.linearVelocity && //
lhs.angularVelocity == rhs.angularVelocity && //
lhs.linearAcceleration == rhs.linearAcceleration && //
Expand Down
29 changes: 24 additions & 5 deletions Library/include/playrho/d2/Position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,39 @@
/// @file
/// @brief Definition of the @c Position class and closely related code.

#include <type_traits>

#include <playrho/Real.hpp>
#include <playrho/Templates.hpp>
#include <playrho/Units.hpp>
#include <playrho/Vector2.hpp>

namespace playrho {
namespace d2 {
namespace playrho::d2 {

/// @brief 2-D positional data structure.
/// @details A 2-element length and angle pair suitable for representing a linear and
/// angular position in 2-D.
struct Position {
Length2 linear; ///< Linear position.
Angle angular; ///< Angular position.
/// @brief Default constructor.
constexpr Position() noexcept = default;

/// @brief Initializing constructor.
constexpr explicit Position(Length2 l, Angle a = 0_deg) noexcept:
linear{l}, angular{a}
{
// Intentionally empty.
}

Length2 linear{}; ///< Linear position.
Angle angular{}; ///< Angular position.
};

// Assert some expected traits...
static_assert(std::is_nothrow_default_constructible_v<Position>);
static_assert(std::is_nothrow_copy_constructible_v<Position>);
static_assert(std::is_nothrow_move_constructible_v<Position>);
static_assert(std::is_nothrow_destructible_v<Position>);

/// @brief Equality operator.
/// @relatedalso Position
constexpr bool operator==(const Position& lhs, const Position& rhs)
Expand Down Expand Up @@ -114,7 +131,9 @@ constexpr Position operator*(const Real scalar, const Position& pos)
return Position{pos.linear * scalar, pos.angular * scalar};
}

} // namespace d2
} // namespace playrho::d2

namespace playrho {

/// @brief Determines if the given value is valid.
/// @relatedalso d2::Position
Expand Down
5 changes: 2 additions & 3 deletions Library/source/playrho/d2/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,14 @@ Body::FlagsType Body::GetFlags(const BodyConf& bd) noexcept

Body::Body(const BodyConf& bd)
: m_xf{::playrho::d2::GetTransformation(bd)},
m_sweep{Position{bd.location, bd.angle}},
m_sweep{bd.sweep},
m_flags{GetFlags(bd)},
m_invMass{(bd.type == playrho::BodyType::Dynamic) ? InvMass{Real{1} / Kilogram} : InvMass{}},
m_linearDamping{bd.linearDamping},
m_angularDamping{bd.angularDamping},
m_shapes(bd.shapes.begin(), bd.shapes.end())
{
assert(IsValid(bd.location));
assert(IsValid(bd.angle));
assert(IsValid(bd.sweep));
assert(IsValid(bd.linearVelocity));
assert(IsValid(bd.angularVelocity));
assert(IsValid(m_xf));
Expand Down
5 changes: 2 additions & 3 deletions Library/source/playrho/d2/BodyConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ BodyConf GetBodyConf(const Body& body)
{
auto def = BodyConf{};
def.type = GetType(body);
def.location = GetLocation(body);
def.angle = GetAngle(body);
def.sweep = GetSweep(body);
def.linearVelocity = GetLinearVelocity(body);
def.angularVelocity = GetAngularVelocity(body);
def.linearAcceleration = GetLinearAcceleration(body);
Expand All @@ -50,7 +49,7 @@ BodyConf GetBodyConf(const Body& body)

Transformation GetTransformation(const BodyConf& conf) noexcept
{
return {conf.location, UnitVec::Get(conf.angle)};
return {conf.sweep.pos0.linear, UnitVec::Get(conf.sweep.pos0.angular)};
}

} // namespace playrho::d2
15 changes: 7 additions & 8 deletions Testbed/Tests/ApplyForce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,13 @@ class ApplyForce : public Test
conf.density = 2_kgpm2;
const auto poly2 = PolygonShapeConf(vertices, conf);

auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.angularDamping = 2_Hz;
bd.linearDamping = 0.5_Hz;
bd.location = Length2{0_m, 2_m};
bd.angle = Pi * 1_rad;
bd.allowSleep = false;
m_body = CreateBody(GetWorld(), bd);
m_body = CreateBody(GetWorld(), BodyConf{}
.Use(BodyType::Dynamic)
.UseAngularDamping(2_Hz)
.UseLinearDamping(0.5_Hz)
.UseLocation(Length2{0_m, 2_m})
.UseAngle(Pi * 1_rad)
.UseAllowSleep(false));
Attach(GetWorld(), m_body, CreateShape(GetWorld(), poly1));
Attach(GetWorld(), m_body, CreateShape(GetWorld(), poly2));
}
Expand Down
6 changes: 3 additions & 3 deletions Testbed/Tests/BasicSliderCrank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BasicSliderCrank : public Test
{
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.location = Vec2(-8.0f, 20.0f) * 1_m;
bd.UseLocation(Vec2(-8.0f, 20.0f) * 1_m);
bd.linearAcceleration = GetGravity();
const auto body = CreateBody(GetWorld(), bd);
auto conf = PolygonShapeConf{};
Expand All @@ -55,7 +55,7 @@ class BasicSliderCrank : public Test
{
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.location = Vec2(4.0f, 20.0f) * 1_m;
bd.UseLocation(Vec2(4.0f, 20.0f) * 1_m);
bd.linearAcceleration = GetGravity();
const auto body = CreateBody(GetWorld(), bd);
auto conf = PolygonShapeConf{};
Expand All @@ -72,7 +72,7 @@ class BasicSliderCrank : public Test
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.fixedRotation = true;
bd.location = Vec2(12.0f, 20.0f) * 1_m;
bd.UseLocation(Vec2(12.0f, 20.0f) * 1_m);
bd.linearAcceleration = GetGravity();
const auto body = CreateBody(GetWorld(), bd);
const auto conf = PolygonShapeConf{}.UseDensity(2_kgpm2).SetAsBox(3_m, 3_m);
Expand Down
7 changes: 3 additions & 4 deletions Testbed/Tests/Breakable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class Breakable : public Test
BodyConf bd;
bd.type = BodyType::Dynamic;
bd.linearAcceleration = GetGravity();
bd.location = Length2{0_m, 40_m};
bd.angle = Pi * 0.25_rad;
bd.Use(Position{Length2{0_m, 40_m}, Pi * 0.25_rad});
m_body1 = CreateBody(GetWorld(), bd);
Attach(GetWorld(), m_body1, m_shape1);
Attach(GetWorld(), m_body1, m_shape2);
Expand Down Expand Up @@ -99,8 +98,8 @@ class Breakable : public Test
BodyConf bd;
bd.type = BodyType::Dynamic;
bd.linearAcceleration = GetGravity();
bd.location = GetLocation(GetWorld(), m_body1);
bd.angle = GetAngle(GetWorld(), m_body1);
bd.UseLocation(GetLocation(GetWorld(), m_body1));
bd.UseAngle(GetAngle(GetWorld(), m_body1));

const auto body2 = CreateBody(GetWorld(), bd);
Attach(GetWorld(), body2, m_shape2);
Expand Down
8 changes: 3 additions & 5 deletions Testbed/Tests/BulletTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class BulletTest : public Test
BulletTest()
{
{
BodyConf bd;
bd.location = Length2{};
const auto body = CreateBody(GetWorld(), bd);
const auto body = CreateBody(GetWorld(), BodyConf{}.UseLocation(Length2{}));
Attach(GetWorld(), body,
CreateShape(GetWorld(),
EdgeShapeConf{Vec2(-10.0f, 0.0f) * 1_m, Vec2(10.0f, 0.0f) * 1_m}));
Expand All @@ -46,7 +44,7 @@ class BulletTest : public Test
BodyConf bd;
bd.type = BodyType::Dynamic;
bd.linearAcceleration = GetGravity();
bd.location = Vec2(0.0f, 4.0f) * 1_m;
bd.UseLocation(Vec2(0.0f, 4.0f) * 1_m);

auto conf = PolygonShapeConf{};
conf.UseDensity(1_kgpm2);
Expand All @@ -60,7 +58,7 @@ class BulletTest : public Test

// m_x = RandomFloat(-1.0f, 1.0f);
m_x = 0.20352793f;
bd.location = Vec2(m_x, 10.0f) * 1_m;
bd.UseLocation(Vec2(m_x, 10.0f) * 1_m);
bd.bullet = true;

m_bullet = CreateBody(GetWorld(), bd);
Expand Down
12 changes: 6 additions & 6 deletions Testbed/Tests/Cantilever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Cantilever : public Test
for (auto i = 0; i < e_count; ++i) {
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.location = Vec2(-14.5f + 1.0f * i, 5.0f) * 1_m;
bd.UseLocation(Vec2(-14.5f + 1.0f * i, 5.0f) * 1_m);
const auto body = CreateBody(GetWorld(), bd);
Attach(GetWorld(), body, shape);

Expand All @@ -67,7 +67,7 @@ class Cantilever : public Test
for (auto i = 0; i < 3; ++i) {
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.location = Vec2(-14.0f + 2.0f * i, 15.0f) * 1_m;
bd.UseLocation(Vec2(-14.0f + 2.0f * i, 15.0f) * 1_m);
const auto body = CreateBody(GetWorld(), bd);
Attach(GetWorld(), body, shape);

Expand All @@ -89,7 +89,7 @@ class Cantilever : public Test
for (auto i = 0; i < e_count; ++i) {
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.location = Vec2(-4.5f + 1.0f * i, 5.0f) * 1_m;
bd.UseLocation(Vec2(-4.5f + 1.0f * i, 5.0f) * 1_m);
const auto body = CreateBody(GetWorld(), bd);
Attach(GetWorld(), body, shape);
if (i > 0) {
Expand All @@ -108,7 +108,7 @@ class Cantilever : public Test
for (auto i = 0; i < e_count; ++i) {
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.location = Vec2(5.5f + 1.0f * i, 10.0f) * 1_m;
bd.UseLocation(Vec2(5.5f + 1.0f * i, 10.0f) * 1_m);
const auto body = CreateBody(GetWorld(), bd);
Attach(GetWorld(), body, shape);
if (i > 0) {
Expand All @@ -129,7 +129,7 @@ class Cantilever : public Test
for (auto i = 0; i < 2; ++i) {
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.location = Vec2(-8.0f + 8.0f * i, 12.0f) * 1_m;
bd.UseLocation(Vec2(-8.0f + 8.0f * i, 12.0f) * 1_m);
Attach(GetWorld(), CreateBody(GetWorld(), bd), polyshape);
}

Expand All @@ -139,7 +139,7 @@ class Cantilever : public Test
for (auto i = 0; i < 2; ++i) {
auto bd = BodyConf{};
bd.type = BodyType::Dynamic;
bd.location = Vec2(-6.0f + 6.0f * i, 10.0f) * 1_m;
bd.UseLocation(Vec2(-6.0f + 6.0f * i, 10.0f) * 1_m);
Attach(GetWorld(), CreateBody(GetWorld(), bd), circleshape);
}

Expand Down
Loading

0 comments on commit a709055

Please sign in to comment.