Skip to content

Commit

Permalink
Fixes issue #8: Refactor joint definition structures to provide build…
Browse files Browse the repository at this point in the history
…er interface.
  • Loading branch information
louis-langholtz committed Jul 30, 2017
1 parent 4fb4df7 commit 4347e39
Show file tree
Hide file tree
Showing 28 changed files with 330 additions and 309 deletions.
9 changes: 9 additions & 0 deletions PlayRho/Common/Vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Vector.cpp
// PlayRho
//
// Created by Louis D. Langholtz on 7/29/17.
//
//

#include "Vector.hpp"
14 changes: 14 additions & 0 deletions PlayRho/Common/Vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Vector.hpp
// PlayRho
//
// Created by Louis D. Langholtz on 7/29/17.
//
//

#ifndef Vector_hpp
#define Vector_hpp

#include <stdio.h>

#endif /* Vector_hpp */
30 changes: 13 additions & 17 deletions PlayRho/Dynamics/Joints/DistanceJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using namespace playrho;
// x2 = x1 + h * v2

// 1-D mass-damper-spring system
// m (v2 - v1) + h * d * v2 + h * k *
// m (v2 - v1) + h * d * v2 + h * k *

// C = norm(p2 - p1) - L
// u = (p2 - p1) / norm(p2 - p1)
Expand All @@ -41,12 +41,11 @@ using namespace playrho;
// = invMass1 + invI1 * cross(r1, u)^2 + invMass2 + invI2 * cross(r2, u)^2

DistanceJointDef::DistanceJointDef(NonNull<Body*> bA, NonNull<Body*> bB,
const Length2D anchor1, const Length2D anchor2,
NonNegative<Frequency> freq, Real damp) noexcept:
JointDef{JointType::Distance, bA, bB},
localAnchorA{GetLocalPoint(*bA, anchor1)}, localAnchorB{GetLocalPoint(*bB, anchor2)},
length{GetLength(anchor2 - anchor1)},
frequency{freq}, dampingRatio{damp}
Length2D anchor1, Length2D anchor2) noexcept
: super{super{JointType::Distance}.UseBodyA(bA).UseBodyB(bB)},
localAnchorA{GetLocalPoint(*bA, anchor1)},
localAnchorB{GetLocalPoint(*bB, anchor2)},
length{GetLength(anchor2 - anchor1)}
{
// Intentionally empty.
}
Expand Down Expand Up @@ -77,7 +76,7 @@ DistanceJoint::DistanceJoint(const DistanceJointDef& def):

void DistanceJoint::InitVelocityConstraints(BodyConstraintsMap& bodies,
const StepConf& step,
const ConstraintSolverConf& conf)
const ConstraintSolverConf&)
{
auto& bodyConstraintA = At(bodies, static_cast<const Body*>(GetBodyA()));
auto& bodyConstraintB = At(bodies, GetBodyB());
Expand All @@ -102,11 +101,7 @@ void DistanceJoint::InitVelocityConstraints(BodyConstraintsMap& bodies,

// Handle singularity.
auto length = Length{0};
m_u = GetUnitVector(deltaLocation, length);
if (length <= conf.linearSlop)
{
m_u = UnitVec2::GetZero();
}
m_u = GetUnitVector(deltaLocation, length, UnitVec2::GetZero());

const auto crAu = Length{Cross(m_rA, m_u)} / Radian;
const auto crBu = Length{Cross(m_rB, m_u)} / Radian;
Expand Down Expand Up @@ -199,11 +194,12 @@ bool DistanceJoint::SolveVelocityConstraints(BodyConstraintsMap& bodies, const S

bodyConstraintA->SetVelocity(velA);
bodyConstraintB->SetVelocity(velB);

return impulse == Momentum{0};
}

bool DistanceJoint::SolvePositionConstraints(BodyConstraintsMap& bodies, const ConstraintSolverConf& conf) const
bool DistanceJoint::SolvePositionConstraints(BodyConstraintsMap& bodies,
const ConstraintSolverConf& conf) const
{
if (m_frequency > Frequency{0})
{
Expand All @@ -228,7 +224,7 @@ bool DistanceJoint::SolvePositionConstraints(BodyConstraintsMap& bodies, const C
const auto rA = Length2D{Rotate(m_localAnchorA - bodyConstraintA->GetLocalCenter(), qA)};
const auto rB = Length2D{Rotate(m_localAnchorB - bodyConstraintB->GetLocalCenter(), qB)};
const auto relLoc = Length2D{(posB.linear + rB) - (posA.linear + rA)};

auto length = Length{0};
const auto u = GetUnitVector(relLoc, length);
const auto deltaLength = length - m_length;
Expand Down Expand Up @@ -270,7 +266,7 @@ Torque DistanceJoint::GetReactionTorque(Frequency inv_dt) const
DistanceJointDef playrho::GetDistanceJointDef(const DistanceJoint& joint) noexcept
{
auto def = DistanceJointDef{};

Set(def, joint);

def.localAnchorA = joint.GetLocalAnchorA();
Expand Down
37 changes: 29 additions & 8 deletions PlayRho/Dynamics/Joints/DistanceJoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,37 @@ namespace playrho {
/// the initial configuration can violate the constraint slightly. This helps when
// saving and loading a game.
/// @warning Do not use a zero or short length.
struct DistanceJointDef : public JointDef
struct DistanceJointDef : public JointBuilder<DistanceJointDef>
{
constexpr DistanceJointDef() noexcept: JointDef{JointType::Distance} {}
using super = JointBuilder<DistanceJointDef>;

constexpr DistanceJointDef() noexcept: super{JointType::Distance} {}

DistanceJointDef(const DistanceJointDef& copy) = default;

/// @brief Initializing constructor.
/// @details Initialize the bodies, anchors, and length using the world anchors.
DistanceJointDef(NonNull<Body*> bodyA, NonNull<Body*> bodyB,
const Length2D anchorA = Length2D(0, 0),
const Length2D anchorB = Length2D(0, 0),
NonNegative<Frequency> freq = NonNegative<Frequency>{0},
Real damp = 0) noexcept;
Length2D anchorA = Length2D(0, 0),
Length2D anchorB = Length2D(0, 0)) noexcept;

constexpr DistanceJointDef& UseLength(Length v) noexcept
{
length = v;
return *this;
}

constexpr DistanceJointDef& UseFrequency(NonNegative<Frequency> v) noexcept
{
frequency = v;
return *this;
}

constexpr DistanceJointDef& UseDampingRatio(Real v) noexcept
{
dampingRatio = v;
return *this;
}

/// @brief Local anchor point relative to bodyA's origin.
Length2D localAnchorA = Length2D(0, 0);
Expand Down Expand Up @@ -108,7 +126,8 @@ class DistanceJoint : public Joint

private:

void InitVelocityConstraints(BodyConstraintsMap& bodies, const StepConf& step, const ConstraintSolverConf& conf) override;
void InitVelocityConstraints(BodyConstraintsMap& bodies, const StepConf& step, const
ConstraintSolverConf&) override;
bool SolveVelocityConstraints(BodyConstraintsMap& bodies, const StepConf& step) override;
bool SolvePositionConstraints(BodyConstraintsMap& bodies, const ConstraintSolverConf& conf) const override;

Expand All @@ -118,9 +137,11 @@ class DistanceJoint : public Joint
NonNegative<Frequency> m_frequency;
Real m_dampingRatio;

// Solver shared
Momentum m_impulse = Momentum{0};

// Solver temp
InvMass m_invGamma;
Momentum m_impulse = Momentum{0};
LinearVelocity m_bias;
Mass m_mass;
UnitVec2 m_u;
Expand Down
2 changes: 1 addition & 1 deletion PlayRho/Dynamics/Joints/FrictionJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using namespace playrho;
// K = invI1 + invI2

FrictionJointDef::FrictionJointDef(Body* bA, Body* bB, const Length2D anchor) noexcept:
JointDef{JointType::Friction, bA, bB},
super{super{JointType::Friction}.UseBodyA(bA).UseBodyB(bB)},
localAnchorA{GetLocalPoint(*bA, anchor)},
localAnchorB{GetLocalPoint(*bB, anchor)}
{
Expand Down
6 changes: 4 additions & 2 deletions PlayRho/Dynamics/Joints/FrictionJoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
namespace playrho {

/// @brief Friction joint definition.
struct FrictionJointDef : public JointDef
struct FrictionJointDef : public JointBuilder<FrictionJointDef>
{
constexpr FrictionJointDef() noexcept: JointDef(JointType::Friction) {}
using super = JointBuilder<FrictionJointDef>;

constexpr FrictionJointDef() noexcept: super{JointType::Friction} {}

/// @brief Initializing constructor.
/// @details Initialize the bodies, anchors, axis, and reference angle using the world
Expand Down
2 changes: 1 addition & 1 deletion PlayRho/Dynamics/Joints/GearJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ using namespace playrho;
// K = J * invM * JT = invMass + invI * cross(r, ug)^2

GearJoint::GearJoint(const GearJointDef& def):
Joint(JointDef(def).UseBodyA(def.joint1->GetBodyB()).UseBodyB(def.joint2->GetBodyB())),
Joint(def),
m_joint1(def.joint1),
m_joint2(def.joint2),
m_typeA(def.joint1->GetType()),
Expand Down
6 changes: 4 additions & 2 deletions PlayRho/Dynamics/Joints/GearJoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ namespace playrho {
/// @brief Gear joint definition.
/// @details This definition requires two existing
/// revolute or prismatic joints (any combination will work).
struct GearJointDef : public JointDef
struct GearJointDef : public JointBuilder<GearJointDef>
{
using super = JointBuilder<GearJointDef>;

GearJointDef(NonNull<Joint*> j1, NonNull<Joint*> j2) noexcept:
JointDef{JointDef(JointType::Gear).UseBodyA(j1->GetBodyB()).UseBodyB(j2->GetBodyB())},
super{super{JointType::Gear}.UseBodyA(j1->GetBodyB()).UseBodyB(j2->GetBodyB())},
joint1{j1}, joint2{j2}
{
// Intentionally empty.
Expand Down
Loading

0 comments on commit 4347e39

Please sign in to comment.