2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
19 #ifndef B2_REVOLUTE_JOINT_H
20 #define B2_REVOLUTE_JOINT_H
24 /// Revolute joint definition. This requires defining an
25 /// anchor point where the bodies are joined. The definition
26 /// uses local anchor points so that the initial configuration
27 /// can violate the constraint slightly. You also need to
28 /// specify the initial relative angle for joint limits. This
29 /// helps when saving and loading a game.
30 /// The local anchor points are measured from the body's origin
31 /// rather than the center of mass because:
32 /// 1. you might not know where the center of mass will be.
33 /// 2. if you add/remove shapes from a body and recompute the mass,
34 /// the joints will be broken.
35 struct b2RevoluteJointDef : public b2JointDef
39 type = e_revoluteJoint;
40 localAnchor1.Set(0.0f, 0.0f);
41 localAnchor2.Set(0.0f, 0.0f);
42 referenceAngle = 0.0f;
45 maxMotorTorque = 0.0f;
51 /// Initialize the bodies, anchors, and reference angle using the world
53 void Initialize(b2Body* body1, b2Body* body2, const b2Vec2& anchor);
55 /// The local anchor point relative to body1's origin.
58 /// The local anchor point relative to body2's origin.
61 /// The body2 angle minus body1 angle in the reference state (radians).
62 float32 referenceAngle;
64 /// A flag to enable joint limits.
67 /// The lower angle for the joint limit (radians).
70 /// The upper angle for the joint limit (radians).
73 /// A flag to enable the joint motor.
76 /// The desired motor speed. Usually in radians per second.
79 /// The maximum motor torque used to achieve the desired motor speed.
81 float32 maxMotorTorque;
84 /// A revolute joint constrains to bodies to share a common point while they
85 /// are free to rotate about the point. The relative rotation about the shared
86 /// point is the joint angle. You can limit the relative rotation with
87 /// a joint limit that specifies a lower and upper angle. You can use a motor
88 /// to drive the relative rotation about the shared point. A maximum motor torque
89 /// is provided so that infinite forces are not generated.
90 class b2RevoluteJoint : public b2Joint
93 b2Vec2 GetAnchor1() const;
94 b2Vec2 GetAnchor2() const;
96 b2Vec2 GetReactionForce() const;
97 float32 GetReactionTorque() const;
99 /// Get the current joint angle in radians.
100 float32 GetJointAngle() const;
102 /// Get the current joint angle speed in radians per second.
103 float32 GetJointSpeed() const;
105 /// Is the joint limit enabled?
106 bool IsLimitEnabled() const;
108 /// Enable/disable the joint limit.
109 void EnableLimit(bool flag);
111 /// Get the lower joint limit in radians.
112 float32 GetLowerLimit() const;
114 /// Get the upper joint limit in radians.
115 float32 GetUpperLimit() const;
117 /// Set the joint limits in radians.
118 void SetLimits(float32 lower, float32 upper);
120 /// Is the joint motor enabled?
121 bool IsMotorEnabled() const;
123 /// Enable/disable the joint motor.
124 void EnableMotor(bool flag);
126 /// Set the motor speed in radians per second.
127 void SetMotorSpeed(float32 speed);
129 /// Get the motor speed in radians per second.
130 float32 GetMotorSpeed() const;
132 /// Set the maximum motor torque, usually in N-m.
133 void SetMaxMotorTorque(float32 torque);
135 /// Get the current motor torque, usually in N-m.
136 float32 GetMotorTorque() const;
138 //--------------- Internals Below -------------------
139 b2RevoluteJoint(const b2RevoluteJointDef* def);
141 void InitVelocityConstraints(const b2TimeStep& step);
142 void SolveVelocityConstraints(const b2TimeStep& step);
144 bool SolvePositionConstraints();
146 b2Vec2 m_localAnchor1; // relative
147 b2Vec2 m_localAnchor2;
149 float32 m_motorForce;
150 float32 m_limitForce;
151 float32 m_limitPositionImpulse;
153 b2Mat22 m_pivotMass; // effective mass for point-to-point constraint.
154 float32 m_motorMass; // effective mass for motor/limit angular constraint.
157 float32 m_maxMotorTorque;
158 float32 m_motorSpeed;
161 float32 m_referenceAngle;
162 float32 m_lowerAngle;
163 float32 m_upperAngle;
164 b2LimitState m_limitState;
167 inline float32 b2RevoluteJoint::GetMotorSpeed() const