Box2D  2.2.1
A 2D Physics Engine for Games
b2WheelJoint.h
00001 /*
00002 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #ifndef B2_WHEEL_JOINT_H
00020 #define B2_WHEEL_JOINT_H
00021 
00022 #include <Box2D/Dynamics/Joints/b2Joint.h>
00023 
00030 struct b2WheelJointDef : public b2JointDef
00031 {
00032         b2WheelJointDef()
00033         {
00034                 type = e_wheelJoint;
00035                 localAnchorA.SetZero();
00036                 localAnchorB.SetZero();
00037                 localAxisA.Set(1.0f, 0.0f);
00038                 enableMotor = false;
00039                 maxMotorTorque = 0.0f;
00040                 motorSpeed = 0.0f;
00041                 frequencyHz = 2.0f;
00042                 dampingRatio = 0.7f;
00043         }
00044 
00047         void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
00048 
00050         b2Vec2 localAnchorA;
00051 
00053         b2Vec2 localAnchorB;
00054 
00056         b2Vec2 localAxisA;
00057 
00059         bool enableMotor;
00060 
00062         float32 maxMotorTorque;
00063 
00065         float32 motorSpeed;
00066 
00068         float32 frequencyHz;
00069 
00071         float32 dampingRatio;
00072 };
00073 
00079 class b2WheelJoint : public b2Joint
00080 {
00081 public:
00082         void GetDefinition(b2WheelJointDef* def) const;
00083 
00084         b2Vec2 GetAnchorA() const;
00085         b2Vec2 GetAnchorB() const;
00086 
00087         b2Vec2 GetReactionForce(float32 inv_dt) const;
00088         float32 GetReactionTorque(float32 inv_dt) const;
00089 
00091         const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
00092 
00094         const b2Vec2& GetLocalAnchorB() const  { return m_localAnchorB; }
00095 
00097         const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
00098 
00100         float32 GetJointTranslation() const;
00101 
00103         float32 GetJointSpeed() const;
00104 
00106         bool IsMotorEnabled() const;
00107 
00109         void EnableMotor(bool flag);
00110 
00112         void SetMotorSpeed(float32 speed);
00113 
00115         float32 GetMotorSpeed() const;
00116 
00118         void SetMaxMotorTorque(float32 torque);
00119         float32 GetMaxMotorTorque() const;
00120 
00122         float32 GetMotorTorque(float32 inv_dt) const;
00123 
00125         void SetSpringFrequencyHz(float32 hz);
00126         float32 GetSpringFrequencyHz() const;
00127 
00129         void SetSpringDampingRatio(float32 ratio);
00130         float32 GetSpringDampingRatio() const;
00131 
00133         void Dump();
00134 
00135 protected:
00136 
00137         friend class b2Joint;
00138         b2WheelJoint(const b2WheelJointDef* def);
00139 
00140         void InitVelocityConstraints(const b2SolverData& data);
00141         void SolveVelocityConstraints(const b2SolverData& data);
00142         bool SolvePositionConstraints(const b2SolverData& data);
00143 
00144         float32 m_frequencyHz;
00145         float32 m_dampingRatio;
00146 
00147         // Solver shared
00148         b2Vec2 m_localAnchorA;
00149         b2Vec2 m_localAnchorB;
00150         b2Vec2 m_localXAxisA;
00151         b2Vec2 m_localYAxisA;
00152 
00153         float32 m_impulse;
00154         float32 m_motorImpulse;
00155         float32 m_springImpulse;
00156 
00157         float32 m_maxMotorTorque;
00158         float32 m_motorSpeed;
00159         bool m_enableMotor;
00160 
00161         // Solver temp
00162         int32 m_indexA;
00163         int32 m_indexB;
00164         b2Vec2 m_localCenterA;
00165         b2Vec2 m_localCenterB;
00166         float32 m_invMassA;
00167         float32 m_invMassB;
00168         float32 m_invIA;
00169         float32 m_invIB;
00170 
00171         b2Vec2 m_ax, m_ay;
00172         float32 m_sAx, m_sBx;
00173         float32 m_sAy, m_sBy;
00174 
00175         float32 m_mass;
00176         float32 m_motorMass;
00177         float32 m_springMass;
00178 
00179         float32 m_bias;
00180         float32 m_gamma;
00181 };
00182 
00183 inline float32 b2WheelJoint::GetMotorSpeed() const
00184 {
00185         return m_motorSpeed;
00186 }
00187 
00188 inline float32 b2WheelJoint::GetMaxMotorTorque() const
00189 {
00190         return m_maxMotorTorque;
00191 }
00192 
00193 inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz)
00194 {
00195         m_frequencyHz = hz;
00196 }
00197 
00198 inline float32 b2WheelJoint::GetSpringFrequencyHz() const
00199 {
00200         return m_frequencyHz;
00201 }
00202 
00203 inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio)
00204 {
00205         m_dampingRatio = ratio;
00206 }
00207 
00208 inline float32 b2WheelJoint::GetSpringDampingRatio() const
00209 {
00210         return m_dampingRatio;
00211 }
00212 
00213 #endif
 All Classes Files Functions Variables Enumerations Enumerator Defines