chipmunk 5.2.2 → 5.3.4.0

Sign up to get free protection for your applications and to get access to all the features.
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,174 +1,210 @@
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 c_cpBB;
29
-
30
- static VALUE
31
- rb_cpBBAlloc(VALUE klass)
32
- {
33
- cpBB *bb = malloc(sizeof(cpBB));
34
- return Data_Wrap_Struct(klass, NULL, free, bb);
35
- }
36
-
37
- static VALUE
38
- rb_cpBBInitialize(VALUE self, VALUE l, VALUE b, VALUE r, VALUE t)
39
- {
40
- cpBB *bb = BBGET(self);
41
- bb->l = NUM2DBL(l);
42
- bb->b = NUM2DBL(b);
43
- bb->r = NUM2DBL(r);
44
- bb->t = NUM2DBL(t);
45
-
46
- return self;
47
- }
48
-
49
- static VALUE
50
- rb_cpBBintersects(VALUE self, VALUE other)
51
- {
52
- int bool = cpBBintersects(*BBGET(self), *BBGET(other));
53
- return bool ? Qtrue : Qfalse;
54
- }
55
-
56
- static VALUE
57
- rb_cpBBClampVect(VALUE self, VALUE v)
58
- {
59
- return VNEW(cpBBClampVect(*BBGET(self), *VGET(v)));
60
- }
61
-
62
- static VALUE
63
- rb_cpBBWrapVect(VALUE self, VALUE v)
64
- {
65
- return VNEW(cpBBWrapVect(*BBGET(self), *VGET(v)));
66
- }
67
-
68
- static VALUE
69
- rb_cpBBGetL(VALUE self)
70
- {
71
- return rb_float_new(BBGET(self)->l);
72
- }
73
-
74
- static VALUE
75
- rb_cpBBGetB(VALUE self)
76
- {
77
- return rb_float_new(BBGET(self)->b);
78
- }
79
-
80
- static VALUE
81
- rb_cpBBGetR(VALUE self)
82
- {
83
- return rb_float_new(BBGET(self)->r);
84
- }
85
-
86
- static VALUE
87
- rb_cpBBGetT(VALUE self)
88
- {
89
- return rb_float_new(BBGET(self)->t);
90
- }
91
-
92
- static VALUE
93
- rb_cpBBSetL(VALUE self, VALUE val)
94
- {
95
- BBGET(self)->l = NUM2DBL(val);
96
- return val;
97
- }
98
-
99
- static VALUE
100
- rb_cpBBSetB(VALUE self, VALUE val)
101
- {
102
- BBGET(self)->b = NUM2DBL(val);
103
- return val;
104
- }
105
-
106
- static VALUE
107
- rb_cpBBSetR(VALUE self, VALUE val)
108
- {
109
- BBGET(self)->r = NUM2DBL(val);
110
- return val;
111
- }
112
-
113
- static VALUE
114
- rb_cpBBSetT(VALUE self, VALUE val)
115
- {
116
- BBGET(self)->t = NUM2DBL(val);
117
- return val;
118
- }
119
-
120
- static VALUE
121
- rb_cpBBcontainsBB(VALUE self, VALUE other)
122
- {
123
- int bool = cpBBcontainsBB(*BBGET(self), *BBGET(other));
124
- return bool ? Qtrue : Qfalse;
125
- }
126
-
127
- static VALUE
128
- rb_cpBBcontainsVect(VALUE self, VALUE vector)
129
- {
130
- int bool = cpBBcontainsVect(*BBGET(self), *VGET(vector));
131
- return bool ? Qtrue : Qfalse;
132
- }
133
-
134
-
135
- static VALUE
136
- rb_cpBBToString(VALUE self)
137
- {
138
- char str[256];
139
- cpBB *bb = BBGET(self);
140
-
141
- sprintf(str, "#<CP::BB:(% .3f, % .3f) -> (% .3f, % .3f)>", bb->l, bb->b, bb->r, bb->t);
142
-
143
- return rb_str_new2(str);
144
- }
145
-
146
- void
147
- Init_cpBB(void)
148
- {
149
- c_cpBB = rb_define_class_under(m_Chipmunk, "BB", rb_cObject);
150
- rb_define_alloc_func(c_cpBB, rb_cpBBAlloc);
151
- rb_define_method(c_cpBB, "initialize", rb_cpBBInitialize, 4);
152
-
153
- rb_define_method(c_cpBB, "l", rb_cpBBGetL, 0);
154
- rb_define_method(c_cpBB, "b", rb_cpBBGetB, 0);
155
- rb_define_method(c_cpBB, "r", rb_cpBBGetR, 0);
156
- rb_define_method(c_cpBB, "t", rb_cpBBGetT, 0);
157
-
158
- rb_define_method(c_cpBB, "l=", rb_cpBBSetL, 1);
159
- rb_define_method(c_cpBB, "b=", rb_cpBBSetB, 1);
160
- rb_define_method(c_cpBB, "r=", rb_cpBBSetR, 1);
161
- rb_define_method(c_cpBB, "t=", rb_cpBBSetT, 1);
162
-
163
- rb_define_method(c_cpBB, "intersect?" , rb_cpBBintersects, 1);
164
- rb_define_method(c_cpBB, "contains_bb?" , rb_cpBBcontainsBB, 1);
165
- rb_define_method(c_cpBB, "contains_vect?" , rb_cpBBcontainsVect, 1);
166
-
167
-
168
- //containsBB
169
- //containsVect
170
- rb_define_method(c_cpBB, "clamp_vect", rb_cpBBClampVect, 1);
171
- rb_define_method(c_cpBB, "wrap_vect", rb_cpBBWrapVect, 1);
172
-
173
- rb_define_method(c_cpBB, "to_s", rb_cpBBToString, 0);
174
- }
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 c_cpBB;
29
+
30
+ static VALUE BBNEW(cpBB bb) {
31
+ cpBB *ptr = malloc(sizeof(cpBB));
32
+ *ptr = bb;
33
+ return Data_Wrap_Struct(c_cpBB, NULL, free, ptr);
34
+ }
35
+
36
+ static VALUE
37
+ rb_cpBBAlloc(VALUE klass)
38
+ {
39
+ cpBB *bb = malloc(sizeof(cpBB));
40
+ return Data_Wrap_Struct(klass, NULL, free, bb);
41
+ }
42
+
43
+ static VALUE
44
+ rb_cpBBInitialize(VALUE self, VALUE l, VALUE b, VALUE r, VALUE t)
45
+ {
46
+ cpBB *bb = BBGET(self);
47
+ bb->l = NUM2DBL(l);
48
+ bb->b = NUM2DBL(b);
49
+ bb->r = NUM2DBL(r);
50
+ bb->t = NUM2DBL(t);
51
+
52
+ return self;
53
+ }
54
+
55
+ static VALUE
56
+ rb_cpBBintersects(VALUE self, VALUE other)
57
+ {
58
+ int value = cpBBintersects(*BBGET(self), *BBGET(other));
59
+ return value ? Qtrue : Qfalse;
60
+ }
61
+
62
+ static VALUE
63
+ rb_cpBBcontainsBB(VALUE self, VALUE other)
64
+ {
65
+ int value = cpBBcontainsBB(*BBGET(self), *BBGET(other));
66
+ return value ? Qtrue : Qfalse;
67
+ }
68
+
69
+ static VALUE
70
+ rb_cpBBcontainsVect(VALUE self, VALUE other)
71
+ {
72
+ int value = cpBBcontainsVect(*BBGET(self), *VGET(other));
73
+ return value ? Qtrue : Qfalse;
74
+ }
75
+
76
+ static VALUE
77
+ rb_cpBBcontains(VALUE self, VALUE other)
78
+ {
79
+ if (rb_class_of(other) == c_cpBB) {
80
+ return rb_cpBBcontainsBB(self, other);
81
+ } else if (rb_class_of(other) == c_cpVect) {
82
+ return rb_cpBBcontainsVect(self, other);
83
+ }
84
+ rb_raise(rb_eArgError, "contains works only with a BB or A Vect2 argument");
85
+ return Qnil;
86
+ }
87
+
88
+ static VALUE
89
+ rb_cpBBClampVect(VALUE self, VALUE v)
90
+ {
91
+ return VNEW(cpBBClampVect(*BBGET(self), *VGET(v)));
92
+ }
93
+
94
+ static VALUE
95
+ rb_cpBBWrapVect(VALUE self, VALUE v)
96
+ {
97
+ return VNEW(cpBBWrapVect(*BBGET(self), *VGET(v)));
98
+ }
99
+
100
+ static VALUE
101
+ rb_cpBBGetL(VALUE self)
102
+ {
103
+ return rb_float_new(BBGET(self)->l);
104
+ }
105
+
106
+ static VALUE
107
+ rb_cpBBGetB(VALUE self)
108
+ {
109
+ return rb_float_new(BBGET(self)->b);
110
+ }
111
+
112
+ static VALUE
113
+ rb_cpBBGetR(VALUE self)
114
+ {
115
+ return rb_float_new(BBGET(self)->r);
116
+ }
117
+
118
+ static VALUE
119
+ rb_cpBBGetT(VALUE self)
120
+ {
121
+ return rb_float_new(BBGET(self)->t);
122
+ }
123
+
124
+ static VALUE
125
+ rb_cpBBSetL(VALUE self, VALUE val)
126
+ {
127
+ BBGET(self)->l = NUM2DBL(val);
128
+ return val;
129
+ }
130
+
131
+ static VALUE
132
+ rb_cpBBSetB(VALUE self, VALUE val)
133
+ {
134
+ BBGET(self)->b = NUM2DBL(val);
135
+ return val;
136
+ }
137
+
138
+ static VALUE
139
+ rb_cpBBSetR(VALUE self, VALUE val)
140
+ {
141
+ BBGET(self)->r = NUM2DBL(val);
142
+ return val;
143
+ }
144
+
145
+ static VALUE
146
+ rb_cpBBSetT(VALUE self, VALUE val)
147
+ {
148
+ BBGET(self)->t = NUM2DBL(val);
149
+ return val;
150
+ }
151
+
152
+ static VALUE
153
+ rb_cpBBToString(VALUE self)
154
+ {
155
+ char str[256];
156
+ cpBB *bb = BBGET(self);
157
+
158
+ sprintf(str, "#<CP::BB:(% .3f, % .3f) -> (% .3f, % .3f)>", bb->l, bb->b, bb->r, bb->t);
159
+
160
+ return rb_str_new2(str);
161
+ }
162
+
163
+ static VALUE
164
+ rb_cpBBmerge(VALUE self, VALUE other) {
165
+ return BBNEW(cpBBmerge(*BBGET(self), *BBGET(other)));
166
+ }
167
+
168
+ static VALUE
169
+ rb_cpBBexpand(VALUE self, VALUE other) {
170
+ return BBNEW(cpBBexpand(*BBGET(self), *VGET(other)));
171
+ }
172
+
173
+
174
+ void
175
+ Init_cpBB(void)
176
+ {
177
+ c_cpBB = rb_define_class_under(m_Chipmunk, "BB", rb_cObject);
178
+ rb_define_alloc_func(c_cpBB, rb_cpBBAlloc);
179
+ rb_define_method(c_cpBB, "initialize", rb_cpBBInitialize, 4);
180
+
181
+ rb_define_method(c_cpBB, "l", rb_cpBBGetL, 0);
182
+ rb_define_method(c_cpBB, "b", rb_cpBBGetB, 0);
183
+ rb_define_method(c_cpBB, "r", rb_cpBBGetR, 0);
184
+ rb_define_method(c_cpBB, "t", rb_cpBBGetT, 0);
185
+
186
+ rb_define_method(c_cpBB, "l=", rb_cpBBSetL, 1);
187
+ rb_define_method(c_cpBB, "b=", rb_cpBBSetB, 1);
188
+ rb_define_method(c_cpBB, "r=", rb_cpBBSetR, 1);
189
+ rb_define_method(c_cpBB, "t=", rb_cpBBSetT, 1);
190
+
191
+ rb_define_method(c_cpBB, "intersect?", rb_cpBBintersects, 1);
192
+ rb_define_method(c_cpBB, "intersects?", rb_cpBBintersects, 1);
193
+ //containsBB
194
+ //containsVect
195
+ rb_define_method(c_cpBB, "contains?", rb_cpBBcontains, 1);
196
+ rb_define_method(c_cpBB, "contain?", rb_cpBBcontains, 1);
197
+
198
+ rb_define_method(c_cpBB, "contains_bb?", rb_cpBBcontainsBB, 1);
199
+ rb_define_method(c_cpBB, "contain_bb?", rb_cpBBcontainsBB, 1);
200
+ rb_define_method(c_cpBB, "contains_vect?", rb_cpBBcontainsVect, 1);
201
+ rb_define_method(c_cpBB, "contain_vect?", rb_cpBBcontainsVect, 1);
202
+
203
+ rb_define_method(c_cpBB, "clamp_vect", rb_cpBBClampVect, 1);
204
+ rb_define_method(c_cpBB, "wrap_vect" , rb_cpBBWrapVect, 1);
205
+ rb_define_method(c_cpBB, "merge" , rb_cpBBmerge, 1);
206
+ rb_define_method(c_cpBB, "expand" , rb_cpBBexpand, 1);
207
+
208
+
209
+ rb_define_method(c_cpBB, "to_s", rb_cpBBToString, 0);
210
+ }
@@ -1,347 +1,504 @@
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 c_cpBody;
29
-
30
- static VALUE
31
- rb_cpBodyAlloc(VALUE klass)
32
- {
33
- cpBody *body = cpBodyNew(1.0f, 1.0f);
34
- return Data_Wrap_Struct(klass, NULL, cpBodyFree, body);
35
- }
36
-
37
- static VALUE
38
- rb_cpBodyInitialize(VALUE self, VALUE m, VALUE i)
39
- {
40
- cpBody *body = BODY(self);
41
- cpBodyInit(body, NUM2DBL(m), NUM2DBL(i));
42
-
43
- return self;
44
- }
45
-
46
- static VALUE
47
- rb_cpBodyGetMass(VALUE self)
48
- {
49
- return rb_float_new(BODY(self)->m);
50
- }
51
-
52
- static VALUE
53
- rb_cpBodyGetMassInv(VALUE self)
54
- {
55
- return rb_float_new(BODY(self)->m_inv);
56
- }
57
-
58
- static VALUE
59
- rb_cpBodyGetMoment(VALUE self)
60
- {
61
- return rb_float_new(BODY(self)->i);
62
- }
63
-
64
- static VALUE
65
- rb_cpBodyGetMomentInv(VALUE self)
66
- {
67
- return rb_float_new(BODY(self)->i_inv);
68
- }
69
-
70
- static VALUE
71
- rb_cpBodyGetPos(VALUE self)
72
- {
73
- return VWRAP(self, &BODY(self)->p);
74
- }
75
-
76
- static VALUE
77
- rb_cpBodyGetVel(VALUE self)
78
- {
79
- return VWRAP(self, &BODY(self)->v);
80
- }
81
-
82
- static VALUE
83
- rb_cpBodyGetForce(VALUE self)
84
- {
85
- return VWRAP(self, &BODY(self)->f);
86
- }
87
-
88
- static VALUE
89
- rb_cpBodyGetAngle(VALUE self)
90
- {
91
- return rb_float_new(BODY(self)->a);
92
- }
93
-
94
- static VALUE
95
- rb_cpBodyGetAVel(VALUE self)
96
- {
97
- return rb_float_new(BODY(self)->w);
98
- }
99
-
100
- static VALUE
101
- rb_cpBodyGetTorque(VALUE self)
102
- {
103
- return rb_float_new(BODY(self)->t);
104
- }
105
-
106
- static VALUE
107
- rb_cpBodyGetRot(VALUE self)
108
- {
109
- return VWRAP(self, &BODY(self)->rot);
110
- }
111
-
112
- static VALUE
113
- rb_cpBodyGetVelLimit(VALUE self)
114
- {
115
- return rb_float_new(BODY(self)->v_limit);
116
- }
117
-
118
- static VALUE
119
- rb_cpBodyGetAVelLimit(VALUE self)
120
- {
121
- return rb_float_new(BODY(self)->w_limit);
122
- }
123
-
124
-
125
- static VALUE
126
- rb_cpBodySetMass(VALUE self, VALUE val)
127
- {
128
- cpBodySetMass(BODY(self), NUM2DBL(val));
129
- return val;
130
- }
131
-
132
- static VALUE
133
- rb_cpBodySetMoment(VALUE self, VALUE val)
134
- {
135
- cpBodySetMoment(BODY(self), NUM2DBL(val));
136
- return val;
137
- }
138
-
139
- static VALUE
140
- rb_cpBodySetMomentInv(VALUE self, VALUE val)
141
- {
142
- cpBodySetMoment(BODY(self), 1.0 / NUM2DBL(val));
143
- return val;
144
- }
145
-
146
- static VALUE
147
- rb_cpBodySetPos(VALUE self, VALUE val)
148
- {
149
- BODY(self)->p = *VGET(val);
150
- return val;
151
- }
152
-
153
- static VALUE
154
- rb_cpBodySetVel(VALUE self, VALUE val)
155
- {
156
- BODY(self)->v = *VGET(val);
157
- return val;
158
- }
159
-
160
- static VALUE
161
- rb_cpBodySetForce(VALUE self, VALUE val)
162
- {
163
- BODY(self)->f = *VGET(val);
164
- return val;
165
- }
166
-
167
- static VALUE
168
- rb_cpBodySetAngle(VALUE self, VALUE val)
169
- {
170
- cpBodySetAngle(BODY(self), NUM2DBL(val));
171
- return val;
172
- }
173
-
174
- static VALUE
175
- rb_cpBodySetAVel(VALUE self, VALUE val)
176
- {
177
- BODY(self)->w = NUM2DBL(val);
178
- return val;
179
- }
180
-
181
- static VALUE
182
- rb_cpBodySetTorque(VALUE self, VALUE val)
183
- {
184
- BODY(self)->t = NUM2DBL(val);
185
- return val;
186
- }
187
-
188
- static VALUE
189
- rb_cpBodySetVelLimit(VALUE self, VALUE val)
190
- {
191
- BODY(self)->v_limit = NUM2DBL(val);
192
- return val;
193
- }
194
-
195
- static VALUE
196
- rb_cpBodySetAVelLimit(VALUE self, VALUE val)
197
- {
198
- BODY(self)->w_limit = NUM2DBL(val);
199
- return val;
200
- }
201
-
202
-
203
- static VALUE
204
- rb_cpBodyLocal2World(VALUE self, VALUE v)
205
- {
206
- return VNEW(cpBodyLocal2World(BODY(self), *VGET(v)));
207
- }
208
-
209
- static VALUE
210
- rb_cpBodyWorld2Local(VALUE self, VALUE v)
211
- {
212
- return VNEW(cpBodyWorld2Local(BODY(self), *VGET(v)));
213
- }
214
-
215
- static VALUE
216
- rb_cpBodyResetForces(VALUE self)
217
- {
218
- cpBodyResetForces(BODY(self));
219
- return self;
220
- }
221
-
222
- static VALUE
223
- rb_cpBodyApplyForce(VALUE self, VALUE f, VALUE r)
224
- {
225
- cpBodyApplyForce(BODY(self), *VGET(f), *VGET(r));
226
- return self;
227
- }
228
-
229
- static VALUE
230
- rb_cpBodyApplyImpulse(VALUE self, VALUE j, VALUE r)
231
- {
232
- cpBodyApplyImpulse(BODY(self), *VGET(j), *VGET(r));
233
- return self;
234
- }
235
-
236
- static VALUE
237
- rb_cpBodyUpdateVelocity(VALUE self, VALUE g, VALUE dmp, VALUE dt)
238
- {
239
- cpBodyUpdateVelocity(BODY(self), *VGET(g), NUM2DBL(dmp), NUM2DBL(dt));
240
- return self;
241
- }
242
-
243
- static VALUE
244
- rb_cpBodyUpdatePosition(VALUE self, VALUE dt)
245
- {
246
- cpBodyUpdatePosition(BODY(self), NUM2DBL(dt));
247
- return self;
248
- }
249
-
250
- // Intended for objects that are moved manually with a custom velocity integration function.
251
- static VALUE
252
- rb_cpBodySlew(VALUE self, VALUE pos, VALUE dt)
253
- {
254
- cpBodySlew(BODY(self), *VGET(pos), NUM2DBL(dt));
255
- return self;
256
- }
257
-
258
- static VALUE
259
- rb_cpApplyDampedSpring(VALUE klass, VALUE a, VALUE b,
260
- VALUE anchr1, VALUE anchr2, VALUE rlen, VALUE k, VALUE dmp, VALUE dt)
261
- {
262
- cpApplyDampedSpring(BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2),
263
- NUM2DBL(rlen), NUM2DBL(k), NUM2DBL(dmp), NUM2DBL(dt));
264
- return klass;
265
- }
266
-
267
-
268
- void cpApplyDampedSpring(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat rlen, cpFloat k, cpFloat dmp, cpFloat dt);
269
-
270
- void
271
- Init_cpBody(void)
272
- {
273
- c_cpBody = rb_define_class_under(m_Chipmunk, "Body", rb_cObject);
274
- rb_define_alloc_func(c_cpBody, rb_cpBodyAlloc);
275
- rb_define_method(c_cpBody, "initialize", rb_cpBodyInitialize, 2);
276
-
277
- rb_define_method(c_cpBody, "m" , rb_cpBodyGetMass, 0);
278
- rb_define_method(c_cpBody, "m_inv" , rb_cpBodyGetMassInv, 0);
279
- rb_define_method(c_cpBody, "i" , rb_cpBodyGetMoment, 0);
280
- rb_define_method(c_cpBody, "i_inv" , rb_cpBodyGetMomentInv, 0);
281
- rb_define_method(c_cpBody, "p" , rb_cpBodyGetPos, 0);
282
- rb_define_method(c_cpBody, "v" , rb_cpBodyGetVel, 0);
283
- rb_define_method(c_cpBody, "f" , rb_cpBodyGetForce, 0);
284
- rb_define_method(c_cpBody, "a" , rb_cpBodyGetAngle, 0);
285
- rb_define_method(c_cpBody, "w" , rb_cpBodyGetAVel, 0);
286
- rb_define_method(c_cpBody, "t" , rb_cpBodyGetTorque, 0);
287
- rb_define_method(c_cpBody, "rot" , rb_cpBodyGetRot, 0);
288
-
289
- rb_define_method(c_cpBody, "m=" , rb_cpBodySetMass, 1);
290
- rb_define_method(c_cpBody, "i=" , rb_cpBodySetMoment, 1);
291
- rb_define_method(c_cpBody, "p=" , rb_cpBodySetPos, 1);
292
- rb_define_method(c_cpBody, "v=" , rb_cpBodySetVel, 1);
293
- rb_define_method(c_cpBody, "f=" , rb_cpBodySetForce, 1);
294
- rb_define_method(c_cpBody, "a=" , rb_cpBodySetAngle, 1);
295
- rb_define_method(c_cpBody, "w=" , rb_cpBodySetAVel, 1);
296
- rb_define_method(c_cpBody, "t=" , rb_cpBodySetTorque, 1);
297
-
298
- rb_define_method(c_cpBody, "mass" , rb_cpBodyGetMass, 0);
299
- rb_define_method(c_cpBody, "moment" , rb_cpBodyGetMoment, 0);
300
- rb_define_method(c_cpBody, "pos" , rb_cpBodyGetPos, 0);
301
- rb_define_method(c_cpBody, "vel" , rb_cpBodyGetVel, 0);
302
- rb_define_method(c_cpBody, "force" , rb_cpBodyGetForce, 0);
303
- rb_define_method(c_cpBody, "angle" , rb_cpBodyGetAngle, 0);
304
- rb_define_method(c_cpBody, "ang_vel" , rb_cpBodyGetAVel, 0);
305
- rb_define_method(c_cpBody, "torque" , rb_cpBodyGetTorque, 0);
306
- rb_define_method(c_cpBody, "rot" , rb_cpBodyGetRot, 0);
307
- rb_define_method(c_cpBody, "v_limit" , rb_cpBodyGetVelLimit, 0);
308
- rb_define_method(c_cpBody, "w_limit" , rb_cpBodyGetAVelLimit, 0);
309
-
310
- rb_define_method(c_cpBody, "mass=" , rb_cpBodySetMass, 1);
311
- rb_define_method(c_cpBody, "moment=" , rb_cpBodySetMoment, 1);
312
- rb_define_method(c_cpBody, "pos=" , rb_cpBodySetPos, 1);
313
- rb_define_method(c_cpBody, "vel=" , rb_cpBodySetVel, 1);
314
- rb_define_method(c_cpBody, "force=" , rb_cpBodySetForce, 1);
315
- rb_define_method(c_cpBody, "angle=" , rb_cpBodySetAngle, 1);
316
- rb_define_method(c_cpBody, "ang_vel=", rb_cpBodySetAVel, 1);
317
- rb_define_method(c_cpBody, "torque=" , rb_cpBodySetTorque, 1);
318
- rb_define_method(c_cpBody, "v_limit=", rb_cpBodySetVelLimit, 1);
319
- rb_define_method(c_cpBody, "w_limit=", rb_cpBodySetAVelLimit, 1);
320
-
321
- /* Some more aliases */
322
- rb_define_method(c_cpBody, "moment_inv" , rb_cpBodyGetMomentInv, 1);
323
- rb_define_method(c_cpBody, "moment_inv=" , rb_cpBodySetMomentInv, 1);
324
- rb_define_method(c_cpBody, "ang_vel_limit" , rb_cpBodyGetAVelLimit, 0);
325
- rb_define_method(c_cpBody, "vel_limit" , rb_cpBodyGetVelLimit, 0);
326
- rb_define_method(c_cpBody, "ang_vel_limit=" , rb_cpBodySetAVelLimit, 1);
327
- rb_define_method(c_cpBody, "vel_limit=" , rb_cpBodySetVelLimit, 1);
328
-
329
-
330
- rb_define_method(c_cpBody, "local2world", rb_cpBodyLocal2World, 1);
331
- rb_define_method(c_cpBody, "world2local", rb_cpBodyWorld2Local, 1);
332
-
333
- rb_define_method(c_cpBody, "reset_forces", rb_cpBodyResetForces, 0);
334
- rb_define_method(c_cpBody, "apply_force", rb_cpBodyApplyForce, 2);
335
- rb_define_method(c_cpBody, "apply_impulse", rb_cpBodyApplyImpulse, 2);
336
-
337
- rb_define_method(c_cpBody, "update_velocity", rb_cpBodyUpdateVelocity, 3);
338
- rb_define_method(c_cpBody, "update_position", rb_cpBodyUpdatePosition, 1);
339
-
340
- /* Manual integration function */
341
- rb_define_method(c_cpBody, "slew", rb_cpBodySlew, 2);
342
-
343
- /* Damped spring function */
344
- rb_define_singleton_method(c_cpBody,
345
- "apply_damped_spring", rb_cpApplyDampedSpring, 8);
346
-
347
- }
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 c_cpBody;
29
+ VALUE c_cpStaticBody;
30
+
31
+ static VALUE
32
+ rb_cpBodyAlloc(VALUE klass)
33
+ {
34
+ cpBody *body = cpBodyNew(1.0f, 1.0f);
35
+ return Data_Wrap_Struct(klass, NULL, cpBodyFree, body);
36
+ }
37
+
38
+ static VALUE
39
+ rb_cpBodyInitialize(VALUE self, VALUE m, VALUE i)
40
+ {
41
+ cpBody *body = BODY(self);
42
+ cpBodyInit(body, NUM2DBL(m), NUM2DBL(i));
43
+ body->data = (void *)self;
44
+ return self;
45
+ }
46
+
47
+ static VALUE
48
+ rb_cpBodyAllocStatic(VALUE klass)
49
+ {
50
+ cpBody *body = cpBodyNewStatic();
51
+ return Data_Wrap_Struct(c_cpStaticBody, NULL, cpBodyFree, body);
52
+ }
53
+
54
+ static VALUE
55
+ rb_cpBodyInitializeStatic(VALUE self)
56
+ {
57
+ cpBody *body = STATICBODY(self);
58
+ cpBodyInitStatic(body);
59
+ body->data = (void *)self;
60
+ return self;
61
+ }
62
+
63
+ static VALUE
64
+ rb_cpStaticBodyNew(VALUE klass) {
65
+ return rb_cpBodyInitializeStatic(rb_cpBodyAllocStatic(klass));
66
+ }
67
+
68
+
69
+ static VALUE
70
+ rb_cpBodyGetMass(VALUE self)
71
+ {
72
+ return rb_float_new(BODY(self)->m);
73
+ }
74
+
75
+ static VALUE
76
+ rb_cpBodyGetMassInv(VALUE self)
77
+ {
78
+ return rb_float_new(BODY(self)->m_inv);
79
+ }
80
+
81
+ static VALUE
82
+ rb_cpBodyGetMoment(VALUE self)
83
+ {
84
+ return rb_float_new(BODY(self)->i);
85
+ }
86
+
87
+
88
+ static VALUE
89
+ rb_cpBodyGetMomentInv(VALUE self)
90
+ {
91
+ return rb_float_new(BODY(self)->i_inv);
92
+ }
93
+
94
+ static VALUE
95
+ rb_cpBodyGetPos(VALUE self)
96
+ {
97
+ return VWRAP(self, &BODY(self)->p);
98
+ }
99
+
100
+ static VALUE
101
+ rb_cpBodyGetVel(VALUE self)
102
+ {
103
+ return VWRAP(self, &BODY(self)->v);
104
+ }
105
+
106
+ static VALUE
107
+ rb_cpBodyGetForce(VALUE self)
108
+ {
109
+ return VWRAP(self, &BODY(self)->f);
110
+ }
111
+
112
+ static VALUE
113
+ rb_cpBodyGetAngle(VALUE self)
114
+ {
115
+ return rb_float_new(BODY(self)->a);
116
+ }
117
+
118
+ static VALUE
119
+ rb_cpBodyGetAVel(VALUE self)
120
+ {
121
+ return rb_float_new(BODY(self)->w);
122
+ }
123
+
124
+ static VALUE
125
+ rb_cpBodyGetTorque(VALUE self)
126
+ {
127
+ return rb_float_new(BODY(self)->t);
128
+ }
129
+
130
+ static VALUE
131
+ rb_cpBodyGetVLimit(VALUE self)
132
+ {
133
+ return rb_float_new(BODY(self)->v_limit);
134
+ }
135
+
136
+ static VALUE
137
+ rb_cpBodyGetWLimit(VALUE self)
138
+ {
139
+ return rb_float_new(BODY(self)->w_limit);
140
+ }
141
+
142
+
143
+ static VALUE
144
+ rb_cpBodyGetRot(VALUE self)
145
+ {
146
+ return VWRAP(self, &BODY(self)->rot);
147
+ }
148
+
149
+
150
+ static VALUE
151
+ rb_cpBodySetMass(VALUE self, VALUE val)
152
+ {
153
+ cpBodySetMass(BODY(self), NUM2DBL(val));
154
+ return val;
155
+ }
156
+
157
+ static VALUE
158
+ rb_cpBodySetMoment(VALUE self, VALUE val)
159
+ {
160
+ cpBodySetMoment(BODY(self), NUM2DBL(val));
161
+ return val;
162
+ }
163
+
164
+ static VALUE
165
+ rb_cpBodySetPos(VALUE self, VALUE val)
166
+ {
167
+ BODY(self)->p = *VGET(val);
168
+ return val;
169
+ }
170
+
171
+ static VALUE
172
+ rb_cpBodySetVel(VALUE self, VALUE val)
173
+ {
174
+ BODY(self)->v = *VGET(val);
175
+ return val;
176
+ }
177
+
178
+ static VALUE
179
+ rb_cpBodySetForce(VALUE self, VALUE val)
180
+ {
181
+ BODY(self)->f = *VGET(val);
182
+ return val;
183
+ }
184
+
185
+ static VALUE
186
+ rb_cpBodySetAngle(VALUE self, VALUE val)
187
+ {
188
+ cpBodySetAngle(BODY(self), NUM2DBL(val));
189
+ return val;
190
+ }
191
+
192
+ static VALUE
193
+ rb_cpBodySetAVel(VALUE self, VALUE val)
194
+ {
195
+ BODY(self)->w = NUM2DBL(val);
196
+ return val;
197
+ }
198
+
199
+ static VALUE
200
+ rb_cpBodySetTorque(VALUE self, VALUE val)
201
+ {
202
+ BODY(self)->t = NUM2DBL(val);
203
+ return val;
204
+ }
205
+
206
+
207
+ static VALUE
208
+ rb_cpBodySetVLimit(VALUE self, VALUE val)
209
+ {
210
+ BODY(self)->v_limit = NUM2DBL(val);
211
+ return val;
212
+ }
213
+
214
+ static VALUE
215
+ rb_cpBodySetWLimit(VALUE self, VALUE val)
216
+ {
217
+ BODY(self)->w_limit = NUM2DBL(val);
218
+ return val;
219
+ }
220
+
221
+
222
+
223
+ static VALUE
224
+ rb_cpBodyLocal2World(VALUE self, VALUE v)
225
+ {
226
+ return VNEW(cpBodyLocal2World(BODY(self), *VGET(v)));
227
+ }
228
+
229
+ static VALUE
230
+ rb_cpBodyWorld2Local(VALUE self, VALUE v)
231
+ {
232
+ return VNEW(cpBodyWorld2Local(BODY(self), *VGET(v)));
233
+ }
234
+
235
+ static VALUE
236
+ rb_cpBodyResetForces(VALUE self)
237
+ {
238
+ cpBodyResetForces(BODY(self));
239
+ return self;
240
+ }
241
+
242
+ static VALUE
243
+ rb_cpBodyApplyForce(VALUE self, VALUE f, VALUE r)
244
+ {
245
+ cpBodyApplyForce(BODY(self), *VGET(f), *VGET(r));
246
+ return self;
247
+ }
248
+
249
+ static VALUE
250
+ rb_cpBodyApplyImpulse(VALUE self, VALUE j, VALUE r)
251
+ {
252
+ cpBodyApplyImpulse(BODY(self), *VGET(j), *VGET(r));
253
+ return self;
254
+ }
255
+
256
+ static VALUE
257
+ rb_cpBodyUpdateVelocity(VALUE self, VALUE g, VALUE dmp, VALUE dt)
258
+ {
259
+ cpBodyUpdateVelocity(BODY(self), *VGET(g), NUM2DBL(dmp), NUM2DBL(dt));
260
+ return self;
261
+ }
262
+
263
+ static VALUE
264
+ rb_cpBodyUpdatePosition(VALUE self, VALUE dt)
265
+ {
266
+ cpBodyUpdatePosition(BODY(self), NUM2DBL(dt));
267
+ return self;
268
+ }
269
+
270
+ static VALUE rb_cpBodyActivate(VALUE self) {
271
+ cpBodyActivate(BODY(self));
272
+ return self;
273
+ }
274
+
275
+ static cpBody * rb_cpBodySleepValidate(VALUE vbody) {
276
+ cpBody * body = BODY(vbody);
277
+ cpSpace *space = body->space;
278
+ if(!space) {
279
+ rb_raise(rb_eArgError, "Cannot put a body to sleep that has not been added to a space.");
280
+ return NULL;
281
+ }
282
+ if (cpBodyIsStatic(body) && cpBodyIsRogue(body)) {
283
+ rb_raise(rb_eArgError, "Rogue AND static bodies cannot be put to sleep.");
284
+ return NULL;
285
+ }
286
+ if(space->locked) {
287
+ rb_raise(rb_eArgError, "Bodies can not be put to sleep during a query or a call to Space#add_collision_func. Put these calls into a post-step callback using Space#add_collision_handler.");
288
+ return NULL;
289
+ }
290
+ return body;
291
+ }
292
+
293
+ static VALUE rb_cpBodySleep(VALUE self) {
294
+ cpBody * body = rb_cpBodySleepValidate(self);
295
+ cpBodySleep(body);
296
+ return self;
297
+ }
298
+
299
+ static VALUE rb_cpBodySleepWithGroup(VALUE self, VALUE vgroup) {
300
+ cpBody * group = NIL_P(vgroup) ? NULL : rb_cpBodySleepValidate(vgroup);
301
+ cpBody * body = rb_cpBodySleepValidate(self);
302
+
303
+ if (!cpBodyIsSleeping(group)) {
304
+ rb_raise(rb_eArgError, "Cannot use a non-sleeping body as a group identifier.");
305
+ }
306
+ cpBodySleepWithGroup(body, group);
307
+ return self;
308
+ }
309
+
310
+
311
+ static VALUE rb_cpBodyIsSleeping(VALUE self) {
312
+ return cpBodyIsSleeping(BODY(self)) ? Qtrue : Qfalse;
313
+ }
314
+
315
+ static VALUE rb_cpBodyIsStatic(VALUE self) {
316
+ cpBody * body = BODY(self);
317
+ cpBool stat = 0;
318
+ // cpBodyInitStatic(body);
319
+ stat = cpBodyIsStatic(body);
320
+ return stat ? Qtrue : Qfalse;
321
+ //
322
+ }
323
+
324
+ static VALUE rb_cpBodyIsRogue(VALUE self) {
325
+ return cpBodyIsRogue(BODY(self)) ? Qtrue : Qfalse;
326
+ }
327
+
328
+ ID id_velocity_func;
329
+ ID id_speed_func;
330
+
331
+ static int
332
+ respondsTo(VALUE obj, ID method) {
333
+ VALUE value = rb_funcall(obj, rb_intern("respond_to?"), 1, ID2SYM(method));
334
+ return RTEST(value);
335
+ }
336
+
337
+ /*
338
+
339
+ typedef void (*cpBodyVelocityFunc)(struct cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt);
340
+ typedef void (*cpBodyPositionFunc)(struct cpBody *body, cpFloat dt);
341
+ */
342
+
343
+ static void
344
+ bodyVelocityCallback(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) {
345
+ VALUE vbody = (VALUE)(body->data);
346
+ VALUE block = rb_iv_get(vbody, "velocity_func");
347
+ VALUE vgravity = VNEW(gravity);
348
+ VALUE vdamping = rb_float_new(damping);
349
+ VALUE vdt = rb_float_new(dt);
350
+ rb_funcall(block, rb_intern("call"), 4, vbody, vgravity, vdamping, vdt);
351
+ }
352
+
353
+ static VALUE
354
+ rb_cpBodySetVelocityFunc(int argc, VALUE *argv, VALUE self)
355
+ {
356
+ VALUE block;
357
+ cpBody * body = BODY(self);
358
+ rb_scan_args(argc, argv, "&", &block);
359
+ // Restore defaults if no block
360
+ if (NIL_P(block)) {
361
+ body->velocity_func = cpBodyUpdateVelocityDefault;
362
+ return Qnil;
363
+ }
364
+ // set block for use in callback
365
+ rb_iv_set(self, "velocity_func", block);
366
+ body->velocity_func = bodyVelocityCallback;
367
+ return self;
368
+ }
369
+
370
+ static void
371
+ bodyPositionCallback(cpBody *body, cpFloat dt) {
372
+ VALUE vbody = (VALUE)(body->data);
373
+ VALUE block = rb_iv_get(vbody, "position_func");
374
+ VALUE vdt = rb_float_new(dt);
375
+ rb_funcall(block, rb_intern("call"), 2, vbody, vdt);
376
+ }
377
+
378
+ static VALUE
379
+ rb_cpBodySetPositionFunc(int argc, VALUE *argv, VALUE self)
380
+ {
381
+ VALUE block;
382
+ cpBody * body = BODY(self);
383
+ rb_scan_args(argc, argv, "&", &block);
384
+ // Restore defaults if no block
385
+ if (NIL_P(block)) {
386
+ body->velocity_func = cpBodyUpdateVelocityDefault;
387
+ return Qnil;
388
+ }
389
+ // set block for use in callback
390
+ rb_iv_set(self, "position_func", block);
391
+ body->position_func = bodyPositionCallback;
392
+ return self;
393
+ }
394
+
395
+ static VALUE
396
+ rb_cpBodyGetData(VALUE self) {
397
+ return rb_iv_get(self, "data");
398
+ }
399
+
400
+ static VALUE
401
+ rb_cpBodySetData(VALUE self, VALUE val) {
402
+ rb_iv_set(self, "data", val);
403
+ return val;
404
+ }
405
+
406
+
407
+ static VALUE
408
+ rb_cpBodySlew(VALUE self, VALUE pos, VALUE dt) {
409
+ cpBodySlew(BODY(self), *VGET(pos), NUM2DBL(dt));
410
+ return self;
411
+ }
412
+
413
+
414
+ void
415
+ Init_cpBody(void)
416
+ {
417
+ c_cpBody = rb_define_class_under(m_Chipmunk, "Body", rb_cObject);
418
+ rb_define_alloc_func(c_cpBody, rb_cpBodyAlloc);
419
+ rb_define_method(c_cpBody, "initialize", rb_cpBodyInitialize, 2);
420
+
421
+ c_cpStaticBody = rb_define_class_under(m_Chipmunk, "StaticBody", c_cpBody);
422
+ // rb_define_alloc_func will not work here, since superclass defines this.
423
+ // so, we define new here in stead.
424
+ rb_define_singleton_method(c_cpStaticBody, "new", rb_cpStaticBodyNew, 0);
425
+ rb_define_method(c_cpStaticBody, "initialize", rb_cpBodyInitializeStatic, 0);
426
+ rb_define_singleton_method(c_cpBody, "new_static", rb_cpStaticBodyNew, 0);
427
+
428
+ rb_define_method(c_cpBody, "m" , rb_cpBodyGetMass, 0);
429
+ rb_define_method(c_cpBody, "i" , rb_cpBodyGetMoment, 0);
430
+
431
+ rb_define_method(c_cpBody, "p" , rb_cpBodyGetPos, 0);
432
+ rb_define_method(c_cpBody, "v" , rb_cpBodyGetVel, 0);
433
+ rb_define_method(c_cpBody, "f" , rb_cpBodyGetForce, 0);
434
+ rb_define_method(c_cpBody, "a" , rb_cpBodyGetAngle, 0);
435
+ rb_define_method(c_cpBody, "w" , rb_cpBodyGetAVel, 0);
436
+ rb_define_method(c_cpBody, "t" , rb_cpBodyGetTorque, 0);
437
+ rb_define_method(c_cpBody, "rot" , rb_cpBodyGetRot, 0);
438
+
439
+ rb_define_method(c_cpBody, "m=", rb_cpBodySetMass, 1);
440
+ rb_define_method(c_cpBody, "i=", rb_cpBodySetMoment, 1);
441
+ rb_define_method(c_cpBody, "p=", rb_cpBodySetPos, 1);
442
+ rb_define_method(c_cpBody, "v=", rb_cpBodySetVel, 1);
443
+ rb_define_method(c_cpBody, "f=", rb_cpBodySetForce, 1);
444
+ rb_define_method(c_cpBody, "a=", rb_cpBodySetAngle, 1);
445
+ rb_define_method(c_cpBody, "w=", rb_cpBodySetAVel, 1);
446
+ rb_define_method(c_cpBody, "t=", rb_cpBodySetTorque, 1);
447
+
448
+ rb_define_method(c_cpBody, "mass" , rb_cpBodyGetMass, 0);
449
+ rb_define_method(c_cpBody, "moment" , rb_cpBodyGetMoment, 0);
450
+ rb_define_method(c_cpBody, "pos" , rb_cpBodyGetPos, 0);
451
+ rb_define_method(c_cpBody, "vel" , rb_cpBodyGetVel, 0);
452
+ rb_define_method(c_cpBody, "force" , rb_cpBodyGetForce, 0);
453
+ rb_define_method(c_cpBody, "angle" , rb_cpBodyGetAngle, 0);
454
+ rb_define_method(c_cpBody, "ang_vel" , rb_cpBodyGetAVel, 0);
455
+ rb_define_method(c_cpBody, "torque" , rb_cpBodyGetTorque, 0);
456
+ rb_define_method(c_cpBody, "rot", rb_cpBodyGetRot, 0);
457
+
458
+ rb_define_method(c_cpBody, "m_inv" , rb_cpBodyGetMassInv, 0);
459
+ rb_define_method(c_cpBody, "mass_inv" , rb_cpBodyGetMassInv, 0);
460
+ rb_define_method(c_cpBody, "moment_inv" , rb_cpBodyGetMomentInv, 0);
461
+ rb_define_method(c_cpBody, "v_limit" , rb_cpBodyGetVLimit, 0);
462
+ rb_define_method(c_cpBody, "w_limit" , rb_cpBodyGetWLimit, 0);
463
+
464
+
465
+ rb_define_method(c_cpBody, "mass=" , rb_cpBodySetMass, 1);
466
+ rb_define_method(c_cpBody, "moment=" , rb_cpBodySetMoment, 1);
467
+ rb_define_method(c_cpBody, "pos=" , rb_cpBodySetPos, 1);
468
+ rb_define_method(c_cpBody, "vel=" , rb_cpBodySetVel, 1);
469
+ rb_define_method(c_cpBody, "force=" , rb_cpBodySetForce, 1);
470
+ rb_define_method(c_cpBody, "angle=" , rb_cpBodySetAngle, 1);
471
+ rb_define_method(c_cpBody, "ang_vel=" , rb_cpBodySetAVel, 1);
472
+ rb_define_method(c_cpBody, "torque=" , rb_cpBodySetTorque, 1);
473
+ rb_define_method(c_cpBody, "v_limit=" , rb_cpBodySetVLimit, 1);
474
+ rb_define_method(c_cpBody, "w_limit=" , rb_cpBodySetWLimit, 1);
475
+
476
+
477
+ rb_define_method(c_cpBody, "local2world", rb_cpBodyLocal2World, 1);
478
+ rb_define_method(c_cpBody, "world2local", rb_cpBodyWorld2Local, 1);
479
+
480
+ rb_define_method(c_cpBody, "reset_forces", rb_cpBodyResetForces, 0);
481
+ rb_define_method(c_cpBody, "apply_force", rb_cpBodyApplyForce, 2);
482
+ rb_define_method(c_cpBody, "apply_impulse", rb_cpBodyApplyImpulse, 2);
483
+
484
+ rb_define_method(c_cpBody, "update_velocity", rb_cpBodyUpdateVelocity, 3);
485
+ rb_define_method(c_cpBody, "update_position", rb_cpBodyUpdatePosition, 1);
486
+ rb_define_method(c_cpBody, "slew" , rb_cpBodySlew , 2);
487
+
488
+ rb_define_method(c_cpBody, "static?", rb_cpBodyIsStatic, 0);
489
+ rb_define_method(c_cpBody, "rogue?", rb_cpBodyIsRogue, 0);
490
+ rb_define_method(c_cpBody, "sleeping?", rb_cpBodyIsSleeping, 0);
491
+ rb_define_method(c_cpBody, "sleep?", rb_cpBodyIsSleeping, 0);
492
+ rb_define_method(c_cpBody, "sleep_with_self" , rb_cpBodySleep, 0);
493
+ rb_define_method(c_cpBody, "sleep_self" , rb_cpBodySleep, 0);
494
+ rb_define_method(c_cpBody, "sleep_alone" , rb_cpBodySleep, 0);
495
+ rb_define_method(c_cpBody, "sleep_with_group", rb_cpBodySleepWithGroup, 1);
496
+ rb_define_method(c_cpBody, "sleep_group" , rb_cpBodySleepWithGroup, 1);
497
+ rb_define_method(c_cpBody, "activate" , rb_cpBodyActivate , 0);
498
+ rb_define_method(c_cpBody, "velocity_func" , rb_cpBodySetVelocityFunc, -1);
499
+ rb_define_method(c_cpBody, "position_func" , rb_cpBodySetPositionFunc, -1);
500
+
501
+ rb_define_method(c_cpBody, "object=" , rb_cpBodySetData, 1);
502
+ rb_define_method(c_cpBody, "object" , rb_cpBodyGetData, 0);
503
+
504
+ }