author | terom |
Mon, 08 Dec 2008 12:02:20 +0000 | |
changeset 282 | e0e4dfc3e528 |
parent 279 | e36f5e1a1c8d |
child 285 | c080c8c70333 |
permissions | -rw-r--r-- |
197 | 1 |
#include "PhysicsObject.hh" |
2 |
#include "Engine.hh" |
|
3 |
||
4 |
#include <cmath> |
|
273 | 5 |
#include <utility> |
6 |
#include <queue> |
|
197 | 7 |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
8 |
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity, ObjectType type, |
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
9 |
float collision_elasticity, bool enabled) : |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
10 |
world(world), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
11 |
position(position), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
12 |
velocity(velocity), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
13 |
mass(mass), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
14 |
inAir(true), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
15 |
collision_elasticity(collision_elasticity), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
16 |
aim(0), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
17 |
facing(FACING_RIGHT), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
18 |
alive(false), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
19 |
shouldDelete(false), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
20 |
type(type), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
21 |
pivot(NULL) |
225 | 22 |
{ |
23 |
if (enabled) |
|
24 |
enable(); |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
25 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
26 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
27 |
PhysicsObject::~PhysicsObject (void) { |
225 | 28 |
// Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */; |
197 | 29 |
} |
30 |
||
31 |
/** |
|
32 |
* Player walks on floor. |
|
33 |
*/ |
|
34 |
Vector PhysicsObject::walk_one_step (float partial, bool right) { |
|
35 |
// which way we are walking |
|
36 |
float deltaX = right ? partial : -partial; |
|
37 |
Vector reached = this->position; |
|
38 |
if(reached.roundToInt() == (reached+Vector(deltaX, 0)).roundToInt()) { |
|
39 |
return reached+Vector(deltaX, 0); |
|
40 |
} |
|
41 |
// Is there upward ramp |
|
42 |
if(!possibleLocation(position+Vector(deltaX, 0))) { |
|
43 |
// Yes. Then we check n pixels up |
|
44 |
for(int i = 1; i < 3; i++) { |
|
45 |
if(possibleLocation(position+Vector(deltaX, -i))) { |
|
46 |
// and when there is finally EMPTY, we can walk |
|
47 |
reached = position+Vector(deltaX, -i); |
|
48 |
break; |
|
49 |
} |
|
50 |
} |
|
51 |
} else { |
|
52 |
// Or downward ramp or flat |
|
53 |
for(int i = 0; 1; i++) { |
|
54 |
||
55 |
// And when there is finally ground we can step on |
|
56 |
// it. If there is no gound we still step there, |
|
57 |
// but will fall one pixel down |
|
58 |
if(possibleLocation(position+Vector(deltaX, i))) { |
|
59 |
reached = position+Vector(deltaX, i); |
|
60 |
} else { |
|
61 |
break; |
|
62 |
} |
|
63 |
||
64 |
// If the fall is big enough, set the worm in the air |
|
65 |
if (i >= 2) { |
|
66 |
// Vector back = walk(dt, !right); |
|
67 |
this->inAir = true; |
|
68 |
// this->velocity.x = right ? velocity : -velocity; |
|
69 |
// Avoid stepping two pixels down when it starts to free fall |
|
70 |
reached.y -= 2; |
|
71 |
// this->velocity = (reached-back)*1000/dt; |
|
72 |
break; |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
// And we return where we got |
|
77 |
return reached; |
|
78 |
} |
|
79 |
void PhysicsObject::walk (TimeMS dt, bool right) { |
|
80 |
float velocity = PLAYER_WALK_SPEED; |
|
81 |
float walkAmount = (velocity*dt)/1000; |
|
82 |
Vector reached = this->position; |
|
221 | 83 |
while (walkAmount > 0 && !this->inAir) { |
197 | 84 |
this->position = walk_one_step((1 < walkAmount ? 1 : walkAmount), right); |
85 |
walkAmount--; |
|
86 |
} |
|
87 |
// TODO: Should the remaining walkAmount be handled somehow? |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Makes the player jump in the air. |
|
92 |
* @param direction -1: jump left, 0: jump up, 1: jump right |
|
93 |
*/ |
|
94 |
void PhysicsObject::jump (int direction) { |
|
95 |
// Jump only if player is "on the ground" |
|
96 |
if (!this->inAir) { |
|
97 |
velocity.y = -100; |
|
98 |
switch (direction) { |
|
99 |
case 1: |
|
100 |
velocity.x += 20; |
|
101 |
break; |
|
102 |
case -1: |
|
103 |
velocity.x -= 20; |
|
104 |
break; |
|
105 |
case 0: |
|
106 |
break; |
|
107 |
default: |
|
108 |
throw std::logic_error("Invalid jump direction"); |
|
109 |
} |
|
110 |
inAir = true; |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
bool PhysicsObject::possibleLocation (Vector loc) { |
|
115 |
for(unsigned int i = 0; i < this->shape.size(); i++) { |
|
116 |
if(world.collides(loc+shape[i])) |
|
117 |
return false; |
|
118 |
} |
|
119 |
return true; |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Updates object speed and position. This function organises force |
|
124 |
* integration and collision detection. |
|
125 |
*/ |
|
205 | 126 |
void PhysicsObject::updatePosition (TimeMS dt) { |
197 | 127 |
// Add gravity to the force queue |
273 | 128 |
applyForce(world.gravity); |
197 | 129 |
|
228 | 130 |
// If the object (practically player) has a pivot point add |
131 |
// a force towards that |
|
132 |
if (pivot != NULL) { |
|
133 |
Vector direction(pivot->getPosition()-position); |
|
134 |
float magnitude = pivot->getPivotForce(this); |
|
135 |
float length = direction.length(); |
|
136 |
if (length > 0) { |
|
137 |
direction = direction / length * magnitude; |
|
273 | 138 |
applyForce(direction); |
228 | 139 |
} |
140 |
} |
|
273 | 141 |
|
142 |
std::pair<Force, TimeMS> force; |
|
143 |
std::queue<std::pair<Force, TimeMS> > newfq; |
|
197 | 144 |
Force total; |
145 |
while (!forceq.empty()) { |
|
273 | 146 |
force = forceq.front(); |
275
fa44b905bc2e
Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents:
273
diff
changeset
|
147 |
if (force.second > dt) { |
fa44b905bc2e
Tried to take input tick into account in updatePosition but it still doesn't seem to work
saiam
parents:
273
diff
changeset
|
148 |
force.second -= dt; |
273 | 149 |
newfq.push(force); |
150 |
} |
|
151 |
total += force.first; |
|
197 | 152 |
forceq.pop(); |
153 |
} |
|
273 | 154 |
forceq = newfq; |
197 | 155 |
|
156 |
// If the player has stopped and there's some ground under some of the 3 some of the 3t |
|
157 |
// set inAir false |
|
158 |
if (this->velocity == Vector(0,0)) { |
|
159 |
this->inAir = !world.collides(this->position+shape[1]+Vector(0, 1)) |
|
160 |
&& !world.collides(this->position+shape[2]+Vector(0, 1)) |
|
161 |
&& !world.collides(this->position+shape[3]+Vector(0, 1)); |
|
162 |
// If, however, there's a force caused by a bomb, e.g., set it in air. |
|
163 |
// Still, we have to be able to separate forces caused by walking attempts |
|
164 |
// and bombs etc (+0.1 because float comparison can be dangerous) |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
165 |
if (total.y < 0.01 || fabs(total.x) > PLAYER_MOVE_FORCE + 0.1) |
197 | 166 |
this->inAir = true; |
167 |
} |
|
168 |
||
169 |
if(!possibleLocation(position)) { |
|
170 |
//if we are trapped in ground form dirtball or something |
|
171 |
//we might want to just return and set velocity to some value |
|
172 |
//return; |
|
173 |
} |
|
174 |
||
175 |
// If the worm is not in the air make it walk, |
|
176 |
// otherwise integrate the new position and velocity |
|
177 |
if (!this->inAir) { |
|
178 |
// It walks only if there's some vertical force |
|
179 |
if (total.x != 0) { |
|
205 | 180 |
walk(dt, total.x > 0); |
197 | 181 |
this->velocity = Vector(0,0); |
247 | 182 |
} |
183 |
// Now the possible walking has been done so we can return from this function. |
|
184 |
// In walk inAir could have been set true, but that will be handled in the next tick. |
|
185 |
return; |
|
197 | 186 |
} |
187 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
188 |
if (!possibleLocation(position)) |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
189 |
Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
190 |
|
197 | 191 |
Vector newPosition; |
192 |
Vector velAfterTick; |
|
193 |
// Calculate new position and velocity to the given references |
|
205 | 194 |
integrate(total, dt, newPosition, velAfterTick); |
197 | 195 |
this->velocity = velAfterTick; |
196 |
||
197 |
// Collision detection |
|
198 |
bool collided = false; |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
247
diff
changeset
|
199 |
Vector collisionPoint; |
197 | 200 |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
201 |
const Vector diffVec = newPosition - position; |
197 | 202 |
const Vector unitVector = diffVec / diffVec.length(); |
203 |
Vector reached = position; |
|
247 | 204 |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
205 |
while ((position - reached).sqrLength() < diffVec.sqrLength()) { |
197 | 206 |
reached += unitVector; |
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
207 |
|
197 | 208 |
// Check if any of the shapes points collide |
209 |
for (uint64_t i = 0; i < shape.size(); i++) { |
|
210 |
if (world.collides(reached+shape[i])) { // Collision |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
247
diff
changeset
|
211 |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
247
diff
changeset
|
212 |
collisionPoint = reached+shape[i]; |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
247
diff
changeset
|
213 |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
214 |
if (inAir) |
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
215 |
this->bounce(world.getNormal(reached+shape[i], reached-unitVector+shape[i])); |
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
216 |
|
197 | 217 |
reached = reached - unitVector; // Return to last point |
218 |
collided = true; |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
219 |
|
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
220 |
if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED) |
197 | 221 |
this->velocity = Vector(0,0); |
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
222 |
|
197 | 223 |
break; |
224 |
} |
|
225 |
} |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
226 |
|
197 | 227 |
if (collided) |
228 |
break; |
|
229 |
} |
|
230 |
||
231 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
232 |
if (!possibleLocation(reached)) |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
233 |
Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position << ", diffVec=" << diffVec; |
197 | 234 |
|
235 |
// In case of some float error check the final coordinate |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
236 |
if (!collided) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
237 |
if (!possibleLocation(newPosition)) { |
197 | 238 |
newPosition = reached; |
239 |
} else { |
|
240 |
// This means everything was ok, so no need to do anything |
|
241 |
} |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
242 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
243 |
this->position = newPosition; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
244 |
|
197 | 245 |
} else { |
246 |
newPosition = reached; |
|
211
d5d52fb191e4
some minor fixes, shots now explode in collisionpoint
nireco
parents:
210
diff
changeset
|
247 |
this->position = newPosition; |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
248 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
249 |
// the following may delete this object, so it must be the last thing called |
272 | 250 |
onCollision(collisionPoint, NULL); |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
251 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
252 |
return; |
197 | 253 |
} |
254 |
} |
|
255 |
||
256 |
/** |
|
257 |
* Bounces from straight wall in any direction. |
|
258 |
* Direction given as normal of that wall |
|
259 |
*/ |
|
260 |
void PhysicsObject::bounce (Vector normal) { |
|
261 |
// normal.sqrLength can't be 0 when got from getNormal() |
|
262 |
if (normal.sqrLength() != 0) { |
|
263 |
Vector nvel = velocity; |
|
264 |
// We project the velocity on normal and remove twice that much from velocity |
|
265 |
nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal); |
|
266 |
velocity = nvel; |
|
267 |
// We lose some of our speed on collision |
|
268 |
this->velocity *= this->collision_elasticity; |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* Integrates given force over time and stores new position to |
|
274 |
* posAfterTick and new velocity to velAfterTick. |
|
275 |
* @param force Force vector. |
|
276 |
* @param dt The time the force is applied (<=PHYSICS_TICK_MS) |
|
277 |
*/ |
|
278 |
void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) { |
|
279 |
posAfterTick = position; |
|
280 |
velAfterTick = velocity; |
|
281 |
Derivative tmpd; |
|
282 |
Derivative k1 = evaluate(force, 0, tmpd, posAfterTick, velAfterTick); |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
283 |
Derivative k2 = evaluate(force, dt / 2, k1, posAfterTick, velAfterTick); |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
284 |
Derivative k3 = evaluate(force, dt / 2, k2, posAfterTick, velAfterTick); |
197 | 285 |
Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick); |
286 |
||
287 |
||
288 |
const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f; |
|
289 |
const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f; |
|
290 |
||
291 |
// Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt; |
|
292 |
posAfterTick = posAfterTick + (dxdt * dt)/1000; |
|
293 |
velAfterTick = velAfterTick + (dvdt * dt)/1000; |
|
294 |
//Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick; |
|
295 |
} |
|
296 |
||
297 |
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) { |
|
298 |
Vector curPos = posAfterTick + (d.dx*dt)/1000; |
|
299 |
Vector curVel = velAfterTick + (d.dv*dt)/1000; |
|
300 |
||
301 |
Derivative out; |
|
302 |
out.dx = curVel; |
|
303 |
out.dv = acceleration(force); |
|
304 |
//Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx; |
|
305 |
return out; |
|
306 |
} |
|
307 |
||
308 |
Vector PhysicsObject::acceleration(const Force &force) { |
|
309 |
return (force/mass); |
|
310 |
} |
|
311 |
||
273 | 312 |
void PhysicsObject::applyForce (Force force, TimeMS dt) { |
197 | 313 |
// Add applied force to the queue |
273 | 314 |
forceq.push(std::make_pair(force, dt)); |
197 | 315 |
} |
316 |
||
317 |
void PhysicsObject::changeAim(float da) { |
|
318 |
this->aim += da; |
|
319 |
||
320 |
if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX; |
|
321 |
if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN; |
|
322 |
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim; |
|
323 |
} |
|
324 |
||
264 | 325 |
void PhysicsObject::setFacing (FacingDirection facing) { |
326 |
this->facing = facing; |
|
197 | 327 |
} |
328 |
||
264 | 329 |
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir, FacingDirection facing, float aim) { |
197 | 330 |
this->position = position; |
331 |
this->velocity = velocity; |
|
332 |
this->inAir = inAir; |
|
264 | 333 |
this->facing = facing; |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
334 |
this->aim = aim; |
197 | 335 |
} |
225 | 336 |
|
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
337 |
Vector PhysicsObject::getPosition (void) const { |
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
338 |
return position; |
197 | 339 |
} |
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
340 |
|
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
341 |
PixelCoordinate PhysicsObject::getCoordinate (void) const { |
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
342 |
return world.getPixelCoordinate(position); |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
343 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
344 |
|
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
345 |
Vector PhysicsObject::getVelocity (void) const { |
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
346 |
return velocity; |
197 | 347 |
} |
348 |
||
264 | 349 |
FacingDirection PhysicsObject::getFacing (void) const { |
350 |
return facing; |
|
197 | 351 |
} |
352 |
||
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
353 |
float PhysicsObject::getAim (void) const { |
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
354 |
return aim; |
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
355 |
} |
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
356 |
|
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
357 |
Vector PhysicsObject::getDirection (void) const { |
264 | 358 |
return facing == FACING_RIGHT ? Vector(cos(aim), -sin(aim)) : Vector(-cos(aim), -sin(aim)); |
235 | 359 |
} |
360 |
||
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
361 |
const std::vector<Vector>& PhysicsObject::getShape () const { |
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
252
diff
changeset
|
362 |
return shape; |
197 | 363 |
} |
364 |
||
365 |
void PhysicsObject::setShape (std::vector<Vector> shape) { |
|
366 |
this->shape = shape; |
|
367 |
} |
|
368 |
||
228 | 369 |
void PhysicsObject::setPivot (PhysicsObject *pivot) { |
370 |
this->pivot = pivot; |
|
371 |
} |
|
372 |
||
205 | 373 |
void PhysicsObject::tick (TimeMS tick_length) { |
374 |
this->updatePosition(tick_length); |
|
197 | 375 |
} |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
376 |
|
225 | 377 |
void PhysicsObject::enable (void) { |
241 | 378 |
// only enable once until disabled |
379 |
if (alive) |
|
380 |
return; |
|
381 |
||
382 |
// mark as alive |
|
225 | 383 |
alive = true; |
241 | 384 |
|
385 |
// add the world objects list |
|
225 | 386 |
world.addPhysicsObject(this); |
387 |
} |
|
388 |
||
389 |
void PhysicsObject::disable (void) { |
|
241 | 390 |
// mark as disabled |
225 | 391 |
alive = false; |
392 |
} |
|
393 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
394 |
void PhysicsObject::destroy (void) { |
241 | 395 |
// mark as disabled and for deletion |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
396 |
alive = false; |
225 | 397 |
shouldDelete = true; |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
398 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
399 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
400 |
bool PhysicsObject::isDestroyed (void) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
401 |
return !alive; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
402 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
403 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
404 |
bool PhysicsObject::removeIfDestroyed (void) { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
405 |
if (!alive) { |
225 | 406 |
if (shouldDelete) |
407 |
delete this; |
|
408 |
||
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
409 |
return true; |
225 | 410 |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
411 |
} else { |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
412 |
return false; |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
413 |
} |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
414 |
} |
197 | 415 |
|
264 | 416 |
float PhysicsObject::getPivotForce (PhysicsObject *bob) { |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
417 |
(void) bob; |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
418 |
|
264 | 419 |
return 0.0; |
197 | 420 |
} |
421 |
||
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
422 |
bool PhysicsObject::collides (const PhysicsObject &obj) { |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
423 |
const std::vector<Vector> oShape = obj.getShape(); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
424 |
Vector p1, p2, p3; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
425 |
int8_t sign, nsign; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
426 |
for (std::vector<Vector>::const_iterator i = oShape.begin(); i != oShape.end(); i++) { // For every point in other shape |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
427 |
p3 = *i + obj.getPosition(); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
428 |
sign = 0; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
429 |
for (std::vector<Vector>::const_iterator j = shape.begin(); j != shape.end(); j++) { |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
430 |
p1 = *j + position; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
431 |
if ( (j+1) == shape.end() ) p2 = *shape.begin() + position; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
432 |
else p2 = *(j+1) + position; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
433 |
nsign = crossProduct(p1, p2, p3); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
434 |
if ( sign == 0 ) sign = nsign; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
435 |
else if ( ((sign < 0) && (nsign < 0)) || ((sign > 0) && (nsign > 0)) ) continue; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
436 |
else return false; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
437 |
} |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
438 |
} |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
439 |
return true; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
440 |
} |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
441 |
|
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
442 |
void PhysicsObject::onCollision (Vector collisionPoint, PhysicsObject *other) { |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
443 |
(void) collisionPoint; |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
444 |
(void) other; |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
445 |
} |
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
446 |
|
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
447 |
int8_t crossProduct (const Vector &p1, const Vector &p2, const Vector &p3) { |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
448 |
float p = (p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
449 |
if (p < 0) |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
450 |
return -1; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
451 |
else |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
452 |
return 1; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
453 |
} |