Debian Codegraph @

dokidoki-support/collision.c

raw symbols SCIP
1 #cxx . . $ `<file>/dokidoki-support/collision.c`/include <math.h>
2
3 #include <lua.h>
4 #include <lualib.h>
5 #include <lauxlib.h>
6
7 #define POLY_MTcxx . . $ `dokidoki-support/collision.c:7:9`! "collision.polygon"
8
9 typedef structcxx . . $ $anonymous_type_a61a911d5ecef4f1_0# {
10 lua_Numbercxx . . $ lua_Number# xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x.;
11 lua_Numbercxx . . $ lua_Number# ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.;
12 } vector_scxx . . $ vector_s#;
13
14 typedef structcxx . . $ $anonymous_type_a61a911d5ecef4f1_1# {
15 size_t vertex_countcxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertex_count.;
16 lua_Numbercxx . . $ lua_Number# bounding_radiuscxx . . $ $anonymous_type_a61a911d5ecef4f1_1#bounding_radius.;
17 vector_scxx . . $ vector_s# verticescxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertices.[1];
18 } polygon_scxx . . $ polygon_s#;
19
20 typedef structcxx . . $ $anonymous_type_a61a911d5ecef4f1_2# {
21 vector_scxx . . $ vector_s# poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos.;
22 vector_scxx . . $ vector_s# facingcxx . . $ $anonymous_type_a61a911d5ecef4f1_2#facing.;
23 polygon_scxx . . $ polygon_s# *polycxx . . $ $anonymous_type_a61a911d5ecef4f1_2#poly.;
24 } body_scxx . . $ body_s#;
25
26 static vector_scxx . . $ vector_s# make_vectorcxx . . $ make_vector(25b0948abef384f1).(lua_Numbercxx . . $ lua_Number# x, lua_Numbercxx . . $ lua_Number# y)
27 {
28 vector_scxx . . $ vector_s# v;
29 v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. = x;
30 v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. = y;
31 return v;
32 }
33
34 static lua_Numbercxx . . $ lua_Number# vector_magnitude_squaredcxx . . $ vector_magnitude_squared(eba334536d85936f).(vector_scxx . . $ vector_s# v)
35 {
36 return v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. * v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. + v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. * v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.;
37 }
38
39 static lua_Numbercxx . . $ lua_Number# vector_magnitudecxx . . $ vector_magnitude(eba334536d85936f).(vector_scxx . . $ vector_s# v)
40 {
41 return sqrt(v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x.*v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. + v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. * v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.);
42 }
43
44 static vector_scxx . . $ vector_s# vector_negcxx . . $ vector_neg(7c97b3741aa4acc1).(vector_scxx . . $ vector_s# v)
45 {
46 return make_vectorcxx . . $ make_vector(25b0948abef384f1).(-v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x., -v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.);
47 }
48
49 static vector_scxx . . $ vector_s# vector_mulcxx . . $ vector_mul(cb3a13153c8b438e).(vector_scxx . . $ vector_s# v, lua_Numbercxx . . $ lua_Number# s)
50 {
51 return make_vectorcxx . . $ make_vector(25b0948abef384f1).(v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. * s, v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. * s);
52 }
53
54 static lua_Numbercxx . . $ lua_Number# vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(vector_scxx . . $ vector_s# v1, vector_scxx . . $ vector_s# v2)
55 {
56 return v1.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. * v2.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. + v1.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. * v2.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.;
57 }
58
59 static vector_scxx . . $ vector_s# vector_subcxx . . $ vector_sub(b451b122edecf1bf).(vector_scxx . . $ vector_s# v1, vector_scxx . . $ vector_s# v2)
60 {
61 return make_vectorcxx . . $ make_vector(25b0948abef384f1).(v1.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. - v2.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x., v1.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. - v2.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.);
62 }
63
64 static vector_scxx . . $ vector_s# vector_rotate_tocxx . . $ vector_rotate_to(b451b122edecf1bf).(vector_scxx . . $ vector_s# v, vector_scxx . . $ vector_s# facing)
65 {
66 return make_vectorcxx . . $ make_vector(25b0948abef384f1).(v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. * facing.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. + v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. * -facing.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.,
67 v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. * facing.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. + v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. * facing.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x.);
68 }
69
70 static vector_scxx . . $ vector_s# vector_rotate_fromcxx . . $ vector_rotate_from(b451b122edecf1bf).(vector_scxx . . $ vector_s# v, vector_scxx . . $ vector_s# facing)
71 {
72 return make_vectorcxx . . $ make_vector(25b0948abef384f1).(v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. * facing.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. + v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. * facing.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.,
73 v.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. * -facing.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. + v.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. * facing.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x.);
74 }
75
76 static int collision_native__make_polygoncxx . . $ collision_native__make_polygon(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
77 {
78 int vertex_count = lua_objlencxx . . $ lua_objlen(6d54da957a24a0fa).(L, 1) / 2;
79
80 polygon_scxx . . $ polygon_s# *poly = (polygon_scxx . . $ polygon_s# *)lua_newuserdatacxx . . $ lua_newuserdata(bb92b2d7de290).(
81 L, sizeof(polygon_scxx . . $ polygon_s#) + (vertex_count - 1) * sizeof(vector_scxx . . $ vector_s#));
82 luaL_getmetatablecxx . . $ `<external>/18095755238171912061/lauxlib.h:117:9`!(L, POLY_MTcxx . . $ `dokidoki-support/collision.c:7:9`!);
83 lua_setmetatablecxx . . $ lua_setmetatable(95e3aa3a3b4bb934).(L, -2);
84
85 poly->vertex_countcxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertex_count. = vertex_count;
86 poly->bounding_radiuscxx . . $ $anonymous_type_a61a911d5ecef4f1_1#bounding_radius. = 0;
87 int i;
88 for(i = 0; i < vertex_count; i++)
89 {
90 lua_rawgeticxx . . $ lua_rawgeti(2c6648a7e86590d).(L, 1, i*2+1);
91 poly->verticescxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertices.[i].xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. = lua_tonumbercxx . . $ lua_tonumber(128e298513b4e6b3).(L, -1);
92 lua_rawgeticxx . . $ lua_rawgeti(2c6648a7e86590d).(L, 1, i*2+2);
93 poly->verticescxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertices.[i].ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. = lua_tonumbercxx . . $ lua_tonumber(128e298513b4e6b3).(L, -1);
94 lua_popcxx . . $ `<external>/15848857005865937740/lua.h:254:9`!(L, 2);
95 lua_Numbercxx . . $ lua_Number# mag = vector_magnitudecxx . . $ vector_magnitude(eba334536d85936f).(poly->verticescxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertices.[i]);
96 if(poly->bounding_radiuscxx . . $ $anonymous_type_a61a911d5ecef4f1_1#bounding_radius. < mag)
97 poly->bounding_radiuscxx . . $ $anonymous_type_a61a911d5ecef4f1_1#bounding_radius. = mag;
98 }
99
100 return 1;
101 }
102
103 static lua_Numbercxx . . $ lua_Number# halfwidth_along_axiscxx . . $ halfwidth_along_axis(d5f7a81c5fc14b5b).(vector_scxx . . $ vector_s# axis, const polygon_scxx . . $ polygon_s# *poly)
104 {
105 lua_Numbercxx . . $ lua_Number# hw = 0;
106 int i;
107 for(i = 0; i < poly->vertex_countcxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertex_count.; i++)
108 {
109 lua_Numbercxx . . $ lua_Number# new_hw = vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(poly->verticescxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertices.[i], axis);
110 if(new_hw > hw) hw = new_hw;
111 }
112 return hw;
113 }
114
115 static int separate_by_axiscxx . . $ separate_by_axis(d52692402233fcde).(vector_scxx . . $ vector_s# axis, lua_Numbercxx . . $ lua_Number# hw1, const body_scxx . . $ body_s# *body1,
116 const body_scxx . . $ body_s# *body2, vector_scxx . . $ vector_s# *out)
117 {
118 //printf("axiss = %lf, %lf\n", axis.x, axis.y);
119 lua_Numbercxx . . $ lua_Number# overlap =
120 vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(body1->poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos., axis) + hw1 +
121 halfwidth_along_axiscxx . . $ halfwidth_along_axis(d5f7a81c5fc14b5b).(
122 vector_negcxx . . $ vector_neg(7c97b3741aa4acc1).(vector_rotate_fromcxx . . $ vector_rotate_from(b451b122edecf1bf).(axis, body2->facingcxx . . $ $anonymous_type_a61a911d5ecef4f1_2#facing.)),
123 body2->polycxx . . $ $anonymous_type_a61a911d5ecef4f1_2#poly.) -
124 vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(body2->poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#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_scxx . . $ vector_s# correction =
134 vector_mulcxx . . $ vector_mul(cb3a13153c8b438e).(axis, overlap/vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(axis, axis));
135 if(correction.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. == 0 && correction.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. == 0) return 0;
136
137 //printf("correction = %lf, %lf\n", correction.x, correction.y);
138 if(vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(correction, correction) < vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(*out, *out))
139 *out = correction;
140 return 1;
141 }
142
143 static int separate_by_axescxx . . $ separate_by_axes(904c8b30927acae0).(const body_scxx . . $ body_s# *body1, const body_scxx . . $ body_s# *body2,
144 vector_scxx . . $ vector_s# *out)
145 {
146 polygon_scxx . . $ polygon_s# *poly1 = body1->polycxx . . $ $anonymous_type_a61a911d5ecef4f1_2#poly.;
147
148 int i, j;
149 for(i = 0, j = poly1->vertex_countcxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertex_count.-1; i < poly1->vertex_countcxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertex_count.; j = i, i++)
150 {
151 vector_scxx . . $ vector_s# axis = vector_rotate_tocxx . . $ vector_rotate_to(b451b122edecf1bf).(vector_subcxx . . $ vector_sub(b451b122edecf1bf).(poly1->verticescxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertices.[i], poly1->verticescxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertices.[j]), make_vectorcxx . . $ make_vector(25b0948abef384f1).(0, -1));
152 //printf("axis = %lf, %lf\n", axis.x, axis.y);
153 lua_Numbercxx . . $ lua_Number# hw1 = vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(poly1->verticescxx . . $ $anonymous_type_a61a911d5ecef4f1_1#vertices.[i], axis);
154 //printf("body1->facing = %lf, %lf\n", body1->facing.x, body1->facing.y);
155 axis = vector_rotate_tocxx . . $ vector_rotate_to(b451b122edecf1bf).(axis, body1->facingcxx . . $ $anonymous_type_a61a911d5ecef4f1_2#facing.);
156 if(!separate_by_axiscxx . . $ separate_by_axis(d52692402233fcde).(axis, hw1, body1, body2, out))
157 return 0;
158 }
159 return 1;
160 }
161
162 static int collision_native__collidecxx . . $ collision_native__collide(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
163 {
164 //printf("#### starting\n");
165 body_scxx . . $ body_s# body1, body2;
166
167 body1.poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos..xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 1);
168 body1.poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos..ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 2);
169 body1.facingcxx . . $ $anonymous_type_a61a911d5ecef4f1_2#facing..xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 3);
170 body1.facingcxx . . $ $anonymous_type_a61a911d5ecef4f1_2#facing..ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 4);
171 //printf("1body1->facing = %lf, %lf\n", body1.facing.x, body1.facing.y);
172 body1.polycxx . . $ $anonymous_type_a61a911d5ecef4f1_2#poly. = luaL_checkudatacxx . . $ luaL_checkudata(ded294d5354abc01).(L, 5, POLY_MTcxx . . $ `dokidoki-support/collision.c:7:9`!);
173 body2.poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos..xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 6);
174 body2.poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos..ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 7);
175 body2.facingcxx . . $ $anonymous_type_a61a911d5ecef4f1_2#facing..xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 8);
176 body2.facingcxx . . $ $anonymous_type_a61a911d5ecef4f1_2#facing..ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. = luaL_checknumbercxx . . $ luaL_checknumber(128e298513b4e6b3).(L, 9);
177 body2.polycxx . . $ $anonymous_type_a61a911d5ecef4f1_2#poly. = luaL_checkudatacxx . . $ luaL_checkudata(ded294d5354abc01).(L, 10, POLY_MTcxx . . $ `dokidoki-support/collision.c:7:9`!);
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_Numbercxx . . $ lua_Number# bounding_distance =
183 body1.polycxx . . $ $anonymous_type_a61a911d5ecef4f1_2#poly.->bounding_radiuscxx . . $ $anonymous_type_a61a911d5ecef4f1_1#bounding_radius. + body2.polycxx . . $ $anonymous_type_a61a911d5ecef4f1_2#poly.->bounding_radiuscxx . . $ $anonymous_type_a61a911d5ecef4f1_1#bounding_radius.;
184 lua_Numbercxx . . $ lua_Number# distance_squared =
185 vector_magnitude_squaredcxx . . $ vector_magnitude_squared(eba334536d85936f).(vector_subcxx . . $ vector_sub(b451b122edecf1bf).(body2.poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos., body1.poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos.));
186 if(bounding_distance * bounding_distance < distance_squared)
187 {
188 //printf("broad phase fail\n");
189 lua_pushbooleancxx . . $ lua_pushboolean(9637dd485cf22a1).(L, 0);
190 return 1;
191 }
192 else
193 {
194 vector_scxx . . $ vector_s# correction = make_vectorcxx . . $ make_vector(25b0948abef384f1).(1.0/0.0, 1.0/0.0);
195
196 if(!separate_by_axescxx . . $ separate_by_axes(904c8b30927acae0).(&body1, &body2, &correction) ||
197 !separate_by_axescxx . . $ separate_by_axes(904c8b30927acae0).(&body2, &body1, &correction))
198 {
199 //printf("fine phase fail\n");
200 lua_pushbooleancxx . . $ lua_pushboolean(9637dd485cf22a1).(L, 0);
201 return 1;
202 }
203
204 if(vector_dotcxx . . $ vector_dot(bf3a7377e795c1a0).(correction, vector_subcxx . . $ vector_sub(b451b122edecf1bf).(body2.poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos., body1.poscxx . . $ $anonymous_type_a61a911d5ecef4f1_2#pos.)) > 0)
205 {
206 correction.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x. = -correction.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x.;
207 correction.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y. = -correction.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.;
208 }
209
210 //printf("positive collision %lf, %lf\n", correction.x, correction.y);
211 lua_pushbooleancxx . . $ lua_pushboolean(9637dd485cf22a1).(L, 1);
212 lua_pushnumbercxx . . $ lua_pushnumber(7d033ffc6f828456).(L, correction.xcxx . . $ $anonymous_type_a61a911d5ecef4f1_0#x.);
213 lua_pushnumbercxx . . $ lua_pushnumber(7d033ffc6f828456).(L, correction.ycxx . . $ $anonymous_type_a61a911d5ecef4f1_0#y.);
214 return 3;
215 }
216 }
217
218 static const luaL_Regcxx . . $ luaL_Reg# collision_native_libcxx . . $ collision_native_lib.[] =
219 {
220 {"make_polygon", collision_native__make_polygoncxx . . $ collision_native__make_polygon(e4b172c3fcff213).},
221 {"collide", collision_native__collidecxx . . $ collision_native__collide(e4b172c3fcff213).},
222 {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!}
223 };
224
225 int luaopen_collision_nativecxx . . $ luaopen_collision_native(e4b172c3fcff213).(lua_Statecxx . . $ lua_State# *L)
226 {
227 luaL_newmetatablecxx . . $ luaL_newmetatable(ac61bb1a0c4e7605).(L, POLY_MTcxx . . $ `dokidoki-support/collision.c:7:9`!);
228
229 lua_newtablecxx . . $ `<external>/15848857005865937740/lua.h:256:9`!(L);
230 luaL_registercxx . . $ luaL_register(78d27bf8f6419b9e).(L, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, collision_native_libcxx . . $ collision_native_lib.);
231 return 1;
232 }