-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulePhysics.cpp
More file actions
302 lines (238 loc) · 7.44 KB
/
ModulePhysics.cpp
File metadata and controls
302 lines (238 loc) · 7.44 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
#include "Globals.h"
#include "Application.h"
#include "ModuleInput.h"
#include "ModuleRender.h"
#include "ModulePhysics.h"
#include "p2Point.h"
#include "math.h"
ModulePhysics::ModulePhysics(Application* app, bool start_enabled) : Module(app, start_enabled)
{
world = NULL;
debug = true;
}
// Destructor
ModulePhysics::~ModulePhysics()
{
}
bool ModulePhysics::Start()
{
LOG("Creating Physics 2D environment");
world = new b2World(b2Vec2(GRAVITY_X, -GRAVITY_Y));
// TODO 3: You need to make ModulePhysics class a contact listener
world->SetContactListener(this);
// big static circle as "ground" in the middle of the screen
int x = SCREEN_WIDTH / 2;
int y = SCREEN_HEIGHT / 1.5f;
int diameter = SCREEN_WIDTH / 2;
b2BodyDef body;
body.type = b2_staticBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
b2Body* b = world->CreateBody(&body);
b2CircleShape shape;
shape.m_radius = PIXEL_TO_METERS(diameter) * 0.5f;
b2FixtureDef fixture;
fixture.shape = &shape;
b->CreateFixture(&fixture);
return true;
}
//
update_status ModulePhysics::PreUpdate()
{
world->Step(1.0f / 60.0f, 6, 2);
// TODO: HomeWork
/*
for(b2Contact* c = world->GetContactList(); c; c = c->GetNext())
{
}
*/
return UPDATE_CONTINUE;
}
PhysBody* ModulePhysics::CreateCircle(int x, int y, int radius)
{
b2BodyDef body;
body.type = b2_dynamicBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
b2Body* b = world->CreateBody(&body);
b2CircleShape shape;
shape.m_radius = PIXEL_TO_METERS(radius);
b2FixtureDef fixture;
fixture.shape = &shape;
fixture.density = 1.0f;
b->CreateFixture(&fixture);
// TODO 4: Add a pointer to PhysBody as UserData to the body
PhysBody* pbody = new PhysBody();
pbody->body = b;
pbody->width = pbody->height = radius;
return pbody;
}
PhysBody* ModulePhysics::CreateRectangle(int x, int y, int width, int height)
{
b2BodyDef body;
body.type = b2_dynamicBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
b2Body* b = world->CreateBody(&body);
b2PolygonShape box;
box.SetAsBox(PIXEL_TO_METERS(width) * 0.5f, PIXEL_TO_METERS(height) * 0.5f);
b2FixtureDef fixture;
fixture.shape = &box;
fixture.density = 1.0f;
b->CreateFixture(&fixture);
PhysBody* pbody = new PhysBody();
pbody->body = b;
pbody->width = width * 0.5f;
pbody->height = height * 0.5f;
return pbody;
}
PhysBody* ModulePhysics::CreateChain(int x, int y, int* points, int size)
{
b2BodyDef body;
body.type = b2_dynamicBody;
body.position.Set(PIXEL_TO_METERS(x), PIXEL_TO_METERS(y));
b2Body* b = world->CreateBody(&body);
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);
b2FixtureDef fixture;
fixture.shape = &shape;
b->CreateFixture(&fixture);
delete p;
PhysBody* pbody = new PhysBody();
pbody->body = b;
pbody->width = pbody->height = 0;
return pbody;
}
//
update_status ModulePhysics::PostUpdate()
{
if(App->input->GetKey(SDL_SCANCODE_F1) == KEY_DOWN)
debug = !debug;
if(!debug)
return UPDATE_CONTINUE;
// Bonus code: this will iterate all objects in the world and draw the circles
// You need to provide your own macro to translate meters to pixels
for(b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
for(b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext())
{
switch(f->GetType())
{
// Draw circles ------------------------------------------------
case b2Shape::e_circle:
{
b2CircleShape* shape = (b2CircleShape*)f->GetShape();
b2Vec2 pos = f->GetBody()->GetPosition();
App->renderer->DrawCircle(METERS_TO_PIXELS(pos.x), METERS_TO_PIXELS(pos.y), METERS_TO_PIXELS(shape->m_radius), 255, 255, 255);
}
break;
// Draw polygons ------------------------------------------------
case b2Shape::e_polygon:
{
b2PolygonShape* polygonShape = (b2PolygonShape*)f->GetShape();
int32 count = polygonShape->GetVertexCount();
b2Vec2 prev, v;
for(int32 i = 0; i < count; ++i)
{
v = b->GetWorldPoint(polygonShape->GetVertex(i));
if(i > 0)
App->renderer->DrawLine(METERS_TO_PIXELS(prev.x), METERS_TO_PIXELS(prev.y), METERS_TO_PIXELS(v.x), METERS_TO_PIXELS(v.y), 255, 100, 100);
prev = v;
}
v = b->GetWorldPoint(polygonShape->GetVertex(0));
App->renderer->DrawLine(METERS_TO_PIXELS(prev.x), METERS_TO_PIXELS(prev.y), METERS_TO_PIXELS(v.x), METERS_TO_PIXELS(v.y), 255, 100, 100);
}
break;
// Draw chains contour -------------------------------------------
case b2Shape::e_chain:
{
b2ChainShape* shape = (b2ChainShape*)f->GetShape();
b2Vec2 prev, v;
for(int32 i = 0; i < shape->m_count; ++i)
{
v = b->GetWorldPoint(shape->m_vertices[i]);
if(i > 0)
App->renderer->DrawLine(METERS_TO_PIXELS(prev.x), METERS_TO_PIXELS(prev.y), METERS_TO_PIXELS(v.x), METERS_TO_PIXELS(v.y), 100, 255, 100);
prev = v;
}
v = b->GetWorldPoint(shape->m_vertices[0]);
App->renderer->DrawLine(METERS_TO_PIXELS(prev.x), METERS_TO_PIXELS(prev.y), METERS_TO_PIXELS(v.x), METERS_TO_PIXELS(v.y), 100, 255, 100);
}
break;
// Draw a single segment(edge) ----------------------------------
case b2Shape::e_edge:
{
b2EdgeShape* shape = (b2EdgeShape*)f->GetShape();
b2Vec2 v1, v2;
v1 = b->GetWorldPoint(shape->m_vertex0);
v1 = b->GetWorldPoint(shape->m_vertex1);
App->renderer->DrawLine(METERS_TO_PIXELS(v1.x), METERS_TO_PIXELS(v1.y), METERS_TO_PIXELS(v2.x), METERS_TO_PIXELS(v2.y), 100, 100, 255);
}
break;
}
}
}
return UPDATE_CONTINUE;
}
// Called before quitting
bool ModulePhysics::CleanUp()
{
LOG("Destroying physics world");
// Delete the whole physics world!
delete world;
return true;
}
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
{
// TODO 1: Write the code to return true in case the point
// is inside ANY of the shapes contained by this body
b2Fixture *f = body->GetFixtureList();
bool ret = false;
for (f; f < nullptr && ret == false; f->GetNext())
{
b2Shape *shape = f->GetShape();
shape->TestPoint(body->GetTransform(), b2Vec2(METERS_TO_PIXELS(x), METERS_TO_PIXELS(y)));
}
return false;
}
int PhysBody::RayCast(int x1, int y1, int x2, int y2, float& normal_x, float& normal_y) const
{
// TODO 2: Write code to test a ray cast between both points provided. If not hit return -1
// if hit, fill normal_x and normal_y and return the distance between x1,y1 and its colliding point
int ret = -1;
b2Fixture *f = body->GetFixtureList();
b2RayCastOutput output;
b2RayCastInput input;
for (f; f < nullptr && ret == -1; f->GetNext())
{
b2Shape *shape = f->GetShape();
if (shape->RayCast(&output, input, body->GetTransform(), shape->GetChildCount()))
{
normal_x = METERS_TO_PIXELS(output.normal.x);
normal_y = METERS_TO_PIXELS(output.normal.y);
ret = METERS_TO_PIXELS(output.fraction);
}
}
return ret;
}
// TODO 3: Make module physics inherit from b2ContactListener
// then override void BeginContact(b2Contact* contact)
// You need to make ModulePhysics class a contact listener
void ModulePhysics::BeginContact(b2Contact* contact)
{
LOG("COLLISION!");
}
// TODO 7: Call the listeners that are not NULL