Skip to content

Commit

Permalink
Fix for issue 632 (erincatto#642)
Browse files Browse the repository at this point in the history
- Fixes issue erincatto#632
- Originally the idea was to rename rope to cloth to avoid confusion with the rope joint. However, I realized the distance joint could be extended to cover the rope joint functionality.
- Extended distance joint to have a minimum and maximum limit
- Removed rope joint
- This is API breaking: 0 stiffness now means the spring is turned off rather than making the joint rigid.
  • Loading branch information
erincatto authored Sep 6, 2020
1 parent bd07c57 commit 8815e09
Show file tree
Hide file tree
Showing 15 changed files with 571 additions and 602 deletions.
59 changes: 47 additions & 12 deletions include/box2d/b2_distance_joint.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
#include "b2_joint.h"

/// Distance joint definition. This requires defining an anchor point on both
/// bodies and the non-zero length of the distance joint. The definition uses local anchor points
/// so that 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.
/// bodies and the non-zero distance of the distance joint. The definition uses
/// local anchor points so that the initial configuration can violate the
/// constraint slightly. This helps when saving and loading a game.
struct b2DistanceJointDef : public b2JointDef
{
b2DistanceJointDef()
Expand All @@ -38,12 +37,14 @@ struct b2DistanceJointDef : public b2JointDef
localAnchorA.Set(0.0f, 0.0f);
localAnchorB.Set(0.0f, 0.0f);
length = 1.0f;
minLength = 0.0f;
maxLength = FLT_MAX;
stiffness = 0.0f;
damping = 0.0f;
}

/// Initialize the bodies, anchors, and length using the world
/// anchors.
/// Initialize the bodies, anchors, and rest length using world space anchors.
/// The minimum and maximum lengths are set to the rest length.
void Initialize(b2Body* bodyA, b2Body* bodyB,
const b2Vec2& anchorA, const b2Vec2& anchorB);

Expand All @@ -53,10 +54,16 @@ struct b2DistanceJointDef : public b2JointDef
/// The local anchor point relative to bodyB's origin.
b2Vec2 localAnchorB;

/// The natural length between the anchor points.
/// The rest length of this joint. Clamped to a stable minimum value.
float length;

/// The linear stiffness in N/m. A value of 0 disables softness.
/// Minimum length. Clamped to a stable minimum value.
float minLength;

/// Maximum length. Must be greater than or equal to the minimum length.
float maxLength;

/// The linear stiffness in N/m.
float stiffness;

/// The linear damping in N*s/m.
Expand Down Expand Up @@ -86,11 +93,30 @@ class b2DistanceJoint : public b2Joint
/// The local anchor point relative to bodyB's origin.
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }

/// Set/get the natural length.
/// Manipulating the length can lead to non-physical behavior when the frequency is zero.
void SetLength(float length) { m_length = length; }
/// Get the rest length
float GetLength() const { return m_length; }

/// Set the rest length
/// @returns clamped rest length
float SetLength(float length);

/// Get the minimum length
float GetMinLength() const { return m_minLength; }

/// Set the minimum length
/// @returns the clamped minimum length
float SetMinLength(float minLength);

/// Get the maximum length
float GetMaxLength() const { return m_maxLength; }

/// Set the maximum length
/// @returns the clamped maximum length
float SetMaxLength(float maxLength);

/// Get the current length
float GetCurrentLength() const;

/// Set/get the linear stiffness in N/m
void SetStiffness(float stiffness) { m_stiffness = stiffness; }
float GetStiffness() const { return m_stiffness; }
Expand All @@ -102,6 +128,9 @@ class b2DistanceJoint : public b2Joint
/// Dump joint to dmLog
void Dump() override;

///
void Draw(b2Draw* draw) const override;

protected:

friend class b2Joint;
Expand All @@ -114,13 +143,17 @@ class b2DistanceJoint : public b2Joint
float m_stiffness;
float m_damping;
float m_bias;
float m_length;
float m_minLength;
float m_maxLength;

// Solver shared
b2Vec2 m_localAnchorA;
b2Vec2 m_localAnchorB;
float m_gamma;
float m_impulse;
float m_length;
float m_lowerImpulse;
float m_upperImpulse;

// Solver temp
int32 m_indexA;
Expand All @@ -130,10 +163,12 @@ class b2DistanceJoint : public b2Joint
b2Vec2 m_rB;
b2Vec2 m_localCenterA;
b2Vec2 m_localCenterB;
float m_currentLength;
float m_invMassA;
float m_invMassB;
float m_invIA;
float m_invIB;
float m_softMass;
float m_mass;
};

Expand Down
118 changes: 0 additions & 118 deletions include/box2d/b2_rope_joint.h

This file was deleted.

1 change: 0 additions & 1 deletion include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
#include "b2_prismatic_joint.h"
#include "b2_pulley_joint.h"
#include "b2_revolute_joint.h"
#include "b2_rope_joint.h"
#include "b2_weld_joint.h"
#include "b2_wheel_joint.h"

Expand Down
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ set(BOX2D_SOURCE_FILES
dynamics/b2_prismatic_joint.cpp
dynamics/b2_pulley_joint.cpp
dynamics/b2_revolute_joint.cpp
dynamics/b2_rope_joint.cpp
dynamics/b2_weld_joint.cpp
dynamics/b2_wheel_joint.cpp
dynamics/b2_world.cpp
Expand Down Expand Up @@ -82,7 +81,6 @@ set(BOX2D_HEADER_FILES
../include/box2d/b2_pulley_joint.h
../include/box2d/b2_revolute_joint.h
../include/box2d/b2_rope.h
../include/box2d/b2_rope_joint.h
../include/box2d/b2_settings.h
../include/box2d/b2_shape.h
../include/box2d/b2_stack_allocator.h
Expand Down
Loading

0 comments on commit 8815e09

Please sign in to comment.