LiquidFun
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
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  void GetDefinition(b2WheelJointDef* def) const;
83 
84  b2Vec2 GetAnchorA() const;
85  b2Vec2 GetAnchorB() const;
86 
87  b2Vec2 GetReactionForce(float32 inv_dt) const;
88  float32 GetReactionTorque(float32 inv_dt) const;
89 
91  const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
92 
94  const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
95 
97  const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
98 
100  float32 GetJointTranslation() const;
101 
103  float32 GetJointSpeed() const;
104 
106  bool IsMotorEnabled() const;
107 
109  void EnableMotor(bool flag);
110 
112  void SetMotorSpeed(float32 speed);
113 
115  float32 GetMotorSpeed() const;
116 
118  void SetMaxMotorTorque(float32 torque);
119  float32 GetMaxMotorTorque() const;
120 
122  float32 GetMotorTorque(float32 inv_dt) const;
123 
125  void SetSpringFrequencyHz(float32 hz);
126  float32 GetSpringFrequencyHz() const;
127 
129  void SetSpringDampingRatio(float32 ratio);
130  float32 GetSpringDampingRatio() const;
131 
133  void Dump();
134 
135 protected:
136 
137  friend class b2Joint;
138  b2WheelJoint(const b2WheelJointDef* def);
139 
140  void InitVelocityConstraints(const b2SolverData& data);
141  void SolveVelocityConstraints(const b2SolverData& data);
142  bool SolvePositionConstraints(const b2SolverData& data);
143 
144  float32 m_frequencyHz;
145  float32 m_dampingRatio;
146 
147  // Solver shared
148  b2Vec2 m_localAnchorA;
149  b2Vec2 m_localAnchorB;
150  b2Vec2 m_localXAxisA;
151  b2Vec2 m_localYAxisA;
152 
153  float32 m_impulse;
154  float32 m_motorImpulse;
155  float32 m_springImpulse;
156 
157  float32 m_maxMotorTorque;
158  float32 m_motorSpeed;
159  bool m_enableMotor;
160 
161  // Solver temp
162  int32 m_indexA;
163  int32 m_indexB;
164  b2Vec2 m_localCenterA;
165  b2Vec2 m_localCenterB;
166  float32 m_invMassA;
167  float32 m_invMassB;
168  float32 m_invIA;
169  float32 m_invIB;
170 
171  b2Vec2 m_ax, m_ay;
172  float32 m_sAx, m_sBx;
173  float32 m_sAy, m_sBy;
174 
175  float32 m_mass;
176  float32 m_motorMass;
177  float32 m_springMass;
178 
179  float32 m_bias;
180  float32 m_gamma;
181 };
182 
183 inline float32 b2WheelJoint::GetMotorSpeed() const
184 {
185  return m_motorSpeed;
186 }
187 
188 inline float32 b2WheelJoint::GetMaxMotorTorque() const
189 {
190  return m_maxMotorTorque;
191 }
192 
193 inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz)
194 {
195  m_frequencyHz = hz;
196 }
197 
198 inline float32 b2WheelJoint::GetSpringFrequencyHz() const
199 {
200  return m_frequencyHz;
201 }
202 
203 inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio)
204 {
205  m_dampingRatio = ratio;
206 }
207 
208 inline float32 b2WheelJoint::GetSpringDampingRatio() const
209 {
210  return m_dampingRatio;
211 }
212 
213 #endif
void SetSpringDampingRatio(float32 ratio)
Set/Get the spring damping ratio.
Definition: b2WheelJoint.h:203
Definition: b2WheelJoint.h:30
b2Vec2 GetAnchorA() const
Get the anchor point on bodyA in world coordinates.
Definition: b2WheelJoint.cpp:329
float32 GetReactionTorque(float32 inv_dt) const
Get the reaction torque on bodyB in N*m.
Definition: b2WheelJoint.cpp:344
b2Body * bodyA
The first attached body.
Definition: b2Joint.h:92
void Dump()
Dump to b2Log.
Definition: b2WheelJoint.cpp:401
b2Vec2 localAnchorA
The local anchor point relative to bodyA&#39;s origin.
Definition: b2WheelJoint.h:50
b2Body * bodyB
The second attached body.
Definition: b2Joint.h:95
void EnableMotor(bool flag)
Enable/disable the joint motor.
Definition: b2WheelJoint.cpp:375
bool IsMotorEnabled() const
Is the joint motor enabled?
Definition: b2WheelJoint.cpp:370
float32 GetJointTranslation() const
Get the current joint translation, usually in meters.
Definition: b2WheelJoint.cpp:349
b2Vec2 GetReactionForce(float32 inv_dt) const
Get the reaction force on bodyB at the joint anchor in Newtons.
Definition: b2WheelJoint.cpp:339
Definition: b2Joint.h:103
float32 frequencyHz
Suspension frequency, zero indicates no suspension.
Definition: b2WheelJoint.h:68
void Set(float32 x_, float32 y_)
Set this vector to some specified coordinates.
Definition: b2Math.h:76
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:73
bool enableMotor
Enable/disable the joint motor.
Definition: b2WheelJoint.h:59
float32 GetMotorSpeed() const
Get the motor speed, usually in radians per second.
Definition: b2WheelJoint.h:183
b2JointType type
The joint type is set automatically for concrete joint types.
Definition: b2Joint.h:86
const b2Vec2 & GetLocalAxisA() const
The local joint axis relative to bodyA.
Definition: b2WheelJoint.h:97
const b2Vec2 & GetLocalAnchorA() const
The local anchor point relative to bodyA&#39;s origin.
Definition: b2WheelJoint.h:91
Solver Data.
Definition: b2TimeStep.h:63
b2Vec2 GetAnchorB() const
Get the anchor point on bodyB in world coordinates.
Definition: b2WheelJoint.cpp:334
float32 maxMotorTorque
The maximum motor torque, usually in N-m.
Definition: b2WheelJoint.h:62
Joint definitions are used to construct joints.
Definition: b2Joint.h:74
b2Vec2 localAnchorB
The local anchor point relative to bodyB&#39;s origin.
Definition: b2WheelJoint.h:53
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:127
float32 motorSpeed
The desired motor speed in radians per second.
Definition: b2WheelJoint.h:65
A 2D column vector.
Definition: b2Math.h:64
const b2Vec2 & GetLocalAnchorB() const
The local anchor point relative to bodyB&#39;s origin.
Definition: b2WheelJoint.h:94
void SetMotorSpeed(float32 speed)
Set the motor speed, usually in radians per second.
Definition: b2WheelJoint.cpp:382
float32 GetJointSpeed() const
Get the current joint translation speed, usually in meters per second.
Definition: b2WheelJoint.cpp:363
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
Definition: b2WheelJoint.cpp:39
Definition: b2WheelJoint.h:79
float32 dampingRatio
Suspension damping ratio, one indicates critical damping.
Definition: b2WheelJoint.h:71
b2Vec2 localAxisA
The local translation axis in bodyA.
Definition: b2WheelJoint.h:56
float32 GetMotorTorque(float32 inv_dt) const
Get the current motor torque given the inverse time step, usually in N-m.
Definition: b2WheelJoint.cpp:396
void SetMaxMotorTorque(float32 torque)
Set/Get the maximum motor force, usually in N-m.
Definition: b2WheelJoint.cpp:389
void SetSpringFrequencyHz(float32 hz)
Set/Get the spring frequency in hertz. Setting the frequency to zero disables the spring...
Definition: b2WheelJoint.h:193