chipmunk 4.1.0-x86-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ /* Copyright (c) 2007 Scott Lembcke
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+ #include "chipmunk.h"
23
+
24
+ #include "ruby.h"
25
+ #include "rb_chipmunk.h"
26
+
27
+ VALUE m_cpJoint;
28
+
29
+ static VALUE
30
+ rb_cpPinJointAlloc(VALUE klass)
31
+ {
32
+ cpPinJoint *joint = cpPinJointAlloc();
33
+ VALUE self = Data_Wrap_Struct(klass, NULL, cpJointFree, joint);
34
+
35
+ return self;
36
+ }
37
+
38
+ static VALUE
39
+ rb_cpPinJointInit(VALUE self, VALUE a, VALUE b, VALUE anchr1, VALUE anchr2)
40
+ {
41
+ cpPinJoint *joint = (cpPinJoint *)JOINT(self);
42
+ cpPinJointInit(joint, BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2));
43
+ rb_iv_set(self, "body_a", a);
44
+ rb_iv_set(self, "body_b", b);
45
+
46
+ return self;
47
+ }
48
+
49
+
50
+ static VALUE
51
+ rb_cpSlideJointAlloc(VALUE klass)
52
+ {
53
+ cpSlideJoint *joint = cpSlideJointAlloc();
54
+ VALUE self = Data_Wrap_Struct(klass, NULL, cpJointFree, joint);
55
+
56
+ return self;
57
+ }
58
+
59
+ static VALUE
60
+ rb_cpSlideJointInit(VALUE self, VALUE a, VALUE b, VALUE anchr1, VALUE anchr2, VALUE min, VALUE max)
61
+ {
62
+ cpSlideJoint *joint = (cpSlideJoint *)JOINT(self);
63
+ cpSlideJointInit(joint, BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2), NUM2DBL(min), NUM2DBL(max));
64
+ rb_iv_set(self, "body_a", a);
65
+ rb_iv_set(self, "body_b", b);
66
+
67
+ return self;
68
+ }
69
+
70
+
71
+ static VALUE
72
+ rb_cpPivotJointAlloc(VALUE klass)
73
+ {
74
+ cpPivotJoint *joint = cpPivotJointAlloc();
75
+ VALUE self = Data_Wrap_Struct(klass, NULL, cpJointFree, joint);
76
+
77
+ return self;
78
+ }
79
+
80
+ static VALUE
81
+ rb_cpPivotJointInit(VALUE self, VALUE a, VALUE b, VALUE pivot)
82
+ {
83
+ cpPivotJoint *joint = (cpPivotJoint *)JOINT(self);
84
+ cpPivotJointInit(joint, BODY(a), BODY(b), *VGET(pivot));
85
+ rb_iv_set(self, "body_a", a);
86
+ rb_iv_set(self, "body_b", b);
87
+
88
+ return self;
89
+ }
90
+
91
+
92
+ static VALUE
93
+ rb_cpGrooveJointAlloc(VALUE klass)
94
+ {
95
+ cpGrooveJoint *joint = cpGrooveJointAlloc();
96
+ VALUE self = Data_Wrap_Struct(klass, NULL, cpJointFree, joint);
97
+
98
+ return self;
99
+ }
100
+
101
+ static VALUE
102
+ rb_cpGrooveJointInit(VALUE self, VALUE a, VALUE b, VALUE grv_a, VALUE grv_b, VALUE anchr2)
103
+ {
104
+ cpGrooveJoint *joint = (cpGrooveJoint *)JOINT(self);
105
+ cpGrooveJointInit(joint, BODY(a), BODY(b), *VGET(grv_a), *VGET(grv_b), *VGET(anchr2));
106
+ rb_iv_set(self, "body_a", a);
107
+ rb_iv_set(self, "body_b", b);
108
+
109
+ return self;
110
+ }
111
+
112
+ void
113
+ Init_cpJoint(void)
114
+ {
115
+ m_cpJoint = rb_define_module_under(m_Chipmunk, "Joint");
116
+
117
+ VALUE c_cpPinJoint = rb_define_class_under(m_cpJoint, "Pin", rb_cObject);
118
+ rb_include_module(c_cpPinJoint, m_cpJoint);
119
+ rb_define_alloc_func(c_cpPinJoint, rb_cpPinJointAlloc);
120
+ rb_define_method(c_cpPinJoint, "initialize", rb_cpPinJointInit, 4);
121
+
122
+ VALUE c_cpSlideJoint = rb_define_class_under(m_cpJoint, "Slide", rb_cObject);
123
+ rb_include_module(c_cpSlideJoint, m_cpJoint);
124
+ rb_define_alloc_func(c_cpSlideJoint, rb_cpSlideJointAlloc);
125
+ rb_define_method(c_cpSlideJoint, "initialize", rb_cpSlideJointInit, 6);
126
+
127
+ VALUE c_cpPivotJoint = rb_define_class_under(m_cpJoint, "Pivot", rb_cObject);
128
+ rb_include_module(c_cpPivotJoint, m_cpJoint);
129
+ rb_define_alloc_func(c_cpPivotJoint, rb_cpPivotJointAlloc);
130
+ rb_define_method(c_cpPivotJoint, "initialize", rb_cpPivotJointInit, 3);
131
+
132
+ VALUE c_cpGrooveJoint = rb_define_class_under(m_cpJoint, "Groove", rb_cObject);
133
+ rb_include_module(c_cpGrooveJoint, m_cpJoint);
134
+ rb_define_alloc_func(c_cpGrooveJoint, rb_cpGrooveJointAlloc);
135
+ rb_define_method(c_cpGrooveJoint, "initialize", rb_cpGrooveJointInit, 5);
136
+ }
@@ -0,0 +1,292 @@
1
+ /* Copyright (c) 2007 Scott Lembcke
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+ #include "chipmunk.h"
23
+
24
+ #include "ruby.h"
25
+ #include "rb_chipmunk.h"
26
+
27
+ ID id_body;
28
+
29
+ VALUE m_cpShape;
30
+ VALUE c_cpCircleShape;
31
+ VALUE c_cpSegmentShape;
32
+ VALUE c_cpPolyShape;
33
+
34
+
35
+
36
+ static VALUE
37
+ rb_cpShapeGetBody(VALUE self)
38
+ {
39
+ return rb_ivar_get(self, id_body);
40
+ }
41
+
42
+ static VALUE
43
+ rb_cpShapeSetBody(VALUE self, VALUE body)
44
+ {
45
+ SHAPE(self)->body = BODY(body);
46
+ rb_ivar_set(self, id_body, body);
47
+
48
+ return body;
49
+ }
50
+
51
+ static VALUE
52
+ rb_cpShapeGetCollType(VALUE self)
53
+ {
54
+ return rb_iv_get(self, "collType");
55
+ }
56
+
57
+ static VALUE
58
+ rb_cpShapeSetCollType(VALUE self, VALUE val)
59
+ {
60
+ VALUE col_type = rb_obj_id(val);
61
+ rb_iv_set(self, "collType", val);
62
+ SHAPE(self)->collision_type = NUM2UINT(col_type);
63
+
64
+ return val;
65
+ }
66
+
67
+ static VALUE
68
+ rb_cpShapeGetGroup(VALUE self)
69
+ {
70
+ return rb_iv_get(self, "group");
71
+ }
72
+
73
+ static VALUE
74
+ rb_cpShapeSetGroup(VALUE self, VALUE val)
75
+ {
76
+ VALUE col_type = rb_obj_id(val);
77
+ rb_iv_set(self, "group", val);
78
+ SHAPE(self)->group = NUM2UINT(col_type);
79
+
80
+ return val;
81
+ }
82
+
83
+ static VALUE
84
+ rb_cpShapeGetLayers(VALUE self)
85
+ {
86
+ return UINT2NUM(SHAPE(self)->layers);
87
+ }
88
+
89
+ static VALUE
90
+ rb_cpShapeSetLayers(VALUE self, VALUE layers)
91
+ {
92
+ SHAPE(self)->layers = NUM2UINT(layers);
93
+
94
+ return layers;
95
+ }
96
+
97
+ static VALUE
98
+ rb_cpShapeGetBB(VALUE self)
99
+ {
100
+ cpBB *bb = malloc(sizeof(cpBB));
101
+ *bb = SHAPE(self)->bb;
102
+ return Data_Wrap_Struct(c_cpBB, NULL, free, bb);
103
+ }
104
+
105
+ static VALUE
106
+ rb_cpShapeCacheBB(VALUE self)
107
+ {
108
+ cpShape *shape = SHAPE(self);
109
+ cpShapeCacheBB(shape);
110
+
111
+ return rb_cpShapeGetBB(self);
112
+ }
113
+
114
+ static VALUE
115
+ rb_cpShapeGetElasticity(VALUE self)
116
+ {
117
+ return rb_float_new(SHAPE(self)->e);
118
+ }
119
+
120
+ static VALUE
121
+ rb_cpShapeGetFriction(VALUE self)
122
+ {
123
+ return rb_float_new(SHAPE(self)->u);
124
+ }
125
+
126
+ static VALUE
127
+ rb_cpShapeSetElasticity(VALUE self, VALUE val)
128
+ {
129
+ SHAPE(self)->e = NUM2DBL(val);
130
+ return val;
131
+ }
132
+
133
+ static VALUE
134
+ rb_cpShapeSetFriction(VALUE self, VALUE val)
135
+ {
136
+ SHAPE(self)->u = NUM2DBL(val);
137
+ return val;
138
+ }
139
+
140
+ static VALUE
141
+ rb_cpShapeGetSurfaceV(VALUE self)
142
+ {
143
+ return VWRAP(self, &SHAPE(self)->surface_v);
144
+ }
145
+
146
+ static VALUE
147
+ rb_cpShapeSetSurfaceV(VALUE self, VALUE val)
148
+ {
149
+ SHAPE(self)->surface_v = *VGET(val);
150
+ return val;
151
+ }
152
+
153
+ static VALUE
154
+ rb_cpShapeResetIdCounter(VALUE self)
155
+ {
156
+ cpResetShapeIdCounter();
157
+ return Qnil;
158
+ }
159
+
160
+
161
+
162
+ //cpCircle
163
+ static VALUE
164
+ rb_cpCircleAlloc(VALUE klass)
165
+ {
166
+ cpCircleShape *circle = cpCircleShapeAlloc();
167
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, circle);
168
+ }
169
+
170
+ static VALUE
171
+ rb_cpCircleInitialize(VALUE self, VALUE body, VALUE radius, VALUE offset)
172
+ {
173
+ cpCircleShape *circle = (cpCircleShape *)SHAPE(self);
174
+
175
+ cpCircleShapeInit(circle, BODY(body), NUM2DBL(radius), *VGET(offset));
176
+ circle->shape.data = (void *)self;
177
+ circle->shape.collision_type = Qnil;
178
+
179
+ rb_ivar_set(self, id_body, body);
180
+
181
+ return self;
182
+ }
183
+
184
+
185
+
186
+ //cpSegment
187
+ static VALUE
188
+ rb_cpSegmentAlloc(VALUE klass)
189
+ {
190
+ cpSegmentShape *seg = cpSegmentShapeAlloc();
191
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, seg);
192
+ }
193
+
194
+ static VALUE
195
+ rb_cpSegmentInitialize(VALUE self, VALUE body, VALUE a, VALUE b, VALUE r)
196
+ {
197
+ cpSegmentShape *seg = (cpSegmentShape *)SHAPE(self);
198
+
199
+ cpSegmentShapeInit(seg, BODY(body), *VGET(a), *VGET(b), NUM2DBL(r));
200
+ seg->shape.data = (void *)self;
201
+ seg->shape.collision_type = Qnil;
202
+
203
+ rb_ivar_set(self, id_body, body);
204
+
205
+ return self;
206
+ }
207
+
208
+
209
+
210
+ //cpPoly
211
+ static VALUE
212
+ rb_cpPolyAlloc(VALUE klass)
213
+ {
214
+ cpPolyShape *poly = cpPolyShapeAlloc();
215
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, poly);
216
+ }
217
+
218
+ static VALUE
219
+ rb_cpPolyInitialize(VALUE self, VALUE body, VALUE arr, VALUE offset)
220
+ {
221
+ cpPolyShape *poly = (cpPolyShape *)SHAPE(self);
222
+
223
+ Check_Type(arr, T_ARRAY);
224
+ int numVerts = RARRAY_LEN(arr);
225
+ cpVect verts[numVerts];
226
+
227
+ for(int i=0; i<numVerts; i++)
228
+ verts[i] = *VGET(RARRAY_PTR(arr)[i]);
229
+
230
+ cpPolyShapeInit(poly, BODY(body), numVerts, verts, *VGET(offset));
231
+ poly->shape.data = (void *)self;
232
+ poly->shape.collision_type = Qnil;
233
+
234
+ rb_ivar_set(self, id_body, body);
235
+
236
+ return self;
237
+ }
238
+
239
+
240
+
241
+ void
242
+ Init_cpShape(void)
243
+ {
244
+ id_body = rb_intern("body");
245
+
246
+ m_cpShape = rb_define_module_under(m_Chipmunk, "Shape");
247
+ rb_define_attr(m_cpShape, "obj", 1, 1);
248
+
249
+ rb_define_method(m_cpShape, "body", rb_cpShapeGetBody, 0);
250
+ rb_define_method(m_cpShape, "body=", rb_cpShapeSetBody, 1);
251
+
252
+ rb_define_method(m_cpShape, "collision_type", rb_cpShapeGetCollType, 0);
253
+ rb_define_method(m_cpShape, "collision_type=", rb_cpShapeSetCollType, 1);
254
+
255
+ rb_define_method(m_cpShape, "group", rb_cpShapeGetGroup, 0);
256
+ rb_define_method(m_cpShape, "group=", rb_cpShapeSetGroup, 1);
257
+
258
+ rb_define_method(m_cpShape, "layers", rb_cpShapeGetLayers, 0);
259
+ rb_define_method(m_cpShape, "layers=", rb_cpShapeSetLayers, 1);
260
+
261
+ rb_define_method(m_cpShape, "bb", rb_cpShapeGetBB, 0);
262
+ rb_define_method(m_cpShape, "cache_bb", rb_cpShapeCacheBB, 0);
263
+
264
+ rb_define_method(m_cpShape, "e", rb_cpShapeGetElasticity, 0);
265
+ rb_define_method(m_cpShape, "u", rb_cpShapeGetFriction, 0);
266
+
267
+ rb_define_method(m_cpShape, "e=", rb_cpShapeSetElasticity, 1);
268
+ rb_define_method(m_cpShape, "u=", rb_cpShapeSetFriction, 1);
269
+
270
+ rb_define_method(m_cpShape, "surface_v", rb_cpShapeGetSurfaceV, 0);
271
+ rb_define_method(m_cpShape, "surface_v=", rb_cpShapeSetSurfaceV, 1);
272
+
273
+ rb_define_singleton_method(m_cpShape, "reset_id_counter", rb_cpShapeResetIdCounter, 0);
274
+
275
+
276
+ c_cpCircleShape = rb_define_class_under(m_cpShape, "Circle", rb_cObject);
277
+ rb_include_module(c_cpCircleShape, m_cpShape);
278
+ rb_define_alloc_func(c_cpCircleShape, rb_cpCircleAlloc);
279
+ rb_define_method(c_cpCircleShape, "initialize", rb_cpCircleInitialize, 3);
280
+
281
+
282
+ c_cpSegmentShape = rb_define_class_under(m_cpShape, "Segment", rb_cObject);
283
+ rb_include_module(c_cpSegmentShape, m_cpShape);
284
+ rb_define_alloc_func(c_cpSegmentShape, rb_cpSegmentAlloc);
285
+ rb_define_method(c_cpSegmentShape, "initialize", rb_cpSegmentInitialize, 4);
286
+
287
+
288
+ c_cpPolyShape = rb_define_class_under(m_cpShape, "Poly", rb_cObject);
289
+ rb_include_module(c_cpPolyShape, m_cpShape);
290
+ rb_define_alloc_func(c_cpPolyShape, rb_cpPolyAlloc);
291
+ rb_define_method(c_cpPolyShape, "initialize", rb_cpPolyInitialize, 3);
292
+ }
@@ -0,0 +1,330 @@
1
+ /* Copyright (c) 2007 Scott Lembcke
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+ #include "chipmunk.h"
23
+
24
+ #include "ruby.h"
25
+ #include "rb_chipmunk.h"
26
+
27
+ ID id_call;
28
+
29
+
30
+ VALUE c_cpSpace;
31
+
32
+ static VALUE
33
+ rb_cpSpaceAlloc(VALUE klass)
34
+ {
35
+ cpSpace *space = cpSpaceAlloc();
36
+ return Data_Wrap_Struct(klass, NULL, cpSpaceFree, space);
37
+ }
38
+
39
+ static VALUE
40
+ rb_cpSpaceInitialize(VALUE self)
41
+ {
42
+ cpSpace *space = SPACE(self);
43
+ cpSpaceInit(space);
44
+
45
+ // These might as well be in one shared hash.
46
+ rb_iv_set(self, "static_shapes", rb_ary_new());
47
+ rb_iv_set(self, "active_shapes", rb_ary_new());
48
+ rb_iv_set(self, "bodies", rb_ary_new());
49
+ rb_iv_set(self, "joints", rb_ary_new());
50
+ rb_iv_set(self, "blocks", rb_hash_new());
51
+
52
+ return self;
53
+ }
54
+
55
+ static VALUE
56
+ rb_cpSpaceGetIterations(VALUE self)
57
+ {
58
+ return INT2NUM(SPACE(self)->iterations);
59
+ }
60
+
61
+ static VALUE
62
+ rb_cpSpaceSetIterations(VALUE self, VALUE val)
63
+ {
64
+ SPACE(self)->iterations = NUM2INT(val);
65
+ return val;
66
+ }
67
+
68
+ static VALUE
69
+ rb_cpSpaceGetElasticIterations(VALUE self)
70
+ {
71
+ return INT2NUM(SPACE(self)->elasticIterations);
72
+ }
73
+
74
+ static VALUE
75
+ rb_cpSpaceSetElasticIterations(VALUE self, VALUE val)
76
+ {
77
+ SPACE(self)->elasticIterations = NUM2INT(val);
78
+ return val;
79
+ }
80
+
81
+ static VALUE
82
+ rb_cpSpaceGetDamping(VALUE self)
83
+ {
84
+ return rb_float_new(SPACE(self)->damping);
85
+ }
86
+
87
+ static VALUE
88
+ rb_cpSpaceSetDamping(VALUE self, VALUE val)
89
+ {
90
+ SPACE(self)->damping = NUM2DBL(val);
91
+ return val;
92
+ }
93
+
94
+ static VALUE
95
+ rb_cpSpaceGetGravity(VALUE self)
96
+ {
97
+ return VWRAP(self, &SPACE(self)->gravity);
98
+ }
99
+
100
+ static VALUE
101
+ rb_cpSpaceSetGravity(VALUE self, VALUE val)
102
+ {
103
+ SPACE(self)->gravity = *VGET(val);
104
+ return val;
105
+ }
106
+
107
+ static int
108
+ collisionCallback(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
109
+ {
110
+ VALUE block = (VALUE)data;
111
+ VALUE shapea = (VALUE)a->data;
112
+ VALUE shapeb = (VALUE)b->data;
113
+
114
+ return rb_funcall(block, id_call, 2, shapea, shapeb);
115
+ }
116
+
117
+ static VALUE
118
+ rb_cpSpaceAddCollisionFunc(int argc, VALUE *argv, VALUE self)
119
+ {
120
+ VALUE a, b, block;
121
+ rb_scan_args(argc, argv, "20&", &a, &b, &block);
122
+
123
+ VALUE id_a = rb_obj_id(a);
124
+ VALUE id_b = rb_obj_id(b);
125
+ if(NIL_P(block)) {
126
+ cpSpaceAddCollisionPairFunc(SPACE(self), NUM2UINT(id_a), NUM2UINT(id_b),
127
+ NULL, NULL);
128
+ } else {
129
+ cpSpaceAddCollisionPairFunc(SPACE(self), NUM2UINT(id_a), NUM2UINT(id_b),
130
+ collisionCallback, (void *)block);
131
+ }
132
+
133
+ VALUE blocks = rb_iv_get(self, "blocks");
134
+ rb_hash_aset(blocks, rb_ary_new3(2, id_a, id_b), block);
135
+
136
+ return Qnil;
137
+ }
138
+
139
+ static VALUE
140
+ rb_cpSpaceRemoveCollisionFunc(VALUE self, VALUE a, VALUE b)
141
+ {
142
+ VALUE id_a = rb_obj_id(a);
143
+ VALUE id_b = rb_obj_id(b);
144
+ cpSpaceRemoveCollisionPairFunc(SPACE(self), NUM2UINT(id_a), NUM2UINT(id_b));
145
+
146
+ VALUE blocks = rb_iv_get(self, "blocks");
147
+ rb_hash_delete(blocks, rb_ary_new3(2, id_a, id_b));
148
+
149
+ return Qnil;
150
+ }
151
+
152
+ static VALUE
153
+ rb_cpSpaceSetDefaultCollisionFunc(int argc, VALUE *argv, VALUE self)
154
+ {
155
+ VALUE block;
156
+ rb_scan_args(argc, argv, "00&", &block);
157
+
158
+ if(NIL_P(block)) {
159
+ cpSpaceSetDefaultCollisionPairFunc(SPACE(self), NULL, NULL);
160
+ } else {
161
+ cpSpaceSetDefaultCollisionPairFunc(SPACE(self), collisionCallback, (void *)block);
162
+ }
163
+
164
+ rb_hash_aset(rb_iv_get(self, "blocks"), ID2SYM(rb_intern("default")), block);
165
+
166
+ return Qnil;
167
+ }
168
+
169
+ static VALUE
170
+ rb_cpSpaceAddShape(VALUE self, VALUE shape)
171
+ {
172
+ cpSpaceAddShape(SPACE(self), SHAPE(shape));
173
+ rb_ary_push(rb_iv_get(self, "active_shapes"), shape);
174
+ return shape;
175
+ }
176
+
177
+ static VALUE
178
+ rb_cpSpaceAddStaticShape(VALUE self, VALUE shape)
179
+ {
180
+ cpSpaceAddStaticShape(SPACE(self), SHAPE(shape));
181
+ rb_ary_push(rb_iv_get(self, "static_shapes"), shape);
182
+ return shape;
183
+ }
184
+
185
+ static VALUE
186
+ rb_cpSpaceAddBody(VALUE self, VALUE body)
187
+ {
188
+ cpSpaceAddBody(SPACE(self), BODY(body));
189
+ rb_ary_push(rb_iv_get(self, "bodies"), body);
190
+ return body;
191
+ }
192
+
193
+ static VALUE
194
+ rb_cpSpaceAddJoint(VALUE self, VALUE joint)
195
+ {
196
+ cpSpaceAddJoint(SPACE(self), JOINT(joint));
197
+ rb_ary_push(rb_iv_get(self, "joints"), joint);
198
+ return joint;
199
+ }
200
+
201
+ static VALUE
202
+ rb_cpSpaceRemoveShape(VALUE self, VALUE shape)
203
+ {
204
+ cpSpaceRemoveShape(SPACE(self), SHAPE(shape));
205
+ return rb_ary_delete(rb_iv_get(self, "active_shapes"), shape);
206
+ }
207
+
208
+ static VALUE
209
+ rb_cpSpaceRemoveStaticShape(VALUE self, VALUE shape)
210
+ {
211
+ cpSpaceRemoveStaticShape(SPACE(self), SHAPE(shape));
212
+ return rb_ary_delete(rb_iv_get(self, "static_shapes"), shape);
213
+ }
214
+
215
+ static VALUE
216
+ rb_cpSpaceRemoveBody(VALUE self, VALUE body)
217
+ {
218
+ cpSpaceRemoveBody(SPACE(self), BODY(body));
219
+ return rb_ary_delete(rb_iv_get(self, "bodies"), body);
220
+ }
221
+
222
+ static VALUE
223
+ rb_cpSpaceRemoveJoint(VALUE self, VALUE joint)
224
+ {
225
+ cpSpaceRemoveJoint(SPACE(self), JOINT(joint));
226
+ return rb_ary_delete(rb_iv_get(self, "joints"), joint);
227
+ }
228
+
229
+ static VALUE
230
+ rb_cpSpaceResizeStaticHash(VALUE self, VALUE dim, VALUE count)
231
+ {
232
+ cpSpaceResizeStaticHash(SPACE(self), NUM2DBL(dim), NUM2INT(count));
233
+ return Qnil;
234
+ }
235
+
236
+ static VALUE
237
+ rb_cpSpaceResizeActiveHash(VALUE self, VALUE dim, VALUE count)
238
+ {
239
+ cpSpaceResizeActiveHash(SPACE(self), NUM2DBL(dim), NUM2INT(count));
240
+ return Qnil;
241
+ }
242
+
243
+ static VALUE
244
+ rb_cpSpaceRehashStatic(VALUE self)
245
+ {
246
+ cpSpaceRehashStatic(SPACE(self));
247
+ return Qnil;
248
+ }
249
+
250
+ static void
251
+ pointQueryHelper(cpShape *shape, void *block)
252
+ {
253
+ rb_funcall((VALUE)block, id_call, 1, (VALUE)shape->data);
254
+ }
255
+
256
+ static VALUE
257
+ rb_cpSpaceShapePointQuery(int argc, VALUE *argv, VALUE self)
258
+ {
259
+ VALUE point, block;
260
+ rb_scan_args(argc, argv, "10&", &point, &block);
261
+
262
+ cpSpaceShapePointQuery(SPACE(self), *VGET(point), pointQueryHelper, (void *)block);
263
+
264
+ return Qnil;
265
+ }
266
+
267
+ static VALUE
268
+ rb_cpSpaceStaticShapePointQuery(int argc, VALUE *argv, VALUE self)
269
+ {
270
+ VALUE point, block;
271
+ rb_scan_args(argc, argv, "10&", &point, &block);
272
+
273
+ cpSpaceStaticShapePointQuery(SPACE(self), *VGET(point), pointQueryHelper, (void *)block);
274
+
275
+ return Qnil;
276
+ }
277
+
278
+ static VALUE
279
+ rb_cpSpaceStep(VALUE self, VALUE dt)
280
+ {
281
+ cpSpaceStep(SPACE(self), NUM2DBL(dt));
282
+ return Qnil;
283
+ }
284
+
285
+
286
+
287
+ void
288
+ Init_cpSpace(void)
289
+ {
290
+ id_call = rb_intern("call");
291
+
292
+ c_cpSpace = rb_define_class_under(m_Chipmunk, "Space", rb_cObject);
293
+ rb_define_alloc_func(c_cpSpace, rb_cpSpaceAlloc);
294
+ rb_define_method(c_cpSpace, "initialize", rb_cpSpaceInitialize, 0);
295
+
296
+ rb_define_method(c_cpSpace, "iterations", rb_cpSpaceGetIterations, 0);
297
+ rb_define_method(c_cpSpace, "iterations=", rb_cpSpaceSetIterations, 1);
298
+
299
+ rb_define_method(c_cpSpace, "elastic_iterations", rb_cpSpaceGetElasticIterations, 0);
300
+ rb_define_method(c_cpSpace, "elastic_iterations=", rb_cpSpaceSetElasticIterations, 1);
301
+
302
+ rb_define_method(c_cpSpace, "damping", rb_cpSpaceGetDamping, 0);
303
+ rb_define_method(c_cpSpace, "damping=", rb_cpSpaceSetDamping, 1);
304
+
305
+ rb_define_method(c_cpSpace, "gravity", rb_cpSpaceGetGravity, 0);
306
+ rb_define_method(c_cpSpace, "gravity=", rb_cpSpaceSetGravity, 1);
307
+
308
+ rb_define_method(c_cpSpace, "add_collision_func", rb_cpSpaceAddCollisionFunc, -1);
309
+ rb_define_method(c_cpSpace, "remove_collision_func", rb_cpSpaceRemoveCollisionFunc, 2);
310
+ rb_define_method(c_cpSpace, "set_default_collision_func", rb_cpSpaceSetDefaultCollisionFunc, -1);
311
+
312
+ rb_define_method(c_cpSpace, "add_shape", rb_cpSpaceAddShape, 1);
313
+ rb_define_method(c_cpSpace, "add_static_shape", rb_cpSpaceAddStaticShape, 1);
314
+ rb_define_method(c_cpSpace, "add_body", rb_cpSpaceAddBody, 1);
315
+ rb_define_method(c_cpSpace, "add_joint", rb_cpSpaceAddJoint, 1);
316
+
317
+ rb_define_method(c_cpSpace, "remove_shape", rb_cpSpaceRemoveShape, 1);
318
+ rb_define_method(c_cpSpace, "remove_static_shape", rb_cpSpaceRemoveStaticShape, 1);
319
+ rb_define_method(c_cpSpace, "remove_body", rb_cpSpaceRemoveBody, 1);
320
+ rb_define_method(c_cpSpace, "remove_joint", rb_cpSpaceRemoveJoint, 1);
321
+
322
+ rb_define_method(c_cpSpace, "resize_static_hash", rb_cpSpaceResizeStaticHash, 2);
323
+ rb_define_method(c_cpSpace, "resize_active_hash", rb_cpSpaceResizeActiveHash, 2);
324
+ rb_define_method(c_cpSpace, "rehash_static", rb_cpSpaceRehashStatic, 0);
325
+
326
+ rb_define_method(c_cpSpace, "shape_point_query", rb_cpSpaceShapePointQuery, -1);
327
+ rb_define_method(c_cpSpace, "static_shape_point_query", rb_cpSpaceStaticShapePointQuery, -1);
328
+
329
+ rb_define_method(c_cpSpace, "step", rb_cpSpaceStep, 1);
330
+ }