chipmunk 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ /* Copyright (c) 2007 Scott Lembcke
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+ #include "chipmunk.h"
23
+
24
+ #include "ruby.h"
25
+ #include "rb_chipmunk.h"
26
+
27
+ VALUE m_cpJoint;
28
+
29
+ static VALUE
30
+ rb_cpPinJointAlloc(VALUE klass)
31
+ {
32
+ cpPinJoint *joint = cpPinJointAlloc();
33
+ VALUE self = Data_Wrap_Struct(klass, NULL, cpJointFree, joint);
34
+
35
+ return self;
36
+ }
37
+
38
+ static VALUE
39
+ rb_cpPinJointInit(VALUE self, VALUE a, VALUE b, VALUE anchr1, VALUE anchr2)
40
+ {
41
+ cpPinJoint *joint = (cpPinJoint *)JOINT(self);
42
+ cpPinJointInit(joint, BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2));
43
+ rb_iv_set(self, "body_a", a);
44
+ rb_iv_set(self, "body_b", b);
45
+
46
+ return self;
47
+ }
48
+
49
+
50
+ static VALUE
51
+ rb_cpSlideJointAlloc(VALUE klass)
52
+ {
53
+ cpSlideJoint *joint = cpSlideJointAlloc();
54
+ VALUE self = Data_Wrap_Struct(klass, NULL, cpJointFree, joint);
55
+
56
+ return self;
57
+ }
58
+
59
+ static VALUE
60
+ rb_cpSlideJointInit(VALUE self, VALUE a, VALUE b, VALUE anchr1, VALUE anchr2, VALUE min, VALUE max)
61
+ {
62
+ cpSlideJoint *joint = (cpSlideJoint *)JOINT(self);
63
+ cpSlideJointInit(joint, BODY(a), BODY(b), *VGET(anchr1), *VGET(anchr2), NUM2DBL(min), NUM2DBL(max));
64
+ rb_iv_set(self, "body_a", a);
65
+ rb_iv_set(self, "body_b", b);
66
+
67
+ return self;
68
+ }
69
+
70
+
71
+ static VALUE
72
+ rb_cpPivotJointAlloc(VALUE klass)
73
+ {
74
+ cpPivotJoint *joint = cpPivotJointAlloc();
75
+ VALUE self = Data_Wrap_Struct(klass, NULL, cpJointFree, joint);
76
+
77
+ return self;
78
+ }
79
+
80
+ static VALUE
81
+ rb_cpPivotJointInit(VALUE self, VALUE a, VALUE b, VALUE pivot)
82
+ {
83
+ cpPivotJoint *joint = (cpPivotJoint *)JOINT(self);
84
+ cpPivotJointInit(joint, BODY(a), BODY(b), *VGET(pivot));
85
+ rb_iv_set(self, "body_a", a);
86
+ rb_iv_set(self, "body_b", b);
87
+
88
+ return self;
89
+ }
90
+
91
+
92
+ static VALUE
93
+ rb_cpGrooveJointAlloc(VALUE klass)
94
+ {
95
+ cpGrooveJoint *joint = cpGrooveJointAlloc();
96
+ VALUE self = Data_Wrap_Struct(klass, NULL, cpJointFree, joint);
97
+
98
+ return self;
99
+ }
100
+
101
+ static VALUE
102
+ rb_cpGrooveJointInit(VALUE self, VALUE a, VALUE b, VALUE grv_a, VALUE grv_b, VALUE anchr2)
103
+ {
104
+ cpGrooveJoint *joint = (cpGrooveJoint *)JOINT(self);
105
+ cpGrooveJointInit(joint, BODY(a), BODY(b), *VGET(grv_a), *VGET(grv_b), *VGET(anchr2));
106
+ rb_iv_set(self, "body_a", a);
107
+ rb_iv_set(self, "body_b", b);
108
+
109
+ return self;
110
+ }
111
+
112
+ void
113
+ Init_cpJoint(void)
114
+ {
115
+ m_cpJoint = rb_define_module_under(m_Chipmunk, "Joint");
116
+
117
+ VALUE c_cpPinJoint = rb_define_class_under(m_cpJoint, "Pin", rb_cObject);
118
+ rb_include_module(c_cpPinJoint, m_cpJoint);
119
+ rb_define_alloc_func(c_cpPinJoint, rb_cpPinJointAlloc);
120
+ rb_define_method(c_cpPinJoint, "initialize", rb_cpPinJointInit, 4);
121
+
122
+ VALUE c_cpSlideJoint = rb_define_class_under(m_cpJoint, "Slide", rb_cObject);
123
+ rb_include_module(c_cpSlideJoint, m_cpJoint);
124
+ rb_define_alloc_func(c_cpSlideJoint, rb_cpSlideJointAlloc);
125
+ rb_define_method(c_cpSlideJoint, "initialize", rb_cpSlideJointInit, 6);
126
+
127
+ VALUE c_cpPivotJoint = rb_define_class_under(m_cpJoint, "Pivot", rb_cObject);
128
+ rb_include_module(c_cpPivotJoint, m_cpJoint);
129
+ rb_define_alloc_func(c_cpPivotJoint, rb_cpPivotJointAlloc);
130
+ rb_define_method(c_cpPivotJoint, "initialize", rb_cpPivotJointInit, 3);
131
+
132
+ VALUE c_cpGrooveJoint = rb_define_class_under(m_cpJoint, "Groove", rb_cObject);
133
+ rb_include_module(c_cpGrooveJoint, m_cpJoint);
134
+ rb_define_alloc_func(c_cpGrooveJoint, rb_cpGrooveJointAlloc);
135
+ rb_define_method(c_cpGrooveJoint, "initialize", rb_cpGrooveJointInit, 5);
136
+ }
@@ -0,0 +1,292 @@
1
+ /* Copyright (c) 2007 Scott Lembcke
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+ #include "chipmunk.h"
23
+
24
+ #include "ruby.h"
25
+ #include "rb_chipmunk.h"
26
+
27
+ ID id_body;
28
+
29
+ VALUE m_cpShape;
30
+ VALUE c_cpCircleShape;
31
+ VALUE c_cpSegmentShape;
32
+ VALUE c_cpPolyShape;
33
+
34
+
35
+
36
+ static VALUE
37
+ rb_cpShapeGetBody(VALUE self)
38
+ {
39
+ return rb_ivar_get(self, id_body);
40
+ }
41
+
42
+ static VALUE
43
+ rb_cpShapeSetBody(VALUE self, VALUE body)
44
+ {
45
+ SHAPE(self)->body = BODY(body);
46
+ rb_ivar_set(self, id_body, body);
47
+
48
+ return body;
49
+ }
50
+
51
+ static VALUE
52
+ rb_cpShapeGetCollType(VALUE self)
53
+ {
54
+ return rb_iv_get(self, "collType");
55
+ }
56
+
57
+ static VALUE
58
+ rb_cpShapeSetCollType(VALUE self, VALUE val)
59
+ {
60
+ VALUE col_type = rb_obj_id(val);
61
+ rb_iv_set(self, "collType", val);
62
+ SHAPE(self)->collision_type = NUM2UINT(col_type);
63
+
64
+ return val;
65
+ }
66
+
67
+ static VALUE
68
+ rb_cpShapeGetGroup(VALUE self)
69
+ {
70
+ return rb_iv_get(self, "group");
71
+ }
72
+
73
+ static VALUE
74
+ rb_cpShapeSetGroup(VALUE self, VALUE val)
75
+ {
76
+ VALUE col_type = rb_obj_id(val);
77
+ rb_iv_set(self, "group", val);
78
+ SHAPE(self)->group = NUM2UINT(col_type);
79
+
80
+ return val;
81
+ }
82
+
83
+ static VALUE
84
+ rb_cpShapeGetLayers(VALUE self)
85
+ {
86
+ return UINT2NUM(SHAPE(self)->layers);
87
+ }
88
+
89
+ static VALUE
90
+ rb_cpShapeSetLayers(VALUE self, VALUE layers)
91
+ {
92
+ SHAPE(self)->layers = NUM2UINT(layers);
93
+
94
+ return layers;
95
+ }
96
+
97
+ static VALUE
98
+ rb_cpShapeGetBB(VALUE self)
99
+ {
100
+ cpBB *bb = malloc(sizeof(cpBB));
101
+ *bb = SHAPE(self)->bb;
102
+ return Data_Wrap_Struct(c_cpBB, NULL, free, bb);
103
+ }
104
+
105
+ static VALUE
106
+ rb_cpShapeCacheBB(VALUE self)
107
+ {
108
+ cpShape *shape = SHAPE(self);
109
+ cpShapeCacheBB(shape);
110
+
111
+ return rb_cpShapeGetBB(self);
112
+ }
113
+
114
+ static VALUE
115
+ rb_cpShapeGetElasticity(VALUE self)
116
+ {
117
+ return rb_float_new(SHAPE(self)->e);
118
+ }
119
+
120
+ static VALUE
121
+ rb_cpShapeGetFriction(VALUE self)
122
+ {
123
+ return rb_float_new(SHAPE(self)->u);
124
+ }
125
+
126
+ static VALUE
127
+ rb_cpShapeSetElasticity(VALUE self, VALUE val)
128
+ {
129
+ SHAPE(self)->e = NUM2DBL(val);
130
+ return val;
131
+ }
132
+
133
+ static VALUE
134
+ rb_cpShapeSetFriction(VALUE self, VALUE val)
135
+ {
136
+ SHAPE(self)->u = NUM2DBL(val);
137
+ return val;
138
+ }
139
+
140
+ static VALUE
141
+ rb_cpShapeGetSurfaceV(VALUE self)
142
+ {
143
+ return VWRAP(self, &SHAPE(self)->surface_v);
144
+ }
145
+
146
+ static VALUE
147
+ rb_cpShapeSetSurfaceV(VALUE self, VALUE val)
148
+ {
149
+ SHAPE(self)->surface_v = *VGET(val);
150
+ return val;
151
+ }
152
+
153
+ static VALUE
154
+ rb_cpShapeResetIdCounter(VALUE self)
155
+ {
156
+ cpResetShapeIdCounter();
157
+ return Qnil;
158
+ }
159
+
160
+
161
+
162
+ //cpCircle
163
+ static VALUE
164
+ rb_cpCircleAlloc(VALUE klass)
165
+ {
166
+ cpCircleShape *circle = cpCircleShapeAlloc();
167
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, circle);
168
+ }
169
+
170
+ static VALUE
171
+ rb_cpCircleInitialize(VALUE self, VALUE body, VALUE radius, VALUE offset)
172
+ {
173
+ cpCircleShape *circle = (cpCircleShape *)SHAPE(self);
174
+
175
+ cpCircleShapeInit(circle, BODY(body), NUM2DBL(radius), *VGET(offset));
176
+ circle->shape.data = (void *)self;
177
+ circle->shape.collision_type = Qnil;
178
+
179
+ rb_ivar_set(self, id_body, body);
180
+
181
+ return self;
182
+ }
183
+
184
+
185
+
186
+ //cpSegment
187
+ static VALUE
188
+ rb_cpSegmentAlloc(VALUE klass)
189
+ {
190
+ cpSegmentShape *seg = cpSegmentShapeAlloc();
191
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, seg);
192
+ }
193
+
194
+ static VALUE
195
+ rb_cpSegmentInitialize(VALUE self, VALUE body, VALUE a, VALUE b, VALUE r)
196
+ {
197
+ cpSegmentShape *seg = (cpSegmentShape *)SHAPE(self);
198
+
199
+ cpSegmentShapeInit(seg, BODY(body), *VGET(a), *VGET(b), NUM2DBL(r));
200
+ seg->shape.data = (void *)self;
201
+ seg->shape.collision_type = Qnil;
202
+
203
+ rb_ivar_set(self, id_body, body);
204
+
205
+ return self;
206
+ }
207
+
208
+
209
+
210
+ //cpPoly
211
+ static VALUE
212
+ rb_cpPolyAlloc(VALUE klass)
213
+ {
214
+ cpPolyShape *poly = cpPolyShapeAlloc();
215
+ return Data_Wrap_Struct(klass, NULL, cpShapeFree, poly);
216
+ }
217
+
218
+ static VALUE
219
+ rb_cpPolyInitialize(VALUE self, VALUE body, VALUE arr, VALUE offset)
220
+ {
221
+ cpPolyShape *poly = (cpPolyShape *)SHAPE(self);
222
+
223
+ Check_Type(arr, T_ARRAY);
224
+ int numVerts = RARRAY_LEN(arr);
225
+ cpVect verts[numVerts];
226
+
227
+ for(int i=0; i<numVerts; i++)
228
+ verts[i] = *VGET(RARRAY_PTR(arr)[i]);
229
+
230
+ cpPolyShapeInit(poly, BODY(body), numVerts, verts, *VGET(offset));
231
+ poly->shape.data = (void *)self;
232
+ poly->shape.collision_type = Qnil;
233
+
234
+ rb_ivar_set(self, id_body, body);
235
+
236
+ return self;
237
+ }
238
+
239
+
240
+
241
+ void
242
+ Init_cpShape(void)
243
+ {
244
+ id_body = rb_intern("body");
245
+
246
+ m_cpShape = rb_define_module_under(m_Chipmunk, "Shape");
247
+ rb_define_attr(m_cpShape, "obj", 1, 1);
248
+
249
+ rb_define_method(m_cpShape, "body", rb_cpShapeGetBody, 0);
250
+ rb_define_method(m_cpShape, "body=", rb_cpShapeSetBody, 1);
251
+
252
+ rb_define_method(m_cpShape, "collision_type", rb_cpShapeGetCollType, 0);
253
+ rb_define_method(m_cpShape, "collision_type=", rb_cpShapeSetCollType, 1);
254
+
255
+ rb_define_method(m_cpShape, "group", rb_cpShapeGetGroup, 0);
256
+ rb_define_method(m_cpShape, "group=", rb_cpShapeSetGroup, 1);
257
+
258
+ rb_define_method(m_cpShape, "layers", rb_cpShapeGetLayers, 0);
259
+ rb_define_method(m_cpShape, "layers=", rb_cpShapeSetLayers, 1);
260
+
261
+ rb_define_method(m_cpShape, "bb", rb_cpShapeGetBB, 0);
262
+ rb_define_method(m_cpShape, "cache_bb", rb_cpShapeCacheBB, 0);
263
+
264
+ rb_define_method(m_cpShape, "e", rb_cpShapeGetElasticity, 0);
265
+ rb_define_method(m_cpShape, "u", rb_cpShapeGetFriction, 0);
266
+
267
+ rb_define_method(m_cpShape, "e=", rb_cpShapeSetElasticity, 1);
268
+ rb_define_method(m_cpShape, "u=", rb_cpShapeSetFriction, 1);
269
+
270
+ rb_define_method(m_cpShape, "surface_v", rb_cpShapeGetSurfaceV, 0);
271
+ rb_define_method(m_cpShape, "surface_v=", rb_cpShapeSetSurfaceV, 1);
272
+
273
+ rb_define_singleton_method(m_cpShape, "reset_id_counter", rb_cpShapeResetIdCounter, 0);
274
+
275
+
276
+ c_cpCircleShape = rb_define_class_under(m_cpShape, "Circle", rb_cObject);
277
+ rb_include_module(c_cpCircleShape, m_cpShape);
278
+ rb_define_alloc_func(c_cpCircleShape, rb_cpCircleAlloc);
279
+ rb_define_method(c_cpCircleShape, "initialize", rb_cpCircleInitialize, 3);
280
+
281
+
282
+ c_cpSegmentShape = rb_define_class_under(m_cpShape, "Segment", rb_cObject);
283
+ rb_include_module(c_cpSegmentShape, m_cpShape);
284
+ rb_define_alloc_func(c_cpSegmentShape, rb_cpSegmentAlloc);
285
+ rb_define_method(c_cpSegmentShape, "initialize", rb_cpSegmentInitialize, 4);
286
+
287
+
288
+ c_cpPolyShape = rb_define_class_under(m_cpShape, "Poly", rb_cObject);
289
+ rb_include_module(c_cpPolyShape, m_cpShape);
290
+ rb_define_alloc_func(c_cpPolyShape, rb_cpPolyAlloc);
291
+ rb_define_method(c_cpPolyShape, "initialize", rb_cpPolyInitialize, 3);
292
+ }