first commit
[blok] / Box2D / Source / Dynamics / Joints / b2RevoluteJoint.h
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
3 *
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.
17 */
18
19 #ifndef B2_REVOLUTE_JOINT_H
20 #define B2_REVOLUTE_JOINT_H
21
22 #include "b2Joint.h"
23
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
36 {
37         b2RevoluteJointDef()
38         {
39                 type = e_revoluteJoint;
40                 localAnchor1.Set(0.0f, 0.0f);
41                 localAnchor2.Set(0.0f, 0.0f);
42                 referenceAngle = 0.0f;
43                 lowerAngle = 0.0f;
44                 upperAngle = 0.0f;
45                 maxMotorTorque = 0.0f;
46                 motorSpeed = 0.0f;
47                 enableLimit = false;
48                 enableMotor = false;
49         }
50
51         /// Initialize the bodies, anchors, and reference angle using the world
52         /// anchor.
53         void Initialize(b2Body* body1, b2Body* body2, const b2Vec2& anchor);
54
55         /// The local anchor point relative to body1's origin.
56         b2Vec2 localAnchor1;
57
58         /// The local anchor point relative to body2's origin.
59         b2Vec2 localAnchor2;
60
61         /// The body2 angle minus body1 angle in the reference state (radians).
62         float32 referenceAngle;
63
64         /// A flag to enable joint limits.
65         bool enableLimit;
66
67         /// The lower angle for the joint limit (radians).
68         float32 lowerAngle;
69
70         /// The upper angle for the joint limit (radians).
71         float32 upperAngle;
72
73         /// A flag to enable the joint motor.
74         bool enableMotor;
75
76         /// The desired motor speed. Usually in radians per second.
77         float32 motorSpeed;
78
79         /// The maximum motor torque used to achieve the desired motor speed.
80         /// Usually in N-m.
81         float32 maxMotorTorque;
82 };
83
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
91 {
92 public:
93         b2Vec2 GetAnchor1() const;
94         b2Vec2 GetAnchor2() const;
95
96         b2Vec2 GetReactionForce() const;
97         float32 GetReactionTorque() const;
98
99         /// Get the current joint angle in radians.
100         float32 GetJointAngle() const;
101
102         /// Get the current joint angle speed in radians per second.
103         float32 GetJointSpeed() const;
104
105         /// Is the joint limit enabled?
106         bool IsLimitEnabled() const;
107
108         /// Enable/disable the joint limit.
109         void EnableLimit(bool flag);
110
111         /// Get the lower joint limit in radians.
112         float32 GetLowerLimit() const;
113
114         /// Get the upper joint limit in radians.
115         float32 GetUpperLimit() const;
116
117         /// Set the joint limits in radians.
118         void SetLimits(float32 lower, float32 upper);
119
120         /// Is the joint motor enabled?
121         bool IsMotorEnabled() const;
122
123         /// Enable/disable the joint motor.
124         void EnableMotor(bool flag);
125
126         /// Set the motor speed in radians per second.
127         void SetMotorSpeed(float32 speed);
128
129         /// Get the motor speed in radians per second.
130         float32 GetMotorSpeed() const;
131
132         /// Set the maximum motor torque, usually in N-m.
133         void SetMaxMotorTorque(float32 torque);
134
135         /// Get the current motor torque, usually in N-m.
136         float32 GetMotorTorque() const;
137
138         //--------------- Internals Below -------------------
139         b2RevoluteJoint(const b2RevoluteJointDef* def);
140
141         void InitVelocityConstraints(const b2TimeStep& step);
142         void SolveVelocityConstraints(const b2TimeStep& step);
143
144         bool SolvePositionConstraints();
145
146         b2Vec2 m_localAnchor1;  // relative
147         b2Vec2 m_localAnchor2;
148         b2Vec2 m_pivotForce;
149         float32 m_motorForce;
150         float32 m_limitForce;
151         float32 m_limitPositionImpulse;
152
153         b2Mat22 m_pivotMass;            // effective mass for point-to-point constraint.
154         float32 m_motorMass;    // effective mass for motor/limit angular constraint.
155         
156         bool m_enableMotor;
157         float32 m_maxMotorTorque;
158         float32 m_motorSpeed;
159
160         bool m_enableLimit;
161         float32 m_referenceAngle;
162         float32 m_lowerAngle;
163         float32 m_upperAngle;
164         b2LimitState m_limitState;
165 };
166
167 inline float32 b2RevoluteJoint::GetMotorSpeed() const
168 {
169         return m_motorSpeed;
170 }
171
172 #endif