author | terom |
Mon, 24 Nov 2008 17:14:29 +0000 | |
changeset 96 | 4a801210096c |
parent 95 | 10704e1df844 |
child 98 | 606c419e42a7 |
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 |
|
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
51 |
TerrainType getType(Vector pos) const; |
42 | 52 |
}; |
53 |
||
54 |
class PhysicsObject { |
|
70 | 55 |
protected: |
56 |
PhysicsWorld &world; |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
57 |
|
70 | 58 |
float mass; |
59 |
Vector position; |
|
60 |
Vector velocity; |
|
74 | 61 |
// Whether the object (worms mainly) is in the air |
62 |
// or firmly on the ground. Affects to physics. |
|
63 |
bool inAir; |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
64 |
|
91 | 65 |
// Shape of the object. We use a polygon with 4 edges |
66 |
// to make easy to draw with Clanlib. The coordinates |
|
67 |
// are relative to the center point. |
|
68 |
std::vector<Vector> shape; |
|
88 | 69 |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
70 |
// Force queue that is emptied on every tick |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
71 |
std::queue<Force> forceq; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
72 |
Vector posAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
73 |
Vector velAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
74 |
|
91 | 75 |
/** |
76 |
* @param shape Corners of the four sided polygon. |
|
77 |
*/ |
|
70 | 78 |
PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity); |
79 |
||
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
80 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
81 |
* Used to handle in-air movement |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
82 |
*/ |
85 | 83 |
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
|
84 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
85 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
86 |
* 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
|
87 |
*/ |
70 | 88 |
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
|
89 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
90 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
91 |
* Handle ground movement |
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 |
* @return new position |
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 |
Vector walk (bool right); |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
96 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
97 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
98 |
* Handle ground-jumping |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
99 |
*/ |
94 | 100 |
void jump (void); |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
101 |
|
70 | 102 |
private: |
103 |
void updatePosition (void); |
|
94 | 104 |
bool possibleLocation (Vector loc); |
69 | 105 |
|
70 | 106 |
/** |
107 |
* Use RK4 to integrate the effects of force over a time intervall. |
|
108 |
*/ |
|
85 | 109 |
void integrate(Force force, TimeMS dt); |
110 |
Derivative evaluate(Force force, TimeMS dt, Derivative &d); |
|
111 |
Vector acceleration(const Force &force); |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
112 |
|
70 | 113 |
public: |
114 |
Vector getPosition (void); |
|
91 | 115 |
std::vector<Vector>& getShape(void); |
116 |
void setShape (std::vector<Vector> shape); |
|
70 | 117 |
|
118 |
void tick (void); |
|
42 | 119 |
}; |
120 |
||
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
121 |
struct Derivative { |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
122 |
Vector dx; // Velocity |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
123 |
Vector dv; // Acceleration |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
124 |
}; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
125 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
126 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
127 |
|
42 | 128 |
#endif |