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