| author | saiam |
| Sat, 29 Nov 2008 17:29:53 +0000 | |
| changeset 133 | c05e84ccc4b3 |
| parent 131 | 5a81c6dc5451 |
| child 135 | d5624d698a78 |
| permissions | -rw-r--r-- |
| 58 | 1 |
|
2 |
#include "Physics.hh" |
|
3 |
#include "Engine.hh" |
|
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
4 |
#include "GameState.hh" |
| 58 | 5 |
|
|
45
32c876923cac
I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents:
44
diff
changeset
|
6 |
#include <algorithm> |
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
7 |
#include <functional> |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
8 |
#include <cmath> |
| 104 | 9 |
#include <assert.h> |
|
45
32c876923cac
I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents:
44
diff
changeset
|
10 |
|
| 60 | 11 |
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions) |
| 105 | 12 |
: tick_timer(PHYSICS_TICK_MS), tick_counter(0), gravity(gravity), dimensions(dimensions), terrain(dimensions.x, std::vector<TerrainType>(dimensions.y, DIRT)) {
|
| 88 | 13 |
|
| 112 | 14 |
generateTerrain(1337); |
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
15 |
|
| 54 | 16 |
slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick); |
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
17 |
tick_timer.enable(); |
|
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
18 |
} |
| 44 | 19 |
|
20 |
void PhysicsWorld::addObject (PhysicsObject *object) {
|
|
| 47 | 21 |
objects.push_back(object); |
| 44 | 22 |
} |
23 |
||
24 |
void PhysicsWorld::tick () {
|
|
| 123 | 25 |
// Engine::log(DEBUG, "physics.apply_force") << "*tick*"; |
| 58 | 26 |
|
| 76 | 27 |
for (std::vector<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) {
|
28 |
(*i)->tick(); |
|
29 |
} |
|
| 105 | 30 |
|
31 |
tick_counter++; |
|
32 |
} |
|
33 |
||
34 |
uint32_t PhysicsWorld::getTick (void) {
|
|
35 |
return tick_counter; |
|
|
48
fa1da22db8a0
It looks like physics could now work, but I doubt it...
saiam
parents:
47
diff
changeset
|
36 |
} |
|
fa1da22db8a0
It looks like physics could now work, but I doubt it...
saiam
parents:
47
diff
changeset
|
37 |
|
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
38 |
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, |
|
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
39 |
Vector position, Vector velocity) |
|
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
40 |
: world(world), mass(mass), position(position), |
|
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
41 |
velocity(velocity), facingRight(true), inAir(true) {
|
| 60 | 42 |
|
43 |
world.addObject(this); |
|
44 |
} |
|
| 44 | 45 |
|
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
46 |
Vector PhysicsObject::walk (bool right) {
|
| 112 | 47 |
Vector cursor = right ? this->position + Vector(1,0) : this->position + Vector(-1,0); |
48 |
Vector reached = this->position; |
|
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
49 |
|
| 112 | 50 |
//for(int steps = 0; steps < 3; steps++) {
|
51 |
||
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
52 |
// Go up but not if the wall is over two pixels |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
53 |
if(world.getType(cursor) != EMPTY) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
54 |
for(int height = 0, max = 3; height < max+42; height++) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
55 |
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
56 |
if(height >= max) |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
57 |
return reached; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
58 |
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
59 |
cursor.y--; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
60 |
if(world.getType(cursor) == EMPTY) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
61 |
// Check that the other parts of the worm don't collide with anything |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
62 |
if(possibleLocation(cursor)) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
63 |
reached = cursor; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
64 |
continue; |
| 123 | 65 |
} else {
|
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
66 |
// Can't get any further |
| 112 | 67 |
return reached; |
68 |
} |
|
69 |
} |
|
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
70 |
} |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
71 |
} else {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
72 |
if(possibleLocation(cursor)) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
73 |
reached = cursor; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
74 |
} |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
75 |
// Start checking if the lower squares are empty |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
76 |
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
77 |
for(int depth = 0, max = 3; depth < max+42; depth++) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
78 |
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
79 |
if(depth >= max) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
80 |
// We can start a free fall now |
| 123 | 81 |
// |
| 112 | 82 |
// TODO it should be set inAir if it falls from a cliff |
| 116 | 83 |
this->inAir = true; |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
84 |
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
85 |
// Put some speed there to make loke smoother |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
86 |
//this->velocity.y = -5; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
87 |
return reached; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
88 |
} |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
89 |
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
90 |
cursor.y++; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
91 |
if(world.getType(cursor) == EMPTY) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
92 |
// Check that the other parts of the worm don't collide with anything |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
93 |
if(possibleLocation(cursor)) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
94 |
reached = cursor; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
95 |
continue; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
96 |
} else {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
97 |
// Can't get any further |
| 112 | 98 |
return reached; |
99 |
} |
|
100 |
} |
|
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
101 |
} |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
102 |
} |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
103 |
|
| 112 | 104 |
// cursor.x += right ? 1 : -1; |
105 |
//} |
|
106 |
return reached; |
|
| 94 | 107 |
} |
108 |
||
109 |
void PhysicsObject::jump () {
|
|
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
110 |
// Jump only if player is "on the ground" |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
111 |
if (!this->inAir) {
|
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
112 |
velocity.y = -100; |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
113 |
inAir = true; |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
114 |
} |
| 94 | 115 |
} |
116 |
||
117 |
bool PhysicsObject::possibleLocation (Vector loc) {
|
|
| 112 | 118 |
for(unsigned int i = 0; i < this->shape.size(); i++) {
|
119 |
if(world.getType(loc+shape[i]) != EMPTY) |
|
120 |
return false; |
|
121 |
} |
|
122 |
return true; |
|
| 94 | 123 |
} |
124 |
||
125 |
/** |
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
126 |
* Updates object speed and position. This function organises force |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
127 |
* integration and collision detection. |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
128 |
*/ |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
129 |
void PhysicsObject::updatePosition () {
|
| 94 | 130 |
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
131 |
// Add gravity to the force queue |
| 85 | 132 |
forceq.push(world.gravity); |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
133 |
|
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
134 |
// Go trough every force in the queue |
|
84
3cb862028a24
No nyt se toimii v?h?n paremmin. Koodia pit?? kyll? viel? siisti? aika huolella.
saiam
parents:
83
diff
changeset
|
135 |
Force total; |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
136 |
posAfterTick = position; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
137 |
velAfterTick = velocity; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
138 |
while (!forceq.empty()) {
|
| 85 | 139 |
total += forceq.front(); |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
140 |
forceq.pop(); |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
141 |
} |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
142 |
|
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
143 |
// TODO: This is _ugly_ (but not so ugly as the last one I think) |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
144 |
// hack. I think we should handle walking from the collision |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
145 |
// detection code. |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
146 |
|
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
147 |
if (!this->inAir) {
|
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
148 |
if (total.x != 0) |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
149 |
this->position = walk(total.x > 0); |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
150 |
return; // argh |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
151 |
} |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
152 |
|
| 85 | 153 |
integrate(total, PHYSICS_TICK_MS); |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
154 |
|
|
84
3cb862028a24
No nyt se toimii v?h?n paremmin. Koodia pit?? kyll? viel? siisti? aika huolella.
saiam
parents:
83
diff
changeset
|
155 |
Vector newPosition = posAfterTick /*+ (velAfterTick * PHYSICS_TICK_MS)/1000*/; |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
156 |
this->velocity = velAfterTick; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
157 |
|
| 123 | 158 |
|
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
159 |
// Collision detection |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
160 |
bool collided = false; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
161 |
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
162 |
const Vector diffVec = newPosition-position; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
163 |
const Vector unitVector = diffVec / diffVec.length(); |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
164 |
Vector reached = position; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
165 |
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
166 |
while ((position-reached).length() < diffVec.length()) {
|
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
167 |
// Check if any of the shapes points collide |
| 123 | 168 |
for (uint64_t i = 0; i < shape.size(); i ++) {
|
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
169 |
if (world.getType(reached+shape[i]) != EMPTY) { // Collision
|
|
124
2fd698e04779
fixed collision detection again, and put some needed rounding to getNormal
nireco
parents:
123
diff
changeset
|
170 |
this->bounce(world.getNormal(reached+shape[i], reached-unitVector+shape[i])); |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
171 |
reached = reached - unitVector; // Return to last point |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
172 |
collided = true; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
173 |
this->velocity *= COLLISION_ELASTICITY; |
| 123 | 174 |
if (this->velocity.length() < PLAYER_MIN_SPEED) {
|
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
175 |
this->inAir = false; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
176 |
this->velocity = Vector(0,0); |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
177 |
} |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
178 |
break; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
179 |
} |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
180 |
} |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
181 |
if (collided) |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
182 |
break; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
183 |
reached += unitVector; |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
184 |
} |
| 123 | 185 |
|
| 95 | 186 |
|
| 123 | 187 |
/* |
188 |
bool collided = false; |
|
| 94 | 189 |
|
| 123 | 190 |
//goes 1 unit forward every step and check if has hit anything |
191 |
Vector unitVector = (newPosition-position) / (newPosition-position).length(); |
|
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
192 |
|
| 123 | 193 |
Vector tmpVector = position; |
194 |
Vector reached = position; |
|
195 |
||
196 |
int steps = (int) (newPosition-position).length() + 2; |
|
197 |
||
198 |
//Engine::log(DEBUG, "physics.update_position") << unitVector-newPosition; |
|
199 |
//Vector foo = position+unitVector*steps-newPosition; |
|
200 |
//Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Virhe: "<< foo; |
|
201 |
||
202 |
for(int i = 0; i < steps; i++) {
|
|
203 |
tmpVector += unitVector; |
|
204 |
||
205 |
float minVelocity = 10; |
|
206 |
// Check if any of the four corners of the worm collide |
|
207 |
for(int sh = 0; sh < 4; sh++) {
|
|
208 |
if(world.getType(tmpVector+shape[sh]) != EMPTY) {
|
|
209 |
reached = position + unitVector*(i-1); |
|
210 |
collided = true; |
|
211 |
this->bounce(world.getNormal(tmpVector+shape[sh], tmpVector-unitVector+shape[sh])); |
|
212 |
this->velocity *= 0.4; |
|
213 |
if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
|
|
214 |
this->inAir = false; |
|
215 |
this->velocity = Vector(0,0); |
|
216 |
} |
|
217 |
break; |
|
218 |
} |
|
219 |
} |
|
220 |
if(collided) |
|
221 |
break; |
|
222 |
} |
|
223 |
*/ |
|
224 |
||
| 92 | 225 |
// In case of some float error check the final coordinate |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
226 |
if(!collided) {
|
| 92 | 227 |
if(world.getType(newPosition+shape[0]) != EMPTY || (world.getType(newPosition+shape[1]) != EMPTY) |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
228 |
|| (world.getType(newPosition+shape[2]) != EMPTY) || (world.getType(newPosition+shape[3]) != EMPTY)) {
|
| 92 | 229 |
|
| 112 | 230 |
Engine::log(DEBUG, "physics.update_position") << "didnt hit"; |
231 |
// There was error, and there is ground |
|
| 92 | 232 |
//newPosition = tmpVector; |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
233 |
} else {
|
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
234 |
// This means everything was ok, so no need to do anything |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
235 |
} |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
236 |
} else {
|
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
237 |
newPosition = reached; |
| 116 | 238 |
onCollision(); |
| 92 | 239 |
//this->velocity = Vector(0, 0); |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
240 |
//TODO: it shouldn't just stop on collision |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
241 |
} |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
242 |
this->position = newPosition; |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
243 |
|
| 44 | 244 |
} |
| 97 | 245 |
|
246 |
/** |
|
247 |
* Gets the index of the given coordinate direction |
|
248 |
* referring to the DIRECTIONS table in Physics.hh |
|
249 |
*/ |
|
| 95 | 250 |
int getDirectionIndex (Vector dir) {
|
| 110 | 251 |
if(dir.x == 0 && dir.y == -1) {
|
252 |
return 0; |
|
253 |
} else if(dir.x == 1 && dir.y == -1) {
|
|
254 |
return 1; |
|
255 |
} else if(dir.x == 1 && dir.y == 0) {
|
|
256 |
return 2; |
|
257 |
} else if(dir.x == 1 && dir.y == 1) {
|
|
258 |
return 3; |
|
259 |
} else if(dir.x == 0 && dir.y == 1) {
|
|
260 |
return 4; |
|
261 |
} else if(dir.x == -1 && dir.y == 1) {
|
|
262 |
return 5; |
|
263 |
} else if(dir.x == -1 && dir.y == 0) {
|
|
264 |
return 6; |
|
265 |
} else if(dir.x == -1 && dir.y == -1) {
|
|
266 |
return 7; |
|
267 |
} |
|
268 |
Engine::log(DEBUG, "physics.getDirectionIndex ") << "invalid direction: " << dir; |
|
269 |
return 0; |
|
| 95 | 270 |
} |
271 |
||
| 97 | 272 |
/** |
273 |
* Computes hitten wall's normal. Calculated from 3*3 grid |
|
274 |
*/ |
|
| 95 | 275 |
Vector PhysicsWorld::getNormal (Vector hitPoint, Vector prevPoint) {
|
| 112 | 276 |
// Search free points with bfs and put them to vector |
277 |
std::vector<Vector> frees; |
|
| 123 | 278 |
Vector hit(hitPoint); |
279 |
Vector prev(prevPoint); |
|
| 104 | 280 |
|
| 112 | 281 |
assert(hit != prev); |
282 |
||
|
124
2fd698e04779
fixed collision detection again, and put some needed rounding to getNormal
nireco
parents:
123
diff
changeset
|
283 |
int dirIdx = getDirectionIndex(prev.roundToInt() - hit.roundToInt()); |
|
122
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
284 |
//float tmp1 = hit.x-prev.x; |
|
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
285 |
//float tmp2 = hit.y-prev.y; |
| 123 | 286 |
// Engine::log(DEBUG, "physics.getNormal ") << dirIdx << " " << tmp1 << " " << tmp2; |
| 104 | 287 |
|
| 112 | 288 |
for(int i = 1; i <= 2; i++) {
|
289 |
if(getType(hit+DIRECTIONS[(dirIdx+i) % 8]) == EMPTY) |
|
290 |
frees.push_back(DIRECTIONS[(dirIdx+i) % 8]); |
|
291 |
else |
|
292 |
break; |
|
293 |
} |
|
294 |
for(int i = 1; i <= 2; i++) {
|
|
295 |
if(getType(hit+DIRECTIONS[(dirIdx-i+8) % 8]) == EMPTY) |
|
296 |
frees.push_back(DIRECTIONS[(dirIdx-i+8) % 8]); |
|
297 |
else |
|
298 |
break; |
|
299 |
} |
|
| 98 | 300 |
frees.push_back(DIRECTIONS[dirIdx]); |
| 97 | 301 |
|
| 112 | 302 |
Vector normal(0,0); |
303 |
for(unsigned int i = 0; i < frees.size(); i++) {
|
|
304 |
normal += frees[i]; |
|
305 |
} |
|
| 110 | 306 |
Engine::log(DEBUG, "physics.getNormal ") << "normal: " << normal; |
| 112 | 307 |
return normal; |
| 97 | 308 |
} |
309 |
||
310 |
/** |
|
311 |
* Bounces from straight wall in any direction. |
|
312 |
* Direction given as normal of that wall |
|
313 |
*/ |
|
| 109 | 314 |
// TODO Bounce doesnt work kun oikealle tai vasemmalle alaviistoon mennään suoralla (tangentaalinen arvo väärän suuntainen) |
| 97 | 315 |
void PhysicsObject::bounce (Vector normal) {
|
316 |
Vector tangent(normal.y, -normal.x); |
|
| 101 | 317 |
Vector tprojection = tangent*(velocity * tangent) / (tangent.length()*tangent.length()); |
318 |
Vector nprojection = normal*(velocity * normal) / (normal.length()*normal.length()); |
|
| 99 | 319 |
velocity = tprojection - nprojection; |
| 97 | 320 |
} |
| 44 | 321 |
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
322 |
/** |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
323 |
* Integrates given force over time and stores new position to |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
324 |
* posAfterTick and new velocity to velAfterTick. |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
325 |
* @param force Force vector. |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
326 |
* @param dt The time the force is applied (<=PHYSICS_TICK_MS) |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
327 |
*/ |
| 85 | 328 |
void PhysicsObject::integrate(Force force, TimeMS dt) {
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
329 |
Derivative tmpd; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
330 |
Derivative k1 = evaluate(force, 0, tmpd); |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
331 |
Derivative k2 = evaluate(force, 0.5f*dt, k1); |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
332 |
Derivative k3 = evaluate(force, 0.5f*dt, k2); |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
333 |
Derivative k4 = evaluate(force, dt, k3); |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
334 |
|
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
335 |
|
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
336 |
const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
337 |
const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
338 |
|
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
339 |
// Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
340 |
posAfterTick = posAfterTick + (dxdt * dt)/1000; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
341 |
velAfterTick = velAfterTick + (dvdt * dt)/1000; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
342 |
//Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick; |
| 70 | 343 |
} |
344 |
||
| 85 | 345 |
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d) {
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
346 |
Vector curPos = posAfterTick + (d.dx*dt)/1000; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
347 |
Vector curVel = velAfterTick + (d.dv*dt)/1000; |
| 58 | 348 |
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
349 |
Derivative out; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
350 |
out.dx = curVel; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
351 |
out.dv = acceleration(force); |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
352 |
//Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
353 |
return out; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
354 |
} |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
355 |
|
| 85 | 356 |
Vector PhysicsObject::acceleration(const Force &force) {
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
357 |
return (force/mass); |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
358 |
} |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
359 |
|
|
122
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
360 |
void PhysicsObject::applyForce (Force force) {
|
| 123 | 361 |
// Add applied force to the queue |
362 |
forceq.push(force); |
|
| 44 | 363 |
} |
364 |
||
| 108 | 365 |
void PhysicsObject::changeAim(float da) {
|
366 |
this->aim += da; |
|
367 |
||
368 |
if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX; |
|
369 |
if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN; |
|
| 109 | 370 |
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim; |
| 108 | 371 |
} |
372 |
||
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
373 |
void PhysicsObject::setFacing(bool facingRight) {
|
| 109 | 374 |
//Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right; |
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
375 |
this->facingRight = facingRight; |
| 108 | 376 |
} |
377 |
||
|
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
378 |
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir) {
|
| 47 | 379 |
this->position = position; |
380 |
this->velocity = velocity; |
|
|
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
381 |
this->inAir = inAir; |
| 44 | 382 |
} |
383 |
||
384 |
Vector PhysicsObject::getPosition () {
|
|
| 47 | 385 |
return this->position; |
| 44 | 386 |
} |
387 |
||
| 108 | 388 |
bool PhysicsObject::getFacing() {
|
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
389 |
return this->facingRight; |
| 108 | 390 |
} |
391 |
||
392 |
float PhysicsObject::getAim() {
|
|
393 |
return this->aim; |
|
394 |
} |
|
395 |
||
| 91 | 396 |
std::vector<Vector>& PhysicsObject::getShape () {
|
397 |
return this->shape; |
|
398 |
} |
|
399 |
||
400 |
void PhysicsObject::setShape (std::vector<Vector> shape) {
|
|
| 123 | 401 |
this->shape = shape; |
| 91 | 402 |
} |
403 |
||
| 44 | 404 |
void PhysicsObject::tick () {
|
| 47 | 405 |
this->updatePosition(); |
| 44 | 406 |
} |
407 |
||
| 75 | 408 |
/** |
409 |
* simple random map generation |
|
410 |
* first fills whole level with dirt |
|
411 |
* then randomizes circles of empty or rock |
|
412 |
* @param seed - seed number for random number generator |
|
413 |
*/ |
|
| 79 | 414 |
void PhysicsWorld::generateTerrain(int seed) {
|
| 75 | 415 |
// generating should use own random number generator, but didn't find easily how that is done |
416 |
srand(seed); |
|
417 |
||
418 |
// some constants to control random generation |
|
| 103 | 419 |
const int min_range = 25; |
420 |
const int max_range = 80; |
|
421 |
const int num = 50; |
|
| 75 | 422 |
const int rock_rarity = 4; // 1 / rock_rarity will be rock circle |
423 |
||
424 |
// loops for amount of circles |
|
425 |
for(int i = 0; i < num; i++) {
|
|
426 |
// information of new circle |
|
| 79 | 427 |
int midx = rand()%(int)dimensions.x; |
428 |
int midy = rand()%(int)dimensions.y; |
|
| 88 | 429 |
int range = rand()%(max_range-min_range)+min_range; |
| 79 | 430 |
|
431 |
// put first circle in the middle of the cave |
|
| 112 | 432 |
// so that we have some area we can certainly spawn into |
433 |
if(i == 0) {
|
|
434 |
midx = dimensions.x/2; |
|
435 |
midy = dimensions.y/2; |
|
436 |
range = 150; |
|
437 |
} |
|
| 79 | 438 |
|
| 90 | 439 |
TerrainType type = EMPTY; |
| 75 | 440 |
if(rand()%rock_rarity == 0) {
|
441 |
type = ROCK; |
|
442 |
} |
|
| 90 | 443 |
|
| 75 | 444 |
// loops for every pixel of circle |
| 79 | 445 |
for(int x = std::max(0, midx-range); x < std::min((int)dimensions.x, midx+range); x++) {
|
446 |
for(int y = std::max(0, midy-range); y < std::min((int)dimensions.y, midy+range); y++) {
|
|
| 109 | 447 |
//if((x-midx) * (x-midx) + (y-midy) * (y-midy) < range*range) {
|
| 123 | 448 |
// and sets it to type |
449 |
terrain[x][y] = type; |
|
| 109 | 450 |
//} |
| 75 | 451 |
} |
452 |
} |
|
453 |
} |
|
454 |
} |
|
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
455 |
|
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
456 |
/** |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
457 |
* Returns terrainType in given tile. ROCK if tile is out of area |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
458 |
* @param pos - coordinate of tile |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
459 |
*/ |
| 112 | 460 |
TerrainType PhysicsWorld::getType(int x, int y) const {
|
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
461 |
if(x < 0 || y < 0 || x >= dimensions.x || y >= dimensions.y) {
|
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
462 |
return ROCK; |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
463 |
} |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
464 |
return terrain[x][y]; |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
465 |
} |
| 112 | 466 |
TerrainType PhysicsWorld::getType(Vector pos) const {
|
467 |
int x = (int)(pos.x); |
|
468 |
int y = (int)(pos.y); |
|
469 |
return getType(x, y); |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* Removes ground from given circle. ROCK is not removed. |
|
474 |
* @param (x, y) or pos - center of circle. |
|
475 |
* @param r - radius of circle |
|
476 |
*/ |
|
477 |
void PhysicsWorld::removeGround(int x, int y, float r) {
|
|
478 |
for(int i = x-(int)r; i < x+r; i++) {
|
|
479 |
for(int j = y-(int)r; j < y+r; j++) {
|
|
480 |
if(getType(i, j) != ROCK) {
|
|
481 |
if((i-x)*(i-x)+(j-y)*(j-y) < r*r) {
|
|
482 |
// Safe because getType returns ROCK if tile is out of bounds |
|
483 |
terrain[i][j] = EMPTY; |
|
484 |
} |
|
485 |
} |
|
486 |
} |
|
487 |
} |
|
488 |
} |
|
489 |
void PhysicsWorld::removeGround(Vector pos, float r) {
|
|
490 |
removeGround((int)pos.x, (int)pos.y, r); |
|
491 |
} |