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_PRISMATIC_JOINT_H
20 #define B2_PRISMATIC_JOINT_H
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
34 type = e_prismaticJoint;
35 localAnchor1.SetZero();
36 localAnchor2.SetZero();
37 localAxis1.Set(1.0f, 0.0f);
38 referenceAngle = 0.0f;
40 lowerTranslation = 0.0f;
41 upperTranslation = 0.0f;
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);
51 /// The local anchor point relative to body1's origin.
54 /// The local anchor point relative to body2's origin.
57 /// The local translation axis in body1.
60 /// The constrained angle between the bodies: body2_angle - body1_angle.
61 float32 referenceAngle;
63 /// Enable/disable the joint limit.
66 /// The lower translation limit, usually in meters.
67 float32 lowerTranslation;
69 /// The upper translation limit, usually in meters.
70 float32 upperTranslation;
72 /// Enable/disable the joint motor.
75 /// The maximum motor torque, usually in N-m.
76 float32 maxMotorForce;
78 /// The desired motor speed in radians per second.
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
89 b2Vec2 GetAnchor1() const;
90 b2Vec2 GetAnchor2() const;
92 b2Vec2 GetReactionForce() const;
93 float32 GetReactionTorque() const;
95 /// Get the current joint translation, usually in meters.
96 float32 GetJointTranslation() const;
98 /// Get the current joint translation speed, usually in meters per second.
99 float32 GetJointSpeed() const;
101 /// Is the joint limit enabled?
102 bool IsLimitEnabled() const;
104 /// Enable/disable the joint limit.
105 void EnableLimit(bool flag);
107 /// Get the lower joint limit, usually in meters.
108 float32 GetLowerLimit() const;
110 /// Get the upper joint limit, usually in meters.
111 float32 GetUpperLimit() const;
113 /// Set the joint limits, usually in meters.
114 void SetLimits(float32 lower, float32 upper);
116 /// Is the joint motor enabled?
117 bool IsMotorEnabled() const;
119 /// Enable/disable the joint motor.
120 void EnableMotor(bool flag);
122 /// Set the motor speed, usually in meters per second.
123 void SetMotorSpeed(float32 speed);
125 /// Get the motor speed, usually in meters per second.
126 float32 GetMotorSpeed() const;
128 /// Set the maximum motor force, usually in N.
129 void SetMaxMotorForce(float32 force);
131 /// Get the current motor force, usually in N.
132 float32 GetMotorForce() const;
134 //--------------- Internals Below -------------------
136 b2PrismaticJoint(const b2PrismaticJointDef* def);
138 void InitVelocityConstraints(const b2TimeStep& step);
139 void SolveVelocityConstraints(const b2TimeStep& step);
140 bool SolvePositionConstraints();
142 b2Vec2 m_localAnchor1;
143 b2Vec2 m_localAnchor2;
144 b2Vec2 m_localXAxis1;
145 b2Vec2 m_localYAxis1;
148 b2Jacobian m_linearJacobian;
149 float32 m_linearMass; // effective mass for point-to-line constraint.
152 float32 m_angularMass; // effective mass for angular constraint.
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;
161 float32 m_lowerTranslation;
162 float32 m_upperTranslation;
163 float32 m_maxMotorForce;
164 float32 m_motorSpeed;
168 b2LimitState m_limitState;
171 inline float32 b2PrismaticJoint::GetMotorSpeed() const