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_WELD_JOINT_H
0020 #define B2_WELD_JOINT_H
0021 
0022 #include <Box2D/Dynamics/Joints/b2Joint.h>
0023 
0024 /// Weld joint definition. You need to specify local anchor points
0025 /// where they are attached and the relative body angle. The position
0026 /// of the anchor points is important for computing the reaction torque.
0027 struct b2WeldJointDef : public b2JointDef
0028 {
0029     b2WeldJointDef()
0030     {
0031         type = e_weldJoint;
0032         localAnchorA.Set(0.0f, 0.0f);
0033         localAnchorB.Set(0.0f, 0.0f);
0034         referenceAngle = 0.0f;
0035         frequencyHz = 0.0f;
0036         dampingRatio = 0.0f;
0037     }
0038 
0039     /// Initialize the bodies, anchors, and reference angle using a world
0040     /// anchor point.
0041     void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
0042 
0043     /// The local anchor point relative to bodyA's origin.
0044     b2Vec2 localAnchorA;
0045 
0046     /// The local anchor point relative to bodyB's origin.
0047     b2Vec2 localAnchorB;
0048 
0049     /// The bodyB angle minus bodyA angle in the reference state (radians).
0050     float32 referenceAngle;
0051     
0052     /// The mass-spring-damper frequency in Hertz. Rotation only.
0053     /// Disable softness with a value of 0.
0054     float32 frequencyHz;
0055 
0056     /// The damping ratio. 0 = no damping, 1 = critical damping.
0057     float32 dampingRatio;
0058 };
0059 
0060 /// A weld joint essentially glues two bodies together. A weld joint may
0061 /// distort somewhat because the island constraint solver is approximate.
0062 class b2WeldJoint : public b2Joint
0063 {
0064 public:
0065     b2Vec2 GetAnchorA() const;
0066     b2Vec2 GetAnchorB() const;
0067 
0068     b2Vec2 GetReactionForce(float32 inv_dt) const;
0069     float32 GetReactionTorque(float32 inv_dt) const;
0070 
0071     /// The local anchor point relative to bodyA's origin.
0072     const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
0073 
0074     /// The local anchor point relative to bodyB's origin.
0075     const b2Vec2& GetLocalAnchorB() const  { return m_localAnchorB; }
0076 
0077     /// Get the reference angle.
0078     float32 GetReferenceAngle() const { return m_referenceAngle; }
0079 
0080     /// Set/get frequency in Hz.
0081     void SetFrequency(float32 hz) { m_frequencyHz = hz; }
0082     float32 GetFrequency() const { return m_frequencyHz; }
0083 
0084     /// Set/get damping ratio.
0085     void SetDampingRatio(float32 ratio) { m_dampingRatio = ratio; }
0086     float32 GetDampingRatio() const { return m_dampingRatio; }
0087 
0088     /// Dump to b2Log
0089     void Dump();
0090 
0091 protected:
0092 
0093     friend class b2Joint;
0094 
0095     b2WeldJoint(const b2WeldJointDef* def);
0096 
0097     void InitVelocityConstraints(const b2SolverData& data);
0098     void SolveVelocityConstraints(const b2SolverData& data);
0099     bool SolvePositionConstraints(const b2SolverData& data);
0100 
0101     float32 m_frequencyHz;
0102     float32 m_dampingRatio;
0103     float32 m_bias;
0104 
0105     // Solver shared
0106     b2Vec2 m_localAnchorA;
0107     b2Vec2 m_localAnchorB;
0108     float32 m_referenceAngle;
0109     float32 m_gamma;
0110     b2Vec3 m_impulse;
0111 
0112     // Solver temp
0113     int32 m_indexA;
0114     int32 m_indexB;
0115     b2Vec2 m_rA;
0116     b2Vec2 m_rB;
0117     b2Vec2 m_localCenterA;
0118     b2Vec2 m_localCenterB;
0119     float32 m_invMassA;
0120     float32 m_invMassB;
0121     float32 m_invIA;
0122     float32 m_invIB;
0123     b2Mat33 m_mass;
0124 };
0125 
0126 #endif