Box2D  2.2.0
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         b2Vec2 GetAnchorA() const;
00083         b2Vec2 GetAnchorB() const;
00084 
00085         b2Vec2 GetReactionForce(float32 inv_dt) const;
00086         float32 GetReactionTorque(float32 inv_dt) const;
00087 
00089         float32 GetJointTranslation() const;
00090 
00092         float32 GetJointSpeed() const;
00093 
00095         bool IsMotorEnabled() const;
00096 
00098         void EnableMotor(bool flag);
00099 
00101         void SetMotorSpeed(float32 speed);
00102 
00104         float32 GetMotorSpeed() const;
00105 
00107         void SetMaxMotorTorque(float32 torque);
00108         float32 GetMaxMotorTorque() const;
00109 
00111         float32 GetMotorTorque(float32 inv_dt) const;
00112 
00114         void SetSpringFrequencyHz(float32 hz);
00115         float32 GetSpringFrequencyHz() const;
00116 
00118         void SetSpringDampingRatio(float32 ratio);
00119         float32 GetSpringDampingRatio() const;
00120 
00121 protected:
00122 
00123         friend class b2Joint;
00124         b2WheelJoint(const b2WheelJointDef* def);
00125 
00126         void InitVelocityConstraints(const b2SolverData& data);
00127         void SolveVelocityConstraints(const b2SolverData& data);
00128         bool SolvePositionConstraints(const b2SolverData& data);
00129 
00130         float32 m_frequencyHz;
00131         float32 m_dampingRatio;
00132 
00133         // Solver shared
00134         b2Vec2 m_localAnchorA;
00135         b2Vec2 m_localAnchorB;
00136         b2Vec2 m_localXAxisA;
00137         b2Vec2 m_localYAxisA;
00138 
00139         float32 m_impulse;
00140         float32 m_motorImpulse;
00141         float32 m_springImpulse;
00142 
00143         float32 m_maxMotorTorque;
00144         float32 m_motorSpeed;
00145         bool m_enableMotor;
00146 
00147         // Solver temp
00148         int32 m_indexA;
00149         int32 m_indexB;
00150         b2Vec2 m_localCenterA;
00151         b2Vec2 m_localCenterB;
00152         float32 m_invMassA;
00153         float32 m_invMassB;
00154         float32 m_invIA;
00155         float32 m_invIB;
00156 
00157         b2Vec2 m_ax, m_ay;
00158         float32 m_sAx, m_sBx;
00159         float32 m_sAy, m_sBy;
00160 
00161         float32 m_mass;
00162         float32 m_motorMass;
00163         float32 m_springMass;
00164 
00165         float32 m_bias;
00166         float32 m_gamma;
00167 };
00168 
00169 inline float32 b2WheelJoint::GetMotorSpeed() const
00170 {
00171         return m_motorSpeed;
00172 }
00173 
00174 inline float32 b2WheelJoint::GetMaxMotorTorque() const
00175 {
00176         return m_maxMotorTorque;
00177 }
00178 
00179 inline void b2WheelJoint::SetSpringFrequencyHz(float32 hz)
00180 {
00181         m_frequencyHz = hz;
00182 }
00183 
00184 inline float32 b2WheelJoint::GetSpringFrequencyHz() const
00185 {
00186         return m_frequencyHz;
00187 }
00188 
00189 inline void b2WheelJoint::SetSpringDampingRatio(float32 ratio)
00190 {
00191         m_dampingRatio = ratio;
00192 }
00193 
00194 inline float32 b2WheelJoint::GetSpringDampingRatio() const
00195 {
00196         return m_dampingRatio;
00197 }
00198 
00199 #endif
 All Classes Files Functions Variables Enumerations Enumerator Defines