RevoluteJoint.hpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
3  * Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 
22 #ifndef PLAYRHO_DYNAMICS_JOINTS_REVOLUTEJOINT_HPP
23 #define PLAYRHO_DYNAMICS_JOINTS_REVOLUTEJOINT_HPP
24 
27 
28 namespace playrho {
29 namespace d2 {
30 
48 class RevoluteJoint : public Joint
49 {
50 public:
51 
56  RevoluteJoint(const RevoluteJointConf& def);
57 
58  void Accept(JointVisitor& visitor) const override;
59  void Accept(JointVisitor& visitor) override;
60 
61  Length2 GetAnchorA() const override;
62  Length2 GetAnchorB() const override;
63 
65  Length2 GetLocalAnchorA() const noexcept { return m_localAnchorA; }
66 
68  Length2 GetLocalAnchorB() const noexcept { return m_localAnchorB; }
69 
71  Angle GetReferenceAngle() const noexcept { return m_referenceAngle; }
72 
74  bool IsLimitEnabled() const noexcept;
75 
77  void EnableLimit(bool flag);
78 
80  Angle GetLowerLimit() const noexcept;
81 
83  Angle GetUpperLimit() const noexcept;
84 
86  void SetLimits(Angle lower, Angle upper);
87 
89  bool IsMotorEnabled() const noexcept;
90 
92  void EnableMotor(bool flag);
93 
95  void SetMotorSpeed(AngularVelocity speed);
96 
98  AngularVelocity GetMotorSpeed() const noexcept;
99 
101  void SetMaxMotorTorque(Torque torque);
102 
104  Torque GetMaxMotorTorque() const noexcept;
105 
107  Momentum2 GetLinearReaction() const override;
108 
110  AngularMomentum GetAngularReaction() const override;
111 
113  AngularMomentum GetMotorImpulse() const noexcept;
114 
118  LimitState GetLimitState() const noexcept;
119 
120 private:
121 
122  void InitVelocityConstraints(BodyConstraintsMap& bodies,
123  const StepConf& step, const ConstraintSolverConf& conf) override;
124 
125  bool SolveVelocityConstraints(BodyConstraintsMap& bodies, const StepConf& step) override;
126 
127  bool SolvePositionConstraints(BodyConstraintsMap& bodies,
128  const ConstraintSolverConf& conf) const override;
129 
130  // Solver shared
131  Length2 m_localAnchorA;
132  Length2 m_localAnchorB;
133 
137  Vec3 m_impulse = Vec3{};
138 
142  AngularMomentum m_motorImpulse = 0;
143 
144  bool m_enableMotor;
145  Torque m_maxMotorTorque;
146  AngularVelocity m_motorSpeed;
147 
148  bool m_enableLimit;
149  Angle m_referenceAngle;
150  Angle m_lowerAngle;
151  Angle m_upperAngle;
152 
153  // Solver cached temporary data. Values set by by InitVelocityConstraints.
154 
155  Length2 m_rA;
156  Length2 m_rB;
157  Mat33 m_mass;
158  RotInertia m_motorMass;
159  LimitState m_limitState = e_inactiveLimit;
160 };
161 
162 inline bool RevoluteJoint::IsLimitEnabled() const noexcept
163 {
164  return m_enableLimit;
165 }
166 
167 inline Angle RevoluteJoint::GetLowerLimit() const noexcept
168 {
169  return m_lowerAngle;
170 }
171 
172 inline Angle RevoluteJoint::GetUpperLimit() const noexcept
173 {
174  return m_upperAngle;
175 }
176 
177 inline bool RevoluteJoint::IsMotorEnabled() const noexcept
178 {
179  return m_enableMotor;
180 }
181 
183 {
184  return m_motorSpeed;
185 }
186 
188 {
189  return m_maxMotorTorque;
190 }
191 
193 {
194  return m_limitState;
195 }
196 
198 {
199  return m_motorImpulse;
200 }
201 
202 // Free functions...
203 
206 Angle GetJointAngle(const RevoluteJoint& joint);
207 
211 
214 inline Torque GetMotorTorque(const RevoluteJoint& joint, Frequency inv_dt) noexcept
215 {
216  return joint.GetMotorImpulse() * inv_dt;
217 }
218 
219 } // namespace d2
220 } // namespace playrho
221 
222 #endif // PLAYRHO_DYNAMICS_JOINTS_REVOLUTEJOINT_HPP