Box2D  2.4.1
A 2D physics engine for games
b2_distance_joint.h
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef B2_DISTANCE_JOINT_H
24 #define B2_DISTANCE_JOINT_H
25 
26 #include "b2_api.h"
27 #include "b2_joint.h"
28 
33 struct B2_API b2DistanceJointDef : public b2JointDef
34 {
36  {
37  type = e_distanceJoint;
38  localAnchorA.Set(0.0f, 0.0f);
39  localAnchorB.Set(0.0f, 0.0f);
40  length = 1.0f;
41  minLength = 0.0f;
42  maxLength = FLT_MAX;
43  stiffness = 0.0f;
44  damping = 0.0f;
45  }
46 
49  void Initialize(b2Body* bodyA, b2Body* bodyB,
50  const b2Vec2& anchorA, const b2Vec2& anchorB);
51 
54 
57 
59  float length;
60 
62  float minLength;
63 
65  float maxLength;
66 
68  float stiffness;
69 
71  float damping;
72 };
73 
76 class B2_API b2DistanceJoint : public b2Joint
77 {
78 public:
79 
80  b2Vec2 GetAnchorA() const override;
81  b2Vec2 GetAnchorB() const override;
82 
85  b2Vec2 GetReactionForce(float inv_dt) const override;
86 
89  float GetReactionTorque(float inv_dt) const override;
90 
92  const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
93 
95  const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
96 
98  float GetLength() const { return m_length; }
99 
102  float SetLength(float length);
103 
105  float GetMinLength() const { return m_minLength; }
106 
109  float SetMinLength(float minLength);
110 
112  float GetMaxLength() const { return m_maxLength; }
113 
116  float SetMaxLength(float maxLength);
117 
119  float GetCurrentLength() const;
120 
122  void SetStiffness(float stiffness) { m_stiffness = stiffness; }
123  float GetStiffness() const { return m_stiffness; }
124 
126  void SetDamping(float damping) { m_damping = damping; }
127  float GetDamping() const { return m_damping; }
128 
130  void Dump() override;
131 
133  void Draw(b2Draw* draw) const override;
134 
135 protected:
136 
137  friend class b2Joint;
138  b2DistanceJoint(const b2DistanceJointDef* data);
139 
140  void InitVelocityConstraints(const b2SolverData& data) override;
141  void SolveVelocityConstraints(const b2SolverData& data) override;
142  bool SolvePositionConstraints(const b2SolverData& data) override;
143 
144  float m_stiffness;
145  float m_damping;
146  float m_bias;
147  float m_length;
148  float m_minLength;
149  float m_maxLength;
150 
151  // Solver shared
152  b2Vec2 m_localAnchorA;
153  b2Vec2 m_localAnchorB;
154  float m_gamma;
155  float m_impulse;
156  float m_lowerImpulse;
157  float m_upperImpulse;
158 
159  // Solver temp
160  int32 m_indexA;
161  int32 m_indexB;
162  b2Vec2 m_u;
163  b2Vec2 m_rA;
164  b2Vec2 m_rB;
165  b2Vec2 m_localCenterA;
166  b2Vec2 m_localCenterB;
167  float m_currentLength;
168  float m_invMassA;
169  float m_invMassB;
170  float m_invIA;
171  float m_invIB;
172  float m_softMass;
173  float m_mass;
174 };
175 
176 #endif
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2DistanceJoint::GetMaxLength
float GetMaxLength() const
Get the maximum length.
Definition: b2_distance_joint.h:112
b2DistanceJointDef::minLength
float minLength
Minimum length. Clamped to a stable minimum value.
Definition: b2_distance_joint.h:62
b2DistanceJoint
Definition: b2_distance_joint.h:76
b2Joint::GetReactionForce
virtual b2Vec2 GetReactionForce(float inv_dt) const =0
Get the reaction force on bodyB at the joint anchor in Newtons.
b2Joint::GetAnchorA
virtual b2Vec2 GetAnchorA() const =0
Get the anchor point on bodyA in world coordinates.
b2DistanceJoint::GetLocalAnchorA
const b2Vec2 & GetLocalAnchorA() const
The local anchor point relative to bodyA's origin.
Definition: b2_distance_joint.h:92
b2Draw
Definition: b2_draw.h:48
b2DistanceJoint::SetDamping
void SetDamping(float damping)
Set/get linear damping in N*s/m.
Definition: b2_distance_joint.h:126
b2JointDef
Joint definitions are used to construct joints.
Definition: b2_joint.h:72
b2DistanceJoint::GetLocalAnchorB
const b2Vec2 & GetLocalAnchorB() const
The local anchor point relative to bodyB's origin.
Definition: b2_distance_joint.h:95
b2DistanceJointDef::localAnchorA
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
Definition: b2_distance_joint.h:53
b2DistanceJointDef::damping
float damping
The linear damping in N*s/m.
Definition: b2_distance_joint.h:71
b2Joint::Draw
virtual void Draw(b2Draw *draw) const
Debug draw this joint.
b2DistanceJointDef::length
float length
The rest length of this joint. Clamped to a stable minimum value.
Definition: b2_distance_joint.h:59
b2Joint::Dump
virtual void Dump()
Dump this joint to the log file.
Definition: b2_joint.h:151
b2DistanceJointDef::stiffness
float stiffness
The linear stiffness in N/m.
Definition: b2_distance_joint.h:68
b2DistanceJoint::GetLength
float GetLength() const
Get the rest length.
Definition: b2_distance_joint.h:98
b2SolverData
Solver Data.
Definition: b2_time_step.h:67
b2DistanceJoint::GetMinLength
float GetMinLength() const
Get the minimum length.
Definition: b2_distance_joint.h:105
b2Joint::GetAnchorB
virtual b2Vec2 GetAnchorB() const =0
Get the anchor point on bodyB in world coordinates.
b2DistanceJointDef::localAnchorB
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
Definition: b2_distance_joint.h:56
b2DistanceJoint::SetStiffness
void SetStiffness(float stiffness)
Set/get the linear stiffness in N/m.
Definition: b2_distance_joint.h:122
b2Joint
Definition: b2_joint.h:110
b2Joint::GetReactionTorque
virtual float GetReactionTorque(float inv_dt) const =0
Get the reaction torque on bodyB in N*m.
b2DistanceJointDef::maxLength
float maxLength
Maximum length. Must be greater than or equal to the minimum length.
Definition: b2_distance_joint.h:65
b2DistanceJointDef
Definition: b2_distance_joint.h:33