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
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2RevoluteJoint::GetReferenceAngle
float GetReferenceAngle() const
Get the reference angle.
Definition: b2_revolute_joint.h:107
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2RevoluteJointDef::upperAngle
float upperAngle
The upper angle for the joint limit (radians).
Definition: b2_revolute_joint.h:75
b2RevoluteJointDef::localAnchorB
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
Definition: b2_revolute_joint.h:63
b2Joint::GetReactionForce
virtual b2Vec2 GetReactionForce(float inv_dt) const =0
Get the reaction force on bodyB at the joint anchor in Newtons.
b2RevoluteJointDef::localAnchorA
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
Definition: b2_revolute_joint.h:60
b2RevoluteJointDef::enableMotor
bool enableMotor
A flag to enable the joint motor.
Definition: b2_revolute_joint.h:78
b2Joint::GetAnchorA
virtual b2Vec2 GetAnchorA() const =0
Get the anchor point on bodyA in world coordinates.
b2Draw
Definition: b2_draw.h:48
b2RevoluteJoint::GetMotorSpeed
float GetMotorSpeed() const
Get the motor speed in radians per second.
Definition: b2_revolute_joint.h:206
b2RevoluteJointDef::maxMotorTorque
float maxMotorTorque
Definition: b2_revolute_joint.h:85
b2JointDef
Joint definitions are used to construct joints.
Definition: b2_joint.h:72
b2RevoluteJointDef::enableLimit
bool enableLimit
A flag to enable joint limits.
Definition: b2_revolute_joint.h:69
b2Joint::Draw
virtual void Draw(b2Draw *draw) const
Debug draw this joint.
b2RevoluteJointDef
Definition: b2_revolute_joint.h:39
b2Joint::Dump
virtual void Dump()
Dump this joint to the log file.
Definition: b2_joint.h:151
b2RevoluteJoint
Definition: b2_revolute_joint.h:94
b2RevoluteJointDef::motorSpeed
float motorSpeed
The desired motor speed. Usually in radians per second.
Definition: b2_revolute_joint.h:81
b2RevoluteJoint::GetLocalAnchorA
const b2Vec2 & GetLocalAnchorA() const
The local anchor point relative to bodyA's origin.
Definition: b2_revolute_joint.h:101
b2RevoluteJointDef::referenceAngle
float referenceAngle
The bodyB angle minus bodyA angle in the reference state (radians).
Definition: b2_revolute_joint.h:66
b2GearJoint
Definition: b2_gear_joint.h:61
b2Mat22
A 2-by-2 matrix. Stored in column-major order.
Definition: b2_math.h:171
b2SolverData
Solver Data.
Definition: b2_time_step.h:67
b2Joint::GetAnchorB
virtual b2Vec2 GetAnchorB() const =0
Get the anchor point on bodyB in world coordinates.
b2Joint
Definition: b2_joint.h:110
b2Joint::GetReactionTorque
virtual float GetReactionTorque(float inv_dt) const =0
Get the reaction torque on bodyB in N*m.
b2RevoluteJointDef::lowerAngle
float lowerAngle
The lower angle for the joint limit (radians).
Definition: b2_revolute_joint.h:72
b2RevoluteJoint::GetLocalAnchorB
const b2Vec2 & GetLocalAnchorB() const
The local anchor point relative to bodyB's origin.
Definition: b2_revolute_joint.h:104