File indexing completed on 2025-08-03 03:49:59
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_LINE_JOINT_H 0020 #define B2_LINE_JOINT_H 0021 0022 #include <Box2D/Dynamics/Joints/b2Joint.h> 0023 0024 /// Line joint definition. This requires defining a line of 0025 /// motion using an axis and an anchor point. The definition uses local 0026 /// anchor points and a local axis so that the initial configuration 0027 /// can violate the constraint slightly. The joint translation is zero 0028 /// when the local anchor points coincide in world space. Using local 0029 /// anchors and a local axis helps when saving and loading a game. 0030 struct b2LineJointDef : public b2JointDef 0031 { 0032 b2LineJointDef() 0033 { 0034 type = e_lineJoint; 0035 localAnchorA.SetZero(); 0036 localAnchorB.SetZero(); 0037 localAxisA.Set(1.0f, 0.0f); 0038 enableLimit = false; 0039 lowerTranslation = 0.0f; 0040 upperTranslation = 0.0f; 0041 enableMotor = false; 0042 maxMotorForce = 0.0f; 0043 motorSpeed = 0.0f; 0044 } 0045 0046 /// Initialize the bodies, anchors, axis, and reference angle using the world 0047 /// anchor and world axis. 0048 void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis); 0049 0050 /// The local anchor point relative to body1's origin. 0051 b2Vec2 localAnchorA; 0052 0053 /// The local anchor point relative to body2's origin. 0054 b2Vec2 localAnchorB; 0055 0056 /// The local translation axis in body1. 0057 b2Vec2 localAxisA; 0058 0059 /// Enable/disable the joint limit. 0060 bool enableLimit; 0061 0062 /// The lower translation limit, usually in meters. 0063 qreal lowerTranslation; 0064 0065 /// The upper translation limit, usually in meters. 0066 qreal upperTranslation; 0067 0068 /// Enable/disable the joint motor. 0069 bool enableMotor; 0070 0071 /// The maximum motor torque, usually in N-m. 0072 qreal maxMotorForce; 0073 0074 /// The desired motor speed in radians per second. 0075 qreal motorSpeed; 0076 }; 0077 0078 /// A line joint. This joint provides two degrees of freedom: translation 0079 /// along an axis fixed in body1 and rotation in the plane. You can use a 0080 /// joint limit to restrict the range of motion and a joint motor to drive 0081 /// the motion or to model joint friction. 0082 class b2LineJoint : public b2Joint 0083 { 0084 public: 0085 b2Vec2 GetAnchorA() const override; 0086 b2Vec2 GetAnchorB() const override; 0087 0088 b2Vec2 GetReactionForce(qreal inv_dt) const override; 0089 qreal GetReactionTorque(qreal inv_dt) const override; 0090 0091 /// Get the current joint translation, usually in meters. 0092 qreal GetJointTranslation() const; 0093 0094 /// Get the current joint translation speed, usually in meters per second. 0095 qreal GetJointSpeed() const; 0096 0097 /// Is the joint limit enabled? 0098 bool IsLimitEnabled() const; 0099 0100 /// Enable/disable the joint limit. 0101 void EnableLimit(bool flag); 0102 0103 /// Get the lower joint limit, usually in meters. 0104 qreal GetLowerLimit() const; 0105 0106 /// Get the upper joint limit, usually in meters. 0107 qreal GetUpperLimit() const; 0108 0109 /// Set the joint limits, usually in meters. 0110 void SetLimits(qreal lower, qreal upper); 0111 0112 /// Is the joint motor enabled? 0113 bool IsMotorEnabled() const; 0114 0115 /// Enable/disable the joint motor. 0116 void EnableMotor(bool flag); 0117 0118 /// Set the motor speed, usually in meters per second. 0119 void SetMotorSpeed(qreal speed); 0120 0121 /// Get the motor speed, usually in meters per second. 0122 qreal GetMotorSpeed() const; 0123 0124 /// Set/Get the maximum motor force, usually in N. 0125 void SetMaxMotorForce(qreal force); 0126 qreal GetMaxMotorForce() const; 0127 0128 /// Get the current motor force given the inverse time step, usually in N. 0129 qreal GetMotorForce(qreal inv_dt) const; 0130 0131 protected: 0132 0133 friend class b2Joint; 0134 b2LineJoint(const b2LineJointDef* def); 0135 0136 void InitVelocityConstraints(const b2TimeStep& step) override; 0137 void SolveVelocityConstraints(const b2TimeStep& step) override; 0138 bool SolvePositionConstraints(qreal baumgarte) override; 0139 0140 b2Vec2 m_localAnchor1; 0141 b2Vec2 m_localAnchor2; 0142 b2Vec2 m_localXAxis1; 0143 b2Vec2 m_localYAxis1; 0144 0145 b2Vec2 m_axis, m_perp; 0146 qreal m_s1, m_s2; 0147 qreal m_a1, m_a2; 0148 0149 b2Mat22 m_K; 0150 b2Vec2 m_impulse; 0151 0152 qreal m_motorMass; // effective mass for motor/limit translational constraint. 0153 qreal m_motorImpulse; 0154 0155 qreal m_lowerTranslation; 0156 qreal m_upperTranslation; 0157 qreal m_maxMotorForce; 0158 qreal m_motorSpeed; 0159 0160 bool m_enableLimit; 0161 bool m_enableMotor; 0162 b2LimitState m_limitState; 0163 }; 0164 0165 inline qreal b2LineJoint::GetMotorSpeed() const 0166 { 0167 return m_motorSpeed; 0168 } 0169 0170 inline qreal b2LineJoint::GetMaxMotorForce() const 0171 { 0172 return m_maxMotorForce; 0173 } 0174 0175 #endif