| 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 | |
| 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 |
| 31 | { |
| 32 | b2WheelJointDef() |
| 33 | { |
| 34 | type = e_wheelJoint; |
| 35 | localAnchorA.SetZero(); |
| 36 | localAnchorB.SetZero(); |
| 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 | |
| 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); |
| 48 | |
| 49 | /// The local anchor point relative to bodyA's origin. |
| 50 | b2Vec2 localAnchorA; |
| 51 | |
| 52 | /// The local anchor point relative to bodyB's origin. |
| 53 | b2Vec2 localAnchorB; |
| 54 | |
| 55 | /// The local translation axis in bodyA. |
| 56 | b2Vec2 localAxisA; |
| 57 | |
| 58 | /// Enable/disable the joint motor. |
| 59 | bool enableMotor; |
| 60 | |
| 61 | /// The maximum motor torque, usually in N-m. |
| 62 | float32 maxMotorTorque; |
| 63 | |
| 64 | /// The desired motor speed in radians per second. |
| 65 | float32 motorSpeed; |
| 66 | |
| 67 | /// Suspension frequency, zero indicates no suspension |
| 68 | float32 frequencyHz; |
| 69 | |
| 70 | /// Suspension damping ratio, one indicates critical damping |
| 71 | float32 dampingRatio; |
| 72 | }; |
| 73 | |
| 74 | /// A wheel joint. This joint provides two degrees of freedom: translation |
| 75 | /// along an axis fixed in bodyA and rotation in the plane. In other words, it is a point to |
| 76 | /// line constraint with a rotational motor and a linear spring/damper. |
| 77 | /// This joint is designed for vehicle suspensions. |
| 78 | class b2WheelJoint : public b2Joint |
| 79 | { |
| 80 | public: |
| 81 | b2Vec2 GetAnchorA() const; |
| 82 | b2Vec2 GetAnchorB() const; |
| 83 | |
| 84 | b2Vec2 GetReactionForce(float32 inv_dt) const; |
| 85 | float32 GetReactionTorque(float32 inv_dt) const; |
| 86 | |
| 87 | /// The local anchor point relative to bodyA's origin. |
| 88 | const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; } |
| 89 | |
| 90 | /// The local anchor point relative to bodyB's origin. |
| 91 | const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; } |
| 92 | |
| 93 | /// The local joint axis relative to bodyA. |
| 94 | const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; } |
| 95 | |
| 96 | /// Get the current joint translation, usually in meters. |
| 97 | float32 GetJointTranslation() const; |
| 98 | |
| 99 | /// Get the current joint translation speed, usually in meters per second. |
| 100 | float32 GetJointSpeed() const; |
| 101 | |
| 102 | /// Is the joint motor enabled? |
| 103 | bool IsMotorEnabled() const; |
| 104 | |
| 105 | /// Enable/disable the joint motor. |
| 106 | void EnableMotor(bool flag); |
| 107 | |
| 108 | /// Set the motor speed, usually in radians per second. |
| 109 | void SetMotorSpeed(float32 speed); |
| 110 | |
| 111 | /// Get the motor speed, usually in radians per second. |
| 112 | float32 GetMotorSpeed() const; |
| 113 | |
| 114 | /// Set/Get the maximum motor force, usually in N-m. |
| 115 | void SetMaxMotorTorque(float32 torque); |
| 116 | float32 GetMaxMotorTorque() const; |
| 117 | |
| 118 | /// Get the current motor torque given the inverse time step, usually in N-m. |
| 119 | float32 GetMotorTorque(float32 inv_dt) const; |
| 120 | |
| 121 | /// Set/Get the spring frequency in hertz. Setting the frequency to zero disables the spring. |
| 122 | void SetSpringFrequencyHz(float32 hz); |
| 123 | float32 GetSpringFrequencyHz() const; |
| 124 | |
| 125 | /// Set/Get the spring damping ratio |
| 126 | void SetSpringDampingRatio(float32 ratio); |
| 127 | float32 GetSpringDampingRatio() const; |
| 128 | |
| 129 | /// Dump to b2Log |
| 130 | void Dump(); |
| 131 | |
| 132 | protected: |
| 133 | |
| 134 | friend class b2Joint; |
| 135 | b2WheelJoint(const b2WheelJointDef* def); |
| 136 | |
| 137 | void InitVelocityConstraints(const b2SolverData& data); |
| 138 | void SolveVelocityConstraints(const b2SolverData& data); |
| 139 | bool SolvePositionConstraints(const b2SolverData& data); |
| 140 | |
| 141 | float32 m_frequencyHz; |
| 142 | float32 m_dampingRatio; |
| 143 | |
| 144 | // Solver shared |
| 145 | b2Vec2 m_localAnchorA; |
| 146 | b2Vec2 m_localAnchorB; |
| 147 | b2Vec2 m_localXAxisA; |
| 148 | b2Vec2 m_localYAxisA; |
| 149 | |
| 150 | float32 m_impulse; |
| 151 | float32 m_motorImpulse; |
| 152 | float32 m_springImpulse; |
| 153 | |
| 154 | float32 m_maxMotorTorque; |
| 155 | float32 m_motorSpeed; |
| 156 | bool m_enableMotor; |
| 157 | |
| 158 | // Solver temp |
| 159 | int32 m_indexA; |
| 160 | int32 m_indexB; |
| 161 | b2Vec2 m_localCenterA; |
| 162 | b2Vec2 m_localCenterB; |
| 163 | float32 m_invMassA; |
| 164 | float32 m_invMassB; |
| 165 | float32 m_invIA; |
| 166 | float32 m_invIB; |
| 167 | |
| 168 | b2Vec2 m_ax, m_ay; |
| 169 | float32 m_sAx, m_sBx; |
| 170 | float32 m_sAy, m_sBy; |
| 171 | |
| 172 | float32 m_mass; |
| 173 | float32 m_motorMass; |
| 174 | float32 m_springMass; |
| 175 | |
| 176 | float32 m_bias; |
| 177 | float32 m_gamma; |
| 178 | }; |
| 179 | |
| 180 | inline float32 b2WheelJoint::GetMotorSpeed() const |
| 181 | { |
| 182 | return m_motorSpeed; |
| 183 | } |
| 184 | |
| 185 | inline float32 b2WheelJoint::GetMaxMotorTorque() const |
| 186 | { |
| 187 | return m_maxMotorTorque; |
| 188 | } |
| 189 | |
| 190 | inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz) |
| 191 | { |
| 192 | m_frequencyHz = hz; |
| 193 | } |
| 194 | |
| 195 | inline float32 b2WheelJoint::GetSpringFrequencyHz() const |
| 196 | { |
| 197 | return m_frequencyHz; |
| 198 | } |
| 199 | |
| 200 | inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio) |
| 201 | { |
| 202 | m_dampingRatio = ratio; |
| 203 | } |
| 204 | |
| 205 | inline float32 b2WheelJoint::GetSpringDampingRatio() const |
| 206 | { |
| 207 | return m_dampingRatio; |
| 208 | } |
| 209 | |
| 210 | #endif |
| 211 | |