-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulePhysics.cpp
More file actions
428 lines (339 loc) · 10.2 KB
/
Copy pathModulePhysics.cpp
File metadata and controls
428 lines (339 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "Globals.h"
#include "Application.h"
#include "ModuleInput.h"
#include "ModuleRender.h"
#include "ModulePhysics.h"
#include "p2Point.h"
#include "math.h"
// Tell the compiler to reference the compiled Box2D libraries
#ifdef _DEBUG
#pragma comment( lib, "Box2D/libx86/Debug/Box2D.lib" )
#else
#pragma comment( lib, "Box2D/libx86/Release/Box2D.lib" )
#endif
ModulePhysics::ModulePhysics(Application* app, bool start_enabled) : Module(app, start_enabled)
{
// Initialise all the internal class variables, at least to NULL pointer
world = NULL;
ground = NULL;
mouse_joint = NULL;
mouse_body = NULL;
debug = true;
}
// Destructor
ModulePhysics::~ModulePhysics()
{
// You should do some memory cleaning here, if required
}
bool ModulePhysics::Start()
{
LOG("Creating Physics 2D environment");
gravity = -GRAVITY_Y;
// Create a new World
world = new b2World(b2Vec2(GRAVITY_X, gravity));
// Set this module as a listener for contacts
world->SetContactListener(this);
// Create the main static ground of the scenario: a big circle in the middle of the screen
//CreateScenarioGround();
// Create a static, shapeless ground body
// This will be used to create joints like a mouse joint
b2BodyDef bd;
ground = world->CreateBody(&bd); // Add the static ground body to the World
return true;
}
update_status ModulePhysics::PreUpdate()
{
// Step (update) the World
// WARNING: WE ARE STEPPING BY CONSTANT 1/60 SECONDS!
world->Step(1.0f / 60.0f, 6, 2);
// Because Box2D does not automatically broadcast collisions/contacts with sensors,
// we have to manually search for collisions and "call" the equivalent to the ModulePhysics::BeginContact() ourselves...
for(b2Contact* c = world->GetContactList(); c; c = c->GetNext())
{
// For each contact detected by Box2D, see if the first one colliding is a sensor
if(c->GetFixtureA()->IsSensor() && c->IsTouching())
{
// If so, we call the OnCollision listener function (only of the sensor), passing as inputs our custom PhysBody classes
PhysBody* pb1 = (PhysBody*)c->GetFixtureA()->GetBody()->GetUserData();
PhysBody* pb2 = (PhysBody*)c->GetFixtureA()->GetBody()->GetUserData();
if(pb1 && pb2 && pb1->listener)
pb1->listener->OnCollision(pb1, pb2);
}
}
// Keep playing
return UPDATE_CONTINUE;
}
update_status ModulePhysics::PostUpdate()
{
//Moved physics drawing to debug, always on top
// Keep playing
return UPDATE_CONTINUE;
}
bool ModulePhysics::CleanUp()
{
LOG("Destroying physics world");
// Delete the whole physics world!
delete world;
return true;
}
void ModulePhysics::CreateScenarioGround()
{
// Get coordinates of the screen center and radius
int x = SCREEN_WIDTH / 2;
int y = SCREEN_HEIGHT / 1.5f;
int diameter = SCREEN_WIDTH / 2;
// Create a static body in the middle of the screen
b2BodyDef body;
body.type = b2_staticBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
// Add this static body to the World
b2Body* big_ball = world->CreateBody(&body);
// Create a big circle shape
b2CircleShape shape;
shape.m_radius = PIXEL_TO_METERS(diameter) * 0.5f;
// Create a fixture and associate the circle to it
b2FixtureDef fixture;
fixture.shape = &shape;
// Add the ficture (plus shape) to the static body
big_ball->CreateFixture(&fixture);
}
PhysBody* ModulePhysics::CreateCircle(int x, int y, int radius)
{
// Create BODY at position x,y
b2BodyDef body;
body.type = b2_dynamicBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
// Add BODY to the world
b2Body* b = world->CreateBody(&body);
// Create SHAPE
b2CircleShape shape;
shape.m_radius = PIXEL_TO_METERS(radius);
// Create FIXTURE
b2FixtureDef fixture;
fixture.shape = &shape;
fixture.density = 1.0f;
// Add fixture to the BODY
b->CreateFixture(&fixture);
// Create our custom PhysBody class
PhysBody* pbody = new PhysBody();
pbody->body = b;
b->SetUserData(pbody);
pbody->width = pbody->height = radius;
// Return our PhysBody class
return pbody;
}
PhysBody* ModulePhysics::CreateCircleSensor(int x, int y, int radius)
{
// Create BODY at position x,y
b2BodyDef body;
body.type = b2_staticBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
// Add BODY to the world
b2Body* b = world->CreateBody(&body);
// Create SHAPE
b2CircleShape shape;
shape.m_radius = PIXEL_TO_METERS(radius);
// Create FIXTURE
b2FixtureDef fixture;
fixture.shape = &shape;
fixture.density = 1.0f;
fixture.isSensor = true;
// Add fixture to the BODY
b->CreateFixture(&fixture);
// Create our custom PhysBody class
PhysBody* pbody = new PhysBody();
pbody->body = b;
b->SetUserData(pbody);
pbody->width = pbody->height = radius;
// Return our PhysBody class
return pbody;
}
PhysBody* ModulePhysics::CreateRectangle(int x, int y, int width, int height)
{
// Create BODY at position x,y
b2BodyDef body;
body.type = b2_dynamicBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
// Add BODY to the world
b2Body* b = world->CreateBody(&body);
// Create SHAPE
b2PolygonShape box;
box.SetAsBox(PIXEL_TO_METERS(width) * 0.5f, PIXEL_TO_METERS(height) * 0.5f);
// Create FIXTURE
b2FixtureDef fixture;
fixture.shape = &box;
fixture.density = 1.0f;
// Add fixture to the BODY
b->CreateFixture(&fixture);
// Create our custom PhysBody class
PhysBody* pbody = new PhysBody();
pbody->body = b;
b->SetUserData(pbody);
pbody->width = width * 0.5f;
pbody->height = height * 0.5f;
// Return our PhysBody class
return pbody;
}
PhysBody* ModulePhysics::CreateRectangleSensor(int x, int y, int width, int height)
{
// Create BODY at position x,y
b2BodyDef body;
body.type = b2_staticBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
// Add BODY to the world
b2Body* b = world->CreateBody(&body);
// Create SHAPE (small "box" rectangle is ok; otherwise create whatever you need)
b2PolygonShape box;
box.SetAsBox(PIXEL_TO_METERS(width) * 0.5f, PIXEL_TO_METERS(height) * 0.5f);
// Create FIXTURE
b2FixtureDef fixture;
fixture.shape = &box;
fixture.density = 1.0f;
fixture.isSensor = true; // Set this fixture as SENSOR type
// Add fixture to the BODY
b->CreateFixture(&fixture);
// Create our custom PhysBody class
PhysBody* pbody = new PhysBody();
pbody->body = b;
b->SetUserData(pbody);
pbody->width = width;
pbody->height = height;
// Return our PhysBody class
return pbody;
}
PhysBody* ModulePhysics::CreateChain(int x, int y, int* points, int size)
{
// Create BODY at position x,y
b2BodyDef body;
body.type = b2_dynamicBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
// Add BODY to the world
b2Body* b = world->CreateBody(&body);
// Create SHAPE
b2ChainShape shape;
b2Vec2* p = new b2Vec2[size / 2];
for(uint i = 0; i < size / 2; ++i)
{
p[i].x = PIXEL_TO_METERS(points[i * 2 + 0]);
p[i].y = PIXEL_TO_METERS(points[i * 2 + 1]);
}
shape.CreateLoop(p, size / 2);
// Create FIXTURE
b2FixtureDef fixture;
fixture.shape = &shape;
// Add fixture to the BODY
b->CreateFixture(&fixture);
// Clean-up temp array
delete p;
// Create our custom PhysBody class
PhysBody* pbody = new PhysBody();
pbody->body = b;
b->SetUserData(pbody);
pbody->width = pbody->height = 0;
// Return our PhysBody class
return pbody;
}
PhysBody* ModulePhysics::CreateChainSensor(int x, int y, int* points, int size)
{
// Create BODY at position x,y
b2BodyDef body;
body.type = b2_dynamicBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
// Add BODY to the world
b2Body* b = world->CreateBody(&body);
// Create SHAPE
b2ChainShape shape;
b2Vec2* p = new b2Vec2[size / 2];
for (uint i = 0; i < size / 2; ++i)
{
p[i].x = PIXEL_TO_METERS(points[i * 2 + 0]);
p[i].y = PIXEL_TO_METERS(points[i * 2 + 1]);
}
shape.CreateLoop(p, size / 2);
// Create FIXTURE
b2FixtureDef fixture;
fixture.shape = &shape;
fixture.isSensor = true;
// Add fixture to the BODY
b->CreateFixture(&fixture);
// Clean-up temp array
delete p;
// Create our custom PhysBody class
PhysBody* pbody = new PhysBody();
pbody->body = b;
b->SetUserData(pbody);
pbody->width = pbody->height = 0;
// Return our PhysBody class
return pbody;
}
// Callback function to collisions with Box2D
void ModulePhysics::BeginContact(b2Contact* contact)
{
// Call the OnCollision listener function to bodies A and B, passing as inputs our custom PhysBody classes
PhysBody* physA = (PhysBody*)contact->GetFixtureA()->GetBody()->GetUserData();
PhysBody* physB = (PhysBody*)contact->GetFixtureB()->GetBody()->GetUserData();
if(physA && physA->listener != NULL)
physA->listener->OnCollision(physA, physB);
if(physB && physB->listener != NULL)
physB->listener->OnCollision(physB, physA);
}
// PHYS BODY FUNCTIONS -------------------------------------------------------------------------------
PhysBody::PhysBody() : listener(NULL), body(NULL)
{
// Initialize all internal class variables
width = height = 0;
body = NULL;
listener = NULL;
}
PhysBody::~PhysBody()
{
// Destroy the associated Box2D body
body->GetWorld()->DestroyBody(body);
}
void PhysBody::GetPosition(int& x, int &y) const
{
b2Vec2 pos = body->GetPosition();
x = METERS_TO_PIXELS(pos.x) - (width);
y = METERS_TO_PIXELS(pos.y) - (height);
}
float PhysBody::GetRotation() const
{
return RADTODEG * body->GetAngle();
}
bool PhysBody::Contains(int x, int y) const
{
b2Vec2 p(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
const b2Fixture* fixture = body->GetFixtureList();
while(fixture != NULL)
{
if(fixture->GetShape()->TestPoint(body->GetTransform(), p) == true)
return true;
fixture = fixture->GetNext();
}
return false;
}
int PhysBody::RayCast(int x1, int y1, int x2, int y2, float& normal_x, float& normal_y) const
{
int ret = -1;
b2RayCastInput input;
b2RayCastOutput output;
input.p1.Set(PIXEL_TO_METERS(x1), PIXEL_TO_METERS(y1));
input.p2.Set(PIXEL_TO_METERS(x2), PIXEL_TO_METERS(y2));
input.maxFraction = 1.0f;
const b2Fixture* fixture = body->GetFixtureList();
while(fixture != NULL)
{
if(fixture->GetShape()->RayCast(&output, input, body->GetTransform(), 0) == true)
{
// do we want the normal ?
float fx = x2 - x1;
float fy = y2 - y1;
float dist = sqrtf((fx*fx) + (fy*fy));
normal_x = output.normal.x;
normal_y = output.normal.y;
return output.fraction * dist;
}
fixture = fixture->GetNext();
}
return ret;
}