chipmunk-ffi 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,71 @@
1
1
  require File.dirname(__FILE__)+'/spec_helper'
2
2
  describe 'Constraints in chipmunk' do
3
- describe 'PinJoint class' do
3
+ describe 'PinJoint class' do
4
4
  it 'can be created' do
5
5
  boda = Body.new 90, 46
6
6
  bodb = Body.new 9, 6
7
7
  CP::PinJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
8
8
  end
9
+
10
+ it 'can get its bodies' do
11
+ boda = Body.new 90, 46
12
+ bodb = Body.new 9, 6
13
+ joint = CP::PinJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
14
+ joint.body_a.should be boda
15
+ joint.body_b.should be bodb
16
+ end
17
+
18
+ it 'can set and get its max_force' do
19
+ boda = Body.new 90, 46
20
+ bodb = Body.new 9, 6
21
+ joint = CP::PinJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
22
+ joint.max_force = 40
23
+ joint.max_force.should == 40.0
24
+ end
25
+
26
+ it 'can set and get its max_bias' do
27
+ boda = Body.new 90, 46
28
+ bodb = Body.new 9, 6
29
+ joint = CP::PinJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
30
+ joint.max_bias = 40
31
+ joint.max_bias.should == 40.0
32
+ end
33
+
34
+ it 'can set and get its bias_coef' do
35
+ boda = Body.new 90, 46
36
+ bodb = Body.new 9, 6
37
+ joint = CP::PinJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
38
+ joint.bias_coef = 40
39
+ joint.bias_coef.should == 40.0
40
+ end
41
+
42
+ it 'can get and set its anchors' do
43
+ boda = Body.new 90, 46
44
+ bodb = Body.new 9, 6
45
+ v1 = vec2(1,2)
46
+ v2 = vec2(3,4)
47
+ v3 = vec2(5,6)
48
+ v4 = vec2(7,8)
49
+
50
+ joint = CP::PinJoint.new(boda,bodb,v1,v2)
51
+ joint.anchr1.should == v1
52
+ joint.anchr2.should == v2
53
+
54
+ joint.anchr1 = v3
55
+ joint.anchr2 = v4
56
+ joint.anchr1.should == v3
57
+ joint.anchr2.should == v4
58
+ end
59
+
60
+ it 'can get and set its dist' do
61
+ boda = CP::Body.new 90, 46
62
+ bodb = CP::Body.new 9, 6
63
+ joint = CP::PinJoint.new(boda,bodb,vec2(3,4),ZERO_VEC_2)
64
+ joint.dist.should == 5.0
65
+ joint.dist = 1
66
+ joint.dist.should == 1.0
67
+ end
68
+
9
69
  end
10
70
 
11
71
  describe 'SlideJoint class' do
@@ -14,6 +74,36 @@ describe 'Constraints in chipmunk' do
14
74
  bodb = Body.new 9, 6
15
75
  CP::SlideJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,4,6)
16
76
  end
77
+
78
+ it 'can get and set its anchors' do
79
+ boda = Body.new 90, 46
80
+ bodb = Body.new 9, 6
81
+ v1 = vec2(1,2)
82
+ v2 = vec2(3,4)
83
+ v3 = vec2(5,6)
84
+ v4 = vec2(7,8)
85
+
86
+ joint = CP::SlideJoint.new(boda,bodb,v1,v2,4,6)
87
+ joint.anchr1.should == v1
88
+ joint.anchr2.should == v2
89
+
90
+ joint.anchr1 = v3
91
+ joint.anchr2 = v4
92
+ joint.anchr1.should == v3
93
+ joint.anchr2.should == v4
94
+ end
95
+
96
+ it 'can get and set its min and max' do
97
+ boda = Body.new 90, 46
98
+ bodb = Body.new 9, 6
99
+ joint = CP::SlideJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,4,6)
100
+ joint.min.should == 4
101
+ joint.max.should == 6
102
+ joint.min = 7
103
+ joint.max = 8
104
+ joint.min.should == 7
105
+ joint.max.should == 8
106
+ end
17
107
  end
18
108
 
19
109
  describe 'PivotJoint class' do
@@ -22,6 +112,24 @@ describe 'Constraints in chipmunk' do
22
112
  bodb = Body.new 9, 6
23
113
  CP::PivotJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
24
114
  end
115
+
116
+ it 'can get and set its anchors' do
117
+ boda = Body.new 90, 46
118
+ bodb = Body.new 9, 6
119
+ v1 = vec2(1,2)
120
+ v2 = vec2(3,4)
121
+ v3 = vec2(5,6)
122
+ v4 = vec2(7,8)
123
+
124
+ joint = CP::PivotJoint.new(boda,bodb,v1,v2)
125
+ joint.anchr1.should == v1
126
+ joint.anchr2.should == v2
127
+
128
+ joint.anchr1 = v3
129
+ joint.anchr2 = v4
130
+ joint.anchr1.should == v3
131
+ joint.anchr2.should == v4
132
+ end
25
133
  end
26
134
 
27
135
  describe 'GrooveJoint class' do
@@ -30,6 +138,17 @@ describe 'Constraints in chipmunk' do
30
138
  bodb = Body.new 9, 6
31
139
  CP::GrooveJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,ZERO_VEC_2)
32
140
  end
141
+
142
+ it 'can get and set anchr2' do
143
+ boda = Body.new 90, 46
144
+ bodb = Body.new 9, 6
145
+ v1 = vec2(1,2)
146
+ v2 = vec2(3,4)
147
+ joint = CP::GrooveJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,v1)
148
+ joint.anchr2.should == v1
149
+ joint.anchr2 = v2
150
+ joint.anchr2.should == v2
151
+ end
33
152
  end
34
153
 
35
154
  describe 'DampedSpring class' do
@@ -38,6 +157,60 @@ describe 'Constraints in chipmunk' do
38
157
  bodb = Body.new 9, 6
39
158
  CP::DampedSpring.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,3,4,5)
40
159
  end
160
+
161
+ it 'can get and set its anchors' do
162
+ boda = Body.new 90, 46
163
+ bodb = Body.new 9, 6
164
+ v1 = vec2(1,2)
165
+ v2 = vec2(3,4)
166
+ v3 = vec2(5,6)
167
+ v4 = vec2(7,8)
168
+
169
+ joint = CP::DampedSpring.new(boda,bodb,v1,v2,3,4,5)
170
+ joint.anchr1.should == v1
171
+ joint.anchr2.should == v2
172
+
173
+ joint.anchr1 = v3
174
+ joint.anchr2 = v4
175
+ joint.anchr1.should == v3
176
+ joint.anchr2.should == v4
177
+ end
178
+
179
+ it 'can get and set its rest length' do
180
+ boda = CP::Body.new 90, 46
181
+ bodb = CP::Body.new 9, 6
182
+ joint = CP::DampedSpring.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,3,4,5)
183
+ joint.rest_length.should == 3.0
184
+ joint.rest_length = 1
185
+ joint.rest_length.should == 1.0
186
+ end
187
+
188
+ it 'can get and set its stiffness' do
189
+ boda = CP::Body.new 90, 46
190
+ bodb = CP::Body.new 9, 6
191
+ joint = CP::DampedSpring.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,3,4,5)
192
+ joint.stiffness.should == 4.0
193
+ joint.stiffness = 1
194
+ joint.stiffness.should == 1.0
195
+ end
196
+
197
+ it 'can get and set its damping' do
198
+ boda = CP::Body.new 90, 46
199
+ bodb = CP::Body.new 9, 6
200
+ joint = CP::DampedSpring.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,3,4,5)
201
+ joint.damping.should == 5.0
202
+ joint.damping = 1
203
+ joint.damping.should == 1.0
204
+ end
205
+
206
+ it 'can get and set its spring force function' do
207
+ boda = CP::Body.new 90, 46
208
+ bodb = CP::Body.new 9, 6
209
+ joint = CP::DampedSpring.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,3,4,5)
210
+ joint.spring_force_func.call(joint,1.0).should == 8.0
211
+ joint.spring_force_func = lambda {|spring,float| float + 1.0 }
212
+ joint.spring_force_func.call(joint,1.0).should == 2.0
213
+ end
41
214
  end
42
215
 
43
216
  describe 'RotaryLimitJoint class' do
@@ -46,6 +219,18 @@ describe 'Constraints in chipmunk' do
46
219
  bodb = Body.new 9, 6
47
220
  CP::RotaryLimitJoint.new(boda,bodb,Math::PI,Math::PI/2)
48
221
  end
222
+
223
+ it 'can get and set min and max' do
224
+ boda = Body.new 90, 46
225
+ bodb = Body.new 9, 6
226
+ joint = CP::RotaryLimitJoint.new(boda,bodb,Math::PI,Math::PI/2)
227
+ joint.min.should == Math::PI
228
+ joint.max.should == Math::PI/2
229
+ joint.min = 0
230
+ joint.max = 1
231
+ joint.min.should == 0
232
+ joint.max.should == 1
233
+ end
49
234
  end
50
235
 
51
236
  describe 'RatchetJoint class' do
@@ -54,6 +239,34 @@ describe 'Constraints in chipmunk' do
54
239
  bodb = Body.new 9, 6
55
240
  CP::RatchetJoint.new(boda,bodb,3,4)
56
241
  end
242
+
243
+ it 'can get and set its angle' do
244
+ boda = Body.new 90, 46
245
+ bodb = Body.new 9, 6
246
+ joint = CP::RatchetJoint.new(boda,bodb,3,4)
247
+ joint.angle.should == 0
248
+ joint.angle = Math::PI/2
249
+ joint.angle.should == Math::PI/2
250
+ end
251
+
252
+ it 'can get and set its phase' do
253
+ boda = Body.new 90, 46
254
+ bodb = Body.new 9, 6
255
+ joint = CP::RatchetJoint.new(boda,bodb,3,4)
256
+ joint.phase.should == 3.0
257
+ joint.phase = 5.0
258
+ joint.phase.should == 5.0
259
+ end
260
+
261
+ it 'can get and set its ratchet' do
262
+ boda = Body.new 90, 46
263
+ bodb = Body.new 9, 6
264
+ joint = CP::RatchetJoint.new(boda,bodb,3,4)
265
+ joint.ratchet.should == 4.0
266
+ joint.ratchet = 6.0
267
+ joint.ratchet.should == 6.0
268
+ end
269
+
57
270
  end
58
271
 
59
272
  describe 'GearJoint class' do
@@ -62,6 +275,26 @@ describe 'Constraints in chipmunk' do
62
275
  bodb = Body.new 9, 6
63
276
  CP::GearJoint.new(boda,bodb,1,2)
64
277
  end
278
+
279
+ it 'can get and set its phase' do
280
+ boda = Body.new 90, 46
281
+ bodb = Body.new 9, 6
282
+ joint = CP::GearJoint.new(boda,bodb,1,2)
283
+ joint.phase.should == 1.0
284
+ joint.phase = 5.0
285
+ joint.phase.should == 5.0
286
+ end
287
+
288
+ it 'can get and set its ratio' do
289
+ boda = Body.new 90, 46
290
+ bodb = Body.new 9, 6
291
+ joint = CP::GearJoint.new(boda,bodb,1,2)
292
+ joint.ratio.should == 2.0
293
+ joint.ratio = 6.0
294
+ joint.ratio.should == 6.0
295
+ joint.struct.ratio_inv.should be_close(1.0/6.0, 0.01)
296
+ end
297
+
65
298
  end
66
299
 
67
300
  describe 'SimpleMotor class' do
@@ -70,5 +303,60 @@ describe 'Constraints in chipmunk' do
70
303
  bodb = Body.new 9, 6
71
304
  CP::SimpleMotor.new(boda,bodb,2)
72
305
  end
306
+
307
+ it 'can get and set its rate' do
308
+ boda = Body.new 90, 46
309
+ bodb = Body.new 9, 6
310
+ joint = CP::SimpleMotor.new(boda,bodb,2)
311
+ joint.rate.should == 2.0
312
+ joint.rate = -2.0
313
+ joint.rate.should == -2.0
314
+ end
315
+ end
316
+
317
+ describe 'DampedRotarySpring class' do
318
+ it 'can be created' do
319
+ boda = Body.new 90, 46
320
+ bodb = Body.new 9, 6
321
+ CP::DampedRotarySpring.new(boda,bodb,3,4,5)
322
+ end
323
+
324
+ it 'can get and set its rest angle' do
325
+ boda = CP::Body.new 90, 46
326
+ bodb = CP::Body.new 9, 6
327
+ joint = CP::DampedRotarySpring.new(boda,bodb,3,4,5)
328
+ joint.rest_angle.should == 3.0
329
+ joint.rest_angle = 1
330
+ joint.rest_angle.should == 1.0
331
+ end
332
+
333
+ it 'can get and set its stiffness' do
334
+ boda = CP::Body.new 90, 46
335
+ bodb = CP::Body.new 9, 6
336
+ joint = CP::DampedRotarySpring.new(boda,bodb,3,4,5)
337
+ joint.stiffness.should == 4.0
338
+ joint.stiffness = 1
339
+ joint.stiffness.should == 1.0
340
+ end
341
+
342
+ it 'can get and set its damping' do
343
+ boda = CP::Body.new 90, 46
344
+ bodb = CP::Body.new 9, 6
345
+ joint = CP::DampedRotarySpring.new(boda,bodb,3,4,5)
346
+ joint.damping.should == 5.0
347
+ joint.damping = 1
348
+ joint.damping.should == 1.0
349
+ end
350
+
351
+ it 'can get and set its spring torque function' do
352
+ boda = CP::Body.new 90, 46
353
+ bodb = CP::Body.new 9, 6
354
+ joint = CP::DampedRotarySpring.new(boda,bodb,3,4,5)
355
+ joint.spring_torque_func.call(joint,1.0).should == -8.0
356
+ joint.spring_torque_func = lambda {|spring,float| float + 1.0 }
357
+ joint.spring_torque_func.call(joint,1.0).should == 2.0
358
+ end
73
359
  end
360
+
361
+
74
362
  end
@@ -129,7 +129,72 @@ describe 'ShapeStruct in chipmunk' do
129
129
  describe 'Poly class' do
130
130
  it 'can be created' do
131
131
  bod = CP::Body.new 90, 76
132
- s = CP::Shape::Poly.new bod, [vec2(1,1), vec2(2,2),vec2(3,3)], CP::ZERO_VEC_2
132
+ s = CP::Shape::Poly.new bod, [vec2(0,0), vec2(1,-1),vec2(-1,-1)], CP::ZERO_VEC_2
133
+ end
134
+
135
+ it 'can strictly validate vertices' do
136
+ verts = [
137
+ vec2(-1,-1),
138
+ vec2(-1, 1),
139
+ vec2( 1, 1),
140
+ vec2( 1,-1)
141
+ ]
142
+ CP::Shape::Poly.strictly_valid_vertices?(verts).should be true
143
+ CP::Shape::Poly.strictly_valid_vertices?(verts.reverse).should be false
144
+
145
+ verts = [
146
+ vec2(-1,-1),
147
+ vec2( 1,-1),
148
+ vec2( 0, 0), # Bad vert!
149
+ vec2( 1, 1),
150
+ vec2(-1, 1)
151
+ ]
152
+ CP::Shape::Poly.strictly_valid_vertices?(verts).should be false
153
+ end
154
+
155
+ it 'can loosely validate vertices' do
156
+ verts = [
157
+ vec2(-1,-1),
158
+ vec2(-1, 1),
159
+ vec2( 1, 1),
160
+ vec2( 1,-1)
161
+ ]
162
+ CP::Shape::Poly.valid_vertices?(verts).should be true
163
+ CP::Shape::Poly.valid_vertices?(verts.reverse).should be true
164
+
165
+ verts = [
166
+ vec2(-1,-1),
167
+ vec2( 1,-1),
168
+ vec2( 0, 0), # Bad vert!
169
+ vec2( 1, 1),
170
+ vec2(-1, 1)
171
+ ]
172
+ CP::Shape::Poly.valid_vertices?(verts).should be false
173
+
174
+ end
175
+
176
+ it 'accepts convex polygons with either winding' do
177
+ bod = CP::Body.new 90, 76
178
+ verts = [
179
+ vec2(-1,-1),
180
+ vec2(-1, 1),
181
+ vec2( 1, 1),
182
+ vec2( 1,-1)
183
+ ]
184
+ CP::Shape::Poly.new(bod,verts,CP::ZERO_VEC_2)
185
+ CP::Shape::Poly.new(bod,verts.reverse,CP::ZERO_VEC_2)
186
+ end
187
+
188
+ it 'rejects concave polygons' do
189
+ bod = CP::Body.new 90, 76
190
+ verts = [
191
+ vec2(-1,-1),
192
+ vec2( 1,-1),
193
+ vec2( 0, 0), # Bad vert!
194
+ vec2( 1, 1),
195
+ vec2(-1, 1)
196
+ ]
197
+ lambda { CP::Shape::Poly.new(bod,verts,CP::ZERO_VEC_2) }.should raise_error
133
198
  end
134
199
  end
135
200
 
@@ -177,5 +177,75 @@ describe 'Shape in chipmunk' do
177
177
  shapes.size.should == 1
178
178
  shapes.first.should == shapy
179
179
  end
180
+
181
+ it 'can do a segment query' do
182
+ space = CP::Space.new
183
+ bod = CP::Body.new 90, 76
184
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
185
+ shapy.collision_type = :foo
186
+
187
+ space.add_shape shapy
188
+
189
+ all_ones = 2**32-1
190
+
191
+ shapes = []
192
+ space.segment_query(vec2(100,100),vec2(-100,-100), all_ones,0) do |shape, t, n|
193
+ shapes << shape
194
+ end
195
+
196
+ shapes.size.should == 1
197
+ shapes.first.should == shapy
198
+ end
199
+
200
+ it 'can do a segment query that returns info' do
201
+ space = CP::Space.new
202
+ bod = CP::Body.new 90, 76
203
+ shapy = CP::Shape::Circle.new bod, 20, CP::ZERO_VEC_2
204
+ shapy.collision_type = :foo
205
+
206
+ space.add_shape(shapy)
207
+
208
+ all_ones = 2**32-1
209
+
210
+ info = space.info_segment_query(vec2(-100,10),vec2(0,10), all_ones, 0)
211
+ info.hit.should be_true
212
+ info.shape.should be shapy
213
+ info.t.should be_close(0.827,0.001)
214
+ info.n.x.should be_close(-0.866, 0.001)
215
+ info.n.y.should be_close(0.5, 0.001)
216
+ end
217
+
218
+ it 'can do a segment query that returns a shape' do
219
+ space = CP::Space.new
220
+ bod = CP::Body.new(90, 76)
221
+ shapy = CP::Shape::Circle.new bod, 20, CP::ZERO_VEC_2
222
+ shapy.collision_type = :foo
223
+
224
+ space.add_shape shapy
225
+
226
+ all_ones = 2**32-1
227
+
228
+ obj = space.shape_segment_query(vec2(-100,10),vec2(0,10))
229
+ obj.should == shapy
230
+ end
231
+
232
+ it 'can do a segment query that finds no shape' do
233
+ space = CP::Space.new
234
+ obj = space.shape_segment_query(vec2(-100,10),vec2(0,10))
235
+
236
+ obj.should be_nil
237
+ end
238
+
239
+ it 'can do a segment query that finds no info' do
240
+ space = CP::Space.new
241
+ info = space.info_segment_query(vec2(-100,10),vec2(0,10))
242
+
243
+ info.hit.should be_false
244
+ info.shape.should be_nil
245
+ info.t.should be_nil
246
+ info.n.should be_nil
247
+ end
248
+
249
+
180
250
 
181
251
  end