author | saiam |
Sat, 29 Nov 2008 17:37:51 +0000 | |
changeset 134 | d45109cf5e9d |
parent 133 | c05e84ccc4b3 |
child 135 | d5624d698a78 |
permissions | -rw-r--r-- |
42 | 1 |
#ifndef PHYSICS_HH |
2 |
#define PHYSICS_HH |
|
3 |
||
4 |
#include "Vector.hh" |
|
5 |
||
72 | 6 |
#include <vector> |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
7 |
#include <queue> |
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
48
diff
changeset
|
8 |
#include <ClanLib/core.h> |
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
48
diff
changeset
|
9 |
|
79 | 10 |
typedef uint16_t TimeMS; |
78 | 11 |
|
79 | 12 |
const TimeMS PHYSICS_TICK_MS = 10; |
42 | 13 |
|
72 | 14 |
enum TerrainType {EMPTY, DIRT, ROCK}; |
71 | 15 |
|
95 | 16 |
const Vector DIRECTIONS[] = { Vector(0,-1), Vector(1,-1), Vector(1,0), Vector(1,1), |
17 |
Vector(0,1), Vector(-1,1), Vector(-1,0), Vector(-1,-1) }; |
|
18 |
||
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
48
diff
changeset
|
19 |
// forward-declare |
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
48
diff
changeset
|
20 |
class PhysicsObject; |
85 | 21 |
typedef Vector Force; |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
22 |
struct Derivative; |
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
48
diff
changeset
|
23 |
|
42 | 24 |
class PhysicsWorld { |
60 | 25 |
friend class PhysicsObject; |
70 | 26 |
|
27 |
private: |
|
28 |
CL_Timer tick_timer; |
|
105 | 29 |
|
30 |
uint32_t tick_counter; |
|
70 | 31 |
|
32 |
protected: |
|
33 |
std::vector<PhysicsObject*> objects; |
|
34 |
Vector gravity; |
|
86 | 35 |
Vector dimensions; |
36 |
||
37 |
||
72 | 38 |
std::vector<std::vector<TerrainType> > terrain; |
71 | 39 |
|
70 | 40 |
CL_SlotContainer slots; |
41 |
||
42 |
PhysicsWorld (Vector gravity, Vector dimensions); |
|
86 | 43 |
|
44 |
||
45 |
public: |
|
70 | 46 |
|
123 | 47 |
/** |
48 |
* Adds objects to the physicsworld. |
|
49 |
* |
|
50 |
* @param object Pointer to PhysicsObject to add. |
|
51 |
*/ |
|
70 | 52 |
void addObject (PhysicsObject *object); |
123 | 53 |
|
54 |
/** |
|
55 |
* Advance one time step in physics simulation. |
|
56 |
*/ |
|
70 | 57 |
void tick (void); |
123 | 58 |
/** |
59 |
* Get current tick in physics simulation. |
|
60 |
*/ |
|
105 | 61 |
uint32_t getTick (void); |
62 |
||
123 | 63 |
|
64 |
/** |
|
65 |
* Generate random terrain. |
|
66 |
*/ |
|
74 | 67 |
void generateTerrain (int seed); |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
68 |
|
123 | 69 |
/** |
70 |
* Return a normal for the wall that has been hit. |
|
71 |
* |
|
72 |
* @param hitPoint The point of the wall that has been hit. |
|
73 |
* @param prevPoint The point from where we were coming. |
|
74 |
*/ |
|
98 | 75 |
Vector getNormal(Vector hitPoint, Vector prevPoint); |
76 |
||
123 | 77 |
/** |
78 |
* Return terrain type in specific position. |
|
79 |
* |
|
80 |
* @param x x-coordinate |
|
81 |
* @param y y-coordinate |
|
82 |
*/ |
|
112 | 83 |
TerrainType getType(int x, int y) const; |
123 | 84 |
/** |
85 |
* Return terrain type in specific position. |
|
86 |
* |
|
87 |
* @param pos Position vector |
|
88 |
*/ |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
89 |
TerrainType getType(Vector pos) const; |
112 | 90 |
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
91 |
/** |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
92 |
* Remove ground from the terrain. Removes a circle with given |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
93 |
* radius. |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
94 |
* |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
95 |
* @param x center x coordinate |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
96 |
* @param y center y coordinate |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
97 |
* @param r circle radius |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
98 |
*/ |
112 | 99 |
void removeGround(int x, int y, float r); |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
100 |
|
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
101 |
/** |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
102 |
* Remove ground from the terrain. Removes a circle with given |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
103 |
* radius. |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
104 |
* |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
105 |
* @param pos circle center |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
106 |
* @param r circle radius |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
107 |
*/ |
112 | 108 |
void removeGround(Vector pos, float r); |
42 | 109 |
}; |
110 |
||
111 |
class PhysicsObject { |
|
70 | 112 |
protected: |
113 |
PhysicsWorld &world; |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
114 |
|
70 | 115 |
float mass; |
116 |
Vector position; |
|
117 |
Vector velocity; |
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
118 |
float aim; // Aim direction (half circle) |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
119 |
bool facingRight; // Player facing |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
120 |
bool inAir; // Is the object "on the ground" |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
121 |
|
129 | 122 |
PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity); |
113 | 123 |
~PhysicsObject() {} |
108 | 124 |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
125 |
/** |
108 | 126 |
* Adds force to the force queue. Force queue is emptied on each |
127 |
* tick. Forces that last over one tick are also handled. This |
|
128 |
* function is only used to handle in air movement. |
|
129 |
* |
|
130 |
* @param force Force vector. |
|
131 |
*/ |
|
133
c05e84ccc4b3
Made unnecessarily virtual function PhysicsObject::applyForce nonvirtual
saiam
parents:
132
diff
changeset
|
132 |
void applyForce (Force force); |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
133 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
134 |
/** |
108 | 135 |
* Changes player aim |
129 | 136 |
* |
137 |
* @param da Aim angle change |
|
108 | 138 |
*/ |
139 |
void changeAim(float da); |
|
140 |
||
141 |
/** |
|
142 |
* Set player facing. |
|
129 | 143 |
* |
144 |
* @param facingRight True if player is facing right. |
|
108 | 145 |
*/ |
129 | 146 |
void setFacing(bool facingRight); |
108 | 147 |
|
148 |
/** |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
149 |
* Called on network clients to sync state from server |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
150 |
*/ |
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
105
diff
changeset
|
151 |
void updatePhysics (Vector position, Vector velocity, bool inAir); |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
152 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
153 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
154 |
* Handle ground-jumping |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
155 |
*/ |
98 | 156 |
void jump (void); |
157 |
||
158 |
/** |
|
159 |
* Handle ground-bounce |
|
129 | 160 |
* |
161 |
* @param normal Normal vector relative to which to bounce |
|
98 | 162 |
*/ |
163 |
void bounce (Vector normal); |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
164 |
|
70 | 165 |
private: |
129 | 166 |
/** |
167 |
* Handle player movement and applying forces. |
|
168 |
*/ |
|
70 | 169 |
void updatePosition (void); |
129 | 170 |
|
171 |
/* |
|
172 |
* TODO: Documentation |
|
173 |
*/ |
|
174 |
bool possibleLocation (Vector loc); |
|
69 | 175 |
|
70 | 176 |
/** |
177 |
* Use RK4 to integrate the effects of force over a time intervall. |
|
129 | 178 |
* |
179 |
* @param force Force to integrate |
|
180 |
* @param dt Time intervall |
|
70 | 181 |
*/ |
85 | 182 |
void integrate(Force force, TimeMS dt); |
129 | 183 |
|
184 |
/** |
|
185 |
* Evaluate the value of the derivative at given time |
|
186 |
* |
|
187 |
* @param force Force |
|
188 |
* @param dt Time |
|
189 |
* @param d Last derivative |
|
190 |
*/ |
|
85 | 191 |
Derivative evaluate(Force force, TimeMS dt, Derivative &d); |
129 | 192 |
|
193 |
/** |
|
194 |
* Return object acceleration with given force. |
|
195 |
* |
|
196 |
* @param force Force |
|
197 |
* @return acceleration |
|
198 |
*/ |
|
85 | 199 |
Vector acceleration(const Force &force); |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
200 |
|
134 | 201 |
/** |
202 |
* Handle ground movement |
|
203 |
* |
|
204 |
* @param right Boolean describing the movement direction. |
|
205 |
* @return new position |
|
206 |
*/ |
|
207 |
Vector walk (bool right); |
|
208 |
||
129 | 209 |
/* |
210 |
* Handle collision. TODO: This is not used. It probably should be? |
|
211 |
*/ |
|
116 | 212 |
virtual void onCollision() {} |
213 |
||
132
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
214 |
// Shape of the object. The vector contains the polygon edge |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
215 |
// points relative to the object location. |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
216 |
std::vector<Vector> shape; |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
217 |
|
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
218 |
// Force queue that is emptied on every tick |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
219 |
std::queue<Force> forceq; |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
220 |
// Helper variables for integration (TODO: should these be |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
221 |
// somewhere else?) |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
222 |
Vector posAfterTick; |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
223 |
Vector velAfterTick; |
957c3c184ea0
Changed some variables that were protected to private from PhysicsObject
saiam
parents:
131
diff
changeset
|
224 |
|
70 | 225 |
public: |
129 | 226 |
/** |
227 |
* Get current object position. |
|
228 |
||
229 |
* @return position vector |
|
230 |
*/ |
|
70 | 231 |
Vector getPosition (void); |
129 | 232 |
|
233 |
/** |
|
234 |
* Return object shape. |
|
235 |
* |
|
236 |
* @return Polygon points |
|
237 |
*/ |
|
108 | 238 |
std::vector<Vector>& getShape(void); |
129 | 239 |
|
240 |
/** |
|
241 |
* Set object shape. |
|
242 |
* |
|
243 |
* @param shape Vector containing polygon points |
|
244 |
*/ |
|
108 | 245 |
void setShape (std::vector<Vector> shape); |
246 |
||
129 | 247 |
/** |
248 |
* Return object facing. |
|
249 |
* |
|
250 |
* @return Object facing (true if facing is right) |
|
251 |
*/ |
|
108 | 252 |
bool getFacing(void); |
129 | 253 |
|
254 |
/** |
|
255 |
* Return object aim angle. |
|
256 |
* |
|
257 |
* @return Object aim angle |
|
258 |
*/ |
|
108 | 259 |
float getAim(void); |
260 |
||
129 | 261 |
/** |
262 |
* Update object in physics simulation. |
|
263 |
*/ |
|
70 | 264 |
void tick (void); |
42 | 265 |
}; |
266 |
||
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
267 |
struct Derivative { |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
268 |
Vector dx; // Velocity |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
269 |
Vector dv; // Acceleration |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
270 |
}; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
271 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
272 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
273 |
|
42 | 274 |
#endif |