author | ekku |
Wed, 03 Dec 2008 12:09:42 +0000 | |
changeset 180 | bfe1077edab3 |
parent 179 | c600984d0428 |
child 181 | 62e0c2cfb714 |
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" |
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
5 |
#include "Terrain.hh" |
58 | 6 |
|
45
32c876923cac
I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents:
44
diff
changeset
|
7 |
#include <algorithm> |
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
8 |
#include <functional> |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
9 |
#include <cmath> |
104 | 10 |
#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
|
11 |
|
60 | 12 |
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions) |
161
ea2f295c279f
Made PhysicsWorld to inherit Terrain instead of having it as an attribute.
saiam
parents:
160
diff
changeset
|
13 |
: Terrain(1337), tick_timer(PHYSICS_TICK_MS), tick_counter(0), dimensions(dimensions), |
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
14 |
gravity(gravity) { |
54 | 15 |
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
|
16 |
tick_timer.enable(); |
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
17 |
} |
44 | 18 |
|
19 |
void PhysicsWorld::addObject (PhysicsObject *object) { |
|
47 | 20 |
objects.push_back(object); |
44 | 21 |
} |
22 |
||
180 | 23 |
void PhysicsWorld::addProjectile (PhysicsObject *projectile) { |
24 |
projectiles.push_back(projectile); |
|
25 |
} |
|
26 |
||
44 | 27 |
void PhysicsWorld::tick () { |
123 | 28 |
// Engine::log(DEBUG, "physics.apply_force") << "*tick*"; |
58 | 29 |
|
76 | 30 |
for (std::vector<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) { |
31 |
(*i)->tick(); |
|
32 |
} |
|
105 | 33 |
|
34 |
tick_counter++; |
|
35 |
} |
|
36 |
||
37 |
uint32_t PhysicsWorld::getTick (void) { |
|
38 |
return tick_counter; |
|
48
fa1da22db8a0
It looks like physics could now work, but I doubt it...
saiam
parents:
47
diff
changeset
|
39 |
} |
fa1da22db8a0
It looks like physics could now work, but I doubt it...
saiam
parents:
47
diff
changeset
|
40 |
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
41 |
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
42 |
Vector position, Vector velocity) |
135
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
43 |
: world(world), position(position), velocity(velocity), |
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
44 |
mass(mass), inAir(true), aim(0), facingRight(true) { |
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
45 |
// TODO: Is thir the right way to do this? |
60 | 46 |
world.addObject(this); |
47 |
} |
|
44 | 48 |
|
176 | 49 |
/** |
50 |
* Player walks on floor. |
|
51 |
*/ |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
52 |
Vector PhysicsObject::walk (bool right) { |
176 | 53 |
float deltaX = right ? 1 : -1; // which way we are walking |
112 | 54 |
Vector reached = this->position; |
168 | 55 |
|
176 | 56 |
// Is there upward ramp |
168 | 57 |
if(!possibleLocation(position+Vector(deltaX, 0))) { |
176 | 58 |
// Yes. Then we check n pixels up |
168 | 59 |
for(int i = 1; i < 3; i++) { |
60 |
if(possibleLocation(position+Vector(deltaX, -i))) { |
|
176 | 61 |
// and when there is finally EMPTY, we can walk |
168 | 62 |
reached = position+Vector(deltaX, -i); |
63 |
break; |
|
112 | 64 |
} |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
65 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
66 |
} else { |
179 | 67 |
// Or downward ramp or flat |
68 |
for(int i = 0; 1; i++) { |
|
69 |
||
70 |
// And when there is finally ground we can step on |
|
71 |
// it. If there is no gound we still step there, |
|
72 |
// but will fall one pixel down |
|
168 | 73 |
if(possibleLocation(position+Vector(deltaX, i))) { |
74 |
reached = position+Vector(deltaX, i); |
|
75 |
} else { |
|
76 |
break; |
|
112 | 77 |
} |
179 | 78 |
|
79 |
// If the fall is big enough, set the worm in the air |
|
80 |
if (i >= 2) { |
|
81 |
this->inAir = true; |
|
82 |
this->velocity.x = right ? 10 : -10; |
|
83 |
// Avoid stepping two pixels down when it starts to free fall |
|
84 |
reached.y -= 2; |
|
85 |
break; |
|
86 |
} |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
87 |
} |
168 | 88 |
} |
176 | 89 |
// And we return where we got |
112 | 90 |
return reached; |
179 | 91 |
|
94 | 92 |
} |
93 |
||
170 | 94 |
/** |
95 |
* Makes the player jump in the air. |
|
96 |
* @param direction -1: jump left, 0: jump up, 1: jump right |
|
97 |
*/ |
|
98 |
void PhysicsObject::jump (int direction) { |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
99 |
// Jump only if player is "on the ground" |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
100 |
if (!this->inAir) { |
159 | 101 |
velocity.y = -100; |
170 | 102 |
switch (direction) { |
103 |
case 1: |
|
104 |
velocity.x += 20; |
|
105 |
break; |
|
106 |
case -1: |
|
107 |
velocity.x -= 20; |
|
108 |
break; |
|
109 |
case 0: |
|
110 |
break; |
|
111 |
default: |
|
112 |
throw std::logic_error("Invalid jump direction"); |
|
113 |
} |
|
159 | 114 |
inAir = true; |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
115 |
} |
94 | 116 |
} |
117 |
||
118 |
bool PhysicsObject::possibleLocation (Vector loc) { |
|
112 | 119 |
for(unsigned int i = 0; i < this->shape.size(); i++) { |
172 | 120 |
if(world.collides(loc+shape[i])) |
112 | 121 |
return false; |
122 |
} |
|
123 |
return true; |
|
94 | 124 |
} |
125 |
||
173 | 126 |
void func1() { |
127 |
||
128 |
} |
|
129 |
||
94 | 130 |
/** |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
131 |
* Updates object speed and position. This function organises force |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
132 |
* integration and collision detection. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
133 |
*/ |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
134 |
void PhysicsObject::updatePosition () { |
94 | 135 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
136 |
// Add gravity to the force queue |
85 | 137 |
forceq.push(world.gravity); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
138 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
139 |
// 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
|
140 |
Force total; |
83
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 |
} |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
145 |
|
179 | 146 |
// If the player has stopped and there's some ground under some of the 3 some of the 3t |
177 | 147 |
// set inAir false |
148 |
if (this->velocity == Vector(0,0)) { |
|
179 | 149 |
this->inAir = !world.collides(this->position+shape[1]+Vector(0, 1)) |
150 |
&& !world.collides(this->position+shape[2]+Vector(0, 1)) |
|
151 |
&& !world.collides(this->position+shape[3]+Vector(0, 1)); |
|
177 | 152 |
// If, however, there's a force caused by a bomb, e.g., set it in air. |
153 |
// Still, we have to be able to separate forces caused by walking attempts |
|
154 |
// and bombs etc (+0.1 because float comparison can be dangerous) |
|
155 |
if (total.y < 0 || abs(total.x) > PLAYER_MOVE_FORCE + 0.1) |
|
156 |
this->inAir = true; |
|
167 | 157 |
} |
158 |
||
171 | 159 |
if(!possibleLocation(position)) { |
160 |
//if we are trapped in ground form dirtball or something |
|
177 | 161 |
//we might want to just return and set velocity to some value |
162 |
//return; |
|
171 | 163 |
} |
164 |
||
177 | 165 |
// If the worm is not in the air make it walk, |
166 |
// otherwise integrate the new position and velocity |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
167 |
if (!this->inAir) { |
179 | 168 |
//std::cout << "Tryin to walk" << std::endl; |
177 | 169 |
// It walks only if there's some vertical force |
179 | 170 |
if (total.x != 0) { |
171 |
std::cout << "Succeeding to walk" << std::endl; |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
172 |
this->position = walk(total.x > 0); |
179 | 173 |
this->velocity = Vector(0,0); |
174 |
} |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
175 |
} |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
176 |
|
173 | 177 |
if(!possibleLocation(position)) { |
178 |
Engine::log(DEBUG, "great failure") << "great failure"; |
|
179 |
func1(); |
|
180 |
} |
|
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
181 |
Vector newPosition; |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
182 |
Vector velAfterTick; |
177 | 183 |
// Calculate new position and velocity to the given references |
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
184 |
integrate(total, PHYSICS_TICK_MS, newPosition, velAfterTick); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
185 |
this->velocity = velAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
186 |
|
123 | 187 |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
188 |
// Collision detection |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
189 |
bool collided = false; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
190 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
191 |
const Vector diffVec = newPosition-position; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
192 |
const Vector unitVector = diffVec / diffVec.length(); |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
193 |
Vector reached = position; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
194 |
|
168 | 195 |
while ((position-reached).sqrLength() < diffVec.sqrLength()) { |
173 | 196 |
reached += unitVector; |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
197 |
// Check if any of the shapes points collide |
154
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
198 |
for (uint64_t i = 0; i < shape.size(); i++) { |
175 | 199 |
if (world.collides(reached+shape[i])) { // Collision |
154
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
200 |
if (inAir) { |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
201 |
// Engine::log(DEBUG, "Here"); |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
202 |
this->bounce(world.getNormal(reached+shape[i], |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
203 |
reached-unitVector+shape[i])); |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
204 |
//this->velocity *= COLLISION_ELASTICITY; |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
205 |
} |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
206 |
reached = reached - unitVector; // Return to last point |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
207 |
collided = true; |
167 | 208 |
if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED) { |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
209 |
this->velocity = Vector(0,0); |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
210 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
211 |
break; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
212 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
213 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
214 |
if (collided) |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
215 |
break; |
173 | 216 |
// reached += unitVector; |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
217 |
} |
123 | 218 |
|
95 | 219 |
|
173 | 220 |
if(!possibleLocation(reached)) { |
221 |
Engine::log(DEBUG, "PhysicsObject.updatePosition") << "logic error reached should not be possible to be impossible.. diffVec: " << diffVec; |
|
222 |
func1(); |
|
223 |
} |
|
123 | 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) { |
171 | 227 |
if(!possibleLocation(newPosition)) { |
228 |
newPosition = reached; |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
229 |
} else { |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
230 |
// 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
|
231 |
} |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
232 |
} else { |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
233 |
newPosition = reached; |
116 | 234 |
onCollision(); |
92 | 235 |
//this->velocity = Vector(0, 0); |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
236 |
//TODO: it shouldn't just stop on collision |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
237 |
} |
173 | 238 |
if(!possibleLocation(newPosition)) { |
239 |
Engine::log(DEBUG, "great failure") << "great failure"; |
|
240 |
func1(); |
|
241 |
} |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
242 |
this->position = newPosition; |
173 | 243 |
if(!possibleLocation(position)) { |
244 |
Engine::log(DEBUG, "great failure") << "great failure"; |
|
245 |
func1(); |
|
246 |
} |
|
158 | 247 |
// Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Pos: " << this->position; |
44 | 248 |
} |
97 | 249 |
|
250 |
/** |
|
251 |
* Bounces from straight wall in any direction. |
|
252 |
* Direction given as normal of that wall |
|
253 |
*/ |
|
254 |
void PhysicsObject::bounce (Vector normal) { |
|
178 | 255 |
// normal.sqrLength can't be 0 when got from getNormal() |
256 |
if (normal.sqrLength() != 0) { |
|
153
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
257 |
Vector nvel = velocity; |
178 | 258 |
// We project the velocity on normal and remove twice that much from velocity |
160 | 259 |
nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal); |
153
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
260 |
velocity = nvel; |
178 | 261 |
// We lose some of our speed on collision |
154
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
262 |
this->velocity *= COLLISION_ELASTICITY; |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
263 |
} |
97 | 264 |
} |
44 | 265 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
266 |
/** |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
267 |
* Integrates given force over time and stores new position to |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
268 |
* posAfterTick and new velocity to velAfterTick. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
269 |
* @param force Force vector. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
270 |
* @param dt The time the force is applied (<=PHYSICS_TICK_MS) |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
271 |
*/ |
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
272 |
void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) { |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
273 |
posAfterTick = position; |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
274 |
velAfterTick = velocity; |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
275 |
Derivative tmpd; |
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
276 |
Derivative k1 = evaluate(force, 0, tmpd, posAfterTick, velAfterTick); |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
277 |
Derivative k2 = evaluate(force, 0.5f*dt, k1, posAfterTick, velAfterTick); |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
278 |
Derivative k3 = evaluate(force, 0.5f*dt, k2, posAfterTick, velAfterTick); |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
279 |
Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
280 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
281 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
282 |
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
|
283 |
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
|
284 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
285 |
// Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
286 |
posAfterTick = posAfterTick + (dxdt * dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
287 |
velAfterTick = velAfterTick + (dvdt * dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
288 |
//Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick; |
70 | 289 |
} |
290 |
||
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
291 |
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
292 |
Vector curPos = posAfterTick + (d.dx*dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
293 |
Vector curVel = velAfterTick + (d.dv*dt)/1000; |
58 | 294 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
295 |
Derivative out; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
296 |
out.dx = curVel; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
297 |
out.dv = acceleration(force); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
298 |
//Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
299 |
return out; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
300 |
} |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
301 |
|
85 | 302 |
Vector PhysicsObject::acceleration(const Force &force) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
303 |
return (force/mass); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
304 |
} |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
305 |
|
122
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
306 |
void PhysicsObject::applyForce (Force force) { |
123 | 307 |
// Add applied force to the queue |
308 |
forceq.push(force); |
|
44 | 309 |
} |
310 |
||
108 | 311 |
void PhysicsObject::changeAim(float da) { |
312 |
this->aim += da; |
|
313 |
||
314 |
if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX; |
|
315 |
if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN; |
|
109 | 316 |
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim; |
108 | 317 |
} |
318 |
||
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
319 |
void PhysicsObject::setFacing(bool facingRight) { |
109 | 320 |
//Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right; |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
321 |
this->facingRight = facingRight; |
108 | 322 |
} |
323 |
||
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
324 |
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir) { |
47 | 325 |
this->position = position; |
326 |
this->velocity = velocity; |
|
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
327 |
this->inAir = inAir; |
44 | 328 |
} |
329 |
||
330 |
Vector PhysicsObject::getPosition () { |
|
47 | 331 |
return this->position; |
44 | 332 |
} |
333 |
||
108 | 334 |
bool PhysicsObject::getFacing() { |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
335 |
return this->facingRight; |
108 | 336 |
} |
337 |
||
338 |
float PhysicsObject::getAim() { |
|
339 |
return this->aim; |
|
340 |
} |
|
341 |
||
91 | 342 |
std::vector<Vector>& PhysicsObject::getShape () { |
343 |
return this->shape; |
|
344 |
} |
|
345 |
||
346 |
void PhysicsObject::setShape (std::vector<Vector> shape) { |
|
123 | 347 |
this->shape = shape; |
91 | 348 |
} |
349 |
||
44 | 350 |
void PhysicsObject::tick () { |
47 | 351 |
this->updatePosition(); |
44 | 352 |
} |
353 |
||
153
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
354 |
void PhysicsObject::draw(CL_GraphicContext *gc) { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
355 |
CL_Quad player( |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
356 |
(position+shape[0]).x, (position+shape[0]).y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
357 |
(position+shape[1]).x, (position+shape[1]).y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
358 |
(position+shape[2]).x, (position+shape[2]).y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
359 |
(position+shape[3]).x, (position+shape[3]).y |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
360 |
); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
361 |
|
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
362 |
gc->fill_quad(player, CL_Color::green); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
363 |
|
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
364 |
const uint16_t chlen = 10; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
365 |
uint16_t x = player.center().x; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
366 |
uint16_t y = player.center().y; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
367 |
if (facingRight) { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
368 |
gc->draw_line(x, y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
369 |
x + std::cos(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
370 |
y - std::sin(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
371 |
CL_Color::black); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
372 |
} else { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
373 |
gc->draw_line(x, y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
374 |
x - std::cos(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
375 |
y - std::sin(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
376 |
CL_Color::black); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
377 |
} |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
378 |
} |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
379 |