author | saiam |
Fri, 28 Nov 2008 22:26:23 +0000 | |
changeset 128 | 890ac82cdcc0 |
parent 123 | 7efb63402b2b |
child 129 | b1ae79a2d2f0 |
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; |
|
105 | 29 |
|
30 |
uint32_t tick_counter; |
|
70 | 31 |
|
32 |
protected: |
|
33 |
std::vector<PhysicsObject*> objects; |
|
34 |
Vector gravity; |
|
86 | 35 |
Vector dimensions; |
36 |
||
37 |
||
72 | 38 |
std::vector<std::vector<TerrainType> > terrain; |
71 | 39 |
|
70 | 40 |
CL_SlotContainer slots; |
41 |
||
42 |
PhysicsWorld (Vector gravity, Vector dimensions); |
|
86 | 43 |
|
44 |
||
45 |
public: |
|
70 | 46 |
|
123 | 47 |
/** |
48 |
* Adds objects to the physicsworld. |
|
49 |
* |
|
50 |
* @param object Pointer to PhysicsObject to add. |
|
51 |
*/ |
|
70 | 52 |
void addObject (PhysicsObject *object); |
123 | 53 |
|
54 |
/** |
|
55 |
* Advance one time step in physics simulation. |
|
56 |
*/ |
|
70 | 57 |
void tick (void); |
123 | 58 |
/** |
59 |
* Get current tick in physics simulation. |
|
60 |
*/ |
|
105 | 61 |
uint32_t getTick (void); |
62 |
||
123 | 63 |
|
64 |
/** |
|
65 |
* Generate random terrain. |
|
66 |
*/ |
|
74 | 67 |
void generateTerrain (int seed); |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
68 |
|
123 | 69 |
/** |
70 |
* TODO: We'll have to think about this. |
|
71 |
*/ |
|
72 |
bool collided (Vector prevPos, Vector newPos); |
|
73 |
||
74 |
/** |
|
75 |
* Return a normal for the wall that has been hit. |
|
76 |
* |
|
77 |
* @param hitPoint The point of the wall that has been hit. |
|
78 |
* @param prevPoint The point from where we were coming. |
|
79 |
*/ |
|
98 | 80 |
Vector getNormal(Vector hitPoint, Vector prevPoint); |
81 |
||
123 | 82 |
/** |
83 |
* Return terrain type in specific position. |
|
84 |
* |
|
85 |
* @param x x-coordinate |
|
86 |
* @param y y-coordinate |
|
87 |
*/ |
|
112 | 88 |
TerrainType getType(int x, int y) const; |
123 | 89 |
/** |
90 |
* Return terrain type in specific position. |
|
91 |
* |
|
92 |
* @param pos Position vector |
|
93 |
*/ |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
94 |
TerrainType getType(Vector pos) const; |
112 | 95 |
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
96 |
/** |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
97 |
* Remove ground from the terrain. Removes a circle with given |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
98 |
* radius. |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
99 |
* |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
100 |
* @param x center x coordinate |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
101 |
* @param y center y coordinate |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
102 |
* @param r circle radius |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
103 |
*/ |
112 | 104 |
void removeGround(int x, int y, float r); |
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
105 |
|
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
106 |
/** |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
107 |
* Remove ground from the terrain. Removes a circle with given |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
108 |
* radius. |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
109 |
* |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
110 |
* @param pos circle center |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
111 |
* @param r circle radius |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
112 |
*/ |
112 | 113 |
void removeGround(Vector pos, float r); |
42 | 114 |
}; |
115 |
||
116 |
class PhysicsObject { |
|
70 | 117 |
protected: |
118 |
PhysicsWorld &world; |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
119 |
|
70 | 120 |
float mass; |
121 |
Vector position; |
|
122 |
Vector velocity; |
|
128
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
123 |
float aim; // Aim direction (half circle) |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
124 |
bool facingRight; // Player facing |
890ac82cdcc0
Documenting more, cleaning variables. This code needs some serious
saiam
parents:
123
diff
changeset
|
125 |
bool inAir; // Is the object "on the ground" |
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
126 |
|
108 | 127 |
// Shape of the object. We use a polygon with 4 edges |
128 |
// to make easy to draw with Clanlib. The coordinates |
|
129 |
// are relative to the center point. |
|
130 |
std::vector<Vector> shape; |
|
131 |
||
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
132 |
// Force queue that is emptied on every tick |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
133 |
std::queue<Force> forceq; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
134 |
Vector posAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
135 |
Vector velAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
136 |
|
108 | 137 |
/** |
138 |
* @param shape Corners of the four sided polygon. |
|
139 |
*/ |
|
70 | 140 |
PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity); |
113 | 141 |
~PhysicsObject() {} |
108 | 142 |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
143 |
/** |
108 | 144 |
* Adds force to the force queue. Force queue is emptied on each |
145 |
* tick. Forces that last over one tick are also handled. This |
|
146 |
* function is only used to handle in air movement. |
|
147 |
* |
|
148 |
* @param force Force vector. |
|
149 |
* @param dt The time the force is applied. |
|
150 |
*/ |
|
122
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
151 |
virtual void applyForce (Force force); |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
152 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
153 |
/** |
108 | 154 |
* Changes player aim |
155 |
*/ |
|
156 |
void changeAim(float da); |
|
157 |
||
158 |
/** |
|
159 |
* Set player facing. |
|
160 |
*/ |
|
161 |
void setFacing(bool right); |
|
162 |
||
163 |
/** |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
164 |
* 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
|
165 |
*/ |
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
105
diff
changeset
|
166 |
void updatePhysics (Vector position, Vector velocity, bool inAir); |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
167 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
168 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
169 |
* Handle ground movement |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
170 |
* |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
171 |
* @return new position |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
172 |
*/ |
98 | 173 |
Vector walk (bool right); |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
174 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
175 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
176 |
* Handle ground-jumping |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
177 |
*/ |
98 | 178 |
void jump (void); |
179 |
||
180 |
/** |
|
181 |
* Handle ground-bounce |
|
182 |
*/ |
|
183 |
void bounce (Vector normal); |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
184 |
|
70 | 185 |
private: |
186 |
void updatePosition (void); |
|
94 | 187 |
bool possibleLocation (Vector loc); |
69 | 188 |
|
70 | 189 |
/** |
190 |
* Use RK4 to integrate the effects of force over a time intervall. |
|
191 |
*/ |
|
85 | 192 |
void integrate(Force force, TimeMS dt); |
193 |
Derivative evaluate(Force force, TimeMS dt, Derivative &d); |
|
194 |
Vector acceleration(const Force &force); |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
195 |
|
116 | 196 |
virtual void onCollision() {} |
197 |
||
70 | 198 |
public: |
199 |
Vector getPosition (void); |
|
108 | 200 |
std::vector<Vector>& getShape(void); |
201 |
void setShape (std::vector<Vector> shape); |
|
202 |
||
203 |
bool getFacing(void); |
|
204 |
float getAim(void); |
|
205 |
||
70 | 206 |
void tick (void); |
42 | 207 |
}; |
208 |
||
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
209 |
struct Derivative { |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
210 |
Vector dx; // Velocity |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
211 |
Vector dv; // Acceleration |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
212 |
}; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
213 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
214 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
215 |
|
42 | 216 |
#endif |