author | Tero Marttila <terom@fixme.fi> |
Sat, 17 Jan 2009 01:22:47 +0200 | |
changeset 404 | 7058ee22ee39 |
parent 322 | f94a5c192097 |
child 423 | 947ab54de4b7 |
permissions | -rw-r--r-- |
197 | 1 |
#ifndef PHYSICS_OBJECT_HH |
2 |
#define PHYSICS_OBJECT_HH |
|
3 |
||
4 |
#include <ClanLib/display.h> |
|
273 | 5 |
#include <utility> |
197 | 6 |
|
7 |
// Forward declares |
|
8 |
class PhysicsObject; |
|
9 |
struct Derivative; |
|
10 |
||
11 |
#include "PhysicsWorld.hh" |
|
12 |
#include "Vector.hh" |
|
205 | 13 |
#include "Timer.hh" |
197 | 14 |
#include "Config.hh" |
15 |
||
16 |
// Type definitions |
|
17 |
typedef Vector Force; |
|
320 | 18 |
|
19 |
/** Specifies possible object types. */ |
|
265 | 20 |
enum ObjectType { PLAYER, PROJECTILE, ROPE }; |
197 | 21 |
|
320 | 22 |
/** Specifies possible facing directions for objects. */ |
264 | 23 |
enum FacingDirection { |
24 |
FACING_LEFT, |
|
25 |
FACING_RIGHT |
|
26 |
}; |
|
197 | 27 |
|
28 |
/** |
|
321 | 29 |
* PhysicsObject class. Represents an object in the physics simulation. |
197 | 30 |
*/ |
31 |
class PhysicsObject { |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
32 |
public: |
320 | 33 |
/** Reference to PhysicsWorld. */ |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
34 |
PhysicsWorld &world; |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
35 |
|
197 | 36 |
protected: |
320 | 37 |
/** Object position. */ |
197 | 38 |
Vector position; |
320 | 39 |
/** Object position on previous physics tick. */ |
285 | 40 |
Vector previousPosition; |
320 | 41 |
/** Object velocity */ |
197 | 42 |
Vector velocity; |
320 | 43 |
/** Object mass. */ |
197 | 44 |
float mass; |
320 | 45 |
/** Tells if the object is "on the ground" */ |
46 |
bool inAir; |
|
47 |
/** Object elasticity. */ |
|
197 | 48 |
float collision_elasticity; |
49 |
||
50 |
// Attributes for players |
|
320 | 51 |
/** Aim angle in radians. */ |
52 |
float aim; |
|
53 |
/** Player facing. */ |
|
54 |
FacingDirection facing; |
|
197 | 55 |
|
320 | 56 |
/** Specifies if the player is alive (or dead). */ |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
57 |
bool alive; |
320 | 58 |
/** True if player object should be removed from the game. */ |
225 | 59 |
bool shouldDelete; |
265 | 60 |
|
320 | 61 |
/** Type of the object */ |
265 | 62 |
ObjectType type; |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
63 |
|
320 | 64 |
/** Pivot object for this object. */ |
228 | 65 |
PhysicsObject *pivot; |
66 |
||
320 | 67 |
/** |
68 |
* Class constructor |
|
69 |
* |
|
70 |
* @param world Reference to PhysicsWorld |
|
71 |
* @param mass Object mass |
|
72 |
* @param position Object position |
|
73 |
* @param velocity Object velocity |
|
74 |
* @param type Object type |
|
75 |
* @param collision_elasticity Object elasticity |
|
76 |
* @param enabled Is the object currently part of the simulation |
|
77 |
*/ |
|
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
273
diff
changeset
|
78 |
PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity, ObjectType type, |
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
273
diff
changeset
|
79 |
float collision_elasticity, bool enabled = true); |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
80 |
virtual ~PhysicsObject (void); |
197 | 81 |
|
82 |
||
83 |
/** |
|
320 | 84 |
* Apply a force to the object. The force is applied to the object |
85 |
* on the next physics tick. |
|
197 | 86 |
* |
87 |
* @param force Force vector |
|
88 |
*/ |
|
273 | 89 |
void applyForce(Force force, TimeMS dt = PHYSICS_TICK_MS); |
197 | 90 |
|
91 |
/** |
|
320 | 92 |
* Change player aim. This function takes care that aim angle |
93 |
* stays inside limits. |
|
197 | 94 |
* |
320 | 95 |
* @param da Aim angle change in radians |
197 | 96 |
*/ |
97 |
void changeAim(float da); |
|
98 |
||
99 |
/** |
|
100 |
* Set player facing. |
|
101 |
* |
|
102 |
* @param facingRight True if player is facing right. |
|
103 |
*/ |
|
264 | 104 |
void setFacing (FacingDirection facing); |
197 | 105 |
|
106 |
/** |
|
107 |
* Makes the player jump in the air. |
|
320 | 108 |
* |
197 | 109 |
* @param direction -1: jump left, 0: jump up, 1: jump right |
110 |
*/ |
|
299 | 111 |
void jump (int direction); |
197 | 112 |
|
113 |
/** |
|
320 | 114 |
* Handle object bounce. |
197 | 115 |
* |
116 |
* @param normal Normal vector relative to which to bounce |
|
117 |
*/ |
|
299 | 118 |
void bounce (Vector normal); |
197 | 119 |
|
120 |
/** |
|
121 |
* Called on network clients to sync state from server |
|
122 |
* |
|
123 |
* @param position New position |
|
124 |
* @param velocity New velocity |
|
125 |
* @param inAir New inAir value |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
126 |
* @param facingRight New facingRight value |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
127 |
* @param aim New aim |
197 | 128 |
*/ |
299 | 129 |
virtual void updatePhysics (Vector position, Vector velocity, bool inAir, FacingDirection facing, float aim); |
197 | 130 |
|
225 | 131 |
/** |
320 | 132 |
* Put the object in the physics simulation. When the object is in |
133 |
* the physics simulation it's movements will be calculated. |
|
225 | 134 |
*/ |
135 |
void enable (void); |
|
136 |
||
137 |
/** |
|
320 | 138 |
* Remove object from the objects physics simulation. When the |
139 |
* object isn't in the physics simulation it won't be deleted but |
|
140 |
* its movements will not be calculated. |
|
225 | 141 |
*/ |
142 |
void disable (void); |
|
143 |
||
197 | 144 |
private: |
320 | 145 |
/** Objects shape. Edgepoints of the polygon */ |
197 | 146 |
std::vector<Vector> shape; |
147 |
||
320 | 148 |
/** Object force queue. The queue is emptied on every physics |
149 |
tick.*/ |
|
273 | 150 |
std::queue<std::pair<Force, TimeMS> > forceq; |
197 | 151 |
|
152 |
/** |
|
153 |
* Handle player movement and apply forces. |
|
320 | 154 |
* |
155 |
* @param dt Time intervall |
|
197 | 156 |
*/ |
299 | 157 |
void updatePosition (TimeMS dt); |
197 | 158 |
|
159 |
/** |
|
160 |
* Use RK4 to integrate the effects of force over a time interwall. |
|
161 |
* |
|
162 |
* @param force Force to integrate |
|
163 |
* @param dt Time intervall |
|
320 | 164 |
* @param posAfterTick Reference to object postition after integration. |
165 |
* @param velAfterTick Reference to object velocity after integration. |
|
197 | 166 |
*/ |
299 | 167 |
void integrate (Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick); |
197 | 168 |
|
169 |
/** |
|
170 |
* Evaluate the value of the derivative at given time |
|
171 |
* |
|
172 |
* @param force Force |
|
173 |
* @param dt Time |
|
174 |
* @param d Previous derivative |
|
320 | 175 |
* @return Derivative |
197 | 176 |
*/ |
299 | 177 |
Derivative evaluate (Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick); |
197 | 178 |
|
179 |
/** |
|
180 |
* Return object acceleration with given force. |
|
181 |
* |
|
182 |
* @param force Force |
|
183 |
* @return Acceleration |
|
184 |
*/ |
|
299 | 185 |
Vector acceleration (const Force &force); |
197 | 186 |
|
249 | 187 |
/** |
197 | 188 |
* Handle ground movement. |
189 |
* |
|
190 |
* @param right Boolean describing the movement direction. |
|
191 |
* @return New position |
|
192 |
*/ |
|
299 | 193 |
void walk (TimeMS, bool right); |
194 |
Vector walk_one_step (float, bool); |
|
197 | 195 |
|
288 | 196 |
public: |
249 | 197 |
/** |
198 |
* Define object behaviour on collisions. |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
279
diff
changeset
|
199 |
* |
320 | 200 |
* @param collisionPoint Point of collision. |
201 |
* @param other The other object we have collided with. |
|
197 | 202 |
*/ |
288 | 203 |
virtual void onCollision (Vector collisionPoint, PhysicsObject *other = NULL); |
272 | 204 |
|
249 | 205 |
/** |
320 | 206 |
* Get object type. |
207 |
* |
|
208 |
* @return Object type |
|
287 | 209 |
*/ |
210 |
ObjectType getType (void) const; |
|
211 |
||
212 |
/** |
|
249 | 213 |
* Checks if it is possible for the object to be in the given |
214 |
* location. |
|
320 | 215 |
* |
216 |
* @param loc Location |
|
197 | 217 |
*/ |
299 | 218 |
bool possibleLocation (Vector loc); |
197 | 219 |
|
220 |
/** |
|
221 |
* Get current object position. |
|
222 |
* |
|
223 |
* @return Position vector |
|
224 |
*/ |
|
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
255
diff
changeset
|
225 |
Vector getPosition (void) const; |
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
226 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
227 |
/** |
285 | 228 |
* Set previous object position. |
229 |
* |
|
230 |
* @param Position vector |
|
231 |
*/ |
|
232 |
void setPosition (Vector pos); |
|
233 |
||
234 |
/** |
|
235 |
* Get previous object position. |
|
236 |
* |
|
237 |
* @return Position vector |
|
238 |
*/ |
|
239 |
Vector getPreviousPosition (void) const; |
|
240 |
||
241 |
/** |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
242 |
* Get current object screen coordinates |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
243 |
* |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
244 |
* @return PixelCoordinate position |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
245 |
*/ |
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
255
diff
changeset
|
246 |
PixelCoordinate getCoordinate (void) const; |
197 | 247 |
|
248 |
/** |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
249 |
* Get current object velocity. |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
250 |
* |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
251 |
* @return Velocity vector |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
252 |
*/ |
299 | 253 |
Vector getVelocity (void) const; |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
254 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
255 |
/** |
197 | 256 |
* Return object shape. |
257 |
* |
|
258 |
* @return Polygon points |
|
259 |
*/ |
|
299 | 260 |
const std::vector<Vector>& getShape (void) const; |
197 | 261 |
|
262 |
/** |
|
263 |
* Set object shape. |
|
264 |
* |
|
320 | 265 |
* @param shape Vector containing polygon points |
197 | 266 |
*/ |
267 |
void setShape(std::vector<Vector> shape); |
|
268 |
||
269 |
/** |
|
270 |
* Return object facing. |
|
271 |
* |
|
272 |
* @return Object facing (true if facing right) |
|
273 |
*/ |
|
264 | 274 |
FacingDirection getFacing() const; |
197 | 275 |
|
276 |
/** |
|
277 |
* Return object aim angle. |
|
278 |
* |
|
279 |
* @return Object aim angle |
|
280 |
*/ |
|
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
255
diff
changeset
|
281 |
float getAim() const; |
197 | 282 |
|
283 |
/** |
|
320 | 284 |
* Get object direction. |
285 |
* |
|
286 |
* @return Unit vector to facing+aim |
|
235 | 287 |
*/ |
257
549783d71e51
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
terom
parents:
255
diff
changeset
|
288 |
Vector getDirection (void) const; |
235 | 289 |
|
290 |
/** |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
291 |
* Mark object as destroyed, it will be delete'd later |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
292 |
*/ |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
293 |
void destroy (void); |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
294 |
|
320 | 295 |
/** |
296 |
* Check if the object is alive. |
|
297 |
* |
|
298 |
* @return Is the object alive? |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
299 |
*/ |
299 | 300 |
bool isAlive (void); |
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
301 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
302 |
/** |
320 | 303 |
* Tells the state of the object. |
304 |
* |
|
305 |
* @return True if object has been destroyed. |
|
222
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
306 |
*/ |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
307 |
bool removeIfDestroyed (void); |
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
308 |
|
293ddf4c067d
reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents:
221
diff
changeset
|
309 |
/** |
320 | 310 |
* Set object pivot. |
311 |
* |
|
312 |
* @param pivot Pivot object |
|
228 | 313 |
*/ |
314 |
void setPivot (PhysicsObject *pivot); |
|
315 |
||
316 |
/** |
|
322 | 317 |
* Return the pivot object pointer. |
318 |
*/ |
|
319 |
PhysicsObject *getPivot (void); |
|
320 |
||
321 |
/** |
|
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
322 |
* Checks if object collides with other objects |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
323 |
* |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
324 |
* @param obj Other PhysicsObject |
320 | 325 |
* @return Did we collide? |
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
326 |
*/ |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
327 |
bool collides (const PhysicsObject &obj); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
328 |
|
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
329 |
/** |
228 | 330 |
* Compute the force that this object (as a pivot) exerts on the given object |
320 | 331 |
* |
332 |
* @param bob Othe object |
|
333 |
* @return Force |
|
228 | 334 |
*/ |
322 | 335 |
virtual Vector getPivotForce (void); |
228 | 336 |
|
337 |
/** |
|
197 | 338 |
* Update object in physics simulation. |
320 | 339 |
* |
340 |
* @param tick_length Length of the physics tick |
|
197 | 341 |
*/ |
221 | 342 |
virtual void tick (TimeMS tick_length); |
197 | 343 |
}; |
344 |
||
320 | 345 |
/** Helper struct for the integration */ |
197 | 346 |
struct Derivative { |
347 |
Vector dx; // Velocity |
|
348 |
Vector dv; // Acceleration |
|
349 |
}; |
|
350 |
||
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
351 |
/** |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
352 |
* Returns the "sign" of the cross product between given points. In |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
353 |
* practice the sign of the return value tels on which side of the |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
354 |
* line drawn between p1 and p2 the point p3 is. |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
355 |
* |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
356 |
* @param p1 Line start point |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
357 |
* @param p2 Line end point |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
358 |
* @param p3 Point |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
359 |
* @return Variable, the sign of which tells on which side of the line p3 is. |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
360 |
*/ |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
361 |
int8_t crossProduct(const Vector &p1, const Vector &p2, const Vector &p3); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
362 |
|
197 | 363 |
#endif |