Box2D  2.4.1
A 2D physics engine for games
b2_revolute_joint.h
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef B2_REVOLUTE_JOINT_H
24 #define B2_REVOLUTE_JOINT_H
25 
26 #include "b2_api.h"
27 #include "b2_joint.h"
28 
39 struct B2_API b2RevoluteJointDef : public b2JointDef
40 {
42  {
43  type = e_revoluteJoint;
44  localAnchorA.Set(0.0f, 0.0f);
45  localAnchorB.Set(0.0f, 0.0f);
46  referenceAngle = 0.0f;
47  lowerAngle = 0.0f;
48  upperAngle = 0.0f;
49  maxMotorTorque = 0.0f;
50  motorSpeed = 0.0f;
51  enableLimit = false;
52  enableMotor = false;
53  }
54 
57  void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor);
58 
61 
64 
67 
70 
72  float lowerAngle;
73 
75  float upperAngle;
76 
79 
81  float motorSpeed;
82 
86 };
87 
94 class B2_API b2RevoluteJoint : public b2Joint
95 {
96 public:
97  b2Vec2 GetAnchorA() const override;
98  b2Vec2 GetAnchorB() const override;
99 
101  const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
102 
104  const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
105 
107  float GetReferenceAngle() const { return m_referenceAngle; }
108 
110  float GetJointAngle() const;
111 
113  float GetJointSpeed() const;
114 
116  bool IsLimitEnabled() const;
117 
119  void EnableLimit(bool flag);
120 
122  float GetLowerLimit() const;
123 
125  float GetUpperLimit() const;
126 
128  void SetLimits(float lower, float upper);
129 
131  bool IsMotorEnabled() const;
132 
134  void EnableMotor(bool flag);
135 
137  void SetMotorSpeed(float speed);
138 
140  float GetMotorSpeed() const;
141 
143  void SetMaxMotorTorque(float torque);
144  float GetMaxMotorTorque() const { return m_maxMotorTorque; }
145 
148  b2Vec2 GetReactionForce(float inv_dt) const override;
149 
152  float GetReactionTorque(float inv_dt) const override;
153 
156  float GetMotorTorque(float inv_dt) const;
157 
159  void Dump() override;
160 
162  void Draw(b2Draw* draw) const override;
163 
164 protected:
165 
166  friend class b2Joint;
167  friend class b2GearJoint;
168 
170 
171  void InitVelocityConstraints(const b2SolverData& data) override;
172  void SolveVelocityConstraints(const b2SolverData& data) override;
173  bool SolvePositionConstraints(const b2SolverData& data) override;
174 
175  // Solver shared
176  b2Vec2 m_localAnchorA;
177  b2Vec2 m_localAnchorB;
178  b2Vec2 m_impulse;
179  float m_motorImpulse;
180  float m_lowerImpulse;
181  float m_upperImpulse;
182  bool m_enableMotor;
183  float m_maxMotorTorque;
184  float m_motorSpeed;
185  bool m_enableLimit;
186  float m_referenceAngle;
187  float m_lowerAngle;
188  float m_upperAngle;
189 
190  // Solver temp
191  int32 m_indexA;
192  int32 m_indexB;
193  b2Vec2 m_rA;
194  b2Vec2 m_rB;
195  b2Vec2 m_localCenterA;
196  b2Vec2 m_localCenterB;
197  float m_invMassA;
198  float m_invMassB;
199  float m_invIA;
200  float m_invIB;
201  b2Mat22 m_K;
202  float m_angle;
203  float m_axialMass;
204 };
205 
206 inline float b2RevoluteJoint::GetMotorSpeed() const
207 {
208  return m_motorSpeed;
209 }
210 
211 #endif
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:129
Definition: b2_draw.h:49
Definition: b2_gear_joint.h:62
Definition: b2_joint.h:111
Definition: b2_revolute_joint.h:95
float GetJointAngle() const
Get the current joint angle in radians.
bool IsMotorEnabled() const
Is the joint motor enabled?
void Dump() override
Dump to b2Log.
float GetUpperLimit() const
Get the upper joint limit in radians.
void EnableLimit(bool flag)
Enable/disable the joint limit.
float GetMotorTorque(float inv_dt) const
void SetMotorSpeed(float speed)
Set the motor speed in radians per second.
float GetMotorSpeed() const
Get the motor speed in radians per second.
Definition: b2_revolute_joint.h:206
b2Vec2 GetReactionForce(float inv_dt) const override
void SetMaxMotorTorque(float torque)
Set the maximum motor torque, usually in N-m.
float GetJointSpeed() const
Get the current joint angle speed in radians per second.
void EnableMotor(bool flag)
Enable/disable the joint motor.
float GetReferenceAngle() const
Get the reference angle.
Definition: b2_revolute_joint.h:107
bool IsLimitEnabled() const
Is the joint limit enabled?
b2Vec2 GetAnchorA() const override
Get the anchor point on bodyA in world coordinates.
b2Vec2 GetAnchorB() const override
Get the anchor point on bodyB in world coordinates.
float GetReactionTorque(float inv_dt) const override
const b2Vec2 & GetLocalAnchorB() const
The local anchor point relative to bodyB's origin.
Definition: b2_revolute_joint.h:104
void Draw(b2Draw *draw) const override
Debug draw this joint.
void SetLimits(float lower, float upper)
Set the joint limits in radians.
const b2Vec2 & GetLocalAnchorA() const
The local anchor point relative to bodyA's origin.
Definition: b2_revolute_joint.h:101
float GetLowerLimit() const
Get the lower joint limit in radians.
Joint definitions are used to construct joints.
Definition: b2_joint.h:73
A 2-by-2 matrix. Stored in column-major order.
Definition: b2_math.h:172
Definition: b2_revolute_joint.h:40
float lowerAngle
The lower angle for the joint limit (radians).
Definition: b2_revolute_joint.h:72
bool enableLimit
A flag to enable joint limits.
Definition: b2_revolute_joint.h:69
float motorSpeed
The desired motor speed. Usually in radians per second.
Definition: b2_revolute_joint.h:81
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
Definition: b2_revolute_joint.h:63
float upperAngle
The upper angle for the joint limit (radians).
Definition: b2_revolute_joint.h:75
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
Definition: b2_revolute_joint.h:60
float referenceAngle
The bodyB angle minus bodyA angle in the reference state (radians).
Definition: b2_revolute_joint.h:66
bool enableMotor
A flag to enable the joint motor.
Definition: b2_revolute_joint.h:78
float maxMotorTorque
Definition: b2_revolute_joint.h:85
Solver Data.
Definition: b2_time_step.h:68
A 2D column vector.
Definition: b2_math.h:42