| author | ekku |
| Mon, 24 Nov 2008 21:48:21 +0000 | |
| changeset 102 | 11e9d02af7a6 |
| parent 98 | 606c419e42a7 |
| child 105 | 91e3f3806b31 |
| 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; |
|
29 |
||
30 |
protected: |
|
31 |
std::vector<PhysicsObject*> objects; |
|
32 |
Vector gravity; |
|
| 86 | 33 |
Vector dimensions; |
34 |
||
35 |
||
| 72 | 36 |
std::vector<std::vector<TerrainType> > terrain; |
| 71 | 37 |
|
| 70 | 38 |
CL_SlotContainer slots; |
39 |
||
40 |
PhysicsWorld (Vector gravity, Vector dimensions); |
|
| 86 | 41 |
|
42 |
||
43 |
public: |
|
| 70 | 44 |
|
45 |
void addObject (PhysicsObject *object); |
|
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
48
diff
changeset
|
46 |
|
| 70 | 47 |
void tick (void); |
| 74 | 48 |
void generateTerrain (int seed); |
49 |
bool collided (Vector oldPos, Vector newPos); |
|
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
50 |
|
| 98 | 51 |
Vector getNormal(Vector hitPoint, Vector prevPoint); |
52 |
||
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
53 |
TerrainType getType(Vector pos) const; |
| 42 | 54 |
}; |
55 |
||
56 |
class PhysicsObject {
|
|
| 70 | 57 |
protected: |
58 |
PhysicsWorld &world; |
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
59 |
|
| 70 | 60 |
float mass; |
61 |
Vector position; |
|
62 |
Vector velocity; |
|
| 74 | 63 |
// Whether the object (worms mainly) is in the air |
64 |
// or firmly on the ground. Affects to physics. |
|
65 |
bool inAir; |
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
66 |
|
| 91 | 67 |
// Shape of the object. We use a polygon with 4 edges |
68 |
// to make easy to draw with Clanlib. The coordinates |
|
69 |
// are relative to the center point. |
|
70 |
std::vector<Vector> shape; |
|
| 88 | 71 |
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
72 |
// Force queue that is emptied on every tick |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
73 |
std::queue<Force> forceq; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
74 |
Vector posAfterTick; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
75 |
Vector velAfterTick; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
76 |
|
| 91 | 77 |
/** |
78 |
* @param shape Corners of the four sided polygon. |
|
79 |
*/ |
|
| 70 | 80 |
PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity); |
81 |
||
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
82 |
/** |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
83 |
* Used to handle in-air movement |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
84 |
*/ |
| 85 | 85 |
virtual void applyForce (Force force, TimeMS dt); |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
86 |
|
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
87 |
/** |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
88 |
* 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
|
89 |
*/ |
| 70 | 90 |
void updatePhysics (Vector position, Vector velocity); |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
91 |
|
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
92 |
/** |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
93 |
* Handle ground movement |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
94 |
* |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
95 |
* @return new position |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
96 |
*/ |
| 98 | 97 |
Vector walk (bool right); |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
98 |
|
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
99 |
/** |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
100 |
* Handle ground-jumping |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
101 |
*/ |
| 98 | 102 |
void jump (void); |
103 |
||
104 |
/** |
|
105 |
* Handle ground-bounce |
|
106 |
*/ |
|
107 |
void bounce (Vector normal); |
|
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
108 |
|
| 70 | 109 |
private: |
110 |
void updatePosition (void); |
|
| 94 | 111 |
bool possibleLocation (Vector loc); |
| 69 | 112 |
|
| 70 | 113 |
/** |
114 |
* Use RK4 to integrate the effects of force over a time intervall. |
|
115 |
*/ |
|
| 85 | 116 |
void integrate(Force force, TimeMS dt); |
117 |
Derivative evaluate(Force force, TimeMS dt, Derivative &d); |
|
118 |
Vector acceleration(const Force &force); |
|
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
119 |
|
| 70 | 120 |
public: |
121 |
Vector getPosition (void); |
|
| 91 | 122 |
std::vector<Vector>& getShape(void); |
123 |
void setShape (std::vector<Vector> shape); |
|
| 70 | 124 |
|
125 |
void tick (void); |
|
| 42 | 126 |
}; |
127 |
||
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
128 |
struct Derivative {
|
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
129 |
Vector dx; // Velocity |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
130 |
Vector dv; // Acceleration |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
131 |
}; |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
132 |
|
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
133 |
|
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
134 |
|
| 42 | 135 |
#endif |