author | saiam |
Sun, 30 Nov 2008 17:22:57 +0000 | |
changeset 151 | 9fc900fbfa79 |
parent 150 | 5e032b540af3 |
child 153 | 73402d5b778e |
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) |
135
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
13 |
: 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) { |
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
15 |
Engine::log(DEBUG, "Yeah"); |
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
16 |
terrain = Terrain(1337); |
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
17 |
Engine::log(DEBUG, "Yeah2"); |
54 | 18 |
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
|
19 |
tick_timer.enable(); |
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
49
diff
changeset
|
20 |
} |
44 | 21 |
|
22 |
void PhysicsWorld::addObject (PhysicsObject *object) { |
|
47 | 23 |
objects.push_back(object); |
44 | 24 |
} |
25 |
||
26 |
void PhysicsWorld::tick () { |
|
123 | 27 |
// Engine::log(DEBUG, "physics.apply_force") << "*tick*"; |
58 | 28 |
|
76 | 29 |
for (std::vector<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) { |
30 |
(*i)->tick(); |
|
31 |
} |
|
105 | 32 |
|
33 |
tick_counter++; |
|
34 |
} |
|
35 |
||
36 |
uint32_t PhysicsWorld::getTick (void) { |
|
37 |
return tick_counter; |
|
48
fa1da22db8a0
It looks like physics could now work, but I doubt it...
saiam
parents:
47
diff
changeset
|
38 |
} |
fa1da22db8a0
It looks like physics could now work, but I doubt it...
saiam
parents:
47
diff
changeset
|
39 |
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
40 |
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
41 |
Vector position, Vector velocity) |
135
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
42 |
: world(world), position(position), velocity(velocity), |
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
43 |
mass(mass), inAir(true), aim(0), facingRight(true) { |
d5624d698a78
Physics.hh rewrite. Documenting and suggesting changes.
saiam
parents:
131
diff
changeset
|
44 |
// TODO: Is thir the right way to do this? |
60 | 45 |
world.addObject(this); |
46 |
} |
|
44 | 47 |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
48 |
Vector PhysicsObject::walk (bool right) { |
112 | 49 |
Vector cursor = right ? this->position + Vector(1,0) : this->position + Vector(-1,0); |
50 |
Vector reached = this->position; |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
51 |
|
112 | 52 |
//for(int steps = 0; steps < 3; steps++) { |
53 |
||
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
54 |
// Go up but not if the wall is over two pixels |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
55 |
if(world.getType(cursor) != EMPTY) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
56 |
for(int height = 0, max = 3; height < max+42; height++) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
57 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
58 |
if(height >= max) |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
59 |
return reached; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
60 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
61 |
cursor.y--; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
62 |
if(world.getType(cursor) == EMPTY) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
63 |
// Check that the other parts of the worm don't collide with anything |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
64 |
if(possibleLocation(cursor)) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
65 |
reached = cursor; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
66 |
continue; |
123 | 67 |
} else { |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
68 |
// Can't get any further |
112 | 69 |
return reached; |
70 |
} |
|
71 |
} |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
72 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
73 |
} else { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
74 |
if(possibleLocation(cursor)) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
75 |
reached = cursor; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
76 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
77 |
// Start checking if the lower squares are empty |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
78 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
79 |
for(int depth = 0, max = 3; depth < max+42; depth++) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
80 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
81 |
if(depth >= max) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
82 |
// We can start a free fall now |
123 | 83 |
// |
112 | 84 |
// TODO it should be set inAir if it falls from a cliff |
116 | 85 |
this->inAir = true; |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
86 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
87 |
// Put some speed there to make loke smoother |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
88 |
//this->velocity.y = -5; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
89 |
return reached; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
90 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
91 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
92 |
cursor.y++; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
93 |
if(world.getType(cursor) == EMPTY) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
94 |
// Check that the other parts of the worm don't collide with anything |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
95 |
if(possibleLocation(cursor)) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
96 |
reached = cursor; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
97 |
continue; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
98 |
} else { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
99 |
// Can't get any further |
112 | 100 |
return reached; |
101 |
} |
|
102 |
} |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
103 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
104 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
105 |
|
112 | 106 |
// cursor.x += right ? 1 : -1; |
107 |
//} |
|
108 |
return reached; |
|
94 | 109 |
} |
110 |
||
111 |
void PhysicsObject::jump () { |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
112 |
// Jump only if player is "on the ground" |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
113 |
if (!this->inAir) { |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
114 |
velocity.y = -100; |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
115 |
inAir = true; |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
116 |
} |
94 | 117 |
} |
118 |
||
119 |
bool PhysicsObject::possibleLocation (Vector loc) { |
|
112 | 120 |
for(unsigned int i = 0; i < this->shape.size(); i++) { |
121 |
if(world.getType(loc+shape[i]) != EMPTY) |
|
122 |
return false; |
|
123 |
} |
|
124 |
return true; |
|
94 | 125 |
} |
126 |
||
127 |
/** |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
128 |
* Updates object speed and position. This function organises force |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
129 |
* integration and collision detection. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
130 |
*/ |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
131 |
void PhysicsObject::updatePosition () { |
94 | 132 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
133 |
// Add gravity to the force queue |
85 | 134 |
forceq.push(world.gravity); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
135 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
136 |
// 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
|
137 |
Force total; |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
138 |
posAfterTick = position; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
139 |
velAfterTick = velocity; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
140 |
while (!forceq.empty()) { |
85 | 141 |
total += forceq.front(); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
142 |
forceq.pop(); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
143 |
} |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
144 |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
145 |
// 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
|
146 |
// 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
|
147 |
// detection code. |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
148 |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
149 |
if (!this->inAir) { |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
150 |
if (total.x != 0) |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
151 |
this->position = walk(total.x > 0); |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
152 |
return; // argh |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
153 |
} |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
154 |
|
85 | 155 |
integrate(total, PHYSICS_TICK_MS); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
156 |
|
84
3cb862028a24
No nyt se toimii v?h?n paremmin. Koodia pit?? kyll? viel? siisti? aika huolella.
saiam
parents:
83
diff
changeset
|
157 |
Vector newPosition = posAfterTick /*+ (velAfterTick * PHYSICS_TICK_MS)/1000*/; |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
158 |
this->velocity = velAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
159 |
|
123 | 160 |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
161 |
// Collision detection |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
162 |
bool collided = false; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
163 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
164 |
const Vector diffVec = newPosition-position; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
165 |
const Vector unitVector = diffVec / diffVec.length(); |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
166 |
Vector reached = position; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
167 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
168 |
while ((position-reached).length() < diffVec.length()) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
169 |
// Check if any of the shapes points collide |
123 | 170 |
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
|
171 |
if (world.getType(reached+shape[i]) != EMPTY) { // Collision |
124
2fd698e04779
fixed collision detection again, and put some needed rounding to getNormal
nireco
parents:
123
diff
changeset
|
172 |
this->bounce(world.getNormal(reached+shape[i], reached-unitVector+shape[i])); |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
173 |
reached = reached - unitVector; // Return to last point |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
174 |
collided = true; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
175 |
this->velocity *= COLLISION_ELASTICITY; |
123 | 176 |
if (this->velocity.length() < PLAYER_MIN_SPEED) { |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
177 |
this->inAir = false; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
178 |
this->velocity = Vector(0,0); |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
179 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
180 |
break; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
181 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
182 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
183 |
if (collided) |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
184 |
break; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
185 |
reached += unitVector; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
186 |
} |
123 | 187 |
|
95 | 188 |
|
123 | 189 |
/* |
190 |
bool collided = false; |
|
94 | 191 |
|
123 | 192 |
//goes 1 unit forward every step and check if has hit anything |
193 |
Vector unitVector = (newPosition-position) / (newPosition-position).length(); |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
194 |
|
123 | 195 |
Vector tmpVector = position; |
196 |
Vector reached = position; |
|
197 |
||
198 |
int steps = (int) (newPosition-position).length() + 2; |
|
199 |
||
200 |
//Engine::log(DEBUG, "physics.update_position") << unitVector-newPosition; |
|
201 |
//Vector foo = position+unitVector*steps-newPosition; |
|
202 |
//Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Virhe: "<< foo; |
|
203 |
||
204 |
for(int i = 0; i < steps; i++) { |
|
205 |
tmpVector += unitVector; |
|
206 |
||
207 |
float minVelocity = 10; |
|
208 |
// Check if any of the four corners of the worm collide |
|
209 |
for(int sh = 0; sh < 4; sh++) { |
|
210 |
if(world.getType(tmpVector+shape[sh]) != EMPTY) { |
|
211 |
reached = position + unitVector*(i-1); |
|
212 |
collided = true; |
|
213 |
this->bounce(world.getNormal(tmpVector+shape[sh], tmpVector-unitVector+shape[sh])); |
|
214 |
this->velocity *= 0.4; |
|
215 |
if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) { |
|
216 |
this->inAir = false; |
|
217 |
this->velocity = Vector(0,0); |
|
218 |
} |
|
219 |
break; |
|
220 |
} |
|
221 |
} |
|
222 |
if(collided) |
|
223 |
break; |
|
224 |
} |
|
225 |
*/ |
|
226 |
||
92 | 227 |
// 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
|
228 |
if(!collided) { |
92 | 229 |
if(world.getType(newPosition+shape[0]) != EMPTY || (world.getType(newPosition+shape[1]) != EMPTY) |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
230 |
|| (world.getType(newPosition+shape[2]) != EMPTY) || (world.getType(newPosition+shape[3]) != EMPTY)) { |
92 | 231 |
|
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
232 |
// Engine::log(DEBUG, "physics.update_position") << "didnt hit"; |
112 | 233 |
// There was error, and there is ground |
92 | 234 |
//newPosition = tmpVector; |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
235 |
} else { |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
236 |
// 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
|
237 |
} |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
238 |
} else { |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
239 |
newPosition = reached; |
116 | 240 |
onCollision(); |
92 | 241 |
//this->velocity = Vector(0, 0); |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
242 |
//TODO: it shouldn't just stop on collision |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
243 |
} |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
244 |
this->position = newPosition; |
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
245 |
// Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Pos: " << this->position; |
44 | 246 |
} |
97 | 247 |
|
248 |
/** |
|
249 |
* Gets the index of the given coordinate direction |
|
250 |
* referring to the DIRECTIONS table in Physics.hh |
|
251 |
*/ |
|
95 | 252 |
int getDirectionIndex (Vector dir) { |
110 | 253 |
if(dir.x == 0 && dir.y == -1) { |
254 |
return 0; |
|
255 |
} else if(dir.x == 1 && dir.y == -1) { |
|
256 |
return 1; |
|
257 |
} else if(dir.x == 1 && dir.y == 0) { |
|
258 |
return 2; |
|
259 |
} else if(dir.x == 1 && dir.y == 1) { |
|
260 |
return 3; |
|
261 |
} else if(dir.x == 0 && dir.y == 1) { |
|
262 |
return 4; |
|
263 |
} else if(dir.x == -1 && dir.y == 1) { |
|
264 |
return 5; |
|
265 |
} else if(dir.x == -1 && dir.y == 0) { |
|
266 |
return 6; |
|
267 |
} else if(dir.x == -1 && dir.y == -1) { |
|
268 |
return 7; |
|
269 |
} |
|
270 |
Engine::log(DEBUG, "physics.getDirectionIndex ") << "invalid direction: " << dir; |
|
271 |
return 0; |
|
95 | 272 |
} |
273 |
||
97 | 274 |
/** |
275 |
* Computes hitten wall's normal. Calculated from 3*3 grid |
|
276 |
*/ |
|
95 | 277 |
Vector PhysicsWorld::getNormal (Vector hitPoint, Vector prevPoint) { |
151 | 278 |
return terrain.getNormal(hitPoint, prevPoint); |
97 | 279 |
} |
280 |
||
281 |
/** |
|
282 |
* Bounces from straight wall in any direction. |
|
283 |
* Direction given as normal of that wall |
|
284 |
*/ |
|
109 | 285 |
// TODO Bounce doesnt work kun oikealle tai vasemmalle alaviistoon mennään suoralla (tangentaalinen arvo väärän suuntainen) |
97 | 286 |
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
|
287 |
if (normal.length() != 0) { |
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
288 |
Vector tangent(normal.y, -normal.x); |
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
289 |
Vector tprojection = tangent*(velocity * tangent) / (tangent.length()*tangent.length()); |
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
290 |
Vector nprojection = normal*(velocity * normal) / (normal.length()*normal.length()); |
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
291 |
velocity = tprojection - nprojection; |
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
292 |
} |
97 | 293 |
} |
44 | 294 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
295 |
/** |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
296 |
* Integrates given force over time and stores new position to |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
297 |
* posAfterTick and new velocity to velAfterTick. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
298 |
* @param force Force vector. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
299 |
* @param dt The time the force is applied (<=PHYSICS_TICK_MS) |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
300 |
*/ |
85 | 301 |
void PhysicsObject::integrate(Force force, TimeMS dt) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
302 |
Derivative tmpd; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
303 |
Derivative k1 = evaluate(force, 0, tmpd); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
304 |
Derivative k2 = evaluate(force, 0.5f*dt, k1); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
305 |
Derivative k3 = evaluate(force, 0.5f*dt, k2); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
306 |
Derivative k4 = evaluate(force, dt, k3); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
307 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
308 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
309 |
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
|
310 |
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
|
311 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
312 |
// Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
313 |
posAfterTick = posAfterTick + (dxdt * dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
314 |
velAfterTick = velAfterTick + (dvdt * dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
315 |
//Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick; |
70 | 316 |
} |
317 |
||
85 | 318 |
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
319 |
Vector curPos = posAfterTick + (d.dx*dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
320 |
Vector curVel = velAfterTick + (d.dv*dt)/1000; |
58 | 321 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
322 |
Derivative out; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
323 |
out.dx = curVel; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
324 |
out.dv = acceleration(force); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
325 |
//Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
326 |
return out; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
327 |
} |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
328 |
|
85 | 329 |
Vector PhysicsObject::acceleration(const Force &force) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
330 |
return (force/mass); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
331 |
} |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
332 |
|
122
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
333 |
void PhysicsObject::applyForce (Force force) { |
123 | 334 |
// Add applied force to the queue |
335 |
forceq.push(force); |
|
44 | 336 |
} |
337 |
||
108 | 338 |
void PhysicsObject::changeAim(float da) { |
339 |
this->aim += da; |
|
340 |
||
341 |
if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX; |
|
342 |
if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN; |
|
109 | 343 |
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim; |
108 | 344 |
} |
345 |
||
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
346 |
void PhysicsObject::setFacing(bool facingRight) { |
109 | 347 |
//Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right; |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
348 |
this->facingRight = facingRight; |
108 | 349 |
} |
350 |
||
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
351 |
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir) { |
47 | 352 |
this->position = position; |
353 |
this->velocity = velocity; |
|
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
354 |
this->inAir = inAir; |
44 | 355 |
} |
356 |
||
357 |
Vector PhysicsObject::getPosition () { |
|
47 | 358 |
return this->position; |
44 | 359 |
} |
360 |
||
108 | 361 |
bool PhysicsObject::getFacing() { |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
362 |
return this->facingRight; |
108 | 363 |
} |
364 |
||
365 |
float PhysicsObject::getAim() { |
|
366 |
return this->aim; |
|
367 |
} |
|
368 |
||
91 | 369 |
std::vector<Vector>& PhysicsObject::getShape () { |
370 |
return this->shape; |
|
371 |
} |
|
372 |
||
373 |
void PhysicsObject::setShape (std::vector<Vector> shape) { |
|
123 | 374 |
this->shape = shape; |
91 | 375 |
} |
376 |
||
44 | 377 |
void PhysicsObject::tick () { |
47 | 378 |
this->updatePosition(); |
44 | 379 |
} |
380 |
||
75 | 381 |
/** |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
382 |
* Returns terrainType in given tile. ROCK if tile is out of area |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
383 |
* @param pos - coordinate of tile |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
384 |
*/ |
112 | 385 |
TerrainType PhysicsWorld::getType(int x, int y) const { |
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
386 |
return terrain.getType((int32_t)x,(int32_t)y); |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
387 |
} |
112 | 388 |
TerrainType PhysicsWorld::getType(Vector pos) const { |
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
389 |
return terrain.getType(pos.x, pos.y); |
112 | 390 |
} |
391 |
||
392 |
/** |
|
393 |
* Removes ground from given circle. ROCK is not removed. |
|
394 |
* @param (x, y) or pos - center of circle. |
|
395 |
* @param r - radius of circle |
|
396 |
*/ |
|
397 |
void PhysicsWorld::removeGround(int x, int y, float r) { |
|
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
398 |
terrain.removeGround(Vector(x,y), r); |
112 | 399 |
} |
400 |
void PhysicsWorld::removeGround(Vector pos, float r) { |
|
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
401 |
terrain.removeGround(pos, r); |
112 | 402 |
} |
138
cc326b64ae20
Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents:
135
diff
changeset
|
403 |
|
cc326b64ae20
Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents:
135
diff
changeset
|
404 |
void PhysicsWorld::drawTerrain(CL_GraphicContext *gc) { |
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
405 |
terrain.draw(gc); |
138
cc326b64ae20
Nyt kaivaminen visualisoidaan oikein, mutta fysiikkakoodi on kyll? edelleen ihan kauheata, pit?isi tuo Terrain v??nt??
saiam
parents:
135
diff
changeset
|
406 |
} |