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
|
#ifndef physics_hpp
#define physics_hpp
#include "asset.hpp"
#include "maths.hpp"
#include <tuple>
struct Arena;
struct Line_Renderer;
struct World;
struct Manifold {
static constexpr int max_contacts = 16;
v3f contacts[max_contacts];
int contact_count;
v3f normal;
};
struct Int_Collider;
struct Physics_Debughook {
virtual void shrinkwrap(
const Int_Collider* col,
int axis,
const v2f* points,
int count
);
virtual void projection(
const Int_Collider* col,
const v3f& n,
int axis,
const v2f* points,
int count
);
};
struct Rigidbody;
struct Int_Collider {
Rigidbody* rb;
Int_Collider* next;
int vert_count;
int face_count;
v3f* verts;
v3f* faces;
bool collide(
Physics_Debughook* hook,
Arena* a,
const Int_Collider& other
) const;
struct Projection {
const Int_Collider* col;
v3f r, u;
struct Iter {
const Projection* p;
int vert;
bool equals(const Iter& other) const {
return vert == other.vert;
}
bool operator==(const Iter& other) const {
return equals(other);
}
bool operator!=(const Iter& other) const {
return !equals(other);
}
Iter operator++() {
vert++;
return *this;
}
v2f operator*();
};
Iter begin() {
return { this, 0 };
}
Iter end() {
return { this, col->vert_count };
}
};
Projection project(const v3f& axis) const;
};
struct Collider : Asset {
struct Face {
v3f normal;
int vert_count;
uint16_t* verts;
};
int face_count;
int vert_count;
Face* faces;
v3f* verts;
m3f moment;
void debug_render(
Line_Renderer& lr,
const m4f& t,
const v3f& c
);
Int_Collider* transform(const m4f& m, Arena* a) const;
};
struct Rigidbody {
v3f prev_pos, pos;
v3f vel, avel;
v3f force;
v3f torque;
v4f prev_rot, rot;
float inv_mass;
Collider* col;
int colliding;
void init(Collider* c, const v3f& p, const v4f& r, float mass);
void set_pos(const v3f& p);
void set_rot(const v4f& r);
void set_mass(float m);
void add_force(const v3f& f);
void add_force(const v3f& f, const v3f& p);
void add_impulse(const v3f& f);
void add_impulse(const v3f& f, const v3f& p);
void add_torque(const v3f& t);
};
Collider* make_box(Arena* a, const v3f& size);
void physics_update(
World& w,
Arena* a,
float ts,
Physics_Debughook* hook
);
void physics_debug(World& w, Line_Renderer& lr);
#endif
|