author | saiam |
Fri, 28 Nov 2008 15:27:51 +0000 | |
changeset 122 | 16a73ebca810 |
parent 116 | 0d36aade845e |
child 123 | 7efb63402b2b |
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 |
|
47 |
void addObject (PhysicsObject *object); |
|
50
9e1a6506f5a1
some rough-handed code modifications towards a newer, better, working Physics
terom
parents:
48
diff
changeset
|
48 |
|
70 | 49 |
void tick (void); |
105 | 50 |
uint32_t getTick (void); |
51 |
||
74 | 52 |
void generateTerrain (int seed); |
53 |
bool collided (Vector oldPos, Vector newPos); |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
54 |
|
98 | 55 |
Vector getNormal(Vector hitPoint, Vector prevPoint); |
56 |
||
112 | 57 |
TerrainType getType(int x, int y) const; |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
75
diff
changeset
|
58 |
TerrainType getType(Vector pos) const; |
112 | 59 |
|
60 |
void removeGround(int x, int y, float r); |
|
61 |
void removeGround(Vector pos, float r); |
|
42 | 62 |
}; |
63 |
||
64 |
class PhysicsObject { |
|
70 | 65 |
protected: |
66 |
PhysicsWorld &world; |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
67 |
|
70 | 68 |
float mass; |
69 |
Vector position; |
|
70 |
Vector velocity; |
|
74 | 71 |
// Whether the object (worms mainly) is in the air |
72 |
// or firmly on the ground. Affects to physics. |
|
73 |
bool inAir; |
|
108 | 74 |
float aim; |
75 |
uint8_t facing; |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
76 |
|
108 | 77 |
// Shape of the object. We use a polygon with 4 edges |
78 |
// to make easy to draw with Clanlib. The coordinates |
|
79 |
// are relative to the center point. |
|
80 |
std::vector<Vector> shape; |
|
81 |
||
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
82 |
// Force queue that is emptied on every tick |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
83 |
std::queue<Force> forceq; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
84 |
Vector posAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
85 |
Vector velAfterTick; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
86 |
|
108 | 87 |
/** |
88 |
* @param shape Corners of the four sided polygon. |
|
89 |
*/ |
|
70 | 90 |
PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity); |
113 | 91 |
~PhysicsObject() {} |
108 | 92 |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
93 |
/** |
108 | 94 |
* Adds force to the force queue. Force queue is emptied on each |
95 |
* tick. Forces that last over one tick are also handled. This |
|
96 |
* function is only used to handle in air movement. |
|
97 |
* |
|
98 |
* @param force Force vector. |
|
99 |
* @param dt The time the force is applied. |
|
100 |
*/ |
|
122
16a73ebca810
No warnings anymore, but well have to think about that applyForce
saiam
parents:
116
diff
changeset
|
101 |
virtual void applyForce (Force force); |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
102 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
103 |
/** |
108 | 104 |
* Changes player aim |
105 |
*/ |
|
106 |
void changeAim(float da); |
|
107 |
||
108 |
/** |
|
109 |
* Set player facing. |
|
110 |
*/ |
|
111 |
void setFacing(bool right); |
|
112 |
||
113 |
/** |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
114 |
* 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
|
115 |
*/ |
107
505bfa531496
send inAir attribute as part of NETWORK_PLAYER_POSITION...
terom
parents:
105
diff
changeset
|
116 |
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
|
117 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
118 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
119 |
* Handle ground movement |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
120 |
* |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
121 |
* @return new position |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
122 |
*/ |
98 | 123 |
Vector walk (bool right); |
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
124 |
|
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
125 |
/** |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
126 |
* Handle ground-jumping |
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
127 |
*/ |
98 | 128 |
void jump (void); |
129 |
||
130 |
/** |
|
131 |
* Handle ground-bounce |
|
132 |
*/ |
|
133 |
void bounce (Vector normal); |
|
96
4a801210096c
fix movement physics+network code to some degree, jumping is now buggy?
terom
parents:
95
diff
changeset
|
134 |
|
70 | 135 |
private: |
136 |
void updatePosition (void); |
|
94 | 137 |
bool possibleLocation (Vector loc); |
69 | 138 |
|
70 | 139 |
/** |
140 |
* Use RK4 to integrate the effects of force over a time intervall. |
|
141 |
*/ |
|
85 | 142 |
void integrate(Force force, TimeMS dt); |
143 |
Derivative evaluate(Force force, TimeMS dt, Derivative &d); |
|
144 |
Vector acceleration(const Force &force); |
|
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
145 |
|
116 | 146 |
virtual void onCollision() {} |
147 |
||
70 | 148 |
public: |
149 |
Vector getPosition (void); |
|
108 | 150 |
std::vector<Vector>& getShape(void); |
151 |
void setShape (std::vector<Vector> shape); |
|
152 |
||
153 |
bool getFacing(void); |
|
154 |
float getAim(void); |
|
155 |
||
70 | 156 |
void tick (void); |
42 | 157 |
}; |
158 |
||
83
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
159 |
struct Derivative { |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
160 |
Vector dx; // Velocity |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
161 |
Vector dv; // Acceleration |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
162 |
}; |
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
163 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
164 |
|
cbba9729e92b
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents:
79
diff
changeset
|
165 |
|
42 | 166 |
#endif |