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