Box2D  2.4.1
A 2D physics engine for games
b2_wheel_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_WHEEL_JOINT_H
24 #define B2_WHEEL_JOINT_H
25 
26 #include "b2_api.h"
27 #include "b2_joint.h"
28 
35 struct B2_API b2WheelJointDef : public b2JointDef
36 {
38  {
39  type = e_wheelJoint;
40  localAnchorA.SetZero();
41  localAnchorB.SetZero();
42  localAxisA.Set(1.0f, 0.0f);
43  enableLimit = false;
44  lowerTranslation = 0.0f;
45  upperTranslation = 0.0f;
46  enableMotor = false;
47  maxMotorTorque = 0.0f;
48  motorSpeed = 0.0f;
49  stiffness = 0.0f;
50  damping = 0.0f;
51  }
52 
55  void Initialize(b2Body* bodyA, b2Body* bodyB, const b2Vec2& anchor, const b2Vec2& axis);
56 
59 
62 
65 
68 
71 
74 
77 
80 
82  float motorSpeed;
83 
85  float stiffness;
86 
88  float damping;
89 };
90 
95 class B2_API b2WheelJoint : public b2Joint
96 {
97 public:
98  b2Vec2 GetAnchorA() const override;
99  b2Vec2 GetAnchorB() const override;
100 
101  b2Vec2 GetReactionForce(float inv_dt) const override;
102  float GetReactionTorque(float inv_dt) const override;
103 
105  const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
106 
108  const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
109 
111  const b2Vec2& GetLocalAxisA() const { return m_localXAxisA; }
112 
114  float GetJointTranslation() const;
115 
117  float GetJointLinearSpeed() const;
118 
120  float GetJointAngle() const;
121 
123  float GetJointAngularSpeed() const;
124 
126  bool IsLimitEnabled() const;
127 
129  void EnableLimit(bool flag);
130 
132  float GetLowerLimit() const;
133 
135  float GetUpperLimit() const;
136 
138  void SetLimits(float lower, float upper);
139 
141  bool IsMotorEnabled() const;
142 
144  void EnableMotor(bool flag);
145 
147  void SetMotorSpeed(float speed);
148 
150  float GetMotorSpeed() const;
151 
153  void SetMaxMotorTorque(float torque);
154  float GetMaxMotorTorque() const;
155 
157  float GetMotorTorque(float inv_dt) const;
158 
160  void SetStiffness(float stiffness);
161  float GetStiffness() const;
162 
164  void SetDamping(float damping);
165  float GetDamping() const;
166 
168  void Dump() override;
169 
171  void Draw(b2Draw* draw) const override;
172 
173 protected:
174 
175  friend class b2Joint;
176  b2WheelJoint(const b2WheelJointDef* def);
177 
178  void InitVelocityConstraints(const b2SolverData& data) override;
179  void SolveVelocityConstraints(const b2SolverData& data) override;
180  bool SolvePositionConstraints(const b2SolverData& data) override;
181 
182  b2Vec2 m_localAnchorA;
183  b2Vec2 m_localAnchorB;
184  b2Vec2 m_localXAxisA;
185  b2Vec2 m_localYAxisA;
186 
187  float m_impulse;
188  float m_motorImpulse;
189  float m_springImpulse;
190 
191  float m_lowerImpulse;
192  float m_upperImpulse;
193  float m_translation;
194  float m_lowerTranslation;
195  float m_upperTranslation;
196 
197  float m_maxMotorTorque;
198  float m_motorSpeed;
199 
200  bool m_enableLimit;
201  bool m_enableMotor;
202 
203  float m_stiffness;
204  float m_damping;
205 
206  // Solver temp
207  int32 m_indexA;
208  int32 m_indexB;
209  b2Vec2 m_localCenterA;
210  b2Vec2 m_localCenterB;
211  float m_invMassA;
212  float m_invMassB;
213  float m_invIA;
214  float m_invIB;
215 
216  b2Vec2 m_ax, m_ay;
217  float m_sAx, m_sBx;
218  float m_sAy, m_sBy;
219 
220  float m_mass;
221  float m_motorMass;
222  float m_axialMass;
223  float m_springMass;
224 
225  float m_bias;
226  float m_gamma;
227 
228 };
229 
230 inline float b2WheelJoint::GetMotorSpeed() const
231 {
232  return m_motorSpeed;
233 }
234 
235 inline float b2WheelJoint::GetMaxMotorTorque() const
236 {
237  return m_maxMotorTorque;
238 }
239 
240 #endif
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:129
Definition: b2_draw.h:49
Definition: b2_joint.h:111
Definition: b2_wheel_joint.h:96
float GetJointAngle() const
Get the current joint angle in radians.
void Draw(b2Draw *draw) const override
Debug draw this joint.
float GetReactionTorque(float inv_dt) const override
Get the reaction torque on bodyB in N*m.
void SetStiffness(float stiffness)
Access spring stiffness.
b2Vec2 GetAnchorA() const override
Get the anchor point on bodyA in world coordinates.
float GetJointAngularSpeed() const
Get the current joint angular speed in radians per second.
float GetLowerLimit() const
Get the lower joint translation limit, usually in meters.
float GetMotorTorque(float inv_dt) const
Get the current motor torque given the inverse time step, usually in N-m.
b2Vec2 GetAnchorB() const override
Get the anchor point on bodyB in world coordinates.
float GetMotorSpeed() const
Get the motor speed, usually in radians per second.
Definition: b2_wheel_joint.h:230
void EnableLimit(bool flag)
Enable/disable the joint translation limit.
const b2Vec2 & GetLocalAnchorB() const
The local anchor point relative to bodyB's origin.
Definition: b2_wheel_joint.h:108
void EnableMotor(bool flag)
Enable/disable the joint motor.
float GetJointTranslation() const
Get the current joint translation, usually in meters.
void Dump() override
Dump to b2Log.
const b2Vec2 & GetLocalAxisA() const
The local joint axis relative to bodyA.
Definition: b2_wheel_joint.h:111
bool IsLimitEnabled() const
Is the joint limit enabled?
void SetDamping(float damping)
Access damping.
void SetMotorSpeed(float speed)
Set the motor speed, usually in radians per second.
const b2Vec2 & GetLocalAnchorA() const
The local anchor point relative to bodyA's origin.
Definition: b2_wheel_joint.h:105
void SetLimits(float lower, float upper)
Set the joint translation limits, usually in meters.
float GetJointLinearSpeed() const
Get the current joint linear speed, usually in meters per second.
void SetMaxMotorTorque(float torque)
Set/Get the maximum motor force, usually in N-m.
float GetUpperLimit() const
Get the upper joint translation limit, usually in meters.
bool IsMotorEnabled() const
Is the joint motor enabled?
b2Vec2 GetReactionForce(float inv_dt) const override
Get the reaction force on bodyB at the joint anchor in Newtons.
Joint definitions are used to construct joints.
Definition: b2_joint.h:73
Solver Data.
Definition: b2_time_step.h:68
A 2D column vector.
Definition: b2_math.h:42
Definition: b2_wheel_joint.h:36
float motorSpeed
The desired motor speed in radians per second.
Definition: b2_wheel_joint.h:82
bool enableLimit
Enable/disable the joint limit.
Definition: b2_wheel_joint.h:67
float stiffness
Suspension stiffness. Typically in units N/m.
Definition: b2_wheel_joint.h:85
float upperTranslation
The upper translation limit, usually in meters.
Definition: b2_wheel_joint.h:73
float lowerTranslation
The lower translation limit, usually in meters.
Definition: b2_wheel_joint.h:70
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
Definition: b2_wheel_joint.h:61
bool enableMotor
Enable/disable the joint motor.
Definition: b2_wheel_joint.h:76
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
Definition: b2_wheel_joint.h:58
float damping
Suspension damping. Typically in units of N*s/m.
Definition: b2_wheel_joint.h:88
float maxMotorTorque
The maximum motor torque, usually in N-m.
Definition: b2_wheel_joint.h:79
b2Vec2 localAxisA
The local translation axis in bodyA.
Definition: b2_wheel_joint.h:64
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)