Box2D  2.3.0
A 2D Physics Engine for Games
b2DistanceJoint.h
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #ifndef B2_DISTANCE_JOINT_H
20 #define B2_DISTANCE_JOINT_H
21 
22 #include <Box2D/Dynamics/Joints/b2Joint.h>
23 
31 {
33  {
34  type = e_distanceJoint;
35  localAnchorA.Set(0.0f, 0.0f);
36  localAnchorB.Set(0.0f, 0.0f);
37  length = 1.0f;
38  frequencyHz = 0.0f;
39  dampingRatio = 0.0f;
40  }
41 
45  const b2Vec2& anchorA, const b2Vec2& anchorB);
46 
49 
52 
54  float32 length;
55 
58  float32 frequencyHz;
59 
61  float32 dampingRatio;
62 };
63 
67 class b2DistanceJoint : public b2Joint
68 {
69 public:
70 
71  b2Vec2 GetAnchorA() const;
72  b2Vec2 GetAnchorB() const;
73 
76  b2Vec2 GetReactionForce(float32 inv_dt) const;
77 
80  float32 GetReactionTorque(float32 inv_dt) const;
81 
83  const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
84 
86  const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
87 
90  void SetLength(float32 length);
91  float32 GetLength() const;
92 
94  void SetFrequency(float32 hz);
95  float32 GetFrequency() const;
96 
98  void SetDampingRatio(float32 ratio);
99  float32 GetDampingRatio() const;
100 
102  void Dump();
103 
104 protected:
105 
106  friend class b2Joint;
107  b2DistanceJoint(const b2DistanceJointDef* data);
108 
109  void InitVelocityConstraints(const b2SolverData& data);
110  void SolveVelocityConstraints(const b2SolverData& data);
111  bool SolvePositionConstraints(const b2SolverData& data);
112 
113  float32 m_frequencyHz;
114  float32 m_dampingRatio;
115  float32 m_bias;
116 
117  // Solver shared
118  b2Vec2 m_localAnchorA;
119  b2Vec2 m_localAnchorB;
120  float32 m_gamma;
121  float32 m_impulse;
122  float32 m_length;
123 
124  // Solver temp
125  int32 m_indexA;
126  int32 m_indexB;
127  b2Vec2 m_u;
128  b2Vec2 m_rA;
129  b2Vec2 m_rB;
130  b2Vec2 m_localCenterA;
131  b2Vec2 m_localCenterB;
132  float32 m_invMassA;
133  float32 m_invMassB;
134  float32 m_invIA;
135  float32 m_invIB;
136  float32 m_mass;
137 };
138 
139 inline void b2DistanceJoint::SetLength(float32 length)
140 {
141  m_length = length;
142 }
143 
144 inline float32 b2DistanceJoint::GetLength() const
145 {
146  return m_length;
147 }
148 
149 inline void b2DistanceJoint::SetFrequency(float32 hz)
150 {
151  m_frequencyHz = hz;
152 }
153 
154 inline float32 b2DistanceJoint::GetFrequency() const
155 {
156  return m_frequencyHz;
157 }
158 
159 inline void b2DistanceJoint::SetDampingRatio(float32 ratio)
160 {
161  m_dampingRatio = ratio;
162 }
163 
164 inline float32 b2DistanceJoint::GetDampingRatio() const
165 {
166  return m_dampingRatio;
167 }
168 
169 #endif
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchorA, const b2Vec2 &anchorB)
Definition: b2DistanceJoint.cpp:38
b2Body * bodyA
The first attached body.
Definition: b2Joint.h:92
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
Definition: b2DistanceJoint.h:48
b2Body * bodyB
The second attached body.
Definition: b2Joint.h:95
b2Vec2 GetReactionForce(float32 inv_dt) const
Definition: b2DistanceJoint.cpp:233
Definition: b2Joint.h:103
void SetLength(float32 length)
Definition: b2DistanceJoint.h:139
void Set(float32 x_, float32 y_)
Set this vector to some specified coordinates.
Definition: b2Math.h:65
float32 GetReactionTorque(float32 inv_dt) const
Definition: b2DistanceJoint.cpp:239
b2JointType type
The joint type is set automatically for concrete joint types.
Definition: b2Joint.h:86
Solver Data.
Definition: b2TimeStep.h:63
void Dump()
Dump joint to dmLog.
Definition: b2DistanceJoint.cpp:245
float32 length
The natural length between the anchor points.
Definition: b2DistanceJoint.h:54
Definition: b2DistanceJoint.h:67
float32 frequencyHz
Definition: b2DistanceJoint.h:58
Joint definitions are used to construct joints.
Definition: b2Joint.h:74
const b2Vec2 & GetLocalAnchorA() const
The local anchor point relative to bodyA's origin.
Definition: b2DistanceJoint.h:83
b2Vec2 GetAnchorB() const
Get the anchor point on bodyB in world coordinates.
Definition: b2DistanceJoint.cpp:228
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
Definition: b2DistanceJoint.h:30
float32 dampingRatio
The damping ratio. 0 = no damping, 1 = critical damping.
Definition: b2DistanceJoint.h:61
void SetDampingRatio(float32 ratio)
Set/get damping ratio.
Definition: b2DistanceJoint.h:159
b2Vec2 GetAnchorA() const
Get the anchor point on bodyA in world coordinates.
Definition: b2DistanceJoint.cpp:223
const b2Vec2 & GetLocalAnchorB() const
The local anchor point relative to bodyB's origin.
Definition: b2DistanceJoint.h:86
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
Definition: b2DistanceJoint.h:51
A 2D column vector.
Definition: b2Math.h:53
void SetFrequency(float32 hz)
Set/get frequency in Hz.
Definition: b2DistanceJoint.h:149