![]() |
Box2D
2.2.0
A 2D Physics Engine for Games
|
00001 /* 00002 * Copyright (c) 2006-2007 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_DISTANCE_JOINT_H 00020 #define B2_DISTANCE_JOINT_H 00021 00022 #include <Box2D/Dynamics/Joints/b2Joint.h> 00023 00030 struct b2DistanceJointDef : public b2JointDef 00031 { 00032 b2DistanceJointDef() 00033 { 00034 type = e_distanceJoint; 00035 localAnchorA.Set(0.0f, 0.0f); 00036 localAnchorB.Set(0.0f, 0.0f); 00037 length = 1.0f; 00038 frequencyHz = 0.0f; 00039 dampingRatio = 0.0f; 00040 } 00041 00044 void Initialize(b2Body* bodyA, b2Body* bodyB, 00045 const b2Vec2& anchorA, const b2Vec2& anchorB); 00046 00048 b2Vec2 localAnchorA; 00049 00051 b2Vec2 localAnchorB; 00052 00054 float32 length; 00055 00057 float32 frequencyHz; 00058 00060 float32 dampingRatio; 00061 }; 00062 00066 class b2DistanceJoint : public b2Joint 00067 { 00068 public: 00069 00070 b2Vec2 GetAnchorA() const; 00071 b2Vec2 GetAnchorB() const; 00072 00075 b2Vec2 GetReactionForce(float32 inv_dt) const; 00076 00079 float32 GetReactionTorque(float32 inv_dt) const; 00080 00083 void SetLength(float32 length); 00084 float32 GetLength() const; 00085 00086 // Set/get frequency in Hz. 00087 void SetFrequency(float32 hz); 00088 float32 GetFrequency() const; 00089 00090 // Set/get damping ratio. 00091 void SetDampingRatio(float32 ratio); 00092 float32 GetDampingRatio() const; 00093 00094 protected: 00095 00096 friend class b2Joint; 00097 b2DistanceJoint(const b2DistanceJointDef* data); 00098 00099 void InitVelocityConstraints(const b2SolverData& data); 00100 void SolveVelocityConstraints(const b2SolverData& data); 00101 bool SolvePositionConstraints(const b2SolverData& data); 00102 00103 float32 m_frequencyHz; 00104 float32 m_dampingRatio; 00105 float32 m_bias; 00106 00107 // Solver shared 00108 b2Vec2 m_localAnchorA; 00109 b2Vec2 m_localAnchorB; 00110 float32 m_gamma; 00111 float32 m_impulse; 00112 float32 m_length; 00113 00114 // Solver temp 00115 int32 m_indexA; 00116 int32 m_indexB; 00117 b2Vec2 m_u; 00118 b2Vec2 m_rA; 00119 b2Vec2 m_rB; 00120 b2Vec2 m_localCenterA; 00121 b2Vec2 m_localCenterB; 00122 float32 m_invMassA; 00123 float32 m_invMassB; 00124 float32 m_invIA; 00125 float32 m_invIB; 00126 float32 m_mass; 00127 }; 00128 00129 inline void b2DistanceJoint::SetLength(float32 length) 00130 { 00131 m_length = length; 00132 } 00133 00134 inline float32 b2DistanceJoint::GetLength() const 00135 { 00136 return m_length; 00137 } 00138 00139 inline void b2DistanceJoint::SetFrequency(float32 hz) 00140 { 00141 m_frequencyHz = hz; 00142 } 00143 00144 inline float32 b2DistanceJoint::GetFrequency() const 00145 { 00146 return m_frequencyHz; 00147 } 00148 00149 inline void b2DistanceJoint::SetDampingRatio(float32 ratio) 00150 { 00151 m_dampingRatio = ratio; 00152 } 00153 00154 inline float32 b2DistanceJoint::GetDampingRatio() const 00155 { 00156 return m_dampingRatio; 00157 } 00158 00159 #endif