author | nireco |
Mon, 01 Dec 2008 23:51:18 +0000 | |
changeset 171 | 05f1c957e776 |
parent 170 | fe74105c07ea |
child 172 | 51b470384ad9 |
permissions | -rw-r--r-- |
58 | 1 |
|
2 |
#include "Physics.hh" |
|
3 |
#include "Engine.hh" |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
4 |
#include "GameState.hh" |
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
5 |
#include "Terrain.hh" |
58 | 6 |
|
45
32c876923cac
I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents:
44
diff
changeset
|
7 |
#include <algorithm> |
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
8 |
#include <functional> |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
9 |
#include <cmath> |
104 | 10 |
#include <assert.h> |
45
32c876923cac
I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents:
44
diff
changeset
|
11 |
|
60 | 12 |
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions) |
161
ea2f295c279f
Made PhysicsWorld to inherit Terrain instead of having it as an attribute.
saiam
parents:
160
diff
changeset
|
13 |
: Terrain(1337), tick_timer(PHYSICS_TICK_MS), tick_counter(0), dimensions(dimensions), |
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
14 |
gravity(gravity) { |
54 | 15 |
slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick); |
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
16 |
tick_timer.enable(); |
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
17 |
} |
44 | 18 |
|
19 |
void PhysicsWorld::addObject (PhysicsObject *object) { |
|
47 | 20 |
objects.push_back(object); |
44 | 21 |
} |
22 |
||
23 |
void PhysicsWorld::tick () { |
|
123 | 24 |
// Engine::log(DEBUG, "physics.apply_force") << "*tick*"; |
58 | 25 |
|
76 | 26 |
for (std::vector<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) { |
27 |
(*i)->tick(); |
|
28 |
} |
|
105 | 29 |
|
30 |
tick_counter++; |
|
31 |
} |
|
32 |
||
33 |
uint32_t PhysicsWorld::getTick (void) { |
|
34 |
return tick_counter; |
|
48
fa1da22db8a0
It looks like physics could now work, but I doubt it...
saiam
parents:
47
diff
changeset
|
35 |
} |
fa1da22db8a0
It looks like physics could now work, but I doubt it...
saiam
parents:
47
diff
changeset
|
36 |
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
37 |
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
38 |
Vector position, Vector velocity) |
135
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
39 |
: world(world), position(position), velocity(velocity), |
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
40 |
mass(mass), inAir(true), aim(0), facingRight(true) { |
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
41 |
// TODO: Is thir the right way to do this? |
60 | 42 |
world.addObject(this); |
43 |
} |
|
44 | 44 |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
45 |
Vector PhysicsObject::walk (bool right) { |
168 | 46 |
float deltaX = right ? 1 : -1; |
112 | 47 |
Vector reached = this->position; |
168 | 48 |
|
49 |
if(!possibleLocation(position+Vector(deltaX, 0))) { |
|
50 |
for(int i = 1; i < 3; i++) { |
|
51 |
if(possibleLocation(position+Vector(deltaX, -i))) { |
|
52 |
reached = position+Vector(deltaX, -i); |
|
53 |
break; |
|
112 | 54 |
} |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
55 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
56 |
} else { |
170 | 57 |
for(int i = 0; i < 3; i++) { |
168 | 58 |
if(possibleLocation(position+Vector(deltaX, i))) { |
59 |
reached = position+Vector(deltaX, i); |
|
60 |
} else { |
|
61 |
break; |
|
112 | 62 |
} |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
63 |
} |
168 | 64 |
} |
112 | 65 |
return reached; |
168 | 66 |
|
94 | 67 |
} |
68 |
||
170 | 69 |
/** |
70 |
* Makes the player jump in the air. |
|
71 |
* @param direction -1: jump left, 0: jump up, 1: jump right |
|
72 |
*/ |
|
73 |
void PhysicsObject::jump (int direction) { |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
74 |
// Jump only if player is "on the ground" |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
75 |
if (!this->inAir) { |
159 | 76 |
velocity.y = -100; |
170 | 77 |
switch (direction) { |
78 |
case 1: |
|
79 |
velocity.x += 20; |
|
80 |
break; |
|
81 |
case -1: |
|
82 |
velocity.x -= 20; |
|
83 |
break; |
|
84 |
case 0: |
|
85 |
break; |
|
86 |
default: |
|
87 |
throw std::logic_error("Invalid jump direction"); |
|
88 |
} |
|
159 | 89 |
inAir = true; |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
90 |
} |
94 | 91 |
} |
92 |
||
93 |
bool PhysicsObject::possibleLocation (Vector loc) { |
|
112 | 94 |
for(unsigned int i = 0; i < this->shape.size(); i++) { |
95 |
if(world.getType(loc+shape[i]) != EMPTY) |
|
96 |
return false; |
|
97 |
} |
|
98 |
return true; |
|
94 | 99 |
} |
100 |
||
101 |
/** |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
102 |
* Updates object speed and position. This function organises force |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
103 |
* integration and collision detection. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
104 |
*/ |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
105 |
void PhysicsObject::updatePosition () { |
94 | 106 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
107 |
// Add gravity to the force queue |
85 | 108 |
forceq.push(world.gravity); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
109 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
110 |
// Go trough every force in the queue |
84
3cb862028a24
No nyt se toimii v?h?n paremmin. Koodia pit?? kyll? viel? siisti? aika huolella.
saiam
parents:
83
diff
changeset
|
111 |
Force total; |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
112 |
while (!forceq.empty()) { |
85 | 113 |
total += forceq.front(); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
114 |
forceq.pop(); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
115 |
} |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
116 |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
117 |
// TODO: This is _ugly_ (but not so ugly as the last one I think) |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
118 |
// hack. I think we should handle walking from the collision |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
119 |
// detection code. |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
120 |
|
167 | 121 |
if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED) { |
122 |
this->inAir = !world.collides(this->position+shape[2]+Vector(0, 1)); |
|
123 |
} |
|
124 |
||
171 | 125 |
if(!possibleLocation(position)) { |
126 |
//if we are trapped in ground form dirtball or something |
|
127 |
//we might want to just return and stuff velocity to some value |
|
128 |
// return; |
|
129 |
} |
|
130 |
||
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
131 |
if (!this->inAir) { |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
132 |
if (total.x != 0) |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
133 |
this->position = walk(total.x > 0); |
154
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
134 |
return; |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
135 |
//total.x = 0; |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
136 |
} |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
137 |
|
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
138 |
Vector newPosition; |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
139 |
Vector velAfterTick; |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
140 |
integrate(total, PHYSICS_TICK_MS, newPosition, velAfterTick); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
141 |
this->velocity = velAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
142 |
|
123 | 143 |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
144 |
// Collision detection |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
145 |
bool collided = false; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
146 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
147 |
const Vector diffVec = newPosition-position; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
148 |
const Vector unitVector = diffVec / diffVec.length(); |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
149 |
Vector reached = position; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
150 |
|
168 | 151 |
while ((position-reached).sqrLength() < diffVec.sqrLength()) { |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
152 |
// Check if any of the shapes points collide |
154
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
153 |
for (uint64_t i = 0; i < shape.size(); i++) { |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
154 |
if (world.getType(reached+shape[i]) != EMPTY) { // Collision |
171 | 155 |
if(i == 0) { |
156 |
Engine::log(DEBUG, "PhysicsObject.updatePosition ") << "i==0 and collision. It should be impossible -> logic error, someone threw us inside gound."; |
|
157 |
} |
|
154
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
158 |
if (inAir) { |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
159 |
// Engine::log(DEBUG, "Here"); |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
160 |
this->bounce(world.getNormal(reached+shape[i], |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
161 |
reached-unitVector+shape[i])); |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
162 |
//this->velocity *= COLLISION_ELASTICITY; |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
163 |
} |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
164 |
reached = reached - unitVector; // Return to last point |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
165 |
collided = true; |
167 | 166 |
if (this->velocity.sqrLength() < PLAYER_MIN_SPEED * PLAYER_MIN_SPEED) { |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
167 |
this->inAir = false; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
168 |
this->velocity = Vector(0,0); |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
169 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
170 |
break; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
171 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
172 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
173 |
if (collided) |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
174 |
break; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
175 |
reached += unitVector; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
176 |
} |
123 | 177 |
|
95 | 178 |
|
123 | 179 |
|
92 | 180 |
// In case of some float error check the final coordinate |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
181 |
if(!collided) { |
171 | 182 |
if(!possibleLocation(newPosition)) { |
183 |
newPosition = reached; |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
184 |
} else { |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
185 |
// This means everything was ok, so no need to do anything |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
186 |
} |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
187 |
} else { |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
188 |
newPosition = reached; |
116 | 189 |
onCollision(); |
92 | 190 |
//this->velocity = Vector(0, 0); |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
191 |
//TODO: it shouldn't just stop on collision |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
192 |
} |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
193 |
this->position = newPosition; |
158 | 194 |
// Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Pos: " << this->position; |
44 | 195 |
} |
97 | 196 |
|
197 |
/** |
|
198 |
* Bounces from straight wall in any direction. |
|
199 |
* Direction given as normal of that wall |
|
200 |
*/ |
|
201 |
void PhysicsObject::bounce (Vector normal) { |
|
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
202 |
if (normal.length() != 0) { |
154
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
203 |
// Engine::log(DEBUG, "PhysicsObject.bounce") << "Velocity: " << velocity; |
153
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
204 |
Vector nvel = velocity; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
205 |
// Engine::log(DEBUG, "PhysicsObject.bounce") << "New Velocity: " << nvel; |
160 | 206 |
// nvel = nvel - ((1+COLLISION_ELASTICITY)*((nvel*normal)/(normal*normal))*normal); |
207 |
||
208 |
nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal); |
|
209 |
// Engine::log(DEBUG, "PhysicsObject.bounce") << "Projection: " << nvel; |
|
153
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
210 |
velocity = nvel; |
154
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
211 |
this->velocity *= COLLISION_ELASTICITY; |
78d144a48354
This version has a nasty bug in collision detection. We'll get rid of it by making the collision detection to return te point
saiam
parents:
153
diff
changeset
|
212 |
} |
97 | 213 |
} |
44 | 214 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
215 |
/** |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
216 |
* Integrates given force over time and stores new position to |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
217 |
* posAfterTick and new velocity to velAfterTick. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
218 |
* @param force Force vector. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
219 |
* @param dt The time the force is applied (<=PHYSICS_TICK_MS) |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
220 |
*/ |
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
221 |
void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) { |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
222 |
posAfterTick = position; |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
223 |
velAfterTick = velocity; |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
224 |
Derivative tmpd; |
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
225 |
Derivative k1 = evaluate(force, 0, tmpd, posAfterTick, velAfterTick); |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
226 |
Derivative k2 = evaluate(force, 0.5f*dt, k1, posAfterTick, velAfterTick); |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
227 |
Derivative k3 = evaluate(force, 0.5f*dt, k2, posAfterTick, velAfterTick); |
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
228 |
Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
229 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
230 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
231 |
const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
232 |
const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
233 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
234 |
// Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
235 |
posAfterTick = posAfterTick + (dxdt * dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
236 |
velAfterTick = velAfterTick + (dvdt * dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
237 |
//Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick; |
70 | 238 |
} |
239 |
||
169
71c5b12a2b3f
Removed unnecessary PhysicsObject attribute posAfterTick and velAfterTick and made integrate to use references.
saiam
parents:
168
diff
changeset
|
240 |
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
241 |
Vector curPos = posAfterTick + (d.dx*dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
242 |
Vector curVel = velAfterTick + (d.dv*dt)/1000; |
58 | 243 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
244 |
Derivative out; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
245 |
out.dx = curVel; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
246 |
out.dv = acceleration(force); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
247 |
//Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
248 |
return out; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
249 |
} |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
250 |
|
85 | 251 |
Vector PhysicsObject::acceleration(const Force &force) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
252 |
return (force/mass); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
253 |
} |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
254 |
|
122
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
255 |
void PhysicsObject::applyForce (Force force) { |
123 | 256 |
// Add applied force to the queue |
257 |
forceq.push(force); |
|
44 | 258 |
} |
259 |
||
108 | 260 |
void PhysicsObject::changeAim(float da) { |
261 |
this->aim += da; |
|
262 |
||
263 |
if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX; |
|
264 |
if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN; |
|
109 | 265 |
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim; |
108 | 266 |
} |
267 |
||
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
268 |
void PhysicsObject::setFacing(bool facingRight) { |
109 | 269 |
//Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right; |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
270 |
this->facingRight = facingRight; |
108 | 271 |
} |
272 |
||
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
273 |
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir) { |
47 | 274 |
this->position = position; |
275 |
this->velocity = velocity; |
|
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
276 |
this->inAir = inAir; |
44 | 277 |
} |
278 |
||
279 |
Vector PhysicsObject::getPosition () { |
|
47 | 280 |
return this->position; |
44 | 281 |
} |
282 |
||
108 | 283 |
bool PhysicsObject::getFacing() { |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
284 |
return this->facingRight; |
108 | 285 |
} |
286 |
||
287 |
float PhysicsObject::getAim() { |
|
288 |
return this->aim; |
|
289 |
} |
|
290 |
||
91 | 291 |
std::vector<Vector>& PhysicsObject::getShape () { |
292 |
return this->shape; |
|
293 |
} |
|
294 |
||
295 |
void PhysicsObject::setShape (std::vector<Vector> shape) { |
|
123 | 296 |
this->shape = shape; |
91 | 297 |
} |
298 |
||
44 | 299 |
void PhysicsObject::tick () { |
47 | 300 |
this->updatePosition(); |
44 | 301 |
} |
302 |
||
153
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
303 |
void PhysicsObject::draw(CL_GraphicContext *gc) { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
304 |
CL_Quad player( |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
305 |
(position+shape[0]).x, (position+shape[0]).y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
306 |
(position+shape[1]).x, (position+shape[1]).y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
307 |
(position+shape[2]).x, (position+shape[2]).y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
308 |
(position+shape[3]).x, (position+shape[3]).y |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
309 |
); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
310 |
|
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
311 |
gc->fill_quad(player, CL_Color::green); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
312 |
|
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
313 |
const uint16_t chlen = 10; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
314 |
uint16_t x = player.center().x; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
315 |
uint16_t y = player.center().y; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
316 |
if (facingRight) { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
317 |
gc->draw_line(x, y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
318 |
x + std::cos(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
319 |
y - std::sin(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
320 |
CL_Color::black); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
321 |
} else { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
322 |
gc->draw_line(x, y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
323 |
x - std::cos(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
324 |
y - std::sin(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
325 |
CL_Color::black); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
326 |
} |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
327 |
} |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
328 |