File indexing completed on 2025-08-03 03:49:58

0001 /*
0002 * Copyright (c) 2006-2007 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_DISTANCE_JOINT_H
0020 #define B2_DISTANCE_JOINT_H
0021 
0022 #include <Box2D/Dynamics/Joints/b2Joint.h>
0023 
0024 /// Distance joint definition. This requires defining an
0025 /// anchor point on both bodies and the non-zero length of the
0026 /// distance joint. The definition uses local anchor points
0027 /// so that the initial configuration can violate the constraint
0028 /// slightly. This helps when saving and loading a game.
0029 /// @warning Do not use a zero or short length.
0030 struct b2DistanceJointDef : public b2JointDef
0031 {
0032     b2DistanceJointDef()
0033     {
0034         type = e_distanceJoint;
0035         localAnchorA.Set(0.0f, 0.0f);
0036         localAnchorB.Set(0.0f, 0.0f);
0037         length = 1.0f;
0038         frequencyHz = 0.0f;
0039         dampingRatio = 0.0f;
0040     }
0041 
0042     /// Initialize the bodies, anchors, and length using the world
0043     /// anchors.
0044     void Initialize(b2Body* bodyA, b2Body* bodyB,
0045                     const b2Vec2& anchorA, const b2Vec2& anchorB);
0046 
0047     /// The local anchor point relative to body1's origin.
0048     b2Vec2 localAnchorA;
0049 
0050     /// The local anchor point relative to body2's origin.
0051     b2Vec2 localAnchorB;
0052 
0053     /// The natural length between the anchor points.
0054     qreal length;
0055 
0056     /// The mass-spring-damper frequency in Hertz.
0057     qreal frequencyHz;
0058 
0059     /// The damping ratio. 0 = no damping, 1 = critical damping.
0060     qreal dampingRatio;
0061 };
0062 
0063 /// A distance joint constrains two points on two bodies
0064 /// to remain at a fixed distance from each other. You can view
0065 /// this as a massless, rigid rod.
0066 class b2DistanceJoint : public b2Joint
0067 {
0068 public:
0069 
0070     b2Vec2 GetAnchorA() const override;
0071     b2Vec2 GetAnchorB() const override;
0072 
0073     /// Get the reaction force given the inverse time step.
0074     /// Unit is N.
0075     b2Vec2 GetReactionForce(qreal inv_dt) const override;
0076 
0077     /// Get the reaction torque given the inverse time step.
0078     /// Unit is N*m. This is always zero for a distance joint.
0079     qreal GetReactionTorque(qreal inv_dt) const override;
0080 
0081     /// Set/get the natural length.
0082     /// Manipulating the length can lead to non-physical behavior when the frequency is zero.
0083     void SetLength(qreal length);
0084     qreal GetLength() const;
0085 
0086     // Set/get frequency in Hz.
0087     void SetFrequency(qreal hz);
0088     qreal GetFrequency() const;
0089 
0090     // Set/get damping ratio.
0091     void SetDampingRatio(qreal ratio);
0092     qreal GetDampingRatio() const;
0093 
0094 protected:
0095 
0096     friend class b2Joint;
0097     b2DistanceJoint(const b2DistanceJointDef* data);
0098 
0099     void InitVelocityConstraints(const b2TimeStep& step) override;
0100     void SolveVelocityConstraints(const b2TimeStep& step) override;
0101     bool SolvePositionConstraints(qreal baumgarte) override;
0102 
0103     b2Vec2 m_localAnchor1;
0104     b2Vec2 m_localAnchor2;
0105     b2Vec2 m_u;
0106     qreal m_frequencyHz;
0107     qreal m_dampingRatio;
0108     qreal m_gamma;
0109     qreal m_bias;
0110     qreal m_impulse;
0111     qreal m_mass;
0112     qreal m_length;
0113 };
0114 
0115 inline void b2DistanceJoint::SetLength(qreal length)
0116 {
0117     m_length = length;
0118 }
0119 
0120 inline qreal b2DistanceJoint::GetLength() const
0121 {
0122     return m_length;
0123 }
0124 
0125 inline void b2DistanceJoint::SetFrequency(qreal hz)
0126 {
0127     m_frequencyHz = hz;
0128 }
0129 
0130 inline qreal b2DistanceJoint::GetFrequency() const
0131 {
0132     return m_frequencyHz;
0133 }
0134 
0135 inline void b2DistanceJoint::SetDampingRatio(qreal ratio)
0136 {
0137     m_dampingRatio = ratio;
0138 }
0139 
0140 inline qreal b2DistanceJoint::GetDampingRatio() const
0141 {
0142     return m_dampingRatio;
0143 }
0144 
0145 #endif