-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathphys.h
163 lines (150 loc) · 4.33 KB
/
phys.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef _PHYS_H_INCLUDED_
#define _PHYS_H_INCLUDED_
#include <map>
#include <set>
#include <vector>
#include "material.h"
#include "scheduler.h"
#include "vec.h"
namespace phys
{
class point; class spring; struct ship; class game; struct AABB; struct BVHNode;
class world
{
friend class point;
friend class spring;
friend class ship;
struct springCalculateTask;
struct pointIntegrateTask;
scheduler springScheduler;
std::vector <point*> points;
std::vector <spring*> springs;
std::vector <ship*> ships;
BVHNode *collisionTree;
float waterheight(float x);
float oceanfloorheight(float x);
void doSprings(double dt);
vec2 gravity;
void buildBVHTree(bool splitInX, std::vector<point*> &pointlist, BVHNode *thisnode, int depth = 1);
public:
float *oceandepthbuffer;
float buoyancy;
float strength;
float waterpressure;
float waveheight;
float seadepth;
bool showstress;
bool quickwaterfix;
bool xraymode;
float time;
void update(double dt);
void render(double left, double right, double bottom, double top);
void renderLand(double left, double right, double bottom, double top);
void renderWater(double left, double right, double bottom, double top);
void destroyAt(vec2 pos);
void drawTo(vec2 target);
world(vec2 _gravity = vec2(0, -9.8), double _buoyancy = 4, double _strength = 0.01);
~world();
};
struct world::springCalculateTask: scheduler::task
{
springCalculateTask(world *_wld, int _first, int _last);
world *wld;
int first, last;
virtual void process();
};
struct world::pointIntegrateTask: scheduler::task
{
pointIntegrateTask(world *_wld, int _first, int _last, float _dt);
world *wld;
float dt;
int first, last;
virtual void process();
};
struct ship
{
world *wld;
struct triangle {
ship *parent;
point *a, *b, *c;
triangle(phys::ship *_parent, point *_a, point *_b, point *_c);
~triangle();
};
std::set<point*> points;
std::set<spring*> springs;
std::map<point*, std::set<point*> > adjacentnodes;
std::set<triangle*> triangles;
void render();
void leakWater(double dt);
void gravitateWater(double dt);
void balancePressure(double dt);
ship(world *_parent);
~ship();
void update(double dt);
};
class point
{
world *wld;
friend class spring;
friend class world;
friend class ship;
static const float radius = 0.4f;
vec2 pos;
vec2 lastpos;
vec2 force;
double buoyancy;
double water;
double getPressure();
public:
std::set<ship::triangle*> tris;
material *mtl;
bool isLeaking;
point(world *_parent, vec2 _pos, material *_mtl, double _buoyancy);
~point();
void applyForce(vec2 f);
void breach(); // set to leaking and remove any incident triangles
void update(double dt);
vec2 getPos();
vec3f getColour(vec3f basecolour);
AABB getAABB();
void render();
};
class spring
{
friend class world;
friend class point;
friend class ship;
world *wld;
point *a, *b;
double length;
material *mtl;
public:
spring(world *_parent, point *_a, point *_b, material *_mtl, double _length = -1);
~spring();
void update();
void damping(float amount);
void render(bool isStressed = false);
bool isStressed();
bool isBroken();
};
struct AABB
{
vec2 bottomleft, topright;
AABB() {}
AABB(vec2 _bottomleft, vec2 _topright);
void extendTo(AABB other);
void render();
};
struct BVHNode
{
AABB volume;
BVHNode *l, *r;
bool isLeaf;
int pointCount;
static const int MAX_DEPTH = 15;
static const int MAX_N_POINTS = 10;
point* points[MAX_N_POINTS];
static BVHNode *allocateTree(int depth = MAX_DEPTH);
};
}
#endif // _PHYS_H_INCLUDED_