Box2D  2.3.0
A 2D Physics Engine for Games
b2WheelJoint.h
1 /*
2 * Copyright (c) 2006-2011 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_WHEEL_JOINT_H
20 #define B2_WHEEL_JOINT_H
21 
22 #include <Box2D/Dynamics/Joints/b2Joint.h>
23 
30 struct b2WheelJointDef : public b2JointDef
31 {
33  {
34  type = e_wheelJoint;
37  localAxisA.Set(1.0f, 0.0f);
38  enableMotor = false;
39  maxMotorTorque = 0.0f;
40  motorSpeed = 0.0f;
41  frequencyHz = 2.0f;
42  dampingRatio = 0.7f;
43  }
44 
47  void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
48 
51 
54 
57 
60 
62  float32 maxMotorTorque;
63 
65  float32 motorSpeed;
66 
68  float32 frequencyHz;
69 
71  float32 dampingRatio;
72 };
73 
79 class b2WheelJoint : public b2Joint
80 {
81 public:
82  b2Vec2 GetAnchorA() const;
83  b2Vec2 GetAnchorB() const;
84 
85  b2Vec2 GetReactionForce(float32 inv_dt) const;
86  float32 GetReactionTorque(float32 inv_dt) const;
87 
89  const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
90 
92  const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
93 
95  const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
96 
98  float32 GetJointTranslation() const;
99 
101  float32 GetJointSpeed() const;
102 
104  bool IsMotorEnabled() const;
105 
107  void EnableMotor(bool flag);
108 
110  void SetMotorSpeed(float32 speed);
111 
113  float32 GetMotorSpeed() const;
114 
116  void SetMaxMotorTorque(float32 torque);
117  float32 GetMaxMotorTorque() const;
118 
120  float32 GetMotorTorque(float32 inv_dt) const;
121 
123  void SetSpringFrequencyHz(float32 hz);
124  float32 GetSpringFrequencyHz() const;
125 
127  void SetSpringDampingRatio(float32 ratio);
128  float32 GetSpringDampingRatio() const;
129 
131  void Dump();
132 
133 protected:
134 
135  friend class b2Joint;
136  b2WheelJoint(const b2WheelJointDef* def);
137 
138  void InitVelocityConstraints(const b2SolverData& data);
139  void SolveVelocityConstraints(const b2SolverData& data);
140  bool SolvePositionConstraints(const b2SolverData& data);
141 
142  float32 m_frequencyHz;
143  float32 m_dampingRatio;
144 
145  // Solver shared
146  b2Vec2 m_localAnchorA;
147  b2Vec2 m_localAnchorB;
148  b2Vec2 m_localXAxisA;
149  b2Vec2 m_localYAxisA;
150 
151  float32 m_impulse;
152  float32 m_motorImpulse;
153  float32 m_springImpulse;
154 
155  float32 m_maxMotorTorque;
156  float32 m_motorSpeed;
157  bool m_enableMotor;
158 
159  // Solver temp
160  int32 m_indexA;
161  int32 m_indexB;
162  b2Vec2 m_localCenterA;
163  b2Vec2 m_localCenterB;
164  float32 m_invMassA;
165  float32 m_invMassB;
166  float32 m_invIA;
167  float32 m_invIB;
168 
169  b2Vec2 m_ax, m_ay;
170  float32 m_sAx, m_sBx;
171  float32 m_sAy, m_sBy;
172 
173  float32 m_mass;
174  float32 m_motorMass;
175  float32 m_springMass;
176 
177  float32 m_bias;
178  float32 m_gamma;
179 };
180 
181 inline float32 b2WheelJoint::GetMotorSpeed() const
182 {
183  return m_motorSpeed;
184 }
185 
186 inline float32 b2WheelJoint::GetMaxMotorTorque() const
187 {
188  return m_maxMotorTorque;
189 }
190 
191 inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz)
192 {
193  m_frequencyHz = hz;
194 }
195 
196 inline float32 b2WheelJoint::GetSpringFrequencyHz() const
197 {
198  return m_frequencyHz;
199 }
200 
201 inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio)
202 {
203  m_dampingRatio = ratio;
204 }
205 
206 inline float32 b2WheelJoint::GetSpringDampingRatio() const
207 {
208  return m_dampingRatio;
209 }
210 
211 #endif