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_GEAR_JOINT_H
0020 #define B2_GEAR_JOINT_H
0021 
0022 #include <Box2D/Dynamics/Joints/b2Joint.h>
0023 
0024 class b2RevoluteJoint;
0025 class b2PrismaticJoint;
0026 
0027 /// Gear joint definition. This definition requires two existing
0028 /// revolute or prismatic joints (any combination will work).
0029 /// The provided joints must attach a dynamic body to a static body.
0030 struct b2GearJointDef : public b2JointDef
0031 {
0032     b2GearJointDef()
0033     {
0034         type = e_gearJoint;
0035         joint1 = NULL;
0036         joint2 = NULL;
0037         ratio = 1.0f;
0038     }
0039 
0040     /// The first revolute/prismatic joint attached to the gear joint.
0041     b2Joint* joint1;
0042 
0043     /// The second revolute/prismatic joint attached to the gear joint.
0044     b2Joint* joint2;
0045 
0046     /// The gear ratio.
0047     /// @see b2GearJoint for explanation.
0048     qreal ratio;
0049 };
0050 
0051 /// A gear joint is used to connect two joints together. Either joint
0052 /// can be a revolute or prismatic joint. You specify a gear ratio
0053 /// to bind the motions together:
0054 /// coordinate1 + ratio * coordinate2 = constant
0055 /// The ratio can be negative or positive. If one joint is a revolute joint
0056 /// and the other joint is a prismatic joint, then the ratio will have units
0057 /// of length or units of 1/length.
0058 /// @warning The revolute and prismatic joints must be attached to
0059 /// fixed bodies (which must be body1 on those joints).
0060 class b2GearJoint : public b2Joint
0061 {
0062 public:
0063     b2Vec2 GetAnchorA() const override;
0064     b2Vec2 GetAnchorB() const override;
0065 
0066     b2Vec2 GetReactionForce(qreal inv_dt) const override;
0067     qreal GetReactionTorque(qreal inv_dt) const override;
0068 
0069     /// Set/Get the gear ratio.
0070     void SetRatio(qreal ratio);
0071     qreal GetRatio() const;
0072 
0073 protected:
0074 
0075     friend class b2Joint;
0076     b2GearJoint(const b2GearJointDef* data);
0077 
0078     void InitVelocityConstraints(const b2TimeStep& step) override;
0079     void SolveVelocityConstraints(const b2TimeStep& step) override;
0080     bool SolvePositionConstraints(qreal baumgarte) override;
0081 
0082     b2Body* m_ground1;
0083     b2Body* m_ground2;
0084 
0085     // One of these is NULL.
0086     b2RevoluteJoint* m_revolute1;
0087     b2PrismaticJoint* m_prismatic1;
0088 
0089     // One of these is NULL.
0090     b2RevoluteJoint* m_revolute2;
0091     b2PrismaticJoint* m_prismatic2;
0092 
0093     b2Vec2 m_groundAnchor1;
0094     b2Vec2 m_groundAnchor2;
0095 
0096     b2Vec2 m_localAnchor1;
0097     b2Vec2 m_localAnchor2;
0098 
0099     b2Jacobian m_J;
0100 
0101     qreal m_constant;
0102     qreal m_ratio;
0103 
0104     // Effective mass
0105     qreal m_mass;
0106 
0107     // Impulse for accumulation/warm starting.
0108     qreal m_impulse;
0109 };
0110 
0111 #endif