chipmunk-ffi 0.2.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ # test for performance vs old ruby bindings
2
+ require 'rubygems'
3
+ require 'benchmark'
4
+
5
+ n = 100000
6
+ #require 'chipmunk-ffi'
7
+ #n.times do
8
+ # vec2(4,3)
9
+ #end
10
+ #exit
11
+
12
+ Benchmark.bm do |x|
13
+ require 'chipmunk'
14
+ x.report("cp vec2 ") {
15
+ n.times do
16
+ vec2(4,3)
17
+ end
18
+ }
19
+ x.report("cp vec2 sub") {
20
+ n.times do
21
+ vec2(4,3)-vec2(5,6)
22
+ end
23
+ }
24
+ require 'chipmunk-ffi'
25
+ x.report("cp-ffi vec2") {
26
+ n.times do
27
+ vec2(4,3)
28
+ end
29
+ }
30
+ x.report("cp vec2 ffi sub") {
31
+ n.times do
32
+ vec2(4,3)-vec2(5,6)
33
+ end
34
+ }
35
+ end
36
+
@@ -53,18 +53,32 @@ describe 'ShapeStruct in chipmunk' do
53
53
  s.struct.collision_type.should == :foo.object_id
54
54
  end
55
55
 
56
- it 'can get its sensor'
56
+ it 'can get its sensor' do
57
+ bod = CP::Body.new 90, 76
58
+ s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
59
+ s.sensor?.should be_false
60
+ s.sensor = true
61
+ s.sensor?.should be_true
62
+ end
63
+
57
64
  it 'can get its u' do
58
65
  bod = CP::Body.new 90, 76
59
66
  s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
60
67
  s.u.should be_close(0,0.001)
61
68
  end
62
- it 'can get its surf vec'
69
+
70
+ it 'can get its surf vec' do
71
+ bod = CP::Body.new 90, 76
72
+ s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
73
+ s.surface_v = vec2(4,5)
74
+ s.surface_v.x.should be_close(4,0.001)
75
+ s.surface_v.y.should be_close(5,0.001)
76
+ end
63
77
 
64
78
  it 'can get its data' do
65
79
  bod = CP::Body.new 90, 76
66
80
  s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
67
- s.data.read_int.should == s.object_id
81
+ s.data.read_long.should == s.object_id
68
82
  end
69
83
 
70
84
  it 'can get its klass' do
@@ -78,6 +92,32 @@ describe 'ShapeStruct in chipmunk' do
78
92
  s = CP::Shape::Circle.new bod, 20, CP::ZERO_VEC_2
79
93
  s.body.v = vec2(4,5)
80
94
  end
95
+
96
+ it 'can set its sensory-ness' do
97
+ bod = CP::Body.new 90, 76
98
+ s = CP::Shape::Circle.new bod, 20, CP::ZERO_VEC_2
99
+ s.sensor?.should be_false
100
+ s.sensor = true
101
+ s.sensor?.should be_true
102
+ end
103
+
104
+ it 'can query if a point hits it' do
105
+ bod = CP::Body.new 90, 76
106
+ s = CP::Shape::Circle.new bod, 20, CP::ZERO_VEC_2
107
+ s.point_query(vec2(0,10)).should be_true
108
+ s.point_query(vec2(0,100)).should be_false
109
+ end
110
+
111
+ it 'can query if a segment hits it' do
112
+ bod = CP::Body.new 90, 76
113
+ s = CP::Shape::Circle.new bod, 20, CP::ZERO_VEC_2
114
+ info = s.segment_query(vec2(-100,10),vec2(0,10))
115
+ GC.start
116
+ info.hit.should be_true
117
+ info.t.should be_close(0.827,0.001)
118
+ info.n.x.should be_close(-0.866, 0.001)
119
+ info.n.y.should be_close(0.5, 0.001)
120
+ end
81
121
  end
82
122
 
83
123
  describe 'Segment class' do
@@ -92,4 +132,6 @@ describe 'ShapeStruct in chipmunk' do
92
132
  s = CP::Shape::Poly.new bod, [vec2(1,1), vec2(2,2),vec2(3,3)], CP::ZERO_VEC_2
93
133
  end
94
134
  end
135
+
136
+
95
137
  end
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__)+'/spec_helper'
2
+ describe 'SpaceHashStruct in chipmunk' do
3
+ describe 'SpaceHash class' do
4
+ it 'can be create' do
5
+ bb_func = Proc.new do |shape|
6
+ puts "hi"
7
+ end
8
+ sh = CP::SpaceHash.new(1,2,&bb_func)
9
+ sh.cell_dim.should == 1
10
+ prime_that_fits = 5
11
+ sh.num_cells.should == prime_that_fits
12
+ end
13
+
14
+ it 'can lookup query by BB empty' do
15
+ bb_func = Proc.new do |shape|
16
+ puts "hi"
17
+ end
18
+ sh = CP::SpaceHash.new(1,2,&bb_func)
19
+ bb = BB.new(1,2,3,4)
20
+
21
+ objects = sh.query_by_bb(bb)
22
+ objects.size.should == 0
23
+
24
+ end
25
+
26
+ it 'can have things inserted' do
27
+ bb_func = Proc.new do |shape|
28
+ puts "hi"
29
+ end
30
+ sh = CP::SpaceHash.new(1,2,&bb_func)
31
+ bod = CP::Body.new 90, 76
32
+ s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
33
+
34
+ sh.insert(s, s.bb)
35
+ # TODO assert that its there
36
+ end
37
+
38
+ it 'can have things removed' do
39
+ bb_func = Proc.new do |shape|
40
+ puts "hi"
41
+ end
42
+ sh = CP::SpaceHash.new(1,2,&bb_func)
43
+ bod = CP::Body.new 90, 76
44
+ s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
45
+
46
+ # TODO assert that its there
47
+ sh.insert(s, s.bb)
48
+
49
+ sh.remove(s)
50
+ # TODO assert that its not there
51
+ end
52
+
53
+ end
54
+ end
@@ -104,5 +104,78 @@ describe 'Shape in chipmunk' do
104
104
  space.add_constraint pj
105
105
  end
106
106
 
107
+ it 'can do a first point query finds the shape' do
108
+ space = CP::Space.new
109
+ bod = CP::Body.new 90, 76
110
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
111
+ shapy.collision_type = :foo
112
+
113
+ space.add_shape shapy
114
+
115
+ obj = space.point_query_first(vec2(20,20),CP::ALL_ONES,0)
116
+ obj.should == shapy
117
+
118
+ end
119
+
120
+ it 'can do a first point query does not find anything' do
121
+ space = CP::Space.new
122
+ bod = CP::Body.new 90, 76
123
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
124
+ shapy.collision_type = :foo
125
+
126
+ space.add_shape shapy
127
+
128
+ all_ones = 2**32-1
129
+ obj = space.point_query_first(vec2(20,50),all_ones,0)
130
+ obj.should be_nil
131
+
132
+ end
133
+
134
+ it 'can do a point query' do
135
+ space = CP::Space.new
136
+ bod = CP::Body.new 90, 76
137
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
138
+ shapy.collision_type = :foo
139
+
140
+ space.add_shape shapy
141
+
142
+ all_ones = 2**32-1
143
+
144
+ shapes = []
145
+ space.point_query vec2(20,20), all_ones,0 do |shape|
146
+ shapes << shape
147
+ end
148
+
149
+ shapes.size.should == 1
150
+ shapes.first.should == shapy
151
+ end
152
+
153
+ it 'can do a point query finds the shape' do
154
+ space = CP::Space.new
155
+ bod = CP::Body.new 90, 76
156
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
157
+ shapy.collision_type = :foo
158
+
159
+ space.add_shape shapy
160
+
161
+ obj = space.shape_point_query(vec2(20,20))
162
+ obj.should == shapy
163
+
164
+ end
165
+
166
+ it 'can do a bb query' do
167
+ space = CP::Space.new
168
+ bod = CP::Body.new 90, 76
169
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
170
+ shapy.collision_type = :foo
171
+
172
+ space.add_shape shapy
173
+
174
+ hash = space.active_shapes_hash
175
+ shapes = hash.query_by_bb BB.new(0,0,5,5)
176
+
177
+ shapes.size.should == 1
178
+ shapes.first.should == shapy
179
+ end
107
180
 
108
181
  end
@@ -2,4 +2,5 @@ require 'spec'
2
2
  $: << File.dirname(__FILE__)+'/../lib'
3
3
 
4
4
  require 'chipmunk-ffi'
5
+ require 'chipmunk-ffi/unsafe'
5
6
  include CP
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__)+'/spec_helper'
2
+ describe 'ShapeStruct in chipmunk' do
3
+ describe 'Circle class' do
4
+ it 'can set its radius' do
5
+ bod = CP::Body.new 90, 76
6
+ s = CP::Shape::Circle.new bod, 20, CP::ZERO_VEC_2
7
+ end
8
+
9
+ it 'can set its offset' do
10
+ bod = CP::Body.new 90, 76
11
+ s = CP::Shape::Circle.new bod, 20, CP::ZERO_VEC_2
12
+ end
13
+ end
14
+
15
+ describe 'Segment class' do
16
+ it 'can set its endpoints' do
17
+ bod = CP::Body.new 90, 76
18
+ s = CP::Shape::Segment.new bod, vec2(1,1), vec2(2,2), 5
19
+ end
20
+
21
+ it 'can set its radius' do
22
+ bod = CP::Body.new 90, 76
23
+ s = CP::Shape::Segment.new bod, vec2(1,1), vec2(2,2), 5
24
+ end
25
+ end
26
+
27
+ describe 'Poly class' do
28
+ it 'can set its verts' do
29
+ bod = CP::Body.new 90, 76
30
+ s = CP::Shape::Poly.new bod, [vec2(1,1), vec2(2,2),vec2(3,3)], CP::ZERO_VEC_2
31
+ s.verts = [[vec2(1,2), vec2(2,1), vec2(2,2)], vec2(1,1)]
32
+ end
33
+ end
34
+
35
+
36
+ end
@@ -114,6 +114,18 @@ describe 'Vect in chipmunk' do
114
114
  v.y.should be_close(0.894,0.001)
115
115
  end
116
116
 
117
+ it 'can normalize_safe' do
118
+ v = CP::Vec2.new(10,20).normalize_safe
119
+ v.x.should be_close(0.447,0.001)
120
+ v.y.should be_close(0.894,0.001)
121
+ end
122
+
123
+ it 'can normalize_safe on zero vec' do
124
+ v = CP::Vec2.new(0,0).normalize_safe
125
+ v.x.should be_close(0,0.001)
126
+ v.y.should be_close(0,0.001)
127
+ end
128
+
117
129
  it 'can be normalized! (with a bang)' do
118
130
  v = CP::Vec2.new(10,20)
119
131
  v2 = v.normalize!
@@ -134,19 +146,94 @@ describe 'Vect in chipmunk' do
134
146
  v.lengthsq.should be_close(500, 0.001)
135
147
  end
136
148
 
137
- it 'can dot'
138
- it 'can cross'
139
- it 'can get dist from other vec2'
140
- it 'can get dist squared from other vec2'
141
- it 'can tell if its near? another vec2'
142
- it 'can perp'
143
- it 'can rperp'
144
- it 'can rotate'
145
- it 'can unrotate'
146
- it 'can lerp'
147
- it 'can lerpconst'
148
- it 'can normalize_safe'
149
- it 'can project'
149
+ it 'can dot' do
150
+ v = CP::Vec2.new(2,3)
151
+ other_v = CP::Vec2.new(4,5)
152
+
153
+ v.dot(other_v).should be_close(23.0, 0.001)
154
+ end
155
+
156
+ it 'can cross' do
157
+ v = CP::Vec2.new(2,3)
158
+ other_v = CP::Vec2.new(4,5)
159
+
160
+ v.cross(other_v).should be_close(-2.0, 0.001)
161
+ end
162
+
163
+ it 'can get dist from other vec2' do
164
+ v = CP::Vec2.new(1,1)
165
+ other_v = CP::Vec2.new(2,2)
166
+
167
+ v.dist(other_v).should be_close(Math.sqrt(2), 0.001)
168
+ end
169
+
170
+ it 'can get dist squared from other vec2' do
171
+ v = CP::Vec2.new(1,1)
172
+ other_v = CP::Vec2.new(2,2)
173
+
174
+ v.distsq(other_v).should be_close(2, 0.001)
175
+ end
176
+
177
+ it 'can tell if its near? another vec2' do
178
+ v = CP::Vec2.new(1,1)
179
+ other_v = CP::Vec2.new(2,2)
180
+ v.near?(other_v, 1).should be_false
181
+ v.near?(other_v, 2).should be_true
182
+ end
183
+
184
+ it 'can rotate' do
185
+ v = CP::Vec2.new(2,3)
186
+ other_v = CP::Vec2.new(4,5)
187
+ rv = v.rotate(other_v)
188
+ rv.x.should be_close(-7,0.001)
189
+ rv.y.should be_close(22,0.001)
190
+ end
191
+
192
+ it 'can unrotate' do
193
+ v = CP::Vec2.new(2,3)
194
+ other_v = CP::Vec2.new(4,5)
195
+ rv = v.unrotate(other_v)
196
+ rv.x.should be_close(23,0.001)
197
+ rv.y.should be_close(2,0.001)
198
+ end
199
+
200
+ it 'can perp' do
201
+ v = CP::Vec2.new(0,1)
202
+ pv = v.perp
203
+ pv.x.should be_close(-1,0.001)
204
+ pv.y.should be_close(0,0.001)
205
+ end
206
+
207
+ it 'can rperp' do
208
+ v = CP::Vec2.new(0,1)
209
+ pv = v.rperp
210
+ pv.x.should be_close(1,0.001)
211
+ pv.y.should be_close(0,0.001)
212
+ end
213
+
214
+ it 'can lerp' do
215
+ v = CP::Vec2.new(2,3)
216
+ other_v = CP::Vec2.new(3,4)
217
+ rv = v.lerp(other_v,0.5)
218
+ rv.x.should be_close(2.5,0.001)
219
+ rv.y.should be_close(3.5,0.001)
220
+ end
221
+
222
+ it 'can lerpconst' do
223
+ v = CP::Vec2.new(2,3)
224
+ other_v = CP::Vec2.new(4,5)
225
+ rv = v.lerpconst(other_v, 6)
226
+ rv.x.should be_close(4,0.001)
227
+ rv.y.should be_close(5,0.001)
228
+ end
229
+
230
+ it 'can project' do
231
+ v = CP::Vec2.new(2,3)
232
+ other_v = CP::Vec2.new(4,5)
233
+ rv = v.project(other_v)
234
+ rv.x.should be_close(2.244,0.001)
235
+ rv.y.should be_close(2.804,0.001)
236
+ end
150
237
 
151
238
  end
152
239
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chipmunk-ffi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn Anderson
@@ -9,9 +9,29 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-16 00:00:00 -05:00
12
+ date: 2010-01-04 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ffi
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.6.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: nice-ffi
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
15
35
  - !ruby/object:Gem::Dependency
16
36
  name: rspec
17
37
  type: :development
@@ -38,13 +58,15 @@ executables: []
38
58
 
39
59
  extensions: []
40
60
 
41
- extra_rdoc_files: []
42
-
61
+ extra_rdoc_files:
62
+ - README.markdown
43
63
  files:
64
+ - README.markdown
44
65
  - Rakefile
45
66
  - VERSION
46
67
  - chipmunk-ffi.gemspec
47
68
  - lib/chipmunk-ffi.rb
69
+ - lib/chipmunk-ffi/arbiter.rb
48
70
  - lib/chipmunk-ffi/bb.rb
49
71
  - lib/chipmunk-ffi/body.rb
50
72
  - lib/chipmunk-ffi/constraint.rb
@@ -60,14 +82,19 @@ files:
60
82
  - lib/chipmunk-ffi/core.rb
61
83
  - lib/chipmunk-ffi/shape.rb
62
84
  - lib/chipmunk-ffi/space.rb
85
+ - lib/chipmunk-ffi/space_hash.rb
86
+ - lib/chipmunk-ffi/unsafe.rb
63
87
  - lib/chipmunk-ffi/vec2.rb
64
88
  - spec/bb_spec.rb
65
89
  - spec/body_spec.rb
66
90
  - spec/constraint_spec.rb
67
91
  - spec/core_spec.rb
92
+ - spec/perf.rb
68
93
  - spec/shape_spec.rb
94
+ - spec/space_hash_spec.rb
69
95
  - spec/space_spec.rb
70
96
  - spec/spec_helper.rb
97
+ - spec/unsafe_spec.rb
71
98
  - spec/vec2_spec.rb
72
99
  has_rdoc: true
73
100
  homepage: http://shawn42.github.com/chipmunk-ffi
@@ -102,7 +129,10 @@ test_files:
102
129
  - spec/body_spec.rb
103
130
  - spec/constraint_spec.rb
104
131
  - spec/core_spec.rb
132
+ - spec/perf.rb
105
133
  - spec/shape_spec.rb
134
+ - spec/space_hash_spec.rb
106
135
  - spec/space_spec.rb
107
136
  - spec/spec_helper.rb
137
+ - spec/unsafe_spec.rb
108
138
  - spec/vec2_spec.rb