File indexing completed on 2024-12-29 03:29:28

0001 /*
0002 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #ifndef B2_PULLEY_JOINT_H
0020 #define B2_PULLEY_JOINT_H
0021 
0022 #include <Box2D/Dynamics/Joints/b2Joint.h>
0023 
0024 const float32 b2_minPulleyLength = 2.0f;
0025 
0026 /// Pulley joint definition. This requires two ground anchors,
0027 /// two dynamic body anchor points, and a pulley ratio.
0028 struct b2PulleyJointDef : public b2JointDef
0029 {
0030     b2PulleyJointDef()
0031     {
0032         type = e_pulleyJoint;
0033         groundAnchorA.Set(-1.0f, 1.0f);
0034         groundAnchorB.Set(1.0f, 1.0f);
0035         localAnchorA.Set(-1.0f, 0.0f);
0036         localAnchorB.Set(1.0f, 0.0f);
0037         lengthA = 0.0f;
0038         lengthB = 0.0f;
0039         ratio = 1.0f;
0040         collideConnected = true;
0041     }
0042 
0043     /// Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
0044     void Initialize(b2Body* bodyA, b2Body* bodyB,
0045                     const b2Vec2& groundAnchorA, const b2Vec2& groundAnchorB,
0046                     const b2Vec2& anchorA, const b2Vec2& anchorB,
0047                     float32 ratio);
0048 
0049     /// The first ground anchor in world coordinates. This point never moves.
0050     b2Vec2 groundAnchorA;
0051 
0052     /// The second ground anchor in world coordinates. This point never moves.
0053     b2Vec2 groundAnchorB;
0054 
0055     /// The local anchor point relative to bodyA's origin.
0056     b2Vec2 localAnchorA;
0057 
0058     /// The local anchor point relative to bodyB's origin.
0059     b2Vec2 localAnchorB;
0060 
0061     /// The a reference length for the segment attached to bodyA.
0062     float32 lengthA;
0063 
0064     /// The a reference length for the segment attached to bodyB.
0065     float32 lengthB;
0066 
0067     /// The pulley ratio, used to simulate a block-and-tackle.
0068     float32 ratio;
0069 };
0070 
0071 /// The pulley joint is connected to two bodies and two fixed ground points.
0072 /// The pulley supports a ratio such that:
0073 /// length1 + ratio * length2 <= constant
0074 /// Yes, the force transmitted is scaled by the ratio.
0075 /// Warning: the pulley joint can get a bit squirrelly by itself. They often
0076 /// work better when combined with prismatic joints. You should also cover the
0077 /// the anchor points with static shapes to prevent one side from going to
0078 /// zero length.
0079 class b2PulleyJoint : public b2Joint
0080 {
0081 public:
0082     b2Vec2 GetAnchorA() const;
0083     b2Vec2 GetAnchorB() const;
0084 
0085     b2Vec2 GetReactionForce(float32 inv_dt) const;
0086     float32 GetReactionTorque(float32 inv_dt) const;
0087 
0088     /// Get the first ground anchor.
0089     b2Vec2 GetGroundAnchorA() const;
0090 
0091     /// Get the second ground anchor.
0092     b2Vec2 GetGroundAnchorB() const;
0093 
0094     /// Get the current length of the segment attached to bodyA.
0095     float32 GetLengthA() const;
0096 
0097     /// Get the current length of the segment attached to bodyB.
0098     float32 GetLengthB() const;
0099 
0100     /// Get the pulley ratio.
0101     float32 GetRatio() const;
0102 
0103     /// Get the current length of the segment attached to bodyA.
0104     float32 GetCurrentLengthA() const;
0105 
0106     /// Get the current length of the segment attached to bodyB.
0107     float32 GetCurrentLengthB() const;
0108 
0109     /// Dump joint to dmLog
0110     void Dump();
0111 
0112     /// Implement b2Joint::ShiftOrigin
0113     void ShiftOrigin(const b2Vec2& newOrigin);
0114 
0115 protected:
0116 
0117     friend class b2Joint;
0118     b2PulleyJoint(const b2PulleyJointDef* data);
0119 
0120     void InitVelocityConstraints(const b2SolverData& data);
0121     void SolveVelocityConstraints(const b2SolverData& data);
0122     bool SolvePositionConstraints(const b2SolverData& data);
0123 
0124     b2Vec2 m_groundAnchorA;
0125     b2Vec2 m_groundAnchorB;
0126     float32 m_lengthA;
0127     float32 m_lengthB;
0128     
0129     // Solver shared
0130     b2Vec2 m_localAnchorA;
0131     b2Vec2 m_localAnchorB;
0132     float32 m_constant;
0133     float32 m_ratio;
0134     float32 m_impulse;
0135 
0136     // Solver temp
0137     int32 m_indexA;
0138     int32 m_indexB;
0139     b2Vec2 m_uA;
0140     b2Vec2 m_uB;
0141     b2Vec2 m_rA;
0142     b2Vec2 m_rB;
0143     b2Vec2 m_localCenterA;
0144     b2Vec2 m_localCenterB;
0145     float32 m_invMassA;
0146     float32 m_invMassB;
0147     float32 m_invIA;
0148     float32 m_invIB;
0149     float32 m_mass;
0150 };
0151 
0152 #endif