File indexing completed on 2025-08-03 03:50:00

0001 /*
0002 * Copyright (c) 2006-2010 Erin Catto http://www.gphysics.com
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_ROPE_JOINT_H
0020 #define B2_ROPE_JOINT_H
0021 
0022 #include <Box2D/Dynamics/Joints/b2Joint.h>
0023 
0024 /// Rope joint definition. This requires two body anchor points and
0025 /// a maximum lengths.
0026 /// Note: by default the connected objects will not collide.
0027 /// see collideConnected in b2JointDef.
0028 struct b2RopeJointDef : public b2JointDef
0029 {
0030     b2RopeJointDef()
0031     {
0032         type = e_ropeJoint;
0033         localAnchorA.Set(-1.0f, 0.0f);
0034         localAnchorB.Set(1.0f, 0.0f);
0035         maxLength = 0.0f;
0036     }
0037 
0038     /// The local anchor point relative to bodyA's origin.
0039     b2Vec2 localAnchorA;
0040 
0041     /// The local anchor point relative to bodyB's origin.
0042     b2Vec2 localAnchorB;
0043 
0044     /// The maximum length of the rope.
0045     /// Warning: this must be larger than b2_linearSlop or
0046     /// the joint will have no effect.
0047     qreal maxLength;
0048 };
0049 
0050 /// A rope joint enforces a maximum distance between two points
0051 /// on two bodies. It has no other effect.
0052 /// Warning: if you attempt to change the maximum length during
0053 /// the simulation you will get some non-physical behavior.
0054 /// A model that would allow you to dynamically modify the length
0055 /// would have some sponginess, so I chose not to implement it
0056 /// that way. See b2DistanceJoint if you want to dynamically
0057 /// control length.
0058 class b2RopeJoint : public b2Joint
0059 {
0060 public:
0061     b2Vec2 GetAnchorA() const override;
0062     b2Vec2 GetAnchorB() const override;
0063 
0064     b2Vec2 GetReactionForce(qreal inv_dt) const override;
0065     qreal GetReactionTorque(qreal inv_dt) const override;
0066 
0067     /// Get the maximum length of the rope.
0068     qreal GetMaxLength() const;
0069 
0070     b2LimitState GetLimitState() const;
0071 
0072 protected:
0073 
0074     friend class b2Joint;
0075     b2RopeJoint(const b2RopeJointDef* data);
0076 
0077     void InitVelocityConstraints(const b2TimeStep& step) override;
0078     void SolveVelocityConstraints(const b2TimeStep& step) override;
0079     bool SolvePositionConstraints(qreal baumgarte) override;
0080 
0081     b2Vec2 m_localAnchorA;
0082     b2Vec2 m_localAnchorB;
0083 
0084     qreal m_maxLength;
0085     qreal m_length;
0086 
0087     // Jacobian info
0088     b2Vec2 m_u, m_rA, m_rB;
0089 
0090     // Effective mass
0091     qreal m_mass;
0092 
0093     // Impulses for accumulation/warm starting.
0094     qreal m_impulse;
0095 
0096     b2LimitState m_state;
0097 };
0098 
0099 #endif