2 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
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.
19 #ifndef B2_WHEEL_JOINT_H
20 #define B2_WHEEL_JOINT_H
22 #include <Box2D/Dynamics/Joints/b2Joint.h>
24 /// Wheel joint definition. This requires defining a line of
25 /// motion using an axis and an anchor point. The definition uses local
26 /// anchor points and a local axis so that the initial configuration
27 /// can violate the constraint slightly. The joint translation is zero
28 /// when the local anchor points coincide in world space. Using local
29 /// anchors and a local axis helps when saving and loading a game.
30 struct b2WheelJointDef : public b2JointDef
35 localAnchorA.SetZero();
36 localAnchorB.SetZero();
37 localAxisA.Set(1.0f, 0.0f);
39 maxMotorTorque = 0.0f;
45 /// Initialize the bodies, anchors, axis, and reference angle using the world
46 /// anchor and world axis.
47 void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
49 /// The local anchor point relative to bodyA's origin.
52 /// The local anchor point relative to bodyB's origin.
55 /// The local translation axis in bodyA.
58 /// Enable/disable the joint motor.
61 /// The maximum motor torque, usually in N-m.
62 float32 maxMotorTorque;
64 /// The desired motor speed in radians per second.
67 /// Suspension frequency, zero indicates no suspension
70 /// Suspension damping ratio, one indicates critical damping
74 /// A wheel joint. This joint provides two degrees of freedom: translation
75 /// along an axis fixed in bodyA and rotation in the plane. You can use a
76 /// joint limit to restrict the range of motion and a joint motor to drive
77 /// the rotation or to model rotational friction.
78 /// This joint is designed for vehicle suspensions.
79 class b2WheelJoint : public b2Joint
82 b2Vec2 GetAnchorA() const;
83 b2Vec2 GetAnchorB() const;
85 b2Vec2 GetReactionForce(float32 inv_dt) const;
86 float32 GetReactionTorque(float32 inv_dt) const;
88 /// The local anchor point relative to bodyA's origin.
89 const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
91 /// The local anchor point relative to bodyB's origin.
92 const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
94 /// The local joint axis relative to bodyA.
95 const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
97 /// Get the current joint translation, usually in meters.
98 float32 GetJointTranslation() const;
100 /// Get the current joint translation speed, usually in meters per second.
101 float32 GetJointSpeed() const;
103 /// Is the joint motor enabled?
104 bool IsMotorEnabled() const;
106 /// Enable/disable the joint motor.
107 void EnableMotor(bool flag);
109 /// Set the motor speed, usually in radians per second.
110 void SetMotorSpeed(float32 speed);
112 /// Get the motor speed, usually in radians per second.
113 float32 GetMotorSpeed() const;
115 /// Set/Get the maximum motor force, usually in N-m.
116 void SetMaxMotorTorque(float32 torque);
117 float32 GetMaxMotorTorque() const;
119 /// Get the current motor torque given the inverse time step, usually in N-m.
120 float32 GetMotorTorque(float32 inv_dt) const;
122 /// Set/Get the spring frequency in hertz. Setting the frequency to zero disables the spring.
123 void SetSpringFrequencyHz(float32 hz);
124 float32 GetSpringFrequencyHz() const;
126 /// Set/Get the spring damping ratio
127 void SetSpringDampingRatio(float32 ratio);
128 float32 GetSpringDampingRatio() const;
135 friend class b2Joint;
136 b2WheelJoint(const b2WheelJointDef* def);
138 void InitVelocityConstraints(const b2SolverData& data);
139 void SolveVelocityConstraints(const b2SolverData& data);
140 bool SolvePositionConstraints(const b2SolverData& data);
142 float32 m_frequencyHz;
143 float32 m_dampingRatio;
146 b2Vec2 m_localAnchorA;
147 b2Vec2 m_localAnchorB;
148 b2Vec2 m_localXAxisA;
149 b2Vec2 m_localYAxisA;
152 float32 m_motorImpulse;
153 float32 m_springImpulse;
155 float32 m_maxMotorTorque;
156 float32 m_motorSpeed;
162 b2Vec2 m_localCenterA;
163 b2Vec2 m_localCenterB;
170 float32 m_sAx, m_sBx;
171 float32 m_sAy, m_sBy;
175 float32 m_springMass;
181 inline float32 b2WheelJoint::GetMotorSpeed() const
186 inline float32 b2WheelJoint::GetMaxMotorTorque() const
188 return m_maxMotorTorque;
191 inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz)
196 inline float32 b2WheelJoint::GetSpringFrequencyHz() const
198 return m_frequencyHz;
201 inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio)
203 m_dampingRatio = ratio;
206 inline float32 b2WheelJoint::GetSpringDampingRatio() const
208 return m_dampingRatio;