-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersect.c
More file actions
291 lines (226 loc) · 8.18 KB
/
intersect.c
File metadata and controls
291 lines (226 loc) · 8.18 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Based on Nathan "noonat" Ostgard's "Intersect" CoffeeScript library
Converted into a Ruby C-extension by Amos Bieler (August, 2013)
NB: "slood" stands for "Saved Length is Out-Of-Date".
*/
#include "ruby.h"
#include <math.h>
static VALUE
m_Intersect,
c_Point,
c_Hit,
c_Sweep,
c_AABB;
/*/ Class definitions /////////////////////*/
/*/ Point class ///////////////////////////*/
/* Thanks, Pythagoras, ya kooky old Samian! */
#define LENGTH(x,y) rb_float_new( sqrt( NUM2DBL(x) * NUM2DBL(x) + NUM2DBL(y) * NUM2DBL(y) ) )
/* Set the Point's X value */
static VALUE rb_point_x_set(VALUE self, VALUE val)
{
rb_iv_set(self, "@x", val);
rb_iv_set(self, "@slood", Qtrue);
return self;
}
/* Set the Point's Y value */
static VALUE rb_point_y_set(VALUE self, VALUE val)
{
rb_iv_set(self, "@y", val);
rb_iv_set(self, "@slood", Qtrue);
return self;
}
/* Recalculate (if necessary) and return the Point's length */
static VALUE rb_point_length_get(VALUE self)
{
VALUE length;
if (rb_iv_get(self, "@slood") == Qtrue) {
length = LENGTH(rb_iv_get(self, "@x"), rb_iv_get(self, "@y"));
rb_iv_set(self, "@slood", Qfalse);
} else {
length = rb_iv_get(self, "@length");
}
rb_iv_set(self, "@length", length);
return length;
}
/* Return a copy of the Point normalized to unit length */
static VALUE rb_point_normalize(VALUE self)
{
VALUE
length = rb_point_length_get(self),
point,
inv_length;
if (length <= 0) return Qnil;
point = rb_class_new_instance(0, NULL, c_Point);
// I'm not sure all this inversion stuff is necessary...
inv_length = 1.0 / NUM2DBL(length);
rb_iv_set(point, "@x", rb_float_new(NUM2DBL(rb_iv_get(self, "@x")) * inv_length));
rb_iv_set(point, "@y", rb_float_new(NUM2DBL(rb_iv_get(self, "@y")) * inv_length));
rb_iv_set(point, "@slood", Qtrue);
return point;
}
/* Normalize the Point in place */
static VALUE rb_point_normalize_bang(VALUE self)
{
VALUE
length = rb_point_length_get(self),
inv_length;
if (length <= 0) return Qnil;
// I'm not sure all this inversion stuff is necessary...
inv_length = 1.0 / NUM2DBL(length);
rb_iv_set(self, "@x", rb_float_new(NUM2DBL(rb_iv_get(self, "@x")) * inv_length));
rb_iv_set(self, "@y", rb_float_new(NUM2DBL(rb_iv_get(self, "@y")) * inv_length));
rb_iv_set(self, "@slood", Qtrue);
return self;
}
/* Initialize a new Point instance */
static VALUE rb_point_init(int argc, VALUE* argv, VALUE self)
{
VALUE
x = INT2NUM(0),
y = INT2NUM(0);
if (argc > 2) { // there should only be 0, 1 or 2 arguments
rb_raise(rb_eArgError, "Wrong number of arguments");
} else if (argc > 0) {
// x = INT2NUM(argv[0]);
// if (argc > 1) y = INT2NUM(argv[1]);
x = argv[0];
if (argc > 1) y = argv[1];
}
rb_iv_set(self, "@x", x);
rb_iv_set(self, "@y", y);
rb_iv_set(self, "@length", LENGTH(x, y));
rb_iv_set(self, "@slood", Qfalse);
return self;
}
/*/ Hit class /////////////////////////////*/
static VALUE rb_hit_init(VALUE self)
{
rb_iv_set(self, "@pos", rb_class_new_instance(0, NULL, c_Point));
rb_iv_set(self, "@delta", rb_class_new_instance(0, NULL, c_Point));
rb_iv_set(self, "@normal", rb_class_new_instance(0, NULL, c_Point));
rb_iv_set(self, "@time", Qnil);
return self;
}
/*/ Sweep class ///////////////////////////*/
static VALUE rb_sweep_init(VALUE self)
{
rb_iv_set(self, "@hit", Qnil);
rb_iv_set(self, "@pos", rb_class_new_instance(0, NULL, c_Point));
return self;
}
/*/ AABB class ////////////////////////////*/
/* Set the AABB's "radius" */
static VALUE rb_aabb_half_set(VALUE self, VALUE val)
{
VALUE
half = Qnil,
radius[2];
radius[0] = INT2NUM(abs(NUM2INT(rb_iv_get(val, "@x"))));
radius[1] = INT2NUM(abs(NUM2INT(rb_iv_get(val, "@y"))));
half = rb_class_new_instance(2, radius, c_Point);
rb_iv_set(self, "@half", half);
return self;
}
static VALUE rb_aabb_intersect_point(VALUE self, VALUE point)
{
VALUE
hit,
dx, dy,
px, py,
sx, sy;
// Find the overlap for the X axis
dx = rb_iv_get(point, "@x") - rb_iv_get(rb_iv_get(self, "@pos"), "@x");
px = rb_iv_get(rb_iv_get(self, "@half"), "@x") - rb_funcall(dx, rb_intern("abs"), 0);
if (NUM2DBL(px) <= 0) return Qnil;
// Find the overlap for the Y axis
dy = rb_iv_get(point, "@y") - rb_iv_get(rb_iv_get(self, "@pos"), "@y");
py = rb_iv_get(rb_iv_get(self, "@half"), "@y") - rb_funcall(dy, rb_intern("abs"), 0);
if (NUM2DBL(py) <= 0) return Qnil;
// Use the axis with the smallest overlap
hit = rb_class_new_instance(0, NULL, c_Hit);
if (px < py) {
sx = rb_funcall(dx, rb_intern("<=>"), 1, INT2NUM(0));
rb_iv_set(rb_iv_get(hit, "@delta"), "@x", rb_float_new(NUM2DBL(px) * NUM2DBL(sx)));
rb_iv_set(rb_iv_get(hit, "@normal"), "@x", sx);
rb_iv_set(rb_iv_get(hit, "@pos"), "@x", NUM2DBL(rb_iv_get(rb_iv_get(self, "@pos"), "@x")) + (NUM2DBL(rb_iv_get(rb_iv_get(self, "@half"), "@x")) * NUM2DBL(sx)));
rb_iv_set(rb_iv_get(hit, "@pos"), "@y", rb_iv_get(point, "@y"));
} else {
sy = rb_funcall(dy, rb_intern("<=>"), 1, INT2NUM(0));
rb_iv_set(rb_iv_get(hit, "@delta"), "@y", rb_float_new(NUM2DBL(py) * NUM2DBL(sy)));
rb_iv_set(rb_iv_get(hit, "@normal"), "@y", sy);
rb_iv_set(rb_iv_get(hit, "@pos"), "@x", rb_iv_get(point, "@x"));
rb_iv_set(rb_iv_get(hit, "@pos"), "@y", NUM2DBL(rb_iv_get(rb_iv_get(self, "@pos"), "@y")) + (NUM2DBL(rb_iv_get(rb_iv_get(self, "@half"), "@y")) * NUM2DBL(sy)));
}
return hit;
}
static VALUE rb_aabb_intersect_segment(VALUE self, VALUE segment)
{
return Qnil;
}
static VALUE rb_aabb_intersect_aabb(VALUE self, VALUE other)
{
return Qnil;
}
static VALUE rb_aabb_sweep_aabb(VALUE self, VALUE other)
{
return Qnil;
}
static VALUE rb_aabb_init(int argc, VALUE* argv, VALUE self)
{
VALUE
pos = Qnil,
half = Qnil,
radius[2] = { INT2NUM(1), INT2NUM(1) };
if (argc > 2) { // there should only be 0, 1 or 2 arguments
rb_raise(rb_eArgError, "Wrong number of arguments");
} else if (argc > 0) {
pos = argv[0];
if (argc > 1) half = argv[1];
} else {
pos = rb_class_new_instance(0, NULL, c_Point);
half = rb_class_new_instance(2, radius, c_Point);
}
rb_iv_set(self, "@pos", pos);
rb_aabb_half_set(self, half);
return self;
}
/*/ Extension Entry Point /////////////////*/
void Init_intersect()
{
/* Define our module */
m_Intersect = rb_define_module("Intersect");
/* Define our classes */
/* A 2D point */
c_Point = rb_define_class_under(m_Intersect, "Point", rb_cObject);
rb_define_method(c_Point, "initialize", rb_point_init, -1);
rb_define_method(c_Point, "x=", rb_point_x_set, 1);
rb_define_method(c_Point, "y=", rb_point_y_set, 1);
rb_define_method(c_Point, "length", rb_point_length_get, 0);
rb_define_method(c_Point, "normalize", rb_point_normalize, 0);
rb_define_method(c_Point, "normalize!", rb_point_normalize_bang, 0);
rb_define_attr(c_Point, "x", TRUE, FALSE);
rb_define_attr(c_Point, "y", TRUE, FALSE);
/* The result of intersection tests */
c_Hit = rb_define_class_under(m_Intersect, "Hit", rb_cObject);
rb_define_method(c_Hit, "initialize", rb_hit_init, 0);
rb_define_attr(c_Hit, "pos", TRUE, TRUE); /* The point where the collision occurred */
rb_define_attr(c_Hit, "delta", TRUE, TRUE); /* The overlap for the collision */
rb_define_attr(c_Hit, "normal", TRUE, TRUE); /* The surface normal of the hit edge */
rb_define_attr(c_Hit, "time", TRUE, TRUE); /* Optional; used for segment intersection */
/* The result of sweep tests */
c_Sweep = rb_define_class_under(m_Intersect, "Sweep", rb_cObject);
rb_define_method(c_Sweep, "initialize", rb_sweep_init, 0);
rb_define_attr(c_Sweep, "hit", TRUE, TRUE); /* The collision result (if one occurred) */
rb_define_attr(c_Sweep, "pos", TRUE, TRUE); /* The farthest point prior to collision */
/* An axis-aligned bounding box */
c_AABB = rb_define_class_under(m_Intersect, "AABB", rb_cObject);
rb_define_method(c_AABB, "initialize", rb_aabb_init, -1);
rb_define_method(c_AABB, "half=", rb_aabb_half_set, 1);
/* The actual intersection tests */
rb_define_method(c_AABB, "intersect_point", rb_aabb_intersect_point, 1);
rb_define_method(c_AABB, "intersect_segment", rb_aabb_intersect_segment, 1);
rb_define_method(c_AABB, "intersect_AABB", rb_aabb_intersect_aabb, 1);
rb_define_method(c_AABB, "sweep_AABB", rb_aabb_sweep_aabb, 1);
rb_define_attr(c_AABB, "pos", TRUE, TRUE);
rb_define_attr(c_AABB, "half", TRUE, FALSE);
}