author | ekku |
Mon, 01 Dec 2008 22:24:17 +0000 | |
changeset 163 | e3db8a0bb89f |
parent 161 | ea2f295c279f |
child 164 | 1d4772967cd0 |
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) { |
163 | 46 |
Vector cursor = this->position + shape[2] + (right ? Vector(1,0) : Vector(-1,0)); |
112 | 47 |
Vector reached = this->position; |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
48 |
|
112 | 49 |
//for(int steps = 0; steps < 3; steps++) { |
50 |
||
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
51 |
// 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
|
52 |
if(world.getType(cursor) != EMPTY) { |
163 | 53 |
for(int height = 0, max = 2; height < max+42; height++) { |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
54 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
55 |
if(height >= max) |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
56 |
return reached; |
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 |
cursor.y--; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
59 |
if(world.getType(cursor) == EMPTY) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
60 |
// 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
|
61 |
if(possibleLocation(cursor)) { |
163 | 62 |
reached = cursor - shape[2]; |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
63 |
continue; |
123 | 64 |
} else { |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
65 |
// Can't get any further |
112 | 66 |
return reached; |
67 |
} |
|
68 |
} |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
69 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
70 |
} else { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
71 |
if(possibleLocation(cursor)) { |
163 | 72 |
reached = cursor - shape[2]; |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
73 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
74 |
// 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
|
75 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
76 |
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
|
77 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
78 |
if(depth >= max) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
79 |
// We can start a free fall now |
123 | 80 |
// |
112 | 81 |
// TODO it should be set inAir if it falls from a cliff |
116 | 82 |
this->inAir = true; |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
83 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
84 |
// 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
|
85 |
//this->velocity.y = -5; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
86 |
return reached; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
87 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
88 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
89 |
cursor.y++; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
90 |
if(world.getType(cursor) == EMPTY) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
91 |
// 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
|
92 |
if(possibleLocation(cursor)) { |
163 | 93 |
reached = cursor - shape[2]; |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
94 |
continue; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
95 |
} else { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
96 |
// Can't get any further |
112 | 97 |
return reached; |
98 |
} |
|
99 |
} |
|
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
100 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
101 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
102 |
|
112 | 103 |
// cursor.x += right ? 1 : -1; |
104 |
//} |
|
105 |
return reached; |
|
94 | 106 |
} |
107 |
||
108 |
void PhysicsObject::jump () { |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
109 |
// Jump only if player is "on the ground" |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
110 |
if (!this->inAir) { |
159 | 111 |
velocity.y = -100; |
112 |
velocity.x += facingRight ? 20 : -20; |
|
113 |
inAir = true; |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
114 |
} |
94 | 115 |
} |
116 |
||
117 |
bool PhysicsObject::possibleLocation (Vector loc) { |
|
112 | 118 |
for(unsigned int i = 0; i < this->shape.size(); i++) { |
119 |
if(world.getType(loc+shape[i]) != EMPTY) |
|
120 |
return false; |
|
121 |
} |
|
122 |
return true; |
|
94 | 123 |
} |
124 |
||
125 |
/** |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
126 |
* Updates object speed and position. This function organises force |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
127 |
* integration and collision detection. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
128 |
*/ |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
129 |
void PhysicsObject::updatePosition () { |
94 | 130 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
131 |
// Add gravity to the force queue |
85 | 132 |
forceq.push(world.gravity); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
133 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
134 |
// 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
|
135 |
Force total; |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
136 |
posAfterTick = position; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
137 |
velAfterTick = velocity; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
138 |
while (!forceq.empty()) { |
85 | 139 |
total += forceq.front(); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
140 |
forceq.pop(); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
141 |
} |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
142 |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
143 |
// 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
|
144 |
// 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
|
145 |
// detection code. |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
146 |
|
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
147 |
if (!this->inAir) { |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
148 |
if (total.x != 0) |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
149 |
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
|
150 |
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
|
151 |
//total.x = 0; |
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
152 |
} |
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
153 |
|
85 | 154 |
integrate(total, PHYSICS_TICK_MS); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
155 |
|
84
3cb862028a24
No nyt se toimii v?h?n paremmin. Koodia pit?? kyll? viel? siisti? aika huolella.
saiam
parents:
83
diff
changeset
|
156 |
Vector newPosition = posAfterTick /*+ (velAfterTick * PHYSICS_TICK_MS)/1000*/; |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
157 |
this->velocity = velAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
158 |
|
123 | 159 |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
160 |
// Collision detection |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
161 |
bool collided = false; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
162 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
163 |
const Vector diffVec = newPosition-position; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
164 |
const Vector unitVector = diffVec / diffVec.length(); |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
165 |
Vector reached = position; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
166 |
|
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
167 |
while ((position-reached).length() < diffVec.length()) { |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
168 |
// 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
|
169 |
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
|
170 |
if (world.getType(reached+shape[i]) != EMPTY) { // Collision |
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
|
171 |
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
|
172 |
// 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
|
173 |
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
|
174 |
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
|
175 |
//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
|
176 |
} |
115
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
177 |
reached = reached - unitVector; // Return to last point |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
178 |
collided = true; |
123 | 179 |
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
|
180 |
this->inAir = false; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
181 |
this->velocity = Vector(0,0); |
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 |
break; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
184 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
185 |
} |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
186 |
if (collided) |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
187 |
break; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
188 |
reached += unitVector; |
237ea0bb125a
Kirjottelin jotain uuden t?rm?ystarkistuksen tapasta, se kyll? bugaa jotenkin.
saiam
parents:
114
diff
changeset
|
189 |
} |
123 | 190 |
|
95 | 191 |
|
123 | 192 |
/* |
193 |
bool collided = false; |
|
94 | 194 |
|
123 | 195 |
//goes 1 unit forward every step and check if has hit anything |
196 |
Vector unitVector = (newPosition-position) / (newPosition-position).length(); |
|
114
71f7e9d3d052
V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents:
112
diff
changeset
|
197 |
|
123 | 198 |
Vector tmpVector = position; |
199 |
Vector reached = position; |
|
200 |
||
201 |
int steps = (int) (newPosition-position).length() + 2; |
|
202 |
||
203 |
//Engine::log(DEBUG, "physics.update_position") << unitVector-newPosition; |
|
204 |
//Vector foo = position+unitVector*steps-newPosition; |
|
205 |
//Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Virhe: "<< foo; |
|
206 |
||
207 |
for(int i = 0; i < steps; i++) { |
|
208 |
tmpVector += unitVector; |
|
209 |
||
210 |
float minVelocity = 10; |
|
211 |
// Check if any of the four corners of the worm collide |
|
212 |
for(int sh = 0; sh < 4; sh++) { |
|
213 |
if(world.getType(tmpVector+shape[sh]) != EMPTY) { |
|
214 |
reached = position + unitVector*(i-1); |
|
215 |
collided = true; |
|
216 |
this->bounce(world.getNormal(tmpVector+shape[sh], tmpVector-unitVector+shape[sh])); |
|
217 |
this->velocity *= 0.4; |
|
218 |
if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) { |
|
219 |
this->inAir = false; |
|
220 |
this->velocity = Vector(0,0); |
|
221 |
} |
|
222 |
break; |
|
223 |
} |
|
224 |
} |
|
225 |
if(collided) |
|
226 |
break; |
|
227 |
} |
|
228 |
*/ |
|
229 |
||
92 | 230 |
// 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
|
231 |
if(!collided) { |
92 | 232 |
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
|
233 |
|| (world.getType(newPosition+shape[2]) != EMPTY) || (world.getType(newPosition+shape[3]) != EMPTY)) { |
92 | 234 |
|
150
5e032b540af3
Now it uses Terrain class, but it isn't properly integrated to the infrastructure.
saiam
parents:
148
diff
changeset
|
235 |
// Engine::log(DEBUG, "physics.update_position") << "didnt hit"; |
112 | 236 |
// There was error, and there is ground |
92 | 237 |
//newPosition = tmpVector; |
77
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 |
// 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
|
240 |
} |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
241 |
} else { |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
242 |
newPosition = reached; |
116 | 243 |
onCollision(); |
92 | 244 |
//this->velocity = Vector(0, 0); |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
245 |
//TODO: it shouldn't just stop on collision |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
246 |
} |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
76
diff
changeset
|
247 |
this->position = newPosition; |
158 | 248 |
// Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Pos: " << this->position; |
44 | 249 |
} |
97 | 250 |
|
251 |
/** |
|
252 |
* Bounces from straight wall in any direction. |
|
253 |
* Direction given as normal of that wall |
|
254 |
*/ |
|
255 |
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
|
256 |
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
|
257 |
// 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
|
258 |
Vector nvel = velocity; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
259 |
// Engine::log(DEBUG, "PhysicsObject.bounce") << "New Velocity: " << nvel; |
160 | 260 |
// nvel = nvel - ((1+COLLISION_ELASTICITY)*((nvel*normal)/(normal*normal))*normal); |
261 |
||
262 |
nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal); |
|
263 |
// 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
|
264 |
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
|
265 |
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
|
266 |
} |
97 | 267 |
} |
44 | 268 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
269 |
/** |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
270 |
* Integrates given force over time and stores new position to |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
271 |
* posAfterTick and new velocity to velAfterTick. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
272 |
* @param force Force vector. |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
273 |
* @param dt The time the force is applied (<=PHYSICS_TICK_MS) |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
274 |
*/ |
85 | 275 |
void PhysicsObject::integrate(Force force, TimeMS dt) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
276 |
Derivative tmpd; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
277 |
Derivative k1 = evaluate(force, 0, tmpd); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
278 |
Derivative k2 = evaluate(force, 0.5f*dt, k1); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
279 |
Derivative k3 = evaluate(force, 0.5f*dt, k2); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
280 |
Derivative k4 = evaluate(force, dt, k3); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
281 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
282 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
283 |
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
|
284 |
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
|
285 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
286 |
// Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
287 |
posAfterTick = posAfterTick + (dxdt * dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
288 |
velAfterTick = velAfterTick + (dvdt * dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
289 |
//Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick; |
70 | 290 |
} |
291 |
||
85 | 292 |
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
293 |
Vector curPos = posAfterTick + (d.dx*dt)/1000; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
294 |
Vector curVel = velAfterTick + (d.dv*dt)/1000; |
58 | 295 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
296 |
Derivative out; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
297 |
out.dx = curVel; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
298 |
out.dv = acceleration(force); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
299 |
//Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
300 |
return out; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
301 |
} |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
302 |
|
85 | 303 |
Vector PhysicsObject::acceleration(const Force &force) { |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
304 |
return (force/mass); |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
305 |
} |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
82
diff
changeset
|
306 |
|
122
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
307 |
void PhysicsObject::applyForce (Force force) { |
123 | 308 |
// Add applied force to the queue |
309 |
forceq.push(force); |
|
44 | 310 |
} |
311 |
||
108 | 312 |
void PhysicsObject::changeAim(float da) { |
313 |
this->aim += da; |
|
314 |
||
315 |
if (this->aim > PLAYER_AIM_MAX) this->aim = PLAYER_AIM_MAX; |
|
316 |
if (this->aim < PLAYER_AIM_MIN) this->aim = PLAYER_AIM_MIN; |
|
109 | 317 |
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim; |
108 | 318 |
} |
319 |
||
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
320 |
void PhysicsObject::setFacing(bool facingRight) { |
109 | 321 |
//Engine::log(DEBUG, "PhysicsObject.setFacing") << "Facing: " << right; |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
322 |
this->facingRight = facingRight; |
108 | 323 |
} |
324 |
||
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
325 |
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir) { |
47 | 326 |
this->position = position; |
327 |
this->velocity = velocity; |
|
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
106
diff
changeset
|
328 |
this->inAir = inAir; |
44 | 329 |
} |
330 |
||
331 |
Vector PhysicsObject::getPosition () { |
|
47 | 332 |
return this->position; |
44 | 333 |
} |
334 |
||
108 | 335 |
bool PhysicsObject::getFacing() { |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
124
diff
changeset
|
336 |
return this->facingRight; |
108 | 337 |
} |
338 |
||
339 |
float PhysicsObject::getAim() { |
|
340 |
return this->aim; |
|
341 |
} |
|
342 |
||
91 | 343 |
std::vector<Vector>& PhysicsObject::getShape () { |
344 |
return this->shape; |
|
345 |
} |
|
346 |
||
347 |
void PhysicsObject::setShape (std::vector<Vector> shape) { |
|
123 | 348 |
this->shape = shape; |
91 | 349 |
} |
350 |
||
44 | 351 |
void PhysicsObject::tick () { |
47 | 352 |
this->updatePosition(); |
44 | 353 |
} |
354 |
||
153
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
355 |
void PhysicsObject::draw(CL_GraphicContext *gc) { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
356 |
CL_Quad player( |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
357 |
(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
|
358 |
(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
|
359 |
(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
|
360 |
(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
|
361 |
); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
362 |
|
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
363 |
gc->fill_quad(player, CL_Color::green); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
364 |
|
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
365 |
const uint16_t chlen = 10; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
366 |
uint16_t x = player.center().x; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
367 |
uint16_t y = player.center().y; |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
368 |
if (facingRight) { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
369 |
gc->draw_line(x, y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
370 |
x + std::cos(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
371 |
y - std::sin(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
372 |
CL_Color::black); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
373 |
} else { |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
374 |
gc->draw_line(x, y, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
375 |
x - std::cos(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
376 |
y - std::sin(aim)*chlen, |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
377 |
CL_Color::black); |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
378 |
} |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
379 |
} |
73402d5b778e
Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents:
151
diff
changeset
|
380 |