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