chipmunk 5.2.2 → 5.3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. data/LICENSE +21 -19
  2. data/README +67 -60
  3. data/Rakefile +85 -50
  4. data/ext/chipmunk/extconf.rb +42 -35
  5. data/ext/chipmunk/rb_chipmunk.c +250 -125
  6. data/ext/chipmunk/rb_chipmunk.h +105 -96
  7. data/ext/chipmunk/rb_cpArbiter.c +253 -225
  8. data/ext/chipmunk/rb_cpBB.c +210 -174
  9. data/ext/chipmunk/rb_cpBody.c +504 -347
  10. data/ext/chipmunk/rb_cpConstraint.c +336 -346
  11. data/ext/chipmunk/rb_cpShape.c +433 -455
  12. data/ext/chipmunk/rb_cpSpace.c +711 -544
  13. data/ext/chipmunk/rb_cpVect.c +346 -321
  14. data/lib/chipmunk.rb +183 -30
  15. metadata +12 -66
  16. data/ext/chipmunk/chipmunk.c +0 -105
  17. data/ext/chipmunk/cpArbiter.c +0 -274
  18. data/ext/chipmunk/cpArray.c +0 -128
  19. data/ext/chipmunk/cpBB.c +0 -47
  20. data/ext/chipmunk/cpBody.c +0 -188
  21. data/ext/chipmunk/cpCollision.c +0 -391
  22. data/ext/chipmunk/cpConstraint.c +0 -54
  23. data/ext/chipmunk/cpDampedRotarySpring.c +0 -106
  24. data/ext/chipmunk/cpDampedSpring.c +0 -117
  25. data/ext/chipmunk/cpGearJoint.c +0 -114
  26. data/ext/chipmunk/cpGrooveJoint.c +0 -138
  27. data/ext/chipmunk/cpHashSet.c +0 -253
  28. data/ext/chipmunk/cpPinJoint.c +0 -117
  29. data/ext/chipmunk/cpPivotJoint.c +0 -114
  30. data/ext/chipmunk/cpPolyShape.c +0 -241
  31. data/ext/chipmunk/cpRatchetJoint.c +0 -128
  32. data/ext/chipmunk/cpRotaryLimitJoint.c +0 -122
  33. data/ext/chipmunk/cpShape.c +0 -400
  34. data/ext/chipmunk/cpSimpleMotor.c +0 -99
  35. data/ext/chipmunk/cpSlideJoint.c +0 -131
  36. data/ext/chipmunk/cpSpace.c +0 -899
  37. data/ext/chipmunk/cpSpaceHash.c +0 -541
  38. data/ext/chipmunk/cpVect.c +0 -71
  39. data/ext/chipmunk/include/chipmunk/chipmunk.h +0 -148
  40. data/ext/chipmunk/include/chipmunk/chipmunk_ffi.h +0 -42
  41. data/ext/chipmunk/include/chipmunk/chipmunk_types.h +0 -80
  42. data/ext/chipmunk/include/chipmunk/chipmunk_unsafe.h +0 -54
  43. data/ext/chipmunk/include/chipmunk/constraints/cpConstraint.h +0 -92
  44. data/ext/chipmunk/include/chipmunk/constraints/cpDampedRotarySpring.h +0 -46
  45. data/ext/chipmunk/include/chipmunk/constraints/cpDampedSpring.h +0 -53
  46. data/ext/chipmunk/include/chipmunk/constraints/cpGearJoint.h +0 -41
  47. data/ext/chipmunk/include/chipmunk/constraints/cpGrooveJoint.h +0 -44
  48. data/ext/chipmunk/include/chipmunk/constraints/cpPinJoint.h +0 -43
  49. data/ext/chipmunk/include/chipmunk/constraints/cpPivotJoint.h +0 -42
  50. data/ext/chipmunk/include/chipmunk/constraints/cpRatchetJoint.h +0 -40
  51. data/ext/chipmunk/include/chipmunk/constraints/cpRotaryLimitJoint.h +0 -39
  52. data/ext/chipmunk/include/chipmunk/constraints/cpSimpleMotor.h +0 -37
  53. data/ext/chipmunk/include/chipmunk/constraints/cpSlideJoint.h +0 -44
  54. data/ext/chipmunk/include/chipmunk/constraints/util.h +0 -116
  55. data/ext/chipmunk/include/chipmunk/cpArbiter.h +0 -136
  56. data/ext/chipmunk/include/chipmunk/cpArray.h +0 -46
  57. data/ext/chipmunk/include/chipmunk/cpBB.h +0 -74
  58. data/ext/chipmunk/include/chipmunk/cpBody.h +0 -160
  59. data/ext/chipmunk/include/chipmunk/cpCollision.h +0 -23
  60. data/ext/chipmunk/include/chipmunk/cpHashSet.h +0 -82
  61. data/ext/chipmunk/include/chipmunk/cpPolyShape.h +0 -103
  62. data/ext/chipmunk/include/chipmunk/cpShape.h +0 -174
  63. data/ext/chipmunk/include/chipmunk/cpSpace.h +0 -180
  64. data/ext/chipmunk/include/chipmunk/cpSpaceHash.h +0 -109
  65. data/ext/chipmunk/include/chipmunk/cpVect.h +0 -157
  66. data/ext/chipmunk/prime.h +0 -68
  67. data/lib/chipmunk/version.rb +0 -3
@@ -1,96 +1,105 @@
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
- extern VALUE m_Chipmunk;
23
-
24
- extern VALUE c_cpArbiter;
25
- extern VALUE c_cpVect;
26
- extern VALUE c_cpBB;
27
- extern VALUE c_cpBody;
28
- extern VALUE m_cpShape;
29
- extern VALUE c_cpCircleShape;
30
- extern VALUE c_cpSegmentShape;
31
- extern VALUE c_cpPolyShape;
32
- extern VALUE m_cpConstraint;
33
- extern VALUE c_cpSpace;
34
-
35
- extern ID id_parent;
36
-
37
- static inline VALUE
38
- VNEW(cpVect v)
39
- {
40
- cpVect *ptr = malloc(sizeof(cpVect));
41
- *ptr = v;
42
- return Data_Wrap_Struct(c_cpVect, NULL, free, ptr);
43
- }
44
-
45
- static inline VALUE
46
- VWRAP(VALUE parent, cpVect *v)
47
- {
48
- VALUE vec_obj = Data_Wrap_Struct(c_cpVect, NULL, NULL, v);
49
- rb_ivar_set(vec_obj, id_parent, parent);
50
-
51
- return vec_obj;
52
- }
53
-
54
- /*
55
- * Arbiters may bot be allocated or deallocated by Ruby.
56
- * Chipmunk handles them itself.
57
- */
58
- static inline VALUE
59
- ARBWRAP(cpArbiter *arb)
60
- {
61
- VALUE arb_obj = Data_Wrap_Struct(c_cpArbiter, NULL, NULL, arb);
62
- return arb_obj;
63
- }
64
-
65
-
66
- #define GETTER_TEMPLATE(func_name, klass, type)\
67
- static inline type *\
68
- func_name(VALUE self)\
69
- {\
70
- if(!rb_obj_is_kind_of(self, klass)){\
71
- VALUE klass_name = rb_funcall(klass, rb_intern("to_s"), 0);\
72
- rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", rb_obj_classname(self), StringValuePtr(klass_name));\
73
- }\
74
- type *ptr;\
75
- Data_Get_Struct(self, type, ptr);\
76
- return ptr;\
77
- }\
78
-
79
- GETTER_TEMPLATE(VGET , c_cpVect , cpVect )
80
- GETTER_TEMPLATE(BBGET, c_cpBB , cpBB )
81
- GETTER_TEMPLATE(BODY , c_cpBody , cpBody )
82
- GETTER_TEMPLATE(SHAPE, m_cpShape, cpShape)
83
- GETTER_TEMPLATE(CONSTRAINT, m_cpConstraint, cpConstraint)
84
- GETTER_TEMPLATE(SPACE, c_cpSpace, cpSpace)
85
- GETTER_TEMPLATE(ARBITER, c_cpArbiter, cpArbiter)
86
-
87
-
88
- void Init_chipmunk(void);
89
- void Init_cpVect();
90
- void Init_cpArbiter();
91
- void Init_cpBB();
92
- void Init_cpBody();
93
- void Init_cpShape();
94
- void Init_cpConstraint();
95
- void Init_cpSpace();
96
-
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
+ extern VALUE m_Chipmunk;
23
+
24
+ extern VALUE c_cpVect;
25
+ extern VALUE c_cpBB;
26
+ extern VALUE c_cpBody;
27
+ extern VALUE m_cpShape;
28
+ extern VALUE c_cpCircleShape;
29
+ extern VALUE c_cpSegmentShape;
30
+ extern VALUE c_cpPolyShape;
31
+ extern VALUE m_cpConstraint;
32
+ extern VALUE c_cpSpace;
33
+ extern VALUE c_cpArbiter;
34
+ extern VALUE c_cpStaticBody;
35
+ extern VALUE c_cpSegmentQueryInfo;
36
+
37
+ extern ID id_parent;
38
+
39
+ static inline VALUE
40
+ VNEW(cpVect v)
41
+ {
42
+ cpVect *ptr = malloc(sizeof(cpVect));
43
+ *ptr = v;
44
+ return Data_Wrap_Struct(c_cpVect, NULL, free, ptr);
45
+ }
46
+
47
+ static inline VALUE
48
+ VWRAP(VALUE parent, cpVect *v)
49
+ {
50
+ VALUE vec_obj = Data_Wrap_Struct(c_cpVect, NULL, NULL, v);
51
+ rb_ivar_set(vec_obj, id_parent, parent);
52
+
53
+ return vec_obj;
54
+ }
55
+
56
+ #define GETTER_TEMPLATE(func_name, klass, type)\
57
+ static inline type *\
58
+ func_name(VALUE self)\
59
+ {\
60
+ if(!rb_obj_is_kind_of(self, klass)){\
61
+ VALUE klass_name = rb_funcall(klass, rb_intern("to_s"), 0);\
62
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", rb_obj_classname(self), StringValuePtr(klass_name));\
63
+ }\
64
+ type *ptr;\
65
+ Data_Get_Struct(self, type, ptr);\
66
+ return ptr;\
67
+ }\
68
+
69
+ GETTER_TEMPLATE(VGET , c_cpVect , cpVect )
70
+ GETTER_TEMPLATE(BBGET, c_cpBB , cpBB )
71
+ GETTER_TEMPLATE(BODY , c_cpBody , cpBody )
72
+ GETTER_TEMPLATE(STATICBODY , c_cpStaticBody , cpBody)
73
+ GETTER_TEMPLATE(SHAPE, m_cpShape, cpShape)
74
+ GETTER_TEMPLATE(CONSTRAINT, m_cpConstraint, cpConstraint)
75
+ GETTER_TEMPLATE(SPACE, c_cpSpace, cpSpace)
76
+ GETTER_TEMPLATE(ARBITER, c_cpArbiter, cpArbiter)
77
+ /* GETTER_TEMPLATE(SPACEHASH, c_cpSpaceHash, cpSpaceHash) */
78
+
79
+ // Helper to wrap cpArbter objects.
80
+ VALUE ARBWRAP(cpArbiter *arb);
81
+
82
+ //Helper that allocates and initializes a SegmenQueryInfo struct
83
+ VALUE rb_cpSegmentQueryInfoNew(VALUE shape, VALUE t, VALUE n);
84
+
85
+ void Init_chipmunk(void);
86
+ void Init_cpVect();
87
+ void Init_cpBB();
88
+ void Init_cpBody();
89
+ void Init_cpShape();
90
+ void Init_cpConstraint();
91
+ void Init_cpSpace();
92
+ void Init_cpArbiter(void);
93
+
94
+ // transforms a boolean VALUE to an int
95
+ #define CP_BOOL_INT(VAL) (((VAL) == Qnil) || ((VAL) == Qfalse) ? 0 : 1)
96
+
97
+ // transforms a C value (pointer or int) to Qtrue of Qfalse
98
+ // For consistency with RBH_BOOL_INT.
99
+ #define CP_INT_BOOL(VAL) ((VAL) ? Qtrue : Qfalse)
100
+
101
+
102
+
103
+
104
+
105
+
@@ -1,225 +1,253 @@
1
- /* Copyright (c) 2010 Beoran (beoran@rubyforge.org)
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 c_cpArbiter;
29
-
30
- /*
31
- * I quote the C docs on cpArbiter:
32
- * Memory Management
33
- * You should never need to create an arbiter, nor will you ever need to free one as they are handled by the space. More importantly, because they are handled by the space you should never hold onto a reference to an arbiter as you don't know when they will be destroyed. Use them within the callback where they are given to you and then forget about them or copy out the information you need from them.
34
- *
35
- * Thios means that Arbiter doesn't need an initialize, and also
36
- * does NOT need any garbage collection.
37
- */
38
-
39
- VALUE rb_cpArbiterWrap(cpArbiter *arb)
40
- {
41
- return Data_Wrap_Struct(c_cpArbiter, NULL, NULL, arb);
42
- }
43
-
44
- /*
45
- static VALUE
46
- rb_cpArbiterAlloc(VALUE klass)
47
- {
48
- cpArbiter *arb = cpArbiterAlloc();
49
- return Data_Wrap_Struct(klass, NULL, cpArbiterFree, arb);
50
- }
51
-
52
-
53
- static VALUE
54
- rb_cpArbiterInitialize(VALUE self, VALUE a, VALUE b, VALUE r, VALUE t)
55
- {
56
- cpArbiter *arb = ARBITER(self);
57
- cpShape *sa = SHAPE(a);
58
- cpShape *sb = SHAPE(b);
59
- cpArbiterInit(arb, sa, sb);
60
- return self;
61
- }
62
- */
63
-
64
- static VALUE
65
- rb_cpArbiterTotalImpulse(VALUE self) {
66
- cpArbiter *arb = ARBITER(self);
67
- return VNEW(cpArbiterTotalImpulse(arb));
68
- }
69
-
70
- static VALUE
71
- rb_cpArbiterTotalImpulseWithFriction(VALUE self) {
72
- cpArbiter *arb = ARBITER(self);
73
- return VNEW(cpArbiterTotalImpulseWithFriction(arb));
74
- }
75
-
76
- static VALUE
77
- rb_cpArbiterGetImpulse(int argc, VALUE *argv, VALUE self)
78
- {
79
- VALUE friction = Qnil;
80
- rb_scan_args(argc, argv, "01", &friction);
81
- if (NIL_P(friction) || friction == Qnil) {
82
- return rb_cpArbiterTotalImpulse(self);
83
- }
84
- /* Here, it's with friction */
85
- return rb_cpArbiterTotalImpulseWithFriction(self);
86
- }
87
-
88
-
89
- static VALUE
90
- rb_cpArbiterIgnore(VALUE self) {
91
- cpArbiter *arb = ARBITER(self);
92
- cpArbiterIgnore(arb);
93
- return Qnil;
94
- }
95
-
96
- static VALUE
97
- rb_cpArbiterGetShapes(VALUE self) {
98
- cpArbiter *arb = ARBITER(self);
99
- CP_ARBITER_GET_SHAPES(arb, a, b)
100
- return rb_ary_new3(2, (VALUE)a->data, (VALUE)b->data);
101
- }
102
-
103
- static VALUE
104
- rb_cpArbiterGetA(VALUE self) {
105
- cpArbiter *arb = ARBITER(self);
106
- CP_ARBITER_GET_SHAPES(arb, a, b)
107
- return (VALUE)a->data;
108
- }
109
-
110
- static VALUE
111
- rb_cpArbiterGetB(VALUE self) {
112
- cpArbiter *arb = ARBITER(self);
113
- CP_ARBITER_GET_SHAPES(arb, a, b)
114
- return (VALUE)b->data;
115
- }
116
-
117
- static VALUE
118
- rb_cpArbiterGetE(VALUE self) {
119
- return rb_float_new(ARBITER(self)->e);
120
- }
121
-
122
- static VALUE
123
- rb_cpArbiterSetE(VALUE self, VALUE e) {
124
- ARBITER(self)->e = NUM2DBL(e);
125
- return e;
126
- }
127
-
128
- static VALUE
129
- rb_cpArbiterGetNumContacts(VALUE self) {
130
- return INT2NUM(ARBITER(self)->numContacts);
131
- }
132
-
133
- static VALUE
134
- rb_cpArbiterGetU(VALUE self) {
135
- return rb_float_new(ARBITER(self)->u);
136
- }
137
-
138
- static VALUE
139
- rb_cpArbiterSetU(VALUE self, VALUE u) {
140
- ARBITER(self)->u = NUM2DBL(u);
141
- return u;
142
- }
143
-
144
- static VALUE
145
- rb_cpArbiterIsFirstContact(VALUE self) {
146
- int bool = cpArbiterIsFirstContact(ARBITER(self));
147
- return bool ? Qtrue : Qfalse;
148
- }
149
-
150
- static VALUE
151
- rb_cpArbiterGetNormal(VALUE self, VALUE index) {
152
- cpArbiter *arb = ARBITER(self);
153
- int i = NUM2LONG(index);
154
- if (i >= arb->numContacts) {
155
- rb_raise(rb_eIndexError, "No such normal.");
156
- }
157
- return VNEW(cpArbiterGetNormal(arb, i));
158
- }
159
-
160
- static VALUE
161
- rb_cpArbiterGetPoint(VALUE self, VALUE index) {
162
- cpArbiter *arb = ARBITER(self);
163
- int i = NUM2LONG(index);
164
- if (i >= arb->numContacts) {
165
- rb_raise(rb_eIndexError, "No such contact point.");
166
- }
167
- return VNEW(cpArbiterGetPoint(arb, i));
168
- }
169
-
170
- static VALUE
171
- rb_cpArbiterToString(VALUE self)
172
- {
173
- char str[256];
174
- cpArbiter *arb = ARBITER(self);
175
- sprintf(str, "#<CP::Arbiter:%p>", arb);
176
- return rb_str_new2(str);
177
- }
178
-
179
- static VALUE
180
- rb_cpArbiterEachContact(VALUE self)
181
- {
182
- cpArbiter *arb = ARBITER(self);
183
- int i = 0;
184
- for( i = 0; i < arb->numContacts; i++) {
185
- VALUE index = INT2NUM(i);
186
- VALUE contact, normal;
187
- normal = rb_cpArbiterGetNormal(self, index);
188
- contact = rb_cpArbiterGetPoint(self, index);
189
- /* Yield an array of contact and normal */
190
- rb_yield(rb_ary_new3(2, contact, normal));
191
- }
192
- return self;
193
- }
194
-
195
-
196
-
197
- void
198
- Init_cpArbiter(void)
199
- {
200
- c_cpArbiter = rb_define_class_under(m_Chipmunk, "Arbiter", rb_cObject);
201
- /*
202
- rb_define_alloc_func(c_cpArbiter, rb_cpArbiterAlloc);
203
- rb_define_method(c_cpArbiter , "initialize", rb_cpArbiterInitialize, 2);
204
- */
205
- rb_define_method(c_cpArbiter, "a", rb_cpArbiterGetA, 0);
206
- rb_define_method(c_cpArbiter, "b", rb_cpArbiterGetB, 0);
207
- rb_define_method(c_cpArbiter, "e" , rb_cpArbiterGetE, 0);
208
- rb_define_method(c_cpArbiter, "u" , rb_cpArbiterGetU, 0);
209
- rb_define_method(c_cpArbiter, "e=", rb_cpArbiterSetE, 1);
210
- rb_define_method(c_cpArbiter, "u=", rb_cpArbiterSetU, 1);
211
-
212
- rb_define_method(c_cpArbiter, "point" , rb_cpArbiterGetPoint , 1);
213
- rb_define_method(c_cpArbiter, "normal" , rb_cpArbiterGetNormal , 1);
214
- rb_define_method(c_cpArbiter, "impulse" , rb_cpArbiterGetImpulse , 1);
215
- rb_define_method(c_cpArbiter, "to_s", rb_cpArbiterToString, 0);
216
-
217
- rb_define_method(c_cpArbiter, "first_contact?", rb_cpArbiterIsFirstContact, 0);
218
- rb_define_method(c_cpArbiter, "num_contacts" , rb_cpArbiterGetNumContacts, 0);
219
- rb_define_method(c_cpArbiter, "each_contact" , rb_cpArbiterEachContact , 0);
220
-
221
- rb_define_method(c_cpArbiter, "shapes" , rb_cpArbiterGetShapes , 0);
222
- /* Ignore is new in the 5.x API, I think. */
223
- rb_define_method(c_cpArbiter, "ignore" , rb_cpArbiterIgnore , 0);
224
-
225
- }
1
+ /* Copyright (c) 2010 Beoran (beoran@rubyforge.org)
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 c_cpArbiter;
29
+
30
+ /*
31
+ * I quote the C docs on cpArbiter:
32
+ * Memory Management
33
+ * You should never need to create an arbiter, nor will you ever need to free
34
+ * one as they are handled by the space. More importantly, because they are
35
+ * handled by the space you should never hold onto a reference to an arbiter as
36
+ * you don't know when they will be destroyed. Use them within the callback
37
+ * where they are given to you and then forget about them or copy out the
38
+ * information you need from them.
39
+ *
40
+ * This means that Arbiter doesn't need an initialize, and also
41
+ * does NOT need any garbage collection.
42
+ */
43
+
44
+ VALUE ARBWRAP(cpArbiter *arb)
45
+ {
46
+ return Data_Wrap_Struct(c_cpArbiter, NULL, NULL, arb);
47
+ }
48
+
49
+
50
+ /*
51
+ static VALUE
52
+ rb_cpArbiterAlloc(VALUE klass)
53
+ {
54
+ cpArbiter *arb = cpArbiterAlloc();
55
+ return Data_Wrap_Struct(klass, NULL, cpArbiterFree, arb);
56
+ }
57
+
58
+
59
+ static VALUE
60
+ rb_cpArbiterInitialize(VALUE self, VALUE a, VALUE b, VALUE r, VALUE t)
61
+ {
62
+ cpArbiter *arb = ARBITER(self);
63
+ cpShape *sa = SHAPE(a);
64
+ cpShape *sb = SHAPE(b);
65
+ cpArbiterInit(arb, sa, sb);
66
+ return self;
67
+ }
68
+ */
69
+
70
+ static VALUE
71
+ rb_cpArbiterTotalImpulse(VALUE self) {
72
+ cpArbiter *arb = ARBITER(self);
73
+ return VNEW(cpArbiterTotalImpulse(arb));
74
+ }
75
+
76
+ static VALUE
77
+ rb_cpArbiterTotalImpulseWithFriction(VALUE self) {
78
+ cpArbiter *arb = ARBITER(self);
79
+ return VNEW(cpArbiterTotalImpulseWithFriction(arb));
80
+ }
81
+
82
+ static VALUE
83
+ rb_cpArbiterGetImpulse(int argc, VALUE *argv, VALUE self)
84
+ {
85
+ VALUE friction = Qnil;
86
+ rb_scan_args(argc, argv, "01", &friction);
87
+ if (NIL_P(friction) || friction == Qnil) {
88
+ return rb_cpArbiterTotalImpulse(self);
89
+ }
90
+ /* Here, it's with friction */
91
+ return rb_cpArbiterTotalImpulseWithFriction(self);
92
+ }
93
+
94
+
95
+ static VALUE
96
+ rb_cpArbiterIgnore(VALUE self) {
97
+ cpArbiter *arb = ARBITER(self);
98
+ cpArbiterIgnore(arb);
99
+ return Qnil;
100
+ }
101
+
102
+ static VALUE
103
+ rb_cpArbiterGetShapes(VALUE self) {
104
+ cpArbiter *arb = ARBITER(self);
105
+ CP_ARBITER_GET_SHAPES(arb, a, b)
106
+ return rb_ary_new3(2, (VALUE)a->data, (VALUE)b->data);
107
+ }
108
+
109
+ static VALUE
110
+ rb_cpArbiterGetA(VALUE self) {
111
+ cpArbiter *arb = ARBITER(self);
112
+ CP_ARBITER_GET_SHAPES(arb, a, b)
113
+ return (VALUE)a->data;
114
+ }
115
+
116
+ static VALUE
117
+ rb_cpArbiterGetB(VALUE self) {
118
+ cpArbiter *arb = ARBITER(self);
119
+ CP_ARBITER_GET_SHAPES(arb, a, b)
120
+ return (VALUE)b->data;
121
+ }
122
+
123
+ static VALUE
124
+ rb_cpArbiterGetE(VALUE self) {
125
+ return rb_float_new(ARBITER(self)->e);
126
+ }
127
+
128
+ static VALUE
129
+ rb_cpArbiterSetE(VALUE self, VALUE e) {
130
+ ARBITER(self)->e = NUM2DBL(e);
131
+ return e;
132
+ }
133
+
134
+ static VALUE
135
+ rb_cpArbiterGetCount(VALUE self) {
136
+ return INT2NUM(cpArbiterGetCount(ARBITER(self)));
137
+ }
138
+
139
+ static VALUE
140
+ rb_cpArbiterGetU(VALUE self) {
141
+ return rb_float_new(ARBITER(self)->u);
142
+ }
143
+
144
+ static VALUE
145
+ rb_cpArbiterSetU(VALUE self, VALUE u) {
146
+ ARBITER(self)->u = NUM2DBL(u);
147
+ return u;
148
+ }
149
+
150
+ static VALUE
151
+ rb_cpArbiterIsFirstContact(VALUE self) {
152
+ int bool = cpArbiterIsFirstContact(ARBITER(self));
153
+ return bool ? Qtrue : Qfalse;
154
+ }
155
+
156
+ static int arbiterBadIndex(cpArbiter *arb, int i) {
157
+ return ((i<0) || (i >= cpArbiterGetCount(arb)));
158
+ }
159
+
160
+
161
+ static VALUE
162
+ rb_cpArbiterGetNormal(VALUE self, VALUE index) {
163
+ cpArbiter *arb = ARBITER(self);
164
+ int i = NUM2LONG(index);
165
+ if (arbiterBadIndex(arb, i)) {
166
+ rb_raise(rb_eIndexError, "No such normal.");
167
+ }
168
+ return VNEW(cpArbiterGetNormal(arb, i));
169
+ }
170
+
171
+ static VALUE
172
+ rb_cpArbiterGetPoint(VALUE self, VALUE index) {
173
+ cpArbiter *arb = ARBITER(self);
174
+ int i = NUM2LONG(index);
175
+ if (arbiterBadIndex(arb, i)) {
176
+ rb_raise(rb_eIndexError, "No such contact point.");
177
+ }
178
+ return VNEW(cpArbiterGetPoint(arb, i));
179
+ }
180
+
181
+ static VALUE
182
+ rb_cpArbiterGetDepth(VALUE self, VALUE index) {
183
+ cpArbiter *arb = ARBITER(self);
184
+ int i = NUM2LONG(index);
185
+ if (arbiterBadIndex(arb, i)) {
186
+ rb_raise(rb_eIndexError, "No such depth.");
187
+ }
188
+ // there"s a typo in the cpArbiter.h class.
189
+ return rb_float_new(cpArbiteGetDepth(arb, i));
190
+ }
191
+
192
+
193
+ static VALUE
194
+ rb_cpArbiterToString(VALUE self)
195
+ {
196
+ char str[256];
197
+ cpArbiter *arb = ARBITER(self);
198
+ sprintf(str, "#<CP::Arbiter:%p>", arb);
199
+ return rb_str_new2(str);
200
+ }
201
+
202
+ static VALUE
203
+ rb_cpArbiterEachContact(VALUE self)
204
+ {
205
+ cpArbiter *arb = ARBITER(self);
206
+ int i = 0;
207
+ for( i = 0; i < cpArbiterGetCount(arb); i++) {
208
+ VALUE index = INT2NUM(i);
209
+ VALUE contact, normal;
210
+ normal = rb_cpArbiterGetNormal(self, index);
211
+ contact = rb_cpArbiterGetPoint(self, index);
212
+ /* Yield an array of contact and normal */
213
+ rb_yield(rb_ary_new3(2, contact, normal));
214
+ }
215
+ return self;
216
+ }
217
+
218
+
219
+
220
+ void
221
+ Init_cpArbiter(void)
222
+ {
223
+ c_cpArbiter = rb_define_class_under(m_Chipmunk, "Arbiter", rb_cObject);
224
+ /*
225
+ rb_define_alloc_func(c_cpArbiter, rb_cpArbiterAlloc);
226
+ rb_define_method(c_cpArbiter , "initialize", rb_cpArbiterInitialize, 2);
227
+ */
228
+ rb_define_method(c_cpArbiter, "a", rb_cpArbiterGetA, 0);
229
+ rb_define_method(c_cpArbiter, "b", rb_cpArbiterGetB, 0);
230
+ rb_define_method(c_cpArbiter, "e" , rb_cpArbiterGetE, 0);
231
+ rb_define_method(c_cpArbiter, "u" , rb_cpArbiterGetU, 0);
232
+ rb_define_method(c_cpArbiter, "e=", rb_cpArbiterSetE, 1);
233
+ rb_define_method(c_cpArbiter, "u=", rb_cpArbiterSetU, 1);
234
+
235
+ rb_define_method(c_cpArbiter, "point" , rb_cpArbiterGetPoint , 1);
236
+ rb_define_method(c_cpArbiter, "normal" , rb_cpArbiterGetNormal , 1);
237
+ rb_define_method(c_cpArbiter, "normal" , rb_cpArbiterGetDepth , 1);
238
+ rb_define_method(c_cpArbiter, "impulse" , rb_cpArbiterGetImpulse , -1);
239
+
240
+ rb_define_method(c_cpArbiter, "to_s", rb_cpArbiterToString, 0);
241
+
242
+ rb_define_method(c_cpArbiter, "first_contact?", rb_cpArbiterIsFirstContact, 0);
243
+ rb_define_method(c_cpArbiter, "count" , rb_cpArbiterGetCount, 0);
244
+ rb_define_method(c_cpArbiter, "contacts" , rb_cpArbiterGetCount, 0);
245
+ rb_define_method(c_cpArbiter, "size" , rb_cpArbiterGetCount, 0);
246
+ rb_define_method(c_cpArbiter, "length" , rb_cpArbiterGetCount, 0);
247
+ rb_define_method(c_cpArbiter, "each_contact" , rb_cpArbiterEachContact , 0);
248
+
249
+ rb_define_method(c_cpArbiter, "shapes" , rb_cpArbiterGetShapes , 0);
250
+ /* Ignore is new in the 5.x API, I think. */
251
+ rb_define_method(c_cpArbiter, "ignore" , rb_cpArbiterIgnore , 0);
252
+
253
+ }