chipmunk 4.1.0 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/LICENSE +20 -0
  2. data/README +60 -0
  3. data/Rakefile +47 -40
  4. data/ext/chipmunk/chipmunk.c +39 -3
  5. data/ext/chipmunk/cpArbiter.c +91 -80
  6. data/ext/chipmunk/cpArray.c +24 -10
  7. data/ext/chipmunk/cpBB.c +5 -4
  8. data/ext/chipmunk/cpBody.c +30 -22
  9. data/ext/chipmunk/cpCollision.c +54 -53
  10. data/ext/chipmunk/cpConstraint.c +54 -0
  11. data/ext/chipmunk/cpDampedRotarySpring.c +106 -0
  12. data/ext/chipmunk/cpDampedSpring.c +117 -0
  13. data/ext/chipmunk/cpGearJoint.c +114 -0
  14. data/ext/chipmunk/cpGrooveJoint.c +138 -0
  15. data/ext/chipmunk/cpHashSet.c +74 -40
  16. data/ext/chipmunk/cpPinJoint.c +117 -0
  17. data/ext/chipmunk/cpPivotJoint.c +114 -0
  18. data/ext/chipmunk/cpPolyShape.c +117 -15
  19. data/ext/chipmunk/cpRatchetJoint.c +128 -0
  20. data/ext/chipmunk/cpRotaryLimitJoint.c +122 -0
  21. data/ext/chipmunk/cpShape.c +174 -18
  22. data/ext/chipmunk/cpSimpleMotor.c +99 -0
  23. data/ext/chipmunk/cpSlideJoint.c +131 -0
  24. data/ext/chipmunk/cpSpace.c +584 -215
  25. data/ext/chipmunk/cpSpaceHash.c +191 -105
  26. data/ext/chipmunk/cpVect.c +18 -10
  27. data/ext/chipmunk/extconf.rb +34 -4
  28. data/ext/chipmunk/{chipmunk.h → include/chipmunk/chipmunk.h} +63 -6
  29. data/ext/chipmunk/include/chipmunk/chipmunk_ffi.h +42 -0
  30. data/ext/chipmunk/include/chipmunk/chipmunk_types.h +80 -0
  31. data/ext/chipmunk/include/chipmunk/chipmunk_unsafe.h +54 -0
  32. data/ext/chipmunk/include/chipmunk/constraints/cpConstraint.h +92 -0
  33. data/ext/chipmunk/include/chipmunk/constraints/cpDampedRotarySpring.h +46 -0
  34. data/ext/chipmunk/include/chipmunk/constraints/cpDampedSpring.h +53 -0
  35. data/ext/chipmunk/include/chipmunk/constraints/cpGearJoint.h +41 -0
  36. data/ext/chipmunk/include/chipmunk/constraints/cpGrooveJoint.h +44 -0
  37. data/ext/chipmunk/include/chipmunk/constraints/cpPinJoint.h +43 -0
  38. data/ext/chipmunk/include/chipmunk/constraints/cpPivotJoint.h +42 -0
  39. data/ext/chipmunk/include/chipmunk/constraints/cpRatchetJoint.h +40 -0
  40. data/ext/chipmunk/include/chipmunk/constraints/cpRotaryLimitJoint.h +39 -0
  41. data/ext/chipmunk/include/chipmunk/constraints/cpSimpleMotor.h +37 -0
  42. data/ext/chipmunk/include/chipmunk/constraints/cpSlideJoint.h +44 -0
  43. data/ext/chipmunk/include/chipmunk/constraints/util.h +116 -0
  44. data/ext/chipmunk/{cpArbiter.h → include/chipmunk/cpArbiter.h} +66 -15
  45. data/ext/chipmunk/{cpArray.h → include/chipmunk/cpArray.h} +2 -1
  46. data/ext/chipmunk/{cpBB.h → include/chipmunk/cpBB.h} +21 -0
  47. data/ext/chipmunk/{cpBody.h → include/chipmunk/cpBody.h} +37 -9
  48. data/ext/chipmunk/{cpCollision.h → include/chipmunk/cpCollision.h} +1 -1
  49. data/ext/chipmunk/{cpHashSet.h → include/chipmunk/cpHashSet.h} +12 -9
  50. data/ext/chipmunk/{cpPolyShape.h → include/chipmunk/cpPolyShape.h} +13 -2
  51. data/ext/chipmunk/{cpShape.h → include/chipmunk/cpShape.h} +51 -18
  52. data/ext/chipmunk/include/chipmunk/cpSpace.h +180 -0
  53. data/ext/chipmunk/{cpSpaceHash.h → include/chipmunk/cpSpaceHash.h} +18 -9
  54. data/ext/chipmunk/{cpVect.h → include/chipmunk/cpVect.h} +61 -10
  55. data/ext/chipmunk/prime.h +32 -32
  56. data/ext/chipmunk/rb_chipmunk.c +125 -109
  57. data/ext/chipmunk/rb_chipmunk.h +96 -77
  58. data/ext/chipmunk/rb_cpArbiter.c +225 -0
  59. data/ext/chipmunk/rb_cpBB.c +174 -154
  60. data/ext/chipmunk/rb_cpBody.c +347 -239
  61. data/ext/chipmunk/rb_cpConstraint.c +346 -0
  62. data/ext/chipmunk/rb_cpShape.c +455 -292
  63. data/ext/chipmunk/rb_cpSpace.c +544 -330
  64. data/ext/chipmunk/rb_cpVect.c +321 -250
  65. data/lib/chipmunk.rb +28 -15
  66. data/lib/chipmunk/version.rb +3 -0
  67. metadata +74 -34
  68. data/ext/chipmunk/cpJoint.c +0 -553
  69. data/ext/chipmunk/cpJoint.h +0 -122
  70. data/ext/chipmunk/cpSpace.h +0 -120
  71. data/ext/chipmunk/rb_cpJoint.c +0 -136
@@ -0,0 +1,346 @@
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 <stdlib.h>
23
+ #include "chipmunk.h"
24
+
25
+ #include "ruby.h"
26
+ #include "rb_chipmunk.h"
27
+
28
+ VALUE m_cpConstraint;
29
+
30
+ #define CONSTRAINT_GETSET_FUNCS(member) \
31
+ static VALUE rb_cpConstraint_get_##member(VALUE self) \
32
+ {return rb_float_new(CONSTRAINT(self)->member);} \
33
+ static VALUE rb_cpConstraint_set_##member(VALUE self, VALUE value) \
34
+ {CONSTRAINT(self)->member = NUM2DBL(value); return value;}
35
+
36
+ CONSTRAINT_GETSET_FUNCS(maxForce)
37
+ CONSTRAINT_GETSET_FUNCS(biasCoef)
38
+ CONSTRAINT_GETSET_FUNCS(maxBias)
39
+
40
+
41
+ #define MAKE_FLT_GETTER(func) \
42
+ static VALUE rb_##func(VALUE self) \
43
+ {return rb_float_new(func(CONSTRAINT(self)));}
44
+
45
+ #define MAKE_FLT_SETTER(func) \
46
+ static VALUE rb_##func(VALUE self, VALUE value) \
47
+ {func(CONSTRAINT(self), NUM2DBL(value)); return value;} \
48
+
49
+ #define MAKE_FLT_ACCESSORS(s, m) \
50
+ MAKE_FLT_GETTER(s##Get##m) \
51
+ MAKE_FLT_SETTER(s##Set##m)
52
+
53
+ #define MAKE_VEC_GETTER(func) \
54
+ static VALUE rb_##func(VALUE self) \
55
+ {return VNEW(func(CONSTRAINT(self)));}
56
+
57
+ #define MAKE_VEC_SETTER(func) \
58
+ static VALUE rb_##func(VALUE self, VALUE value) \
59
+ {func(CONSTRAINT(self), *VGET(value)); return value;} \
60
+
61
+ #define MAKE_VEC_ACCESSORS(s, m) \
62
+ MAKE_VEC_GETTER(s##Get##m) \
63
+ MAKE_VEC_SETTER(s##Set##m)
64
+
65
+
66
+ #define ALLOC_TEMPLATE(s, alloc) \
67
+ static VALUE rb_##s##_alloc(VALUE klass) \
68
+ {return Data_Wrap_Struct(klass, NULL, cpConstraintFree, alloc);}
69
+
70
+ ALLOC_TEMPLATE(cpPinJoint, cpPinJointAlloc())
71
+
72
+ static VALUE
73
+ rb_cpPinJoint_init(VALUE self, VALUE a, VALUE b, VALUE anchr1, VALUE anchr2)
74
+ {
75
+ cpPinJoint *joint = (cpPinJoint *)CONSTRAINT(self);
76
+ cpPinJointInit(joint, BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2));
77
+ rb_iv_set(self, "@body_a", a);
78
+ rb_iv_set(self, "@body_b", b);
79
+
80
+ return self;
81
+ }
82
+
83
+ MAKE_FLT_ACCESSORS(cpPinJoint, Dist)
84
+ MAKE_VEC_ACCESSORS(cpPinJoint, Anchr1)
85
+ MAKE_VEC_ACCESSORS(cpPinJoint, Anchr2)
86
+
87
+
88
+ ALLOC_TEMPLATE(cpDampedRotarySpring, cpDampedRotarySpringAlloc())
89
+
90
+ static VALUE
91
+ rb_cpDampedRotarySpring_init(VALUE self, VALUE a, VALUE b, VALUE restAngle, VALUE stiffness, VALUE damping)
92
+ {
93
+ cpDampedRotarySpring *spring = (cpDampedRotarySpring *)CONSTRAINT(self);
94
+ cpDampedRotarySpringInit(spring, BODY(a), BODY(b), NUM2DBL(restAngle), NUM2DBL(stiffness), NUM2DBL(damping));
95
+ rb_iv_set(self, "@body_a", a);
96
+ rb_iv_set(self, "@body_b", b);
97
+
98
+ return self;
99
+ }
100
+
101
+ MAKE_FLT_ACCESSORS(cpDampedRotarySpring, RestAngle);
102
+ MAKE_FLT_ACCESSORS(cpDampedRotarySpring, Stiffness);
103
+ MAKE_FLT_ACCESSORS(cpDampedRotarySpring, Damping);
104
+
105
+
106
+ ALLOC_TEMPLATE(cpDampedSpring, cpDampedSpringAlloc())
107
+
108
+ static VALUE
109
+ rb_cpDampedSpring_init(VALUE self, VALUE a, VALUE b, VALUE anchr1, VALUE anchr2, VALUE restLength, VALUE stiffness, VALUE damping)
110
+ {
111
+ cpDampedSpring *spring = (cpDampedSpring *)CONSTRAINT(self);
112
+ cpDampedSpringInit(spring, BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2), NUM2DBL(restLength), NUM2DBL(stiffness), NUM2DBL(damping));
113
+ rb_iv_set(self, "@body_a", a);
114
+ rb_iv_set(self, "@body_b", b);
115
+
116
+ return self;
117
+ }
118
+
119
+ MAKE_VEC_ACCESSORS(cpDampedSpring, Anchr1)
120
+ MAKE_VEC_ACCESSORS(cpDampedSpring, Anchr2)
121
+ MAKE_FLT_ACCESSORS(cpDampedSpring, RestLength);
122
+ MAKE_FLT_ACCESSORS(cpDampedSpring, Stiffness);
123
+ MAKE_FLT_ACCESSORS(cpDampedSpring, Damping);
124
+
125
+
126
+ ALLOC_TEMPLATE(cpGearJoint, cpGearJointAlloc())
127
+
128
+ static VALUE
129
+ rb_cpGearJoint_init(VALUE self, VALUE a, VALUE b, VALUE phase, VALUE ratio)
130
+ {
131
+ cpGearJoint *joint = (cpGearJoint *)CONSTRAINT(self);
132
+ cpGearJointInit(joint, BODY(a), BODY(b), NUM2DBL(phase), NUM2DBL(ratio));
133
+ rb_iv_set(self, "@body_a", a);
134
+ rb_iv_set(self, "@body_b", b);
135
+
136
+ return self;
137
+ }
138
+
139
+ MAKE_FLT_ACCESSORS(cpGearJoint, Phase);
140
+ MAKE_FLT_ACCESSORS(cpGearJoint, Ratio);
141
+
142
+
143
+ ALLOC_TEMPLATE(cpPivotJoint, cpPivotJointAlloc())
144
+
145
+ static VALUE
146
+ rb_cpPivotJoint_init(VALUE self, VALUE a, VALUE b, VALUE anchr1, VALUE anchr2)
147
+ {
148
+ cpPivotJoint *joint = (cpPivotJoint *)CONSTRAINT(self);
149
+ cpPivotJointInit(joint, BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2));
150
+ rb_iv_set(self, "@body_a", a);
151
+ rb_iv_set(self, "@body_b", b);
152
+
153
+ return self;
154
+ }
155
+
156
+ MAKE_VEC_ACCESSORS(cpPivotJoint, Anchr1);
157
+ MAKE_VEC_ACCESSORS(cpPivotJoint, Anchr2);
158
+
159
+
160
+ ALLOC_TEMPLATE(cpRotaryLimitJoint, cpRotaryLimitJointAlloc())
161
+
162
+ static VALUE
163
+ rb_cpRotaryLimitJoint_init(VALUE self, VALUE a, VALUE b, VALUE min, VALUE max)
164
+ {
165
+ cpRotaryLimitJoint *joint = (cpRotaryLimitJoint *)CONSTRAINT(self);
166
+ cpRotaryLimitJointInit(joint, BODY(a), BODY(b), NUM2DBL(min), NUM2DBL(max));
167
+ rb_iv_set(self, "@body_a", a);
168
+ rb_iv_set(self, "@body_b", b);
169
+
170
+ return self;
171
+ }
172
+
173
+ MAKE_FLT_ACCESSORS(cpRotaryLimitJoint, Min);
174
+ MAKE_FLT_ACCESSORS(cpRotaryLimitJoint, Max);
175
+
176
+
177
+ ALLOC_TEMPLATE(cpSimpleMotor, cpSimpleMotorAlloc())
178
+
179
+ static VALUE
180
+ rb_cpSimpleMotor_init(VALUE self, VALUE a, VALUE b, VALUE rate)
181
+ {
182
+ cpSimpleMotor *motor = (cpSimpleMotor *)CONSTRAINT(self);
183
+ cpSimpleMotorInit(motor, BODY(a), BODY(b), NUM2DBL(rate));
184
+ rb_iv_set(self, "@body_a", a);
185
+ rb_iv_set(self, "@body_b", b);
186
+
187
+ return self;
188
+ }
189
+
190
+ MAKE_FLT_ACCESSORS(cpSimpleMotor, Rate);
191
+
192
+
193
+ ALLOC_TEMPLATE(cpSlideJoint, cpSlideJointAlloc())
194
+
195
+ static VALUE
196
+ rb_cpSlideJoint_init(VALUE self, VALUE a, VALUE b, VALUE anchr1, VALUE anchr2, VALUE min, VALUE max)
197
+ {
198
+ cpSlideJoint *joint = (cpSlideJoint *)CONSTRAINT(self);
199
+ cpSlideJointInit(joint, BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2), NUM2DBL(min), NUM2DBL(max));
200
+ rb_iv_set(self, "@body_a", a);
201
+ rb_iv_set(self, "@body_b", b);
202
+
203
+ return self;
204
+ }
205
+
206
+ MAKE_VEC_ACCESSORS(cpSlideJoint, Anchr1)
207
+ MAKE_VEC_ACCESSORS(cpSlideJoint, Anchr2)
208
+ MAKE_FLT_ACCESSORS(cpSlideJoint, Min);
209
+ MAKE_FLT_ACCESSORS(cpSlideJoint, Max);
210
+
211
+
212
+ ALLOC_TEMPLATE(cpGrooveJoint, cpGrooveJointAlloc())
213
+
214
+ static VALUE
215
+ rb_cpGrooveJoint_init(VALUE self, VALUE a, VALUE b, VALUE grv_a, VALUE grv_b, VALUE anchr2)
216
+ {
217
+ cpGrooveJoint *joint = (cpGrooveJoint *)CONSTRAINT(self);
218
+ cpGrooveJointInit(joint, BODY(a), BODY(b), *VGET(grv_a), *VGET(grv_b), *VGET(anchr2));
219
+ rb_iv_set(self, "@body_a", a);
220
+ rb_iv_set(self, "@body_b", b);
221
+
222
+ return self;
223
+ }
224
+
225
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, Anchr2)
226
+
227
+ /*
228
+ These are for later, when Chipmunk C supports them.
229
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, Grv_a)
230
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, Grv_b)
231
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, Grv_n)
232
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, R1)
233
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, R2)
234
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, K1)
235
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, K2)
236
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, Bias)
237
+ MAKE_VEC_ACCESSORS(cpGrooveJoint, JAcc)
238
+ MAKE_FLT_ACCESSORS(cpGrooveJoint, Clamp)
239
+ MAKE_FLT_ACCESSORS(cpGrooveJoint, JMaxLen)
240
+ */
241
+
242
+ ALLOC_TEMPLATE(cpRatchetJoint, cpRatchetJointAlloc())
243
+
244
+ static VALUE
245
+ rb_cpRatchetJoint_init(VALUE self, VALUE a, VALUE b, VALUE phase, VALUE ratchet)
246
+ {
247
+ cpRatchetJoint *joint = (cpRatchetJoint *)CONSTRAINT(self);
248
+ cpRatchetJointInit(joint, BODY(a), BODY(b), NUM2DBL(phase), NUM2DBL(ratchet));
249
+ rb_iv_set(self, "@body_a", a);
250
+ rb_iv_set(self, "@body_b", b);
251
+
252
+ return self;
253
+ }
254
+
255
+ MAKE_FLT_ACCESSORS(cpRatchetJoint, Angle)
256
+ MAKE_FLT_ACCESSORS(cpRatchetJoint, Phase)
257
+ MAKE_FLT_ACCESSORS(cpRatchetJoint, Ratchet)
258
+
259
+
260
+ #define STRINGIFY(v) #v
261
+ #define ACCESSOR_METHODS(s, m, name) \
262
+ rb_define_method(c_##s, STRINGIFY(name), rb_##s##Get##m, 0); \
263
+ rb_define_method(c_##s, STRINGIFY(name=), rb_##s##Set##m, 1);
264
+
265
+ static VALUE
266
+ make_class(char *name, void *alloc_func, void *init_func, int init_params)
267
+ {
268
+ VALUE klass = rb_define_class_under(m_cpConstraint, name, rb_cObject);
269
+ rb_include_module(klass, m_cpConstraint);
270
+ rb_define_alloc_func(klass, alloc_func);
271
+ rb_define_method(klass, "initialize", init_func, init_params);
272
+
273
+ return klass;
274
+ }
275
+
276
+ void
277
+ Init_cpConstraint(void)
278
+ {
279
+ m_cpConstraint = rb_define_module_under(m_Chipmunk, "Constraint");
280
+ rb_define_attr(m_cpConstraint, "body_a", 1, 0);
281
+ rb_define_attr(m_cpConstraint, "body_b", 1, 0);
282
+ rb_define_method(m_cpConstraint, "max_force", rb_cpConstraint_get_maxForce, 0);
283
+ rb_define_method(m_cpConstraint, "max_force=", rb_cpConstraint_set_maxForce, 1);
284
+ rb_define_method(m_cpConstraint, "bias_coef", rb_cpConstraint_get_biasCoef, 0);
285
+ rb_define_method(m_cpConstraint, "bias_coef=", rb_cpConstraint_set_biasCoef, 1);
286
+ rb_define_method(m_cpConstraint, "max_bias", rb_cpConstraint_get_maxBias, 0);
287
+ rb_define_method(m_cpConstraint, "max_bias=", rb_cpConstraint_set_maxBias, 1);
288
+
289
+ VALUE c_cpPinJoint = make_class("PinJoint", rb_cpPinJoint_alloc, rb_cpPinJoint_init, 4);
290
+ ACCESSOR_METHODS(cpPinJoint, Anchr1, anchr1)
291
+ ACCESSOR_METHODS(cpPinJoint, Anchr2, anchr2)
292
+ ACCESSOR_METHODS(cpPinJoint, Dist, dist)
293
+
294
+ VALUE c_cpDampedRotarySpring = make_class("DampedRotarySpring", rb_cpDampedRotarySpring_alloc, rb_cpDampedRotarySpring_init, 5);
295
+ ACCESSOR_METHODS(cpDampedRotarySpring, RestAngle, rest_angle)
296
+ ACCESSOR_METHODS(cpDampedRotarySpring, Stiffness, stiffness)
297
+ ACCESSOR_METHODS(cpDampedRotarySpring, Damping, damping)
298
+
299
+ VALUE c_cpDampedSpring = make_class("DampedSpring", rb_cpDampedSpring_alloc, rb_cpDampedSpring_init, 7);
300
+ ACCESSOR_METHODS(cpDampedSpring, Anchr1, anchr1)
301
+ ACCESSOR_METHODS(cpDampedSpring, Anchr2, anchr2)
302
+ ACCESSOR_METHODS(cpDampedSpring, RestLength, rest_length)
303
+ ACCESSOR_METHODS(cpDampedSpring, Stiffness, stiffness)
304
+ ACCESSOR_METHODS(cpDampedSpring, Damping, damping)
305
+
306
+ VALUE c_cpGearJoint = make_class("GearJoint", rb_cpGearJoint_alloc, rb_cpGearJoint_init, 4);
307
+ ACCESSOR_METHODS(cpGearJoint, Phase, phase)
308
+ ACCESSOR_METHODS(cpGearJoint, Ratio, ratio)
309
+
310
+ VALUE c_cpPivotJoint = make_class("PivotJoint", rb_cpPivotJoint_alloc, rb_cpPivotJoint_init, 4);
311
+ ACCESSOR_METHODS(cpPivotJoint, Anchr1, anchr1)
312
+ ACCESSOR_METHODS(cpPivotJoint, Anchr2, anchr2)
313
+
314
+ VALUE c_cpRotaryLimitJoint = make_class("RotaryLimitJoint", rb_cpRotaryLimitJoint_alloc, rb_cpRotaryLimitJoint_init, 4);
315
+ ACCESSOR_METHODS(cpRotaryLimitJoint, Min, min)
316
+ ACCESSOR_METHODS(cpRotaryLimitJoint, Max, max)
317
+
318
+ VALUE c_cpSimpleMotor = make_class("SimpleMotor", rb_cpSimpleMotor_alloc, rb_cpSimpleMotor_init, 3);
319
+ ACCESSOR_METHODS(cpSimpleMotor, Rate, rate);
320
+
321
+ VALUE c_cpSlideJoint = make_class("SlideJoint", rb_cpSlideJoint_alloc, rb_cpSlideJoint_init, 6);
322
+ ACCESSOR_METHODS(cpSlideJoint, Anchr1, anchr1)
323
+ ACCESSOR_METHODS(cpSlideJoint, Anchr2, anchr2)
324
+ ACCESSOR_METHODS(cpSlideJoint, Min, min)
325
+ ACCESSOR_METHODS(cpSlideJoint, Max, max)
326
+
327
+ VALUE c_cpGrooveJoint = make_class("GrooveJoint", rb_cpGrooveJoint_alloc, rb_cpGrooveJoint_init, 5);
328
+ ACCESSOR_METHODS(cpGrooveJoint, Anchr2, anchr2)
329
+
330
+ /*
331
+ ACCESSOR_METHODS(cpGrooveJoint, Clamp, clamp)
332
+ ACCESSOR_METHODS(cpGrooveJoint, JMaxLen, max)
333
+ */
334
+
335
+ VALUE c_cpRatchetJoint =
336
+ make_class("RatchetJoint", rb_cpRatchetJoint_alloc, rb_cpRatchetJoint_init, 4);
337
+ ACCESSOR_METHODS(cpRatchetJoint, Angle, angle)
338
+ ACCESSOR_METHODS(cpRatchetJoint, Phase, phase)
339
+ ACCESSOR_METHODS(cpRatchetJoint, Ratchet, ratchet)
340
+
341
+
342
+
343
+ // TODO groove joint accessors
344
+
345
+ // TODO breakable joint
346
+ }
@@ -1,292 +1,455 @@
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
- }
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 <stdlib.h>
23
+ #include "chipmunk.h"
24
+
25
+ #include "ruby.h"
26
+ #include "rb_chipmunk.h"
27
+
28
+ ID id_body;
29
+
30
+ VALUE m_cpShape;
31
+ VALUE c_cpCircleShape;
32
+ VALUE c_cpSegmentShape;
33
+ VALUE c_cpPolyShape;
34
+ VALUE c_cpSegmentQueryInfo;
35
+
36
+ //c_cpSegmentQueryInfo
37
+ GETTER_TEMPLATE(SQINFO, c_cpSegmentQueryInfo, cpSegmentQueryInfo)
38
+
39
+ static cpSegmentQueryInfo * cpSegmentQueryInfoAlloc()
40
+ {
41
+ return malloc(sizeof(cpSegmentQueryInfo));
42
+ }
43
+
44
+ static cpSegmentQueryInfo * cpSegmentQueryInfoInit(cpSegmentQueryInfo * self,
45
+ cpShape * shape, cpFloat t, cpVect n)
46
+ {
47
+ self->shape = shape;
48
+ self->t = t;
49
+ self->n = n;
50
+ return self;
51
+ }
52
+
53
+ static VALUE
54
+ rb_cpSegmentQueryInfoAlloc(VALUE klass)
55
+ {
56
+ struct cpSegmentQueryInfo * info = cpSegmentQueryInfoAlloc();
57
+ return Data_Wrap_Struct(klass, NULL, free, info);
58
+ }
59
+
60
+ static VALUE
61
+ rb_cpSeqmentQueryInfoInitialize(VALUE self, VALUE shape, VALUE t, VALUE n)
62
+ {
63
+ struct cpSegmentQueryInfo * info;
64
+ info = SQINFO(self);
65
+ cpSegmentQueryInfoInit(info, SHAPE(shape), NUM2DBL(t), *VGET(n));
66
+ return self;
67
+ }
68
+
69
+ static VALUE
70
+ rb_cpSeqmentQueryInfoGetShape(VALUE self)
71
+ {
72
+ cpShape * shape = SQINFO(self)->shape;
73
+ if (!shape) return Qnil;
74
+ //XXX: technically speaking, this is wrong... :p.
75
+ // It's hard to wrap, though...
76
+ return (VALUE) SQINFO(self)->shape->data;
77
+ // return Qtrue;
78
+ // return Data_Wrap_Struct(c_cpShape, 0, cpShapeFree, SQINFO(self)->shape);
79
+ }
80
+
81
+ static VALUE
82
+ rb_cpSeqmentQueryInfoGetT(VALUE self)
83
+ {
84
+ return rb_float_new(SQINFO(self)->t);
85
+ }
86
+
87
+ static VALUE
88
+ rb_cpSeqmentQueryInfoGetN(VALUE self)
89
+ {
90
+ return VNEW(SQINFO(self)->n);
91
+ }
92
+
93
+ static VALUE
94
+ rb_cpSeqmentQueryInfoHitPoint(VALUE self, VALUE start, VALUE end)
95
+ {
96
+ return VNEW(cpSegmentQueryHitPoint(*VGET(start), *VGET(end), *SQINFO(self)));
97
+ }
98
+
99
+ static VALUE
100
+ rb_cpSeqmentQueryInfoHitDist(VALUE self, VALUE start, VALUE end)
101
+ {
102
+ return rb_float_new(cpSegmentQueryHitDist(*VGET(start), *VGET(end), *SQINFO(self)));
103
+ }
104
+
105
+
106
+ //c_cpShape
107
+ static VALUE
108
+ rb_cpShapeGetBody(VALUE self)
109
+ {
110
+ return rb_ivar_get(self, id_body);
111
+ }
112
+
113
+ static VALUE
114
+ rb_cpShapeSetBody(VALUE self, VALUE body)
115
+ {
116
+ SHAPE(self)->body = BODY(body);
117
+ rb_ivar_set(self, id_body, body);
118
+
119
+ return body;
120
+ }
121
+
122
+ static VALUE
123
+ rb_cpShapeGetCollType(VALUE self)
124
+ {
125
+ return rb_iv_get(self, "collType");
126
+ }
127
+
128
+ static VALUE
129
+ rb_cpShapeSetCollType(VALUE self, VALUE val)
130
+ {
131
+ VALUE col_type = rb_obj_id(val);
132
+ rb_iv_set(self, "collType", val);
133
+ SHAPE(self)->collision_type = NUM2UINT(col_type);
134
+
135
+ return val;
136
+ }
137
+
138
+ static VALUE
139
+ rb_cpShapeGetGroup(VALUE self)
140
+ {
141
+ return rb_iv_get(self, "group");
142
+ }
143
+
144
+ static VALUE
145
+ rb_cpShapeSetGroup(VALUE self, VALUE val)
146
+ {
147
+ VALUE group_type = rb_obj_id(val);
148
+ rb_iv_set(self, "group", val);
149
+
150
+ if (val == rb_intern("no_group")) {
151
+ SHAPE(self)->group = CP_NO_GROUP;
152
+ } else {
153
+ SHAPE(self)->group = NUM2UINT(group_type);
154
+ }
155
+
156
+ return val;
157
+ }
158
+
159
+ static VALUE
160
+ rb_cpShapeGetLayers(VALUE self)
161
+ {
162
+ if(SHAPE(self)->layers == CP_ALL_LAYERS) {
163
+ return rb_intern("all_layers");
164
+ }
165
+ return UINT2NUM(SHAPE(self)->layers);
166
+ }
167
+
168
+ static VALUE
169
+ rb_cpShapeSetLayers(VALUE self, VALUE layers)
170
+ {
171
+ if (layers == rb_intern("all_layers")) {
172
+ SHAPE(self)->layers = CP_ALL_LAYERS;
173
+ } else {
174
+ SHAPE(self)->layers = NUM2UINT(layers);
175
+ }
176
+ return layers;
177
+ }
178
+
179
+ static VALUE
180
+ rb_cpShapeGetBB(VALUE self)
181
+ {
182
+ cpBB *bb = malloc(sizeof(cpBB));
183
+ *bb = SHAPE(self)->bb;
184
+ return Data_Wrap_Struct(c_cpBB, NULL, free, bb);
185
+ }
186
+
187
+ static VALUE
188
+ rb_cpShapeCacheBB(VALUE self)
189
+ {
190
+ cpShape *shape = SHAPE(self);
191
+ cpShapeCacheBB(shape);
192
+
193
+ return rb_cpShapeGetBB(self);
194
+ }
195
+
196
+ static VALUE
197
+ rb_cpShapeGetElasticity(VALUE self)
198
+ {
199
+ return rb_float_new(SHAPE(self)->e);
200
+ }
201
+
202
+ static VALUE
203
+ rb_cpShapeGetFriction(VALUE self)
204
+ {
205
+ return rb_float_new(SHAPE(self)->u);
206
+ }
207
+
208
+
209
+ static VALUE
210
+ rb_cpShapeSetElasticity(VALUE self, VALUE val)
211
+ {
212
+ SHAPE(self)->e = NUM2DBL(val);
213
+ return val;
214
+ }
215
+
216
+ static VALUE
217
+ rb_cpShapeSetFriction(VALUE self, VALUE val)
218
+ {
219
+ SHAPE(self)->u = NUM2DBL(val);
220
+ return val;
221
+ }
222
+
223
+ static VALUE
224
+ rb_cpShapeGetSurfaceV(VALUE self)
225
+ {
226
+ return VWRAP(self, &SHAPE(self)->surface_v);
227
+ }
228
+
229
+ static VALUE
230
+ rb_cpShapeSetSurfaceV(VALUE self, VALUE val)
231
+ {
232
+ SHAPE(self)->surface_v = *VGET(val);
233
+ return val;
234
+ }
235
+
236
+ static VALUE
237
+ rb_cpShapeResetIdCounter(VALUE self)
238
+ {
239
+ cpResetShapeIdCounter();
240
+ return Qnil;
241
+ }
242
+
243
+ static VALUE
244
+ rb_cpShapeGetData(VALUE self)
245
+ {
246
+ return (VALUE) (SHAPE(self)->data);
247
+ }
248
+
249
+
250
+ static VALUE
251
+ rb_cpShapeGetSensor(VALUE self)
252
+ {
253
+ int bool = (SHAPE(self)->sensor);
254
+ return bool ? Qtrue : Qfalse;
255
+ }
256
+
257
+ static VALUE
258
+ rb_cpShapeSetSensor(VALUE self, VALUE val)
259
+ {
260
+ int bool = ((val == Qnil) || (val == Qfalse)) ? 0 : 1;
261
+ SHAPE(self)->sensor = bool;
262
+ return val;
263
+ }
264
+
265
+ static VALUE
266
+ rb_cpShapePointQuery(VALUE self, VALUE point) {
267
+ int bool = cpShapePointQuery(SHAPE(self), *VGET(point));
268
+ return bool ? Qtrue : Qfalse;
269
+ }
270
+
271
+ // Shape/segment query
272
+ static VALUE
273
+ rb_cpShapeSegmentQuery(VALUE self, VALUE a, VALUE b)
274
+ {
275
+ cpSegmentQueryInfo * info = cpSegmentQueryInfoAlloc();
276
+ int bool = cpShapeSegmentQuery(SHAPE(self), *VGET(a), *VGET(b), info);
277
+ if (bool) {
278
+ return Data_Wrap_Struct(c_cpSegmentQueryInfo, NULL, free, info);
279
+ } else {
280
+ return Qnil;
281
+ }
282
+ }
283
+
284
+
285
+ //cpCircle
286
+ static VALUE
287
+ rb_cpCircleAlloc(VALUE klass)
288
+ {
289
+ cpCircleShape *circle = cpCircleShapeAlloc();
290
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, circle);
291
+ }
292
+
293
+ static VALUE
294
+ rb_cpCircleInitialize(VALUE self, VALUE body, VALUE radius, VALUE offset)
295
+ {
296
+ cpCircleShape *circle = (cpCircleShape *)SHAPE(self);
297
+
298
+ cpCircleShapeInit(circle, BODY(body), NUM2DBL(radius), *VGET(offset));
299
+ circle->shape.data = (void *)self;
300
+ circle->shape.collision_type = Qnil;
301
+
302
+ rb_ivar_set(self, id_body, body);
303
+
304
+ return self;
305
+ }
306
+
307
+
308
+
309
+ //cpSegment
310
+ static VALUE
311
+ rb_cpSegmentAlloc(VALUE klass)
312
+ {
313
+ cpSegmentShape *seg = cpSegmentShapeAlloc();
314
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, seg);
315
+ }
316
+
317
+ static VALUE
318
+ rb_cpSegmentInitialize(VALUE self, VALUE body, VALUE a, VALUE b, VALUE r)
319
+ {
320
+ cpSegmentShape *seg = (cpSegmentShape *)SHAPE(self);
321
+
322
+ cpSegmentShapeInit(seg, BODY(body), *VGET(a), *VGET(b), NUM2DBL(r));
323
+ seg->shape.data = (void *)self;
324
+ seg->shape.collision_type = Qnil;
325
+
326
+ rb_ivar_set(self, id_body, body);
327
+
328
+ return self;
329
+ }
330
+
331
+
332
+
333
+ //cpPoly
334
+ static VALUE
335
+ rb_cpPolyAlloc(VALUE klass)
336
+ {
337
+ cpPolyShape *poly = cpPolyShapeAlloc();
338
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, poly);
339
+ }
340
+
341
+ static VALUE
342
+ rb_cpPolyInitialize(VALUE self, VALUE body, VALUE arr, VALUE offset)
343
+ {
344
+ cpPolyShape *poly = (cpPolyShape *)SHAPE(self);
345
+
346
+ Check_Type(arr, T_ARRAY);
347
+ int numVerts = RARRAY_LEN(arr);
348
+ VALUE *ary_ptr = RARRAY_PTR(arr);
349
+ cpVect verts[numVerts];
350
+
351
+ for(int i=0; i<numVerts; i++)
352
+ verts[i] = *VGET(ary_ptr[i]);
353
+
354
+ cpPolyShapeInit(poly, BODY(body), numVerts, verts, *VGET(offset));
355
+ poly->shape.data = (void *)self;
356
+ poly->shape.collision_type = Qnil;
357
+
358
+ rb_ivar_set(self, id_body, body);
359
+
360
+ return self;
361
+ }
362
+
363
+
364
+
365
+
366
+
367
+
368
+ void
369
+ Init_cpShape(void)
370
+ {
371
+ id_body = rb_intern("body");
372
+
373
+ m_cpShape = rb_define_module_under(m_Chipmunk, "Shape");
374
+ rb_define_attr(m_cpShape, "obj", 1, 1);
375
+
376
+ rb_define_method(m_cpShape, "body", rb_cpShapeGetBody, 0);
377
+ rb_define_method(m_cpShape, "body=", rb_cpShapeSetBody, 1);
378
+
379
+ rb_define_method(m_cpShape, "collision_type", rb_cpShapeGetCollType, 0);
380
+ rb_define_method(m_cpShape, "collision_type=", rb_cpShapeSetCollType, 1);
381
+
382
+ rb_define_method(m_cpShape, "group", rb_cpShapeGetGroup, 0);
383
+ rb_define_method(m_cpShape, "group=", rb_cpShapeSetGroup, 1);
384
+
385
+ rb_define_method(m_cpShape, "layers", rb_cpShapeGetLayers, 0);
386
+ rb_define_method(m_cpShape, "layers=", rb_cpShapeSetLayers, 1);
387
+
388
+ rb_define_method(m_cpShape, "bb", rb_cpShapeGetBB, 0);
389
+ rb_define_method(m_cpShape, "cache_bb", rb_cpShapeCacheBB, 0);
390
+
391
+ rb_define_method(m_cpShape, "e", rb_cpShapeGetElasticity, 0);
392
+ rb_define_method(m_cpShape, "u", rb_cpShapeGetFriction, 0);
393
+
394
+ rb_define_method(m_cpShape, "e=", rb_cpShapeSetElasticity, 1);
395
+ rb_define_method(m_cpShape, "u=", rb_cpShapeSetFriction, 1);
396
+
397
+ rb_define_method(m_cpShape, "surface_v" , rb_cpShapeGetSurfaceV, 0);
398
+ rb_define_method(m_cpShape, "surface_v=" , rb_cpShapeSetSurfaceV, 1);
399
+
400
+ rb_define_method(m_cpShape, "data" , rb_cpShapeGetData, 0);
401
+ /*
402
+ XXX: do we need data= ? Data is used internally, so perhaps what
403
+ chipmunk-ffi isdoing doesn't make sense here?
404
+ */
405
+ rb_define_method(m_cpShape, "sensor" , rb_cpShapeGetSensor, 0);
406
+ rb_define_method(m_cpShape, "sensor?" , rb_cpShapeGetSensor, 0);
407
+ rb_define_method(m_cpShape, "sensor=" , rb_cpShapeSetSensor, 1);
408
+
409
+ rb_define_method(m_cpShape, "point_query" , rb_cpShapePointQuery, 1);
410
+ rb_define_method(m_cpShape, "segment_query", rb_cpShapeSegmentQuery, 2);
411
+
412
+
413
+
414
+ rb_define_singleton_method(m_cpShape, "reset_id_counter", rb_cpShapeResetIdCounter, 0);
415
+
416
+
417
+ c_cpCircleShape = rb_define_class_under(m_cpShape, "Circle", rb_cObject);
418
+ rb_include_module(c_cpCircleShape, m_cpShape);
419
+ rb_define_alloc_func(c_cpCircleShape, rb_cpCircleAlloc);
420
+ rb_define_method(c_cpCircleShape, "initialize", rb_cpCircleInitialize, 3);
421
+
422
+
423
+ c_cpSegmentShape = rb_define_class_under(m_cpShape, "Segment", rb_cObject);
424
+ rb_include_module(c_cpSegmentShape, m_cpShape);
425
+ rb_define_alloc_func(c_cpSegmentShape, rb_cpSegmentAlloc);
426
+ rb_define_method(c_cpSegmentShape, "initialize", rb_cpSegmentInitialize, 4);
427
+
428
+
429
+ c_cpPolyShape = rb_define_class_under(m_cpShape, "Poly", rb_cObject);
430
+ rb_include_module(c_cpPolyShape, m_cpShape);
431
+ rb_define_alloc_func(c_cpPolyShape, rb_cpPolyAlloc);
432
+ rb_define_method(c_cpPolyShape, "initialize", rb_cpPolyInitialize, 3);
433
+
434
+
435
+ c_cpSegmentQueryInfo = rb_define_class_under(m_cpShape,
436
+ "SegmentQueryInfo", rb_cObject);
437
+ rb_define_alloc_func(c_cpSegmentQueryInfo, rb_cpSegmentQueryInfoAlloc);
438
+ rb_define_method(c_cpSegmentQueryInfo, "initialize",
439
+ rb_cpSeqmentQueryInfoInitialize, 3);
440
+
441
+ rb_define_method(c_cpSegmentQueryInfo, "shape",
442
+ rb_cpSeqmentQueryInfoGetShape, 0);
443
+ rb_define_method(c_cpSegmentQueryInfo, "hit",
444
+ rb_cpSeqmentQueryInfoGetShape, 0);
445
+ rb_define_method(c_cpSegmentQueryInfo, "t",
446
+ rb_cpSeqmentQueryInfoGetT, 0);
447
+ rb_define_method(c_cpSegmentQueryInfo, "n",
448
+ rb_cpSeqmentQueryInfoGetN, 0);
449
+ rb_define_method(c_cpSegmentQueryInfo, "hit_point",
450
+ rb_cpSeqmentQueryInfoHitPoint, 2);
451
+ rb_define_method(c_cpSegmentQueryInfo, "hit_distance",
452
+ rb_cpSeqmentQueryInfoHitDist, 2);
453
+
454
+ }
455
+