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 /// @warning at least one body should by dynamic with a non-fixed rotation.
31 struct b2PrismaticJointDef : public b2JointDef
32 {
b2PrismaticJointDefb2PrismaticJointDef33 b2PrismaticJointDef()
34 {
35 type = e_prismaticJoint;
36 localAnchorA.SetZero();
37 localAnchorB.SetZero();
38 localAxis1.Set(1.0f, 0.0f);
39 referenceAngle = 0.0f;
40 enableLimit = false;
41 lowerTranslation = 0.0f;
42 upperTranslation = 0.0f;
43 enableMotor = false;
44 maxMotorForce = 0.0f;
45 motorSpeed = 0.0f;
46 }
47
48 /// Initialize the bodies, anchors, axis, and reference angle using the world
49 /// anchor and world axis.
50 void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
51
52 /// The local anchor point relative to body1's origin.
53 b2Vec2 localAnchorA;
54
55 /// The local anchor point relative to body2's origin.
56 b2Vec2 localAnchorB;
57
58 /// The local translation axis in body1.
59 b2Vec2 localAxis1;
60
61 /// The constrained angle between the bodies: body2_angle - body1_angle.
62 float32 referenceAngle;
63
64 /// Enable/disable the joint limit.
65 bool enableLimit;
66
67 /// The lower translation limit, usually in meters.
68 float32 lowerTranslation;
69
70 /// The upper translation limit, usually in meters.
71 float32 upperTranslation;
72
73 /// Enable/disable the joint motor.
74 bool enableMotor;
75
76 /// The maximum motor torque, usually in N-m.
77 float32 maxMotorForce;
78
79 /// The desired motor speed in radians per second.
80 float32 motorSpeed;
81 };
82
83 /// A prismatic joint. This joint provides one degree of freedom: translation
84 /// along an axis fixed in body1. Relative rotation is prevented. You can
85 /// use a joint limit to restrict the range of motion and a joint motor to
86 /// drive the motion or to model joint friction.
87 class b2PrismaticJoint : public b2Joint
88 {
89 public:
90 b2Vec2 GetAnchorA() const;
91 b2Vec2 GetAnchorB() const;
92
93 b2Vec2 GetReactionForce(float32 inv_dt) const;
94 float32 GetReactionTorque(float32 inv_dt) const;
95
96 /// Get the current joint translation, usually in meters.
97 float32 GetJointTranslation() const;
98
99 /// Get the current joint translation speed, usually in meters per second.
100 float32 GetJointSpeed() const;
101
102 /// Is the joint limit enabled?
103 bool IsLimitEnabled() const;
104
105 /// Enable/disable the joint limit.
106 void EnableLimit(bool flag);
107
108 /// Get the lower joint limit, usually in meters.
109 float32 GetLowerLimit() const;
110
111 /// Get the upper joint limit, usually in meters.
112 float32 GetUpperLimit() const;
113
114 /// Set the joint limits, usually in meters.
115 void SetLimits(float32 lower, float32 upper);
116
117 /// Is the joint motor enabled?
118 bool IsMotorEnabled() const;
119
120 /// Enable/disable the joint motor.
121 void EnableMotor(bool flag);
122
123 /// Set the motor speed, usually in meters per second.
124 void SetMotorSpeed(float32 speed);
125
126 /// Get the motor speed, usually in meters per second.
127 float32 GetMotorSpeed() const;
128
129 /// Set the maximum motor force, usually in N.
130 void SetMaxMotorForce(float32 force);
131
132 /// Get the current motor force, usually in N.
133 float32 GetMotorForce() const;
134
135 protected:
136 friend class b2Joint;
137 friend class b2GearJoint;
138 b2PrismaticJoint(const b2PrismaticJointDef* def);
139
140 void InitVelocityConstraints(const b2TimeStep& step);
141 void SolveVelocityConstraints(const b2TimeStep& step);
142 bool SolvePositionConstraints(float32 baumgarte);
143
144 b2Vec2 m_localAnchor1;
145 b2Vec2 m_localAnchor2;
146 b2Vec2 m_localXAxis1;
147 b2Vec2 m_localYAxis1;
148 float32 m_refAngle;
149
150 b2Vec2 m_axis, m_perp;
151 float32 m_s1, m_s2;
152 float32 m_a1, m_a2;
153
154 b2Mat33 m_K;
155 b2Vec3 m_impulse;
156
157 float32 m_motorMass; // effective mass for motor/limit translational constraint.
158 float32 m_motorImpulse;
159
160 float32 m_lowerTranslation;
161 float32 m_upperTranslation;
162 float32 m_maxMotorForce;
163 float32 m_motorSpeed;
164
165 bool m_enableLimit;
166 bool m_enableMotor;
167 b2LimitState m_limitState;
168 };
169
GetMotorSpeed()170 inline float32 b2PrismaticJoint::GetMotorSpeed() const
171 {
172 return m_motorSpeed;
173 }
174
175 #endif
176