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_PULLEY_JOINT_H 20 #define B2_PULLEY_JOINT_H 21 22 #include <Box2D/Dynamics/Joints/b2Joint.h> 23 24 const float32 b2_minPulleyLength = 2.0f; 25 26 /// Pulley joint definition. This requires two ground anchors, 27 /// two dynamic body anchor points, and a pulley ratio. 28 struct b2PulleyJointDef : public b2JointDef 29 { b2PulleyJointDefb2PulleyJointDef30 b2PulleyJointDef() 31 { 32 type = e_pulleyJoint; 33 groundAnchorA.Set(-1.0f, 1.0f); 34 groundAnchorB.Set(1.0f, 1.0f); 35 localAnchorA.Set(-1.0f, 0.0f); 36 localAnchorB.Set(1.0f, 0.0f); 37 lengthA = 0.0f; 38 lengthB = 0.0f; 39 ratio = 1.0f; 40 collideConnected = true; 41 } 42 43 /// Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors. 44 void Initialize(b2Body* bodyA, b2Body* bodyB, 45 const b2Vec2& groundAnchorA, const b2Vec2& groundAnchorB, 46 const b2Vec2& anchorA, const b2Vec2& anchorB, 47 float32 ratio); 48 49 /// The first ground anchor in world coordinates. This point never moves. 50 b2Vec2 groundAnchorA; 51 52 /// The second ground anchor in world coordinates. This point never moves. 53 b2Vec2 groundAnchorB; 54 55 /// The local anchor point relative to bodyA's origin. 56 b2Vec2 localAnchorA; 57 58 /// The local anchor point relative to bodyB's origin. 59 b2Vec2 localAnchorB; 60 61 /// The a reference length for the segment attached to bodyA. 62 float32 lengthA; 63 64 /// The a reference length for the segment attached to bodyB. 65 float32 lengthB; 66 67 /// The pulley ratio, used to simulate a block-and-tackle. 68 float32 ratio; 69 }; 70 71 /// The pulley joint is connected to two bodies and two fixed ground points. 72 /// The pulley supports a ratio such that: 73 /// length1 + ratio * length2 <= constant 74 /// Yes, the force transmitted is scaled by the ratio. 75 /// Warning: the pulley joint can get a bit squirrelly by itself. They often 76 /// work better when combined with prismatic joints. You should also cover the 77 /// the anchor points with static shapes to prevent one side from going to 78 /// zero length. 79 class b2PulleyJoint : public b2Joint 80 { 81 public: 82 b2Vec2 GetAnchorA() const; 83 b2Vec2 GetAnchorB() const; 84 85 b2Vec2 GetReactionForce(float32 inv_dt) const; 86 float32 GetReactionTorque(float32 inv_dt) const; 87 88 /// Get the first ground anchor. 89 b2Vec2 GetGroundAnchorA() const; 90 91 /// Get the second ground anchor. 92 b2Vec2 GetGroundAnchorB() const; 93 94 /// Get the current length of the segment attached to bodyA. 95 float32 GetLengthA() const; 96 97 /// Get the current length of the segment attached to bodyB. 98 float32 GetLengthB() const; 99 100 /// Get the pulley ratio. 101 float32 GetRatio() const; 102 103 /// Get the current length of the segment attached to bodyA. 104 float32 GetCurrentLengthA() const; 105 106 /// Get the current length of the segment attached to bodyB. 107 float32 GetCurrentLengthB() const; 108 109 /// Dump joint to dmLog 110 void Dump(); 111 112 /// Implement b2Joint::ShiftOrigin 113 void ShiftOrigin(const b2Vec2& newOrigin); 114 115 protected: 116 117 friend class b2Joint; 118 b2PulleyJoint(const b2PulleyJointDef* data); 119 120 void InitVelocityConstraints(const b2SolverData& data); 121 void SolveVelocityConstraints(const b2SolverData& data); 122 bool SolvePositionConstraints(const b2SolverData& data); 123 124 b2Vec2 m_groundAnchorA; 125 b2Vec2 m_groundAnchorB; 126 float32 m_lengthA; 127 float32 m_lengthB; 128 129 // Solver shared 130 b2Vec2 m_localAnchorA; 131 b2Vec2 m_localAnchorB; 132 float32 m_constant; 133 float32 m_ratio; 134 float32 m_impulse; 135 136 // Solver temp 137 int32 m_indexA; 138 int32 m_indexB; 139 b2Vec2 m_uA; 140 b2Vec2 m_uB; 141 b2Vec2 m_rA; 142 b2Vec2 m_rB; 143 b2Vec2 m_localCenterA; 144 b2Vec2 m_localCenterB; 145 float32 m_invMassA; 146 float32 m_invMassB; 147 float32 m_invIA; 148 float32 m_invIB; 149 float32 m_mass; 150 }; 151 152 #endif 153