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

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_GEAR_JOINT_H
0020 #define B2_GEAR_JOINT_H
0021 
0022 #include <Box2D/Dynamics/Joints/b2Joint.h>
0023 
0024 /// Gear joint definition. This definition requires two existing
0025 /// revolute or prismatic joints (any combination will work).
0026 struct b2GearJointDef : public b2JointDef
0027 {
0028     b2GearJointDef()
0029     {
0030         type = e_gearJoint;
0031         joint1 = NULL;
0032         joint2 = NULL;
0033         ratio = 1.0f;
0034     }
0035 
0036     /// The first revolute/prismatic joint attached to the gear joint.
0037     b2Joint* joint1;
0038 
0039     /// The second revolute/prismatic joint attached to the gear joint.
0040     b2Joint* joint2;
0041 
0042     /// The gear ratio.
0043     /// @see b2GearJoint for explanation.
0044     float32 ratio;
0045 };
0046 
0047 /// A gear joint is used to connect two joints together. Either joint
0048 /// can be a revolute or prismatic joint. You specify a gear ratio
0049 /// to bind the motions together:
0050 /// coordinate1 + ratio * coordinate2 = constant
0051 /// The ratio can be negative or positive. If one joint is a revolute joint
0052 /// and the other joint is a prismatic joint, then the ratio will have units
0053 /// of length or units of 1/length.
0054 /// @warning You have to manually destroy the gear joint if joint1 or joint2
0055 /// is destroyed.
0056 class b2GearJoint : public b2Joint
0057 {
0058 public:
0059     b2Vec2 GetAnchorA() const;
0060     b2Vec2 GetAnchorB() const;
0061 
0062     b2Vec2 GetReactionForce(float32 inv_dt) const;
0063     float32 GetReactionTorque(float32 inv_dt) const;
0064 
0065     /// Get the first joint.
0066     b2Joint* GetJoint1() { return m_joint1; }
0067 
0068     /// Get the second joint.
0069     b2Joint* GetJoint2() { return m_joint2; }
0070 
0071     /// Set/Get the gear ratio.
0072     void SetRatio(float32 ratio);
0073     float32 GetRatio() const;
0074 
0075     /// Dump joint to dmLog
0076     void Dump();
0077 
0078 protected:
0079 
0080     friend class b2Joint;
0081     b2GearJoint(const b2GearJointDef* data);
0082 
0083     void InitVelocityConstraints(const b2SolverData& data);
0084     void SolveVelocityConstraints(const b2SolverData& data);
0085     bool SolvePositionConstraints(const b2SolverData& data);
0086 
0087     b2Joint* m_joint1;
0088     b2Joint* m_joint2;
0089 
0090     b2JointType m_typeA;
0091     b2JointType m_typeB;
0092 
0093     // Body A is connected to body C
0094     // Body B is connected to body D
0095     b2Body* m_bodyC;
0096     b2Body* m_bodyD;
0097 
0098     // Solver shared
0099     b2Vec2 m_localAnchorA;
0100     b2Vec2 m_localAnchorB;
0101     b2Vec2 m_localAnchorC;
0102     b2Vec2 m_localAnchorD;
0103 
0104     b2Vec2 m_localAxisC;
0105     b2Vec2 m_localAxisD;
0106 
0107     float32 m_referenceAngleA;
0108     float32 m_referenceAngleB;
0109 
0110     float32 m_constant;
0111     float32 m_ratio;
0112 
0113     float32 m_impulse;
0114 
0115     // Solver temp
0116     int32 m_indexA, m_indexB, m_indexC, m_indexD;
0117     b2Vec2 m_lcA, m_lcB, m_lcC, m_lcD;
0118     float32 m_mA, m_mB, m_mC, m_mD;
0119     float32 m_iA, m_iB, m_iC, m_iD;
0120     b2Vec2 m_JvAC, m_JvBD;
0121     float32 m_JwA, m_JwB, m_JwC, m_JwD;
0122     float32 m_mass;
0123 };
0124 
0125 #endif