author | ekku |
Mon, 08 Dec 2008 22:37:27 +0000 | |
changeset 322 | f94a5c192097 |
parent 299 | e4dacf550ba1 |
child 336 | ef598a3dc1b7 |
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), |
285 | 12 |
previousPosition(position), |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
13 |
velocity(velocity), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
14 |
mass(mass), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
15 |
inAir(true), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
16 |
collision_elasticity(collision_elasticity), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
17 |
aim(0), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
18 |
facing(FACING_RIGHT), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
19 |
alive(false), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
20 |
shouldDelete(false), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
21 |
type(type), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
22 |
pivot(NULL) |
225 | 23 |
{ |
24 |
if (enabled) |
|
25 |
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
|
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 |
|
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
|
28 |
PhysicsObject::~PhysicsObject (void) { |
225 | 29 |
// Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */; |
197 | 30 |
} |
31 |
||
32 |
/** |
|
33 |
* Player walks on floor. |
|
34 |
*/ |
|
35 |
Vector PhysicsObject::walk_one_step (float partial, bool right) { |
|
36 |
// which way we are walking |
|
37 |
float deltaX = right ? partial : -partial; |
|
38 |
Vector reached = this->position; |
|
39 |
if(reached.roundToInt() == (reached+Vector(deltaX, 0)).roundToInt()) { |
|
40 |
return reached+Vector(deltaX, 0); |
|
41 |
} |
|
42 |
// Is there upward ramp |
|
43 |
if(!possibleLocation(position+Vector(deltaX, 0))) { |
|
44 |
// Yes. Then we check n pixels up |
|
45 |
for(int i = 1; i < 3; i++) { |
|
46 |
if(possibleLocation(position+Vector(deltaX, -i))) { |
|
47 |
// and when there is finally EMPTY, we can walk |
|
48 |
reached = position+Vector(deltaX, -i); |
|
49 |
break; |
|
50 |
} |
|
51 |
} |
|
52 |
} else { |
|
53 |
// Or downward ramp or flat |
|
54 |
for(int i = 0; 1; i++) { |
|
55 |
||
56 |
// And when there is finally ground we can step on |
|
57 |
// it. If there is no gound we still step there, |
|
58 |
// but will fall one pixel down |
|
59 |
if(possibleLocation(position+Vector(deltaX, i))) { |
|
60 |
reached = position+Vector(deltaX, i); |
|
61 |
} else { |
|
62 |
break; |
|
63 |
} |
|
64 |
||
65 |
// If the fall is big enough, set the worm in the air |
|
66 |
if (i >= 2) { |
|
67 |
// Vector back = walk(dt, !right); |
|
68 |
this->inAir = true; |
|
69 |
// this->velocity.x = right ? velocity : -velocity; |
|
70 |
// Avoid stepping two pixels down when it starts to free fall |
|
71 |
reached.y -= 2; |
|
72 |
// this->velocity = (reached-back)*1000/dt; |
|
73 |
break; |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
// And we return where we got |
|
78 |
return reached; |
|
79 |
} |
|
80 |
void PhysicsObject::walk (TimeMS dt, bool right) { |
|
81 |
float velocity = PLAYER_WALK_SPEED; |
|
82 |
float walkAmount = (velocity*dt)/1000; |
|
285 | 83 |
while (walkAmount > 0){// && !this->inAir) { |
84 |
setPosition (walk_one_step((1 < walkAmount ? 1 : walkAmount), right)); |
|
197 | 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) { |
|
322 | 133 |
applyForce(getPivotForce()); |
228 | 134 |
} |
273 | 135 |
|
136 |
std::pair<Force, TimeMS> force; |
|
137 |
std::queue<std::pair<Force, TimeMS> > newfq; |
|
197 | 138 |
Force total; |
139 |
while (!forceq.empty()) { |
|
273 | 140 |
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
|
141 |
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
|
142 |
force.second -= dt; |
273 | 143 |
newfq.push(force); |
144 |
} |
|
145 |
total += force.first; |
|
197 | 146 |
forceq.pop(); |
147 |
} |
|
273 | 148 |
forceq = newfq; |
197 | 149 |
|
150 |
// If the player has stopped and there's some ground under some of the 3 some of the 3t |
|
151 |
// set inAir false |
|
152 |
if (this->velocity == Vector(0,0)) { |
|
153 |
this->inAir = !world.collides(this->position+shape[1]+Vector(0, 1)) |
|
154 |
&& !world.collides(this->position+shape[2]+Vector(0, 1)) |
|
155 |
&& !world.collides(this->position+shape[3]+Vector(0, 1)); |
|
156 |
// If, however, there's a force caused by a bomb, e.g., set it in air. |
|
157 |
// Still, we have to be able to separate forces caused by walking attempts |
|
158 |
// 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
|
159 |
if (total.y < 0.01 || fabs(total.x) > PLAYER_MOVE_FORCE + 0.1) |
197 | 160 |
this->inAir = true; |
161 |
} |
|
162 |
||
163 |
if(!possibleLocation(position)) { |
|
164 |
//if we are trapped in ground form dirtball or something |
|
165 |
//we might want to just return and set velocity to some value |
|
166 |
//return; |
|
167 |
} |
|
168 |
||
169 |
// If the worm is not in the air make it walk, |
|
170 |
// otherwise integrate the new position and velocity |
|
171 |
if (!this->inAir) { |
|
172 |
// It walks only if there's some vertical force |
|
173 |
if (total.x != 0) { |
|
205 | 174 |
walk(dt, total.x > 0); |
197 | 175 |
this->velocity = Vector(0,0); |
247 | 176 |
} |
177 |
// Now the possible walking has been done so we can return from this function. |
|
178 |
// In walk inAir could have been set true, but that will be handled in the next tick. |
|
179 |
return; |
|
197 | 180 |
} |
181 |
||
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
|
182 |
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
|
183 |
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
|
184 |
|
197 | 185 |
Vector newPosition; |
186 |
Vector velAfterTick; |
|
187 |
// Calculate new position and velocity to the given references |
|
205 | 188 |
integrate(total, dt, newPosition, velAfterTick); |
197 | 189 |
this->velocity = velAfterTick; |
190 |
||
191 |
// Collision detection |
|
192 |
bool collided = false; |
|
252
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
247
diff
changeset
|
193 |
Vector collisionPoint; |
197 | 194 |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
195 |
const Vector diffVec = newPosition - position; |
197 | 196 |
const Vector unitVector = diffVec / diffVec.length(); |
297 | 197 |
if(unitVector == Vector(0, 0)) { |
198 |
return; |
|
199 |
} |
|
197 | 200 |
Vector reached = position; |
247 | 201 |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
202 |
while ((position - reached).sqrLength() < diffVec.sqrLength()) { |
197 | 203 |
reached += unitVector; |
204 |
// Check if any of the shapes points collide |
|
205 |
for (uint64_t i = 0; i < shape.size(); i++) { |
|
206 |
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
|
207 |
|
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
247
diff
changeset
|
208 |
collisionPoint = reached+shape[i]; |
25054ce94d07
Rope is released if the ground on the pivot point is destroyed.
ekku
parents:
247
diff
changeset
|
209 |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
210 |
if (inAir) |
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
211 |
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
|
212 |
|
197 | 213 |
reached = reached - unitVector; // Return to last point |
214 |
collided = true; |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
215 |
|
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
275
diff
changeset
|
216 |
if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED) |
197 | 217 |
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
|
218 |
|
197 | 219 |
break; |
220 |
} |
|
221 |
} |
|
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 |
if (collided) |
224 |
break; |
|
225 |
} |
|
226 |
||
227 |
||
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
|
228 |
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
|
229 |
Engine::log(DEBUG, "PhysicsObject.updatePosition") << "impossible location: " << position << ", diffVec=" << diffVec; |
197 | 230 |
|
231 |
// 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
|
232 |
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
|
233 |
if (!possibleLocation(newPosition)) { |
197 | 234 |
newPosition = reached; |
235 |
} else { |
|
236 |
// This means everything was ok, so no need to do anything |
|
237 |
} |
|
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
|
238 |
|
285 | 239 |
setPosition (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
|
240 |
|
197 | 241 |
} else { |
242 |
newPosition = reached; |
|
285 | 243 |
setPosition (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
|
244 |
|
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
|
245 |
// the following may delete this object, so it must be the last thing called |
288 | 246 |
onCollision(collisionPoint); |
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
|
247 |
|
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 |
return; |
197 | 249 |
} |
250 |
} |
|
251 |
||
252 |
/** |
|
253 |
* Bounces from straight wall in any direction. |
|
254 |
* Direction given as normal of that wall |
|
255 |
*/ |
|
256 |
void PhysicsObject::bounce (Vector normal) { |
|
257 |
// normal.sqrLength can't be 0 when got from getNormal() |
|
258 |
if (normal.sqrLength() != 0) { |
|
259 |
Vector nvel = velocity; |
|
260 |
// We project the velocity on normal and remove twice that much from velocity |
|
261 |
nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal); |
|
262 |
velocity = nvel; |
|
263 |
// We lose some of our speed on collision |
|
264 |
this->velocity *= this->collision_elasticity; |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Integrates given force over time and stores new position to |
|
270 |
* posAfterTick and new velocity to velAfterTick. |
|
271 |
* @param force Force vector. |
|
272 |
* @param dt The time the force is applied (<=PHYSICS_TICK_MS) |
|
273 |
*/ |
|
274 |
void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) { |
|
275 |
posAfterTick = position; |
|
276 |
velAfterTick = velocity; |
|
277 |
Derivative tmpd; |
|
278 |
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
|
279 |
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
|
280 |
Derivative k3 = evaluate(force, dt / 2, k2, posAfterTick, velAfterTick); |
197 | 281 |
Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick); |
282 |
||
283 |
||
284 |
const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f; |
|
285 |
const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f; |
|
286 |
||
287 |
// Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt; |
|
288 |
posAfterTick = posAfterTick + (dxdt * dt)/1000; |
|
289 |
velAfterTick = velAfterTick + (dvdt * dt)/1000; |
|
290 |
//Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick; |
|
291 |
} |
|
292 |
||
293 |
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) { |
|
294 |
Vector curPos = posAfterTick + (d.dx*dt)/1000; |
|
295 |
Vector curVel = velAfterTick + (d.dv*dt)/1000; |
|
296 |
||
297 |
Derivative out; |
|
298 |
out.dx = curVel; |
|
299 |
out.dv = acceleration(force); |
|
300 |
//Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx; |
|
301 |
return out; |
|
302 |
} |
|
303 |
||
304 |
Vector PhysicsObject::acceleration(const Force &force) { |
|
305 |
return (force/mass); |
|
306 |
} |
|
307 |
||
273 | 308 |
void PhysicsObject::applyForce (Force force, TimeMS dt) { |
197 | 309 |
// Add applied force to the queue |
273 | 310 |
forceq.push(std::make_pair(force, dt)); |
197 | 311 |
} |
312 |
||
313 |
void PhysicsObject::changeAim(float da) { |
|
314 |
this->aim += da; |
|
315 |
||
316 |
if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX; |
|
317 |
if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN; |
|
318 |
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim; |
|
319 |
} |
|
320 |
||
287 | 321 |
ObjectType PhysicsObject::getType (void) const { |
322 |
return this->type; |
|
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) { |
285 | 330 |
setPosition (position); |
197 | 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 |
|
285 | 341 |
Vector PhysicsObject::getPreviousPosition (void) const { |
342 |
return previousPosition; |
|
343 |
} |
|
344 |
||
345 |
void PhysicsObject::setPosition (Vector pos) { |
|
346 |
this->previousPosition = this->position; |
|
347 |
this->position = pos; |
|
348 |
} |
|
349 |
||
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
|
350 |
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
|
351 |
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
|
352 |
} |
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
|
353 |
|
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
|
354 |
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
|
355 |
return velocity; |
197 | 356 |
} |
357 |
||
264 | 358 |
FacingDirection PhysicsObject::getFacing (void) const { |
359 |
return facing; |
|
197 | 360 |
} |
361 |
||
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
|
362 |
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
|
363 |
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
|
364 |
} |
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
|
365 |
|
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
|
366 |
Vector PhysicsObject::getDirection (void) const { |
264 | 367 |
return facing == FACING_RIGHT ? Vector(cos(aim), -sin(aim)) : Vector(-cos(aim), -sin(aim)); |
235 | 368 |
} |
369 |
||
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
|
370 |
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
|
371 |
return shape; |
197 | 372 |
} |
373 |
||
374 |
void PhysicsObject::setShape (std::vector<Vector> shape) { |
|
375 |
this->shape = shape; |
|
376 |
} |
|
377 |
||
322 | 378 |
PhysicsObject *PhysicsObject::getPivot (void) { |
379 |
return this->pivot; |
|
380 |
} |
|
381 |
||
228 | 382 |
void PhysicsObject::setPivot (PhysicsObject *pivot) { |
383 |
this->pivot = pivot; |
|
384 |
} |
|
385 |
||
205 | 386 |
void PhysicsObject::tick (TimeMS tick_length) { |
387 |
this->updatePosition(tick_length); |
|
197 | 388 |
} |
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
|
389 |
|
225 | 390 |
void PhysicsObject::enable (void) { |
241 | 391 |
// only enable once until disabled |
392 |
if (alive) |
|
393 |
return; |
|
394 |
||
395 |
// mark as alive |
|
225 | 396 |
alive = true; |
241 | 397 |
|
398 |
// add the world objects list |
|
225 | 399 |
world.addPhysicsObject(this); |
400 |
} |
|
401 |
||
402 |
void PhysicsObject::disable (void) { |
|
241 | 403 |
// mark as disabled |
225 | 404 |
alive = false; |
405 |
} |
|
406 |
||
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
|
407 |
void PhysicsObject::destroy (void) { |
241 | 408 |
// 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
|
409 |
alive = false; |
225 | 410 |
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
|
411 |
} |
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 |
|
299 | 413 |
bool PhysicsObject::isAlive (void) { |
414 |
return alive; |
|
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
|
415 |
} |
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
|
416 |
|
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
|
417 |
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
|
418 |
if (!alive) { |
225 | 419 |
if (shouldDelete) |
420 |
delete this; |
|
421 |
||
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
|
422 |
return true; |
225 | 423 |
|
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
|
424 |
} 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
|
425 |
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
|
426 |
} |
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
|
427 |
} |
197 | 428 |
|
322 | 429 |
Vector PhysicsObject::getPivotForce (void) { |
430 |
return Vector(0,0); |
|
197 | 431 |
} |
432 |
||
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
433 |
bool PhysicsObject::collides (const PhysicsObject &obj) { |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
434 |
const std::vector<Vector> oShape = obj.getShape(); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
435 |
Vector p1, p2, p3; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
436 |
int8_t sign, nsign; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
437 |
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
|
438 |
p3 = *i + obj.getPosition(); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
439 |
sign = 0; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
440 |
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
|
441 |
p1 = *j + position; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
442 |
if ( (j+1) == shape.end() ) p2 = *shape.begin() + position; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
443 |
else p2 = *(j+1) + position; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
444 |
nsign = crossProduct(p1, p2, p3); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
445 |
if ( sign == 0 ) sign = nsign; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
446 |
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
|
447 |
else return false; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
448 |
} |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
449 |
} |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
450 |
return true; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
451 |
} |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
452 |
|
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
453 |
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
|
454 |
(void) collisionPoint; |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
455 |
(void) other; |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
456 |
} |
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
457 |
|
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
458 |
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
|
459 |
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
|
460 |
if (p < 0) |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
461 |
return -1; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
462 |
else |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
463 |
return 1; |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
464 |
} |