author | terom |
Mon, 08 Dec 2008 01:38:43 +0000 | |
changeset 279 | e36f5e1a1c8d |
parent 273 | eeb699e1d908 |
child 282 | e0e4dfc3e528 |
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; |
|
265 | 18 |
|
19 |
enum ObjectType { PLAYER, PROJECTILE, ROPE }; |
|
197 | 20 |
|
264 | 21 |
enum FacingDirection { |
22 |
FACING_LEFT, |
|
23 |
FACING_RIGHT |
|
24 |
}; |
|
197 | 25 |
|
26 |
/** |
|
27 |
* PhysicObject class. A basic PhysicsObject class. |
|
28 |
*/ |
|
29 |
class PhysicsObject { |
|
30 |
protected: |
|
31 |
Vector position; |
|
32 |
Vector velocity; |
|
33 |
float mass; |
|
34 |
bool inAir; // Is the object "on the ground" |
|
35 |
float collision_elasticity; |
|
36 |
||
37 |
// Attributes for players |
|
38 |
float aim; // Aim direction (half circle) |
|
264 | 39 |
FacingDirection facing; // Player facing |
197 | 40 |
|
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
|
41 |
bool alive; |
225 | 42 |
bool shouldDelete; |
265 | 43 |
|
44 |
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
|
45 |
|
228 | 46 |
PhysicsObject *pivot; |
47 |
||
279
e36f5e1a1c8d
let projectiles bounce, the new BounceBounce weapon puts the Physics engine into an infinite loop
terom
parents:
273
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
virtual ~PhysicsObject (void); |
197 | 51 |
|
52 |
||
53 |
/** |
|
54 |
* Add force to the force queue to be applied on next tick. |
|
55 |
* |
|
56 |
* @param force Force vector |
|
57 |
*/ |
|
273 | 58 |
void applyForce(Force force, TimeMS dt = PHYSICS_TICK_MS); |
197 | 59 |
|
60 |
/** |
|
61 |
* Change player aim |
|
62 |
* |
|
63 |
* @param da Aim angle change |
|
64 |
*/ |
|
65 |
void changeAim(float da); |
|
66 |
||
67 |
/** |
|
68 |
* Set player facing. |
|
69 |
* |
|
70 |
* @param facingRight True if player is facing right. |
|
71 |
*/ |
|
264 | 72 |
void setFacing (FacingDirection facing); |
197 | 73 |
|
74 |
/** |
|
75 |
* Makes the player jump in the air. |
|
76 |
* @param direction -1: jump left, 0: jump up, 1: jump right |
|
77 |
*/ |
|
78 |
void jump(int direction); |
|
79 |
||
80 |
/** |
|
81 |
* Handle ground-bounce |
|
82 |
* |
|
83 |
* @param normal Normal vector relative to which to bounce |
|
84 |
*/ |
|
85 |
void bounce(Vector normal); |
|
86 |
||
87 |
/** |
|
88 |
* Called on network clients to sync state from server |
|
89 |
* |
|
90 |
* @param position New position |
|
91 |
* @param velocity New velocity |
|
92 |
* @param inAir New inAir value |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
93 |
* @param facingRight New facingRight value |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
197
diff
changeset
|
94 |
* @param aim New aim |
197 | 95 |
*/ |
264 | 96 |
virtual void updatePhysics(Vector position, Vector velocity, bool inAir, FacingDirection facing, float aim); |
197 | 97 |
|
225 | 98 |
/** |
99 |
* Put object to the objects list so that its movement will be calculated. |
|
100 |
*/ |
|
101 |
void enable (void); |
|
102 |
||
103 |
/** |
|
104 |
* Remove object from the objects list but don't delete the object itself. |
|
105 |
*/ |
|
106 |
void disable (void); |
|
107 |
||
197 | 108 |
private: |
109 |
// Shape of the object. Edge points of the shape polygon. |
|
110 |
std::vector<Vector> shape; |
|
111 |
||
112 |
// Force queue that is emptied on every tick |
|
273 | 113 |
std::queue<std::pair<Force, TimeMS> > forceq; |
197 | 114 |
|
115 |
/** |
|
116 |
* Handle player movement and apply forces. |
|
117 |
*/ |
|
205 | 118 |
void updatePosition(TimeMS dt); |
197 | 119 |
|
120 |
// TODO: Should these be moved to PhysicsWorld? |
|
121 |
/** |
|
122 |
* Use RK4 to integrate the effects of force over a time interwall. |
|
123 |
* |
|
124 |
* @param force Force to integrate |
|
125 |
* @param dt Time intervall |
|
126 |
*/ |
|
127 |
void integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick); |
|
128 |
||
129 |
/** |
|
130 |
* Evaluate the value of the derivative at given time |
|
131 |
* |
|
132 |
* @param force Force |
|
133 |
* @param dt Time |
|
134 |
* @param d Previous derivative |
|
135 |
*/ |
|
136 |
Derivative evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick); |
|
137 |
||
138 |
/** |
|
139 |
* Return object acceleration with given force. |
|
140 |
* |
|
141 |
* @param force Force |
|
142 |
* @return Acceleration |
|
143 |
*/ |
|
144 |
Vector acceleration(const Force &force); |
|
145 |
||
249 | 146 |
/** |
197 | 147 |
* Handle ground movement. |
148 |
* |
|
149 |
* @param right Boolean describing the movement direction. |
|
150 |
* @return New position |
|
151 |
*/ |
|
152 |
void walk(TimeMS, bool right); |
|
153 |
Vector walk_one_step(float, bool); |
|
154 |
||
249 | 155 |
/** |
156 |
* Define object behaviour on collisions. |
|
197 | 157 |
*/ |
272 | 158 |
virtual void onCollision (Vector collisionPoint, PhysicsObject *other) {} |
159 |
||
160 |
public: |
|
161 |
PhysicsWorld &world; |
|
197 | 162 |
|
249 | 163 |
/** |
164 |
* Checks if it is possible for the object to be in the given |
|
165 |
* location. |
|
197 | 166 |
*/ |
167 |
bool possibleLocation(Vector loc); |
|
168 |
||
169 |
/** |
|
170 |
* Get current object position. |
|
171 |
* |
|
172 |
* @return Position vector |
|
173 |
*/ |
|
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
|
174 |
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
|
175 |
|
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
176 |
/** |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
177 |
* 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
|
178 |
* |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
179 |
* @return PixelCoordinate position |
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
252
diff
changeset
|
180 |
*/ |
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
|
181 |
PixelCoordinate getCoordinate (void) const; |
197 | 182 |
|
183 |
/** |
|
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
|
184 |
* 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
|
185 |
* |
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
|
186 |
* @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
|
187 |
*/ |
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
|
188 |
Vector getVelocity() 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
|
189 |
|
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
|
190 |
/** |
197 | 191 |
* Return object shape. |
192 |
* |
|
193 |
* @return Polygon points |
|
194 |
*/ |
|
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
|
195 |
const std::vector<Vector>& getShape() const; |
197 | 196 |
|
197 |
/** |
|
198 |
* Set object shape. |
|
199 |
* |
|
200 |
* @param shape Vector containing polygon poinst |
|
201 |
*/ |
|
202 |
void setShape(std::vector<Vector> shape); |
|
203 |
||
204 |
/** |
|
205 |
* Return object facing. |
|
206 |
* |
|
207 |
* @return Object facing (true if facing right) |
|
208 |
*/ |
|
264 | 209 |
FacingDirection getFacing() const; |
197 | 210 |
|
211 |
/** |
|
212 |
* Return object aim angle. |
|
213 |
* |
|
214 |
* @return Object aim angle |
|
215 |
*/ |
|
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
|
216 |
float getAim() const; |
197 | 217 |
|
218 |
/** |
|
235 | 219 |
* Returns facing+aim as a unit vector |
220 |
*/ |
|
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
|
221 |
Vector getDirection (void) const; |
235 | 222 |
|
223 |
/** |
|
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
|
224 |
* 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
|
225 |
*/ |
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
|
226 |
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
|
227 |
|
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
|
228 |
/* |
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
|
229 |
* Had the object been destroyed? |
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
|
230 |
*/ |
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
|
231 |
bool isDestroyed (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
|
232 |
|
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
|
233 |
/** |
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
|
234 |
* Delete ourselves if we've been destroyed and return true, else return false |
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
|
235 |
*/ |
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
|
236 |
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
|
237 |
|
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
|
238 |
/** |
228 | 239 |
* Sets this object's pivot to the given value, which will then exert a force on this object |
240 |
*/ |
|
241 |
void setPivot (PhysicsObject *pivot); |
|
242 |
||
243 |
/** |
|
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
244 |
* Checks if object collides with other objects |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
245 |
* |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
246 |
* @param obj Other PhysicsObject |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
247 |
*/ |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
248 |
bool collides (const PhysicsObject &obj); |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
249 |
|
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
250 |
/** |
228 | 251 |
* Compute the force that this object (as a pivot) exerts on the given object |
252 |
*/ |
|
253 |
virtual float getPivotForce (PhysicsObject *bob); |
|
254 |
||
255 |
/** |
|
197 | 256 |
* Update object in physics simulation. |
257 |
*/ |
|
221 | 258 |
virtual void tick (TimeMS tick_length); |
197 | 259 |
}; |
260 |
||
261 |
struct Derivative { |
|
262 |
Vector dx; // Velocity |
|
263 |
Vector dv; // Acceleration |
|
264 |
}; |
|
265 |
||
268
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
266 |
/** |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
267 |
* 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
|
268 |
* 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
|
269 |
* line drawn between p1 and p2 the point p3 is. |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
270 |
* |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
271 |
* @param p1 Line start point |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
272 |
* @param p2 Line end point |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
273 |
* @param p3 Point |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
274 |
* @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
|
275 |
*/ |
0b96b88af335
Added collision detection method to PhysicsObject. Not tested.
saiam
parents:
265
diff
changeset
|
276 |
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
|
277 |
|
197 | 278 |
#endif |