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