Debian Codegraph @

dokidoki-support/collision.c

raw symbols SCIP
1 #include <math.h>
2
3 #include <lua.h>
4 #include <lualib.h>
5 #include <lauxlib.h>
6
7 #define POLY_MT "collision.polygon"
8
9 typedef struct {
10 lua_Number x;
11 lua_Number y;
12 } vector_s;
13
14 typedef struct {
15 size_t vertex_count;
16 lua_Number bounding_radius;
17 vector_s vertices[1];
18 } polygon_s;
19
20 typedef struct {
21 vector_s pos;
22 vector_s facing;
23 polygon_s *poly;
24 } body_s;
25
26 static vector_s make_vector(lua_Number x, lua_Number y)
27 {
28 vector_s v;
29 v.x = x;
30 v.y = y;
31 return v;
32 }
33
34 static lua_Number vector_magnitude_squared(vector_s v)
35 {
36 return v.x * v.x + v.y * v.y;
37 }
38
39 static lua_Number vector_magnitude(vector_s v)
40 {
41 return sqrt(v.x*v.x + v.y * v.y);
42 }
43
44 static vector_s vector_neg(vector_s v)
45 {
46 return make_vector(-v.x, -v.y);
47 }
48
49 static vector_s vector_mul(vector_s v, lua_Number s)
50 {
51 return make_vector(v.x * s, v.y * s);
52 }
53
54 static lua_Number vector_dot(vector_s v1, vector_s v2)
55 {
56 return v1.x * v2.x + v1.y * v2.y;
57 }
58
59 static vector_s vector_sub(vector_s v1, vector_s v2)
60 {
61 return make_vector(v1.x - v2.x, v1.y - v2.y);
62 }
63
64 static vector_s vector_rotate_to(vector_s v, vector_s facing)
65 {
66 return make_vector(v.x * facing.x + v.y * -facing.y,
67 v.x * facing.y + v.y * facing.x);
68 }
69
70 static vector_s vector_rotate_from(vector_s v, vector_s facing)
71 {
72 return make_vector(v.x * facing.x + v.y * facing.y,
73 v.x * -facing.y + v.y * facing.x);
74 }
75
76 static int collision_native__make_polygon(lua_State *L)
77 {
78 int vertex_count = lua_objlen(L, 1) / 2;
79
80 polygon_s *poly = (polygon_s *)lua_newuserdata(
81 L, sizeof(polygon_s) + (vertex_count - 1) * sizeof(vector_s));
82 luaL_getmetatable(L, POLY_MT);
83 lua_setmetatable(L, -2);
84
85 poly->vertex_count = vertex_count;
86 poly->bounding_radius = 0;
87 int i;
88 for(i = 0; i < vertex_count; i++)
89 {
90 lua_rawgeti(L, 1, i*2+1);
91 poly->vertices[i].x = lua_tonumber(L, -1);
92 lua_rawgeti(L, 1, i*2+2);
93 poly->vertices[i].y = lua_tonumber(L, -1);
94 lua_pop(L, 2);
95 lua_Number mag = vector_magnitude(poly->vertices[i]);
96 if(poly->bounding_radius < mag)
97 poly->bounding_radius = mag;
98 }
99
100 return 1;
101 }
102
103 static lua_Number halfwidth_along_axis(vector_s axis, const polygon_s *poly)
104 {
105 lua_Number hw = 0;
106 int i;
107 for(i = 0; i < poly->vertex_count; i++)
108 {
109 lua_Number new_hw = vector_dot(poly->vertices[i], axis);
110 if(new_hw > hw) hw = new_hw;
111 }
112 return hw;
113 }
114
115 static int separate_by_axis(vector_s axis, lua_Number hw1, const body_s *body1,
116 const body_s *body2, vector_s *out)
117 {
118 //printf("axiss = %lf, %lf\n", axis.x, axis.y);
119 lua_Number overlap =
120 vector_dot(body1->pos, axis) + hw1 +
121 halfwidth_along_axis(
122 vector_neg(vector_rotate_from(axis, body2->facing)),
123 body2->poly) -
124 vector_dot(body2->pos, axis);
125 //printf("overlap = %lf - %lf + %lf - %lf = %lf\n",
126 // vector_dot(body1->pos, axis), hw1,
127 // halfwidth_along_axis(
128 // vector_neg(vector_rotate_from(axis, body2->facing)),
129 // body2->poly),
130 // vector_dot(body2->pos, axis), overlap);
131 if(overlap <= 0) return 0;
132
133 vector_s correction =
134 vector_mul(axis, overlap/vector_dot(axis, axis));
135 if(correction.x == 0 && correction.y == 0) return 0;
136
137 //printf("correction = %lf, %lf\n", correction.x, correction.y);
138 if(vector_dot(correction, correction) < vector_dot(*out, *out))
139 *out = correction;
140 return 1;
141 }
142
143 static int separate_by_axes(const body_s *body1, const body_s *body2,
144 vector_s *out)
145 {
146 polygon_s *poly1 = body1->poly;
147
148 int i, j;
149 for(i = 0, j = poly1->vertex_count-1; i < poly1->vertex_count; j = i, i++)
150 {
151 vector_s axis = vector_rotate_to(vector_sub(poly1->vertices[i], poly1->vertices[j]), make_vector(0, -1));
152 //printf("axis = %lf, %lf\n", axis.x, axis.y);
153 lua_Number hw1 = vector_dot(poly1->vertices[i], axis);
154 //printf("body1->facing = %lf, %lf\n", body1->facing.x, body1->facing.y);
155 axis = vector_rotate_to(axis, body1->facing);
156 if(!separate_by_axis(axis, hw1, body1, body2, out))
157 return 0;
158 }
159 return 1;
160 }
161
162 static int collision_native__collide(lua_State *L)
163 {
164 //printf("#### starting\n");
165 body_s body1, body2;
166
167 body1.pos.x = luaL_checknumber(L, 1);
168 body1.pos.y = luaL_checknumber(L, 2);
169 body1.facing.x = luaL_checknumber(L, 3);
170 body1.facing.y = luaL_checknumber(L, 4);
171 //printf("1body1->facing = %lf, %lf\n", body1.facing.x, body1.facing.y);
172 body1.poly = luaL_checkudata(L, 5, POLY_MT);
173 body2.pos.x = luaL_checknumber(L, 6);
174 body2.pos.y = luaL_checknumber(L, 7);
175 body2.facing.x = luaL_checknumber(L, 8);
176 body2.facing.y = luaL_checknumber(L, 9);
177 body2.poly = luaL_checkudata(L, 10, POLY_MT);
178
179 //printf("pos1 = %lf, %lf\n", body1.pos.x, body1.pos.y);
180 //printf("pos2 = %lf, %lf\n", body2.pos.x, body2.pos.y);
181
182 lua_Number bounding_distance =
183 body1.poly->bounding_radius + body2.poly->bounding_radius;
184 lua_Number distance_squared =
185 vector_magnitude_squared(vector_sub(body2.pos, body1.pos));
186 if(bounding_distance * bounding_distance < distance_squared)
187 {
188 //printf("broad phase fail\n");
189 lua_pushboolean(L, 0);
190 return 1;
191 }
192 else
193 {
194 vector_s correction = make_vector(1.0/0.0, 1.0/0.0);
195
196 if(!separate_by_axes(&body1, &body2, &correction) ||
197 !separate_by_axes(&body2, &body1, &correction))
198 {
199 //printf("fine phase fail\n");
200 lua_pushboolean(L, 0);
201 return 1;
202 }
203
204 if(vector_dot(correction, vector_sub(body2.pos, body1.pos)) > 0)
205 {
206 correction.x = -correction.x;
207 correction.y = -correction.y;
208 }
209
210 //printf("positive collision %lf, %lf\n", correction.x, correction.y);
211 lua_pushboolean(L, 1);
212 lua_pushnumber(L, correction.x);
213 lua_pushnumber(L, correction.y);
214 return 3;
215 }
216 }
217
218 static const luaL_Reg collision_native_lib[] =
219 {
220 {"make_polygon", collision_native__make_polygon},
221 {"collide", collision_native__collide},
222 {NULL, NULL}
223 };
224
225 int luaopen_collision_native(lua_State *L)
226 {
227 luaL_newmetatable(L, POLY_MT);
228
229 lua_newtable(L);
230 luaL_register(L, NULL, collision_native_lib);
231 return 1;
232 }