first commit
[blok] / Box2D / Source / Dynamics / Joints / b2PrismaticJoint.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_PRISMATIC_JOINT_H
20 #define B2_PRISMATIC_JOINT_H
21
22 #include "b2Joint.h"
23
24 /// Prismatic joint definition. This requires defining a line of
25 /// motion using an axis and an anchor point. The definition uses local
26 /// anchor points and a local axis so that the initial configuration
27 /// can violate the constraint slightly. The joint translation is zero
28 /// when the local anchor points coincide in world space. Using local
29 /// anchors and a local axis helps when saving and loading a game.
30 struct b2PrismaticJointDef : public b2JointDef
31 {
32         b2PrismaticJointDef()
33         {
34                 type = e_prismaticJoint;
35                 localAnchor1.SetZero();
36                 localAnchor2.SetZero();
37                 localAxis1.Set(1.0f, 0.0f);
38                 referenceAngle = 0.0f;
39                 enableLimit = false;
40                 lowerTranslation = 0.0f;
41                 upperTranslation = 0.0f;
42                 enableMotor = false;
43                 maxMotorForce = 0.0f;
44                 motorSpeed = 0.0f;
45         }
46
47         /// Initialize the bodies, anchors, axis, and reference angle using the world
48         /// anchor and world axis.
49         void Initialize(b2Body* body1, b2Body* body2, const b2Vec2& anchor, const b2Vec2& axis);
50
51         /// The local anchor point relative to body1's origin.
52         b2Vec2 localAnchor1;
53
54         /// The local anchor point relative to body2's origin.
55         b2Vec2 localAnchor2;
56
57         /// The local translation axis in body1.
58         b2Vec2 localAxis1;
59
60         /// The constrained angle between the bodies: body2_angle - body1_angle.
61         float32 referenceAngle;
62
63         /// Enable/disable the joint limit.
64         bool enableLimit;
65
66         /// The lower translation limit, usually in meters.
67         float32 lowerTranslation;
68
69         /// The upper translation limit, usually in meters.
70         float32 upperTranslation;
71
72         /// Enable/disable the joint motor.
73         bool enableMotor;
74
75         /// The maximum motor torque, usually in N-m.
76         float32 maxMotorForce;
77
78         /// The desired motor speed in radians per second.
79         float32 motorSpeed;
80 };
81
82 /// A prismatic joint. This joint provides one degree of freedom: translation
83 /// along an axis fixed in body1. Relative rotation is prevented. You can
84 /// use a joint limit to restrict the range of motion and a joint motor to
85 /// drive the motion or to model joint friction.
86 class b2PrismaticJoint : public b2Joint
87 {
88 public:
89         b2Vec2 GetAnchor1() const;
90         b2Vec2 GetAnchor2() const;
91
92         b2Vec2 GetReactionForce() const;
93         float32 GetReactionTorque() const;
94
95         /// Get the current joint translation, usually in meters.
96         float32 GetJointTranslation() const;
97
98         /// Get the current joint translation speed, usually in meters per second.
99         float32 GetJointSpeed() const;
100
101         /// Is the joint limit enabled?
102         bool IsLimitEnabled() const;
103
104         /// Enable/disable the joint limit.
105         void EnableLimit(bool flag);
106
107         /// Get the lower joint limit, usually in meters.
108         float32 GetLowerLimit() const;
109
110         /// Get the upper joint limit, usually in meters.
111         float32 GetUpperLimit() const;
112
113         /// Set the joint limits, usually in meters.
114         void SetLimits(float32 lower, float32 upper);
115
116         /// Is the joint motor enabled?
117         bool IsMotorEnabled() const;
118
119         /// Enable/disable the joint motor.
120         void EnableMotor(bool flag);
121
122         /// Set the motor speed, usually in meters per second.
123         void SetMotorSpeed(float32 speed);
124
125         /// Get the motor speed, usually in meters per second.
126         float32 GetMotorSpeed() const;
127
128         /// Set the maximum motor force, usually in N.
129         void SetMaxMotorForce(float32 force);
130
131         /// Get the current motor force, usually in N.
132         float32 GetMotorForce() const;
133
134         //--------------- Internals Below -------------------
135
136         b2PrismaticJoint(const b2PrismaticJointDef* def);
137
138         void InitVelocityConstraints(const b2TimeStep& step);
139         void SolveVelocityConstraints(const b2TimeStep& step);
140         bool SolvePositionConstraints();
141
142         b2Vec2 m_localAnchor1;
143         b2Vec2 m_localAnchor2;
144         b2Vec2 m_localXAxis1;
145         b2Vec2 m_localYAxis1;
146         float32 m_refAngle;
147
148         b2Jacobian m_linearJacobian;
149         float32 m_linearMass;                           // effective mass for point-to-line constraint.
150         float32 m_force;
151         
152         float32 m_angularMass;                  // effective mass for angular constraint.
153         float32 m_torque;
154
155         b2Jacobian m_motorJacobian;
156         float32 m_motorMass;                    // effective mass for motor/limit translational constraint.
157         float32 m_motorForce;
158         float32 m_limitForce;
159         float32 m_limitPositionImpulse;
160
161         float32 m_lowerTranslation;
162         float32 m_upperTranslation;
163         float32 m_maxMotorForce;
164         float32 m_motorSpeed;
165         
166         bool m_enableLimit;
167         bool m_enableMotor;
168         b2LimitState m_limitState;
169 };
170
171 inline float32 b2PrismaticJoint::GetMotorSpeed() const
172 {
173         return m_motorSpeed;
174 }
175
176 #endif