-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModulePhysics.cpp
353 lines (283 loc) · 9.03 KB
/
ModulePhysics.cpp
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
#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;
worldBody = 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");
// Create a new World
world = new b2World(b2Vec2(GRAVITY_X, GRAVITY_Y));
// 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;
worldBody = world->CreateBody(&bd); // Add the static shapeless 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()
{
// Keep playing
return UPDATE_CONTINUE;
}
bool ModulePhysics::CleanUp()
{
LOG("Destroying physics world");
// Delete the whole physics world!
delete world;
return true;
}
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;
fixture.restitution = 0.2f;
// 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;
}
b2RevoluteJoint* ModulePhysics::CreateRevoluteJoint(PhysBody* pBody1, PhysBody* pBody2, float anchorX, float anchorY)
{
b2RevoluteJointDef def;
def.Initialize(pBody1->body, pBody2->body, { PIXEL_TO_METERS(anchorX), PIXEL_TO_METERS(anchorY) });
def.collideConnected = false;
return (b2RevoluteJoint*)world->CreateJoint(&def);
}
b2PrismaticJoint* ModulePhysics::CreatePrismaticJoint(PhysBody* pBody1, PhysBody* pBody2, float anchorX, float anchorY, b2Vec2 axis)
{
b2PrismaticJointDef def;
def.Initialize(pBody1->body, pBody2->body, b2Vec2(PIXEL_TO_METERS(anchorX), PIXEL_TO_METERS(anchorY)), axis);
def.collideConnected = false;
return (b2PrismaticJoint*)world->CreateJoint(&def);
}
// 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);
}
void ModulePhysics::EndContact(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->EndCollision(physA, physB);
if (physB && physB->listener != NULL)
physB->listener->EndCollision(physB, physA);
}
// PHYS BODY FUNCTIONS -------------------------------------------------------------------------------
PhysBody::PhysBody() : listener(NULL), body(NULL), prop(nullptr)
{
// Initialize all internal class variables
width = height = 0;
body = NULL;
listener = NULL;
prop = nullptr;
}
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;
}