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

0001 /*
0002 * Copyright (c) 2006-2012 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_MOTOR_JOINT_H
0020 #define B2_MOTOR_JOINT_H
0021 
0022 #include <Box2D/Dynamics/Joints/b2Joint.h>
0023 
0024 /// Motor joint definition.
0025 struct b2MotorJointDef : public b2JointDef
0026 {
0027     b2MotorJointDef()
0028     {
0029         type = e_motorJoint;
0030         linearOffset.SetZero();
0031         angularOffset = 0.0f;
0032         maxForce = 1.0f;
0033         maxTorque = 1.0f;
0034         correctionFactor = 0.3f;
0035     }
0036 
0037     /// Initialize the bodies and offsets using the current transforms.
0038     void Initialize(b2Body* bodyA, b2Body* bodyB);
0039 
0040     /// Position of bodyB minus the position of bodyA, in bodyA's frame, in meters.
0041     b2Vec2 linearOffset;
0042 
0043     /// The bodyB angle minus bodyA angle in radians.
0044     float32 angularOffset;
0045     
0046     /// The maximum motor force in N.
0047     float32 maxForce;
0048 
0049     /// The maximum motor torque in N-m.
0050     float32 maxTorque;
0051 
0052     /// Position correction factor in the range [0,1].
0053     float32 correctionFactor;
0054 };
0055 
0056 /// A motor joint is used to control the relative motion
0057 /// between two bodies. A typical usage is to control the movement
0058 /// of a dynamic body with respect to the ground.
0059 class b2MotorJoint : public b2Joint
0060 {
0061 public:
0062     b2Vec2 GetAnchorA() const;
0063     b2Vec2 GetAnchorB() const;
0064 
0065     b2Vec2 GetReactionForce(float32 inv_dt) const;
0066     float32 GetReactionTorque(float32 inv_dt) const;
0067 
0068     /// Set/get the target linear offset, in frame A, in meters.
0069     void SetLinearOffset(const b2Vec2& linearOffset);
0070     const b2Vec2& GetLinearOffset() const;
0071 
0072     /// Set/get the target angular offset, in radians.
0073     void SetAngularOffset(float32 angularOffset);
0074     float32 GetAngularOffset() const;
0075 
0076     /// Set the maximum friction force in N.
0077     void SetMaxForce(float32 force);
0078 
0079     /// Get the maximum friction force in N.
0080     float32 GetMaxForce() const;
0081 
0082     /// Set the maximum friction torque in N*m.
0083     void SetMaxTorque(float32 torque);
0084 
0085     /// Get the maximum friction torque in N*m.
0086     float32 GetMaxTorque() const;
0087 
0088     /// Set the position correction factor in the range [0,1].
0089     void SetCorrectionFactor(float32 factor);
0090 
0091     /// Get the position correction factor in the range [0,1].
0092     float32 GetCorrectionFactor() const;
0093 
0094     /// Dump to b2Log
0095     void Dump();
0096 
0097 protected:
0098 
0099     friend class b2Joint;
0100 
0101     b2MotorJoint(const b2MotorJointDef* def);
0102 
0103     void InitVelocityConstraints(const b2SolverData& data);
0104     void SolveVelocityConstraints(const b2SolverData& data);
0105     bool SolvePositionConstraints(const b2SolverData& data);
0106 
0107     // Solver shared
0108     b2Vec2 m_linearOffset;
0109     float32 m_angularOffset;
0110     b2Vec2 m_linearImpulse;
0111     float32 m_angularImpulse;
0112     float32 m_maxForce;
0113     float32 m_maxTorque;
0114     float32 m_correctionFactor;
0115 
0116     // Solver temp
0117     int32 m_indexA;
0118     int32 m_indexB;
0119     b2Vec2 m_rA;
0120     b2Vec2 m_rB;
0121     b2Vec2 m_localCenterA;
0122     b2Vec2 m_localCenterB;
0123     b2Vec2 m_linearError;
0124     float32 m_angularError;
0125     float32 m_invMassA;
0126     float32 m_invMassB;
0127     float32 m_invIA;
0128     float32 m_invIB;
0129     b2Mat22 m_linearMass;
0130     float32 m_angularMass;
0131 };
0132 
0133 #endif