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