| author | saiam |
| Tue, 09 Dec 2008 02:26:42 +0000 | |
| changeset 348 | 3ebcccc7ee44 |
| parent 300 | 417183866f35 |
| child 351 | 7e6d373d8c98 |
| permissions | -rw-r--r-- |
| 197 | 1 |
|
2 |
#include "PhysicsWorld.hh" |
|
3 |
#include "Engine.hh" |
|
4 |
||
|
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
|
5 |
#include <functional> |
|
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 |
|
|
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
|
7 |
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions) : |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
255
diff
changeset
|
8 |
// XXX: assume Vector == PixelCoordinate |
|
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
255
diff
changeset
|
9 |
Terrain((unsigned int) dimensions.x, (unsigned int) dimensions.y, 1337), |
|
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
255
diff
changeset
|
10 |
dimensions(dimensions), |
|
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
255
diff
changeset
|
11 |
gravity(gravity), |
|
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
255
diff
changeset
|
12 |
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
|
13 |
{
|
| 205 | 14 |
slots.connect(tick_timer.sig_tick(), this, &PhysicsWorld::tick); |
15 |
tick_timer.start(); |
|
| 197 | 16 |
} |
17 |
||
|
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
|
18 |
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
|
19 |
objects.push_back(po); |
| 197 | 20 |
} |
21 |
||
| 225 | 22 |
void PhysicsWorld::removePhysicsObject (PhysicsObject *po) {
|
23 |
objects.remove(po); |
|
24 |
} |
|
| 197 | 25 |
|
| 288 | 26 |
|
27 |
float distancePointToLine(Vector l1, Vector l2, Vector p) {
|
|
28 |
Vector v(l2.y - l1.y, -(l2.x - l1.x)); |
|
29 |
Vector r(l1.x-p.x, l1.y-p.y); |
|
30 |
v = v/v.length(); |
|
31 |
float dist = v*r/v.sqrLength(); |
|
32 |
return dist; |
|
33 |
} |
|
34 |
||
| 205 | 35 |
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
|
36 |
// 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
|
37 |
for (std::list<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) {
|
| 205 | 38 |
(*i)->tick(tick_length); |
| 197 | 39 |
} |
| 288 | 40 |
for (std::list<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) {
|
41 |
for (std::list<PhysicsObject*>::iterator j = i; j != objects.end(); j++) {
|
|
42 |
if(i == j) |
|
43 |
continue; |
|
44 |
float range_sum_sqr = 2; |
|
45 |
if((*i)->getType() == PLAYER) {
|
|
46 |
range_sum_sqr += PLAYER_RADIUS-1; |
|
47 |
} |
|
48 |
if((*j)->getType() == PLAYER) {
|
|
49 |
range_sum_sqr += PLAYER_RADIUS-1; |
|
50 |
} |
|
51 |
range_sum_sqr *= range_sum_sqr; |
|
52 |
bool collision = false; |
|
53 |
Vector a1 = (*i)->getPreviousPosition(); |
|
54 |
Vector a2 = (*i)->getPosition(); |
|
55 |
Vector b1 = (*j)->getPreviousPosition(); |
|
56 |
Vector b2 = (*j)->getPosition(); |
|
57 |
/* if(a1 == a2) {
|
|
58 |
float d = abs(distancePointToLine(b1, b2, a1)); |
|
59 |
if(d*d < range_sum_sqr) |
|
60 |
collision = true; |
|
61 |
} else if(b1 == b2) {
|
|
62 |
float d = abs(distancePointToLine(a1, a2, b1)); |
|
63 |
if(d*d < range_sum_sqr) |
|
64 |
collision = true; |
|
65 |
} else {*/
|
|
66 |
float db1 = distancePointToLine(a1, a2, b1); |
|
67 |
float db2 = distancePointToLine(a1, a2, b2); |
|
68 |
float da1 = distancePointToLine(b1, b2, a1); |
|
69 |
float da2 = distancePointToLine(b1, b2, a2); |
|
70 |
if(db1*db2 < 0 && da1*da2 < 0) {
|
|
71 |
// lines intersected |
|
72 |
// collision = true; |
|
73 |
} |
|
74 |
float dab = (a2-b2).sqrLength(); |
|
75 |
if(dab < range_sum_sqr) {
|
|
76 |
collision = true; |
|
77 |
} |
|
78 |
// } |
|
79 |
if(collision) {
|
|
80 |
(*i)->onCollision(a2, *j); |
|
81 |
(*j)->onCollision(b2, *i); |
|
82 |
} |
|
83 |
} |
|
84 |
} |
|
| 197 | 85 |
|
|
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
|
86 |
// 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
|
87 |
objects.remove_if(std::mem_fun(&PhysicsObject::removeIfDestroyed)); |
| 197 | 88 |
} |
89 |
||
| 300 | 90 |
TickCount PhysicsWorld::getTicks (void) {
|
91 |
return tick_timer.get_ticks(); |
|
92 |
} |
|
93 |