author | Tero Marttila <terom@fixme.fi> |
Tue, 27 Jan 2009 00:25:58 +0200 | |
changeset 439 | 9823e6cd1086 |
parent 423 | 947ab54de4b7 |
permissions | -rw-r--r-- |
197 | 1 |
|
2 |
#include "PhysicsWorld.hh" |
|
3 |
#include "Engine.hh" |
|
4 |
||
423
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
5 |
#include <algorithm> |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
208
diff
changeset
|
6 |
#include <functional> |
423
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
408
diff
changeset
|
7 |
#include <cmath> |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
208
diff
changeset
|
8 |
|
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
351
diff
changeset
|
9 |
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions, Terrain &terrain) : |
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
351
diff
changeset
|
10 |
terrain(terrain), |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
255
diff
changeset
|
11 |
dimensions(dimensions), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
255
diff
changeset
|
12 |
gravity(gravity), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
255
diff
changeset
|
13 |
tick_timer(PHYSICS_TICK_MS) |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
208
diff
changeset
|
14 |
{ |
408
e6cfc44266af
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents:
351
diff
changeset
|
15 |
// wire up our timer |
205 | 16 |
slots.connect(tick_timer.sig_tick(), this, &PhysicsWorld::tick); |
17 |
tick_timer.start(); |
|
197 | 18 |
} |
19 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
208
diff
changeset
|
20 |
void PhysicsWorld::addPhysicsObject (PhysicsObject *po) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
208
diff
changeset
|
21 |
objects.push_back(po); |
197 | 22 |
} |
23 |
||
225 | 24 |
void PhysicsWorld::removePhysicsObject (PhysicsObject *po) { |
25 |
objects.remove(po); |
|
26 |
} |
|
197 | 27 |
|
288 | 28 |
|
29 |
float distancePointToLine(Vector l1, Vector l2, Vector p) { |
|
30 |
Vector v(l2.y - l1.y, -(l2.x - l1.x)); |
|
31 |
Vector r(l1.x-p.x, l1.y-p.y); |
|
32 |
v = v/v.length(); |
|
33 |
float dist = v*r/v.sqrLength(); |
|
34 |
return dist; |
|
35 |
} |
|
36 |
||
205 | 37 |
void PhysicsWorld::tick (TimeMS tick_length) { |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
225
diff
changeset
|
38 |
// tick each object in turn |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
208
diff
changeset
|
39 |
for (std::list<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) { |
205 | 40 |
(*i)->tick(tick_length); |
197 | 41 |
} |
351
7e6d373d8c98
cleaned object-object collision detection in PhysicsWorld.cc:tick
nireco
parents:
300
diff
changeset
|
42 |
// Object - object collision detection |
288 | 43 |
for (std::list<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) { |
44 |
for (std::list<PhysicsObject*>::iterator j = i; j != objects.end(); j++) { |
|
45 |
if(i == j) |
|
46 |
continue; |
|
47 |
float range_sum_sqr = 2; |
|
48 |
if((*i)->getType() == PLAYER) { |
|
49 |
range_sum_sqr += PLAYER_RADIUS-1; |
|
50 |
} |
|
51 |
if((*j)->getType() == PLAYER) { |
|
52 |
range_sum_sqr += PLAYER_RADIUS-1; |
|
53 |
} |
|
54 |
range_sum_sqr *= range_sum_sqr; |
|
55 |
bool collision = false; |
|
56 |
Vector a2 = (*i)->getPosition(); |
|
57 |
Vector b2 = (*j)->getPosition(); |
|
351
7e6d373d8c98
cleaned object-object collision detection in PhysicsWorld.cc:tick
nireco
parents:
300
diff
changeset
|
58 |
float dab = (a2-b2).sqrLength(); |
7e6d373d8c98
cleaned object-object collision detection in PhysicsWorld.cc:tick
nireco
parents:
300
diff
changeset
|
59 |
if(dab < range_sum_sqr) { |
7e6d373d8c98
cleaned object-object collision detection in PhysicsWorld.cc:tick
nireco
parents:
300
diff
changeset
|
60 |
collision = true; |
7e6d373d8c98
cleaned object-object collision detection in PhysicsWorld.cc:tick
nireco
parents:
300
diff
changeset
|
61 |
} |
288 | 62 |
if(collision) { |
63 |
(*i)->onCollision(a2, *j); |
|
64 |
(*j)->onCollision(b2, *i); |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
197 | 68 |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
208
diff
changeset
|
69 |
// Delete destroyed objects |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
208
diff
changeset
|
70 |
objects.remove_if(std::mem_fun(&PhysicsObject::removeIfDestroyed)); |
197 | 71 |
} |
72 |
||
300 | 73 |
TickCount PhysicsWorld::getTicks (void) { |
74 |
return tick_timer.get_ticks(); |
|
75 |
} |
|
76 |