35
|
1 |
#ifndef PHYSICS_HH
|
|
2 |
#define PHYSICS_HH
|
|
3 |
|
|
4 |
#include "Vector.hh"
|
|
5 |
|
41
|
6 |
#include <vector>
|
|
7 |
#include <queue>
|
35
|
8 |
#include <ClanLib/core.h>
|
|
9 |
|
41
|
10 |
typedef uint16_t TimeMS;
|
|
11 |
|
|
12 |
const TimeMS PHYSICS_TICK_MS = 10;
|
|
13 |
|
|
14 |
enum TerrainType {EMPTY, DIRT, ROCK};
|
35
|
15 |
|
|
16 |
// forward-declare
|
|
17 |
class PhysicsObject;
|
41
|
18 |
typedef Vector Force;
|
|
19 |
struct Derivative;
|
35
|
20 |
|
|
21 |
class PhysicsWorld {
|
|
22 |
friend class PhysicsObject;
|
41
|
23 |
|
|
24 |
private:
|
|
25 |
CL_Timer tick_timer;
|
|
26 |
|
|
27 |
protected:
|
|
28 |
std::vector<PhysicsObject*> objects;
|
|
29 |
Vector gravity;
|
|
30 |
Vector dimensions;
|
35
|
31 |
|
|
32 |
|
41
|
33 |
std::vector<std::vector<TerrainType> > terrain;
|
35
|
34 |
|
41
|
35 |
CL_SlotContainer slots;
|
|
36 |
|
|
37 |
PhysicsWorld (Vector gravity, Vector dimensions);
|
35
|
38 |
|
41
|
39 |
|
|
40 |
public:
|
|
41 |
|
|
42 |
void addObject (PhysicsObject *object);
|
|
43 |
|
|
44 |
void tick (void);
|
|
45 |
void generateTerrain (int seed);
|
|
46 |
bool collided (Vector oldPos, Vector newPos);
|
|
47 |
|
|
48 |
TerrainType getType(Vector pos) const;
|
35
|
49 |
};
|
|
50 |
|
|
51 |
class PhysicsObject {
|
41
|
52 |
protected:
|
|
53 |
PhysicsWorld &world;
|
35
|
54 |
|
41
|
55 |
float mass;
|
|
56 |
Vector position;
|
|
57 |
Vector velocity;
|
|
58 |
// Whether the object (worms mainly) is in the air
|
|
59 |
// or firmly on the ground. Affects to physics.
|
|
60 |
bool inAir;
|
35
|
61 |
|
41
|
62 |
// Shape of the object.
|
|
63 |
// //TODO
|
|
64 |
std::vector<Vector> edges;
|
|
65 |
|
|
66 |
// Force queue that is emptied on every tick
|
|
67 |
std::queue<Force> forceq;
|
|
68 |
Vector posAfterTick;
|
|
69 |
Vector velAfterTick;
|
|
70 |
|
|
71 |
PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
|
35
|
72 |
|
41
|
73 |
virtual void applyForce (Force force, TimeMS dt);
|
|
74 |
void updatePhysics (Vector position, Vector velocity);
|
|
75 |
|
|
76 |
private:
|
|
77 |
void updatePosition (void);
|
35
|
78 |
|
41
|
79 |
/**
|
|
80 |
* Use RK4 to integrate the effects of force over a time intervall.
|
|
81 |
*/
|
|
82 |
void integrate(Force force, TimeMS dt);
|
|
83 |
Derivative evaluate(Force force, TimeMS dt, Derivative &d);
|
|
84 |
Vector acceleration(const Force &force);
|
35
|
85 |
|
41
|
86 |
public:
|
|
87 |
Vector getPosition (void);
|
|
88 |
|
|
89 |
void tick (void);
|
35
|
90 |
};
|
|
91 |
|
41
|
92 |
struct Derivative {
|
|
93 |
Vector dx; // Velocity
|
|
94 |
Vector dv; // Acceleration
|
|
95 |
};
|
|
96 |
|
|
97 |
|
|
98 |
|
35
|
99 |
#endif
|