car.cpp
Go to the documentation of this file.
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 #include "test.h"
24 
25 // This is a fun demo that shows off the wheel joint
26 class Car : public Test
27 {
28 public:
29  Car()
30  {
31  m_speed = 50.0f;
32 
33  b2Body* ground = NULL;
34  {
35  b2BodyDef bd;
36  ground = m_world->CreateBody(&bd);
37 
38  b2EdgeShape shape;
39 
40  b2FixtureDef fd;
41  fd.shape = &shape;
42  fd.density = 0.0f;
43  fd.friction = 0.6f;
44 
45  shape.SetTwoSided(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
46  ground->CreateFixture(&fd);
47 
48  float hs[10] = {0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f};
49 
50  float x = 20.0f, y1 = 0.0f, dx = 5.0f;
51 
52  for (int32 i = 0; i < 10; ++i)
53  {
54  float y2 = hs[i];
55  shape.SetTwoSided(b2Vec2(x, y1), b2Vec2(x + dx, y2));
56  ground->CreateFixture(&fd);
57  y1 = y2;
58  x += dx;
59  }
60 
61  for (int32 i = 0; i < 10; ++i)
62  {
63  float y2 = hs[i];
64  shape.SetTwoSided(b2Vec2(x, y1), b2Vec2(x + dx, y2));
65  ground->CreateFixture(&fd);
66  y1 = y2;
67  x += dx;
68  }
69 
70  shape.SetTwoSided(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
71  ground->CreateFixture(&fd);
72 
73  x += 80.0f;
74  shape.SetTwoSided(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
75  ground->CreateFixture(&fd);
76 
77  x += 40.0f;
78  shape.SetTwoSided(b2Vec2(x, 0.0f), b2Vec2(x + 10.0f, 5.0f));
79  ground->CreateFixture(&fd);
80 
81  x += 20.0f;
82  shape.SetTwoSided(b2Vec2(x, 0.0f), b2Vec2(x + 40.0f, 0.0f));
83  ground->CreateFixture(&fd);
84 
85  x += 40.0f;
86  shape.SetTwoSided(b2Vec2(x, 0.0f), b2Vec2(x, 20.0f));
87  ground->CreateFixture(&fd);
88  }
89 
90  // Teeter
91  {
92  b2BodyDef bd;
93  bd.position.Set(140.0f, 1.0f);
94  bd.type = b2_dynamicBody;
95  b2Body* body = m_world->CreateBody(&bd);
96 
97  b2PolygonShape box;
98  box.SetAsBox(10.0f, 0.25f);
99  body->CreateFixture(&box, 1.0f);
100 
102  jd.Initialize(ground, body, body->GetPosition());
103  jd.lowerAngle = -8.0f * b2_pi / 180.0f;
104  jd.upperAngle = 8.0f * b2_pi / 180.0f;
105  jd.enableLimit = true;
106  m_world->CreateJoint(&jd);
107 
108  body->ApplyAngularImpulse(100.0f, true);
109  }
110 
111  // Bridge
112  {
113  int32 N = 20;
114  b2PolygonShape shape;
115  shape.SetAsBox(1.0f, 0.125f);
116 
117  b2FixtureDef fd;
118  fd.shape = &shape;
119  fd.density = 1.0f;
120  fd.friction = 0.6f;
121 
123 
124  b2Body* prevBody = ground;
125  for (int32 i = 0; i < N; ++i)
126  {
127  b2BodyDef bd;
128  bd.type = b2_dynamicBody;
129  bd.position.Set(161.0f + 2.0f * i, -0.125f);
130  b2Body* body = m_world->CreateBody(&bd);
131  body->CreateFixture(&fd);
132 
133  b2Vec2 anchor(160.0f + 2.0f * i, -0.125f);
134  jd.Initialize(prevBody, body, anchor);
135  m_world->CreateJoint(&jd);
136 
137  prevBody = body;
138  }
139 
140  b2Vec2 anchor(160.0f + 2.0f * N, -0.125f);
141  jd.Initialize(prevBody, ground, anchor);
142  m_world->CreateJoint(&jd);
143  }
144 
145  // Boxes
146  {
147  b2PolygonShape box;
148  box.SetAsBox(0.5f, 0.5f);
149 
150  b2Body* body = NULL;
151  b2BodyDef bd;
152  bd.type = b2_dynamicBody;
153 
154  bd.position.Set(230.0f, 0.5f);
155  body = m_world->CreateBody(&bd);
156  body->CreateFixture(&box, 0.5f);
157 
158  bd.position.Set(230.0f, 1.5f);
159  body = m_world->CreateBody(&bd);
160  body->CreateFixture(&box, 0.5f);
161 
162  bd.position.Set(230.0f, 2.5f);
163  body = m_world->CreateBody(&bd);
164  body->CreateFixture(&box, 0.5f);
165 
166  bd.position.Set(230.0f, 3.5f);
167  body = m_world->CreateBody(&bd);
168  body->CreateFixture(&box, 0.5f);
169 
170  bd.position.Set(230.0f, 4.5f);
171  body = m_world->CreateBody(&bd);
172  body->CreateFixture(&box, 0.5f);
173  }
174 
175  // Car
176  {
177  b2PolygonShape chassis;
178  b2Vec2 vertices[8];
179  vertices[0].Set(-1.5f, -0.5f);
180  vertices[1].Set(1.5f, -0.5f);
181  vertices[2].Set(1.5f, 0.0f);
182  vertices[3].Set(0.0f, 0.9f);
183  vertices[4].Set(-1.15f, 0.9f);
184  vertices[5].Set(-1.5f, 0.2f);
185  chassis.Set(vertices, 6);
186 
187  b2CircleShape circle;
188  circle.m_radius = 0.4f;
189 
190  b2BodyDef bd;
191  bd.type = b2_dynamicBody;
192  bd.position.Set(0.0f, 1.0f);
193  m_car = m_world->CreateBody(&bd);
194  m_car->CreateFixture(&chassis, 1.0f);
195 
196  b2FixtureDef fd;
197  fd.shape = &circle;
198  fd.density = 1.0f;
199  fd.friction = 0.9f;
200 
201  bd.position.Set(-1.0f, 0.35f);
202  m_wheel1 = m_world->CreateBody(&bd);
203  m_wheel1->CreateFixture(&fd);
204 
205  bd.position.Set(1.0f, 0.4f);
206  m_wheel2 = m_world->CreateBody(&bd);
207  m_wheel2->CreateFixture(&fd);
208 
209  b2WheelJointDef jd;
210  b2Vec2 axis(0.0f, 1.0f);
211 
212  float mass1 = m_wheel1->GetMass();
213  float mass2 = m_wheel2->GetMass();
214 
215  float hertz = 4.0f;
216  float dampingRatio = 0.7f;
217  float omega = 2.0f * b2_pi * hertz;
218 
220  jd.motorSpeed = 0.0f;
221  jd.maxMotorTorque = 20.0f;
222  jd.enableMotor = true;
223  jd.stiffness = mass1 * omega * omega;
224  jd.damping = 2.0f * mass1 * dampingRatio * omega;
225  jd.lowerTranslation = -0.25f;
226  jd.upperTranslation = 0.25f;
227  jd.enableLimit = true;
229 
231  jd.motorSpeed = 0.0f;
232  jd.maxMotorTorque = 10.0f;
233  jd.enableMotor = false;
234  jd.stiffness = mass2 * omega * omega;
235  jd.damping = 2.0f * mass2 * dampingRatio * omega;
236  jd.lowerTranslation = -0.25f;
237  jd.upperTranslation = 0.25f;
238  jd.enableLimit = true;
240  }
241  }
242 
243  void Keyboard(int key) override
244  {
245  switch (key)
246  {
247  case GLFW_KEY_A:
249  break;
250 
251  case GLFW_KEY_S:
253  break;
254 
255  case GLFW_KEY_D:
257  break;
258  }
259  }
260 
261  void Step(Settings& settings) override
262  {
263  g_debugDraw.DrawString(5, m_textLine, "Keys: left = a, brake = s, right = d, hz down = q, hz up = e");
265 
267  Test::Step(settings);
268  }
269 
270  static Test* Create()
271  {
272  return new Car;
273  }
274 
278 
279  float m_speed;
282 };
283 
284 static int testIndex = RegisterTest("Examples", "Car", Car::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
float GetMass() const
Definition: b2_body.h:544
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
static int testIndex
Definition: car.cpp:284
float lowerAngle
The lower angle for the joint limit (radians).
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
float m_speed
Definition: car.cpp:279
f
b2Body * m_car
Definition: car.cpp:275
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
float x
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
float lowerTranslation
The lower translation limit, usually in meters.
Definition: test.h:80
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
const b2Vec2 & GetPosition() const
Definition: b2_body.h:484
b2WheelJoint * m_spring2
Definition: car.cpp:281
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
#define GLFW_KEY_A
Definition: glfw3.h:378
Definition: car.cpp:26
signed int int32
Definition: b2_types.h:28
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
b2BodyType type
Definition: b2_body.h:74
void SetMotorSpeed(float speed)
Set the motor speed, usually in radians per second.
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
float maxMotorTorque
The maximum motor torque, usually in N-m.
void Keyboard(int key) override
Definition: car.cpp:243
float m_radius
Definition: b2_shape.h:102
b2Vec2 m_center
Definition: draw.h:53
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
#define GLFW_KEY_S
Definition: glfw3.h:396
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
bool enableMotor
Enable/disable the joint motor.
b2Body * m_wheel1
Definition: car.cpp:276
int32 m_textIncrement
Definition: test.h:135
#define GLFW_KEY_D
Definition: glfw3.h:381
b2World * m_world
Definition: test.h:128
bool enableLimit
Enable/disable the joint limit.
#define b2_pi
Definition: b2_common.h:41
static Test * Create()
Definition: car.cpp:270
b2Vec2 position
Definition: b2_body.h:78
float upperAngle
The upper angle for the joint limit (radians).
void Set(const b2Vec2 *points, int32 count)
b2Body * m_wheel2
Definition: car.cpp:277
void ApplyAngularImpulse(float impulse, bool wake)
Definition: b2_body.h:836
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
Car()
Definition: car.cpp:29
b2WheelJoint * m_spring1
Definition: car.cpp:280
Camera g_camera
Definition: draw.cpp:33
bool enableLimit
A flag to enable joint limits.
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
void Step(Settings &settings) override
Definition: car.cpp:261
float motorSpeed
The desired motor speed in radians per second.
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
float damping
Suspension damping. Typically in units of N*s/m.
float stiffness
Suspension stiffness. Typically in units N/m.
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82
float upperTranslation
The upper translation limit, usually in meters.


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19