chipmunk-ffi 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
@@ -5,18 +5,31 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chipmunk-ffi}
8
- s.version = "0.0.1"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Shawn Anderson"]
12
- s.date = %q{2009-12-02}
12
+ s.date = %q{2009-12-04}
13
13
  s.description = %q{FFI bindings for chipmunk physics lib.}
14
14
  s.email = %q{shawn42@gmail.com}
15
15
  s.files = [
16
16
  "Rakefile",
17
17
  "VERSION",
18
18
  "chipmunk-ffi.gemspec",
19
- "lib/chipmunk-ffi.rb"
19
+ "lib/chipmunk-ffi.rb",
20
+ "lib/chipmunk-ffi/bb.rb",
21
+ "lib/chipmunk-ffi/body.rb",
22
+ "lib/chipmunk-ffi/core.rb",
23
+ "lib/chipmunk-ffi/shape.rb",
24
+ "lib/chipmunk-ffi/space.rb",
25
+ "lib/chipmunk-ffi/vec2.rb",
26
+ "spec/bb_spec.rb",
27
+ "spec/body_spec.rb",
28
+ "spec/core_spec.rb",
29
+ "spec/shape_spec.rb",
30
+ "spec/space_spec.rb",
31
+ "spec/spec_helper.rb",
32
+ "spec/vec2_spec.rb"
20
33
  ]
21
34
  s.homepage = %q{http://shawn42.github.com/chipmunk-ffi}
22
35
  s.rdoc_options = ["--charset=UTF-8"]
@@ -24,6 +37,15 @@ Gem::Specification.new do |s|
24
37
  s.rubyforge_project = %q{chipmunk-ffi}
25
38
  s.rubygems_version = %q{1.3.5}
26
39
  s.summary = %q{FFI bindings for chipmunk physics lib.}
40
+ s.test_files = [
41
+ "spec/bb_spec.rb",
42
+ "spec/body_spec.rb",
43
+ "spec/core_spec.rb",
44
+ "spec/shape_spec.rb",
45
+ "spec/space_spec.rb",
46
+ "spec/spec_helper.rb",
47
+ "spec/vec2_spec.rb"
48
+ ]
27
49
 
28
50
  if s.respond_to? :specification_version then
29
51
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'nice-ffi'
3
2
 
4
3
  module CP
@@ -16,12 +15,12 @@ module CP
16
15
 
17
16
  end
18
17
  load_library "chipmunk", CP::LOAD_PATHS
19
- def self.cp_static_inline(func_sym, ret, args)
18
+ def self.cp_static_inline(func_sym, args, ret)
20
19
  func_name = "_#{func_sym}"
21
20
  attach_variable func_name, :pointer
22
21
  const_func_name = func_sym.to_s.upcase
23
22
 
24
- func = FFI::Function.new(Vect.by_value, args, FFI::Pointer.new(self.send(func_name)), :convention => :default )
23
+ func = FFI::Function.new(ret, args, FFI::Pointer.new(self.send(func_name)), :convention => :default )
25
24
  const_set const_func_name, func
26
25
 
27
26
  instance_eval <<-METHOD
@@ -33,187 +32,10 @@ module CP
33
32
 
34
33
  CP_FLOAT = :double
35
34
 
36
- class Vect < NiceFFI::Struct
37
- layout( :x, CP_FLOAT,
38
- :y, CP_FLOAT )
39
-
40
- end
41
-
42
- cp_static_inline :cpv, Vect.by_value, [CP_FLOAT,CP_FLOAT]
43
- cp_static_inline :cpvneg, Vect.by_value, [Vect.by_value]
44
- cp_static_inline :cpvadd, Vect.by_value, [Vect.by_value,Vect.by_value]
45
- cp_static_inline :cpvsub, Vect.by_value, [Vect.by_value,Vect.by_value]
46
- cp_static_inline :cpvmult, Vect.by_value, [Vect.by_value,Vect.by_value]
47
- cp_static_inline :cpvdot, Vect.by_value, [Vect.by_value,Vect.by_value]
48
- cp_static_inline :cpvcross, Vect.by_value, [Vect.by_value,Vect.by_value]
49
-
50
- cp_static_inline :cpvperp, Vect.by_value, [Vect.by_value]
51
- cp_static_inline :cpvrperp, Vect.by_value, [Vect.by_value]
52
- cp_static_inline :cpvproject, Vect.by_value, [Vect.by_value,Vect.by_value]
53
- cp_static_inline :cpvrotate, Vect.by_value, [Vect.by_value,Vect.by_value]
54
- cp_static_inline :cpvunrotate, Vect.by_value, [Vect.by_value,Vect.by_value]
55
-
56
- cp_static_inline :cpvlengthsq, CP_FLOAT, [Vect.by_value]
57
-
58
- cp_static_inline :cpvlerp, Vect.by_value, [Vect.by_value,Vect.by_value]
59
-
60
- cp_static_inline :cpvnormalize, Vect.by_value, [Vect.by_value]
61
- cp_static_inline :cpvnormalize_safe, Vect.by_value, [Vect.by_value]
62
-
63
- cp_static_inline :cpvclamp, Vect.by_value, [Vect.by_value,Vect.by_value]
64
- cp_static_inline :cpvlerpconst, Vect.by_value, [Vect.by_value,Vect.by_value]
65
- cp_static_inline :cpvdist, CP_FLOAT, [Vect.by_value,Vect.by_value]
66
- cp_static_inline :cpvdistsq, CP_FLOAT, [Vect.by_value,Vect.by_value]
67
-
68
- cp_static_inline :cpvnear, :int, [Vect.by_value,Vect.by_value, CP_FLOAT]
69
-
70
- func :cpvlength, [Vect.by_value], CP_FLOAT
71
- func :cpvforangle, [CP_FLOAT], Vect.by_value
72
- func :cpvslerp, [Vect.by_value, Vect.by_value, CP_FLOAT], Vect.by_value
73
- func :cpvslerpconst, [Vect.by_value, Vect.by_value, CP_FLOAT], Vect.by_value
74
- func :cpvtoangle, [Vect.by_value], CP_FLOAT
75
- func :cpvstr, [Vect.by_value], :string
76
-
77
- class Vec2
78
- attr_accessor :struct
79
- def initialize(x,y)
80
- @struct = CP.cpv(x,y)
81
- end
82
-
83
- def x
84
- @struct.x
85
- end
86
- def x=(new_x)
87
- @struct.x = new_x
88
- end
89
- def y
90
- @struct.y
91
- end
92
- def y=(new_y)
93
- @struct.y = new_y
94
- end
95
-
96
- def self.for_angle(angle)
97
- create_from_struct CP.cpvforangle(angle)
98
- end
99
-
100
- def to_s
101
- CP.cpvstr @struct
102
- end
103
-
104
- def to_angle
105
- CP.cpvtoangle @struct
106
- end
107
-
108
- def to_a
109
- [@struct.x,@struct.y]
110
- end
111
-
112
- def -@
113
- create_from_struct CP.cpvneg(@struct)
114
- end
115
-
116
- def +(other_vec)
117
- create_from_struct CP.cpvadd(@struct, other_vec.struct)
118
- end
119
-
120
- def -(other_vec)
121
- create_from_struct CP.cpvsub(@struct, other_vec.struct)
122
- end
123
-
124
- def *(s)
125
- create_from_struct CP.cpvmult(@struct, s)
126
- end
127
-
128
- def /(s)
129
- factor = 1.0/s
130
- create_from_struct CP.cpvmult(@struct, s)
131
- end
132
-
133
- def dot(other_vec)
134
- CP.cpvdot(@struct, other_vec.struct)
135
- end
136
-
137
- def cross(other_vec)
138
- CP.cpvcross(@struct, other_vec.struct)
139
- end
140
-
141
- def perp
142
- create_from_struct CP.cpvperp(@struct)
143
- end
144
-
145
- def rperp
146
- create_from_struct CP.cpvperp(@struct)
147
- end
148
-
149
- def project(other_vec)
150
- create_from_struct CP.cpvproject(@struct, other_vec.struct)
151
- end
152
-
153
- def rotate(other_vec)
154
- create_from_struct CP.cpvrotate(@struct, other_vec.struct)
155
- end
156
-
157
- def unrotate(other_vec)
158
- create_from_struct CP.cpvunrotate(@struct, other_vec.struct)
159
- end
160
-
161
- def lengthsq
162
- CP.cpvlengthsq(@struct)
163
- end
164
-
165
- def lerp(other_vec)
166
- end
167
-
168
- def normalize
169
- create_from_struct CP.cpvnormalize(@struct)
170
- end
171
-
172
- def normalize!
173
- @struct = CP.cpvnormalize(@struct)
174
- end
175
-
176
- def normalize_safe
177
- create_from_struct CP.cpvnormalize_safe(@struct)
178
- end
179
-
180
- def clamp(other_vec)
181
- create_from_struct CP.cpvclamp(@struct)
182
- end
183
-
184
- def lerpconst(other_vec)
185
- create_from_struct CP.cpvlerpconst(@struct)
186
- end
187
-
188
- def dist(other_vec)
189
- CP.cpvdist(@struct)
190
- end
191
-
192
- def distsq(other_vec)
193
- CP.cpvdistsq(@struct)
194
- end
195
-
196
- def near?(other_vec, dist)
197
- delta_v = CP.cpvsub(@struct, other_vec.struct)
198
- CP.cpvdot(delta_v, delta_v) < dist*dist
199
- end
200
-
201
- def length
202
- CP::cpvlength @struct
203
- end
204
-
205
- private
206
-
207
- def create_from_struct(struct)
208
- new_v = dup
209
- new_v.struct = struct
210
- new_v
211
- end
212
-
213
- end
214
- ZERO_VEC_2 = Vec2.new(0,0).freeze
215
-
216
35
  end
217
- def vec2(x,y)
218
- CP::Vec2.new x, y
36
+ libs = %w{vec2 core bb body shape space}
37
+ # Init_cpConstraint();
38
+ $: << File.dirname(__FILE__)
39
+ libs.each do |lib|
40
+ require "chipmunk-ffi/#{lib}"
219
41
  end
@@ -0,0 +1,71 @@
1
+ module CP
2
+
3
+ class BBStruct < NiceFFI::Struct
4
+ layout( :l, CP_FLOAT,
5
+ :b, CP_FLOAT,
6
+ :r, CP_FLOAT,
7
+ :t, CP_FLOAT )
8
+ end
9
+
10
+ cp_static_inline :cpBBNew, [CP_FLOAT,CP_FLOAT,CP_FLOAT,CP_FLOAT], BBStruct.by_value
11
+ cp_static_inline :cpBBintersects, [BBStruct.by_value,BBStruct.by_value], :int
12
+ cp_static_inline :cpBBcontainsBB, [BBStruct.by_value,BBStruct.by_value], :int
13
+ cp_static_inline :cpBBcontainsVect, [BBStruct.by_value,Vect.by_value], :int
14
+
15
+ func :cpBBClampVect, [BBStruct.by_value,Vect.by_value], Vect.by_value
16
+ func :cpBBWrapVect, [BBStruct.by_value,Vect.by_value], Vect.by_value
17
+
18
+ class BB
19
+ attr_reader :struct
20
+ def initialize(*args)
21
+ case args.size
22
+ when 1
23
+ @struct = args.first
24
+ when 4
25
+ @struct = CP.cpBBNew(*args)
26
+ else
27
+ raise "wrong number of args for BB, got #{args.size}, but expected 4"
28
+ end
29
+ end
30
+ def l;@struct.l;end
31
+ def b;@struct.b;end
32
+ def r;@struct.r;end
33
+ def t;@struct.t;end
34
+
35
+ def l=(new_l);@struct.l=new_l;end
36
+ def b=(new_b);@struct.b=new_b;end
37
+ def r=(new_r);@struct.r=new_r;end
38
+ def t=(new_t);@struct.t=new_t;end
39
+
40
+ def intersect?(other_bb)
41
+ b = CP.cpBBintersects(@struct,other_bb.struct)
42
+ b == 0 ? false : true
43
+ end
44
+
45
+ def contains_bb?(other_bb)
46
+ b = CP.cpBBcontainsBB(@struct,other_bb.struct)
47
+ b == 0 ? false : true
48
+ end
49
+
50
+ def contains_vect?(other_bb)
51
+ b = CP.cpBBcontainsVect(@struct,other_bb.struct)
52
+ b == 0 ? false : true
53
+ end
54
+
55
+ def clamp_vect(v)
56
+ v_struct = CP.cpBBClampVect(@struct,v.struct)
57
+ Vec2.new v_struct
58
+ end
59
+
60
+ def wrap_vect(v)
61
+ v_struct = CP.cpBBWrapVect(@struct,v.struct)
62
+ Vec2.new v_struct
63
+ end
64
+
65
+ def to_s
66
+ "#<CP::BB:(% .3f, % .3f) -> (% .3f, % .3f)>" % [l,b,r,t]
67
+ end
68
+ end
69
+
70
+ end
71
+
@@ -0,0 +1,171 @@
1
+ module CP
2
+
3
+ callback :cpBodyVelocityFunc, [:pointer, Vect.by_value, CP_FLOAT, CP_FLOAT], :void
4
+ callback :cpBodyPostitionFunc, [:pointer, CP_FLOAT], :void
5
+
6
+ class BodyStruct < NiceFFI::Struct
7
+ layout(
8
+ # :bodyVelocityFunc, :cpBodyVelocityFunc,
9
+ # :bodyPositionFunc, :cpBodyPositionFunc,
10
+ # TODO not sure if :pointer is right here...
11
+ :bodyVelocityFunc, :pointer,
12
+ :bodyPositionFunc, :pointer,
13
+ :m, CP_FLOAT,
14
+ :m_inv, CP_FLOAT,
15
+ :i, CP_FLOAT,
16
+ :i_inv, CP_FLOAT,
17
+ :p, Vect,
18
+ :v, Vect,
19
+ :f, Vect,
20
+ :a, CP_FLOAT,
21
+ :w, CP_FLOAT,
22
+ :t, CP_FLOAT,
23
+ :rot, Vect,
24
+ :data, :pointer,
25
+ :v_bias, Vect,
26
+ :w_bias, CP_FLOAT
27
+ )
28
+
29
+ def self.release(me)
30
+ # TODO is this right?
31
+ CP.cpBodyDestroy me
32
+ end
33
+ end
34
+ func :cpBodyNew, [CP_FLOAT, CP_FLOAT], BodyStruct
35
+ func :cpBodyDestroy, [BodyStruct], :void
36
+ func :cpBodyUpdateVelocity, [BodyStruct,Vect.by_value,CP_FLOAT,CP_FLOAT], :void
37
+ func :cpBodyUpdatePosition, [BodyStruct,CP_FLOAT], :void
38
+ func :cpBodyApplyForce, [:pointer, Vect.by_value, Vect.by_value], :void
39
+ func :cpBodyResetForces, [:pointer], :void
40
+
41
+ cp_static_inline :cpBodyLocal2World, [:pointer, Vect.by_value], Vect.by_value
42
+ cp_static_inline :cpBodyWorld2Local, [:pointer, Vect.by_value], Vect.by_value
43
+ cp_static_inline :cpBodyApplyImpulse, [:pointer, Vect.by_value, Vect.by_value], :void
44
+
45
+ class Body
46
+ attr_reader :struct
47
+ def initialize(*args)
48
+ case args.size
49
+ when 1
50
+ @struct = args.first
51
+ when 2
52
+ ptr = CP.cpBodyNew(*args)
53
+ @struct = BodyStruct.new ptr
54
+ else
55
+ raise "wrong number of args for Body, got #{args.size}, but expected 2"
56
+ end
57
+ end
58
+
59
+ def m
60
+ @struct.m
61
+ end
62
+ def m=(pm)
63
+ @struct.m = pm
64
+ end
65
+ alias :mass :m
66
+ alias :mass= :m=
67
+
68
+ def i
69
+ @struct.i
70
+ end
71
+ def i=(pi)
72
+ @struct.i = pi
73
+ end
74
+ alias :moment :i
75
+ alias :moment= :i=
76
+
77
+ def p
78
+ Vec2.new @struct.p
79
+ end
80
+ def p=(new_p)
81
+ @struct.p.pointer.put_bytes 0, new_p.struct.to_bytes, 0,Vect.size
82
+ # TODO XXX this probably leaks?
83
+ # @struct.p.send :pointer=, new_p.struct.pointer
84
+ self
85
+ end
86
+ alias :pos :p
87
+ alias :pos= :p=
88
+
89
+ def v
90
+ Vec2.new @struct.v
91
+ end
92
+ def v=(pv)
93
+ @struct.v = pv.struct
94
+ @struct.v.pointer.put_bytes 0, pv.struct.to_bytes, 0,Vect.size
95
+ self
96
+ end
97
+ alias :vel :v
98
+ alias :vel= :v=
99
+
100
+ def f
101
+ Vec2.new @struct.f
102
+ end
103
+ def f=(pf)
104
+ @struct.f.pointer.put_bytes 0, pf.struct.to_bytes, 0,Vect.size
105
+ @struct.f = pf.struct
106
+ self
107
+ end
108
+ alias :force :f
109
+ alias :force= :f=
110
+
111
+ def a
112
+ @struct.a
113
+ end
114
+ def a=(pa)
115
+ @struct.a = pa
116
+ end
117
+ alias :angle :a
118
+ alias :angle= :a=
119
+
120
+ def w
121
+ @struct.w
122
+ end
123
+ def w=(pw)
124
+ @struct.w = pw
125
+ end
126
+ alias :ang_vel :w
127
+ alias :ang_vel= :w=
128
+
129
+ def t
130
+ @struct.t
131
+ end
132
+ def t=(pt)
133
+ @struct.t = pt
134
+ end
135
+ alias :torque :t
136
+ alias :torque= :t=
137
+
138
+ def rot
139
+ Vec2.new @struct.rot
140
+ end
141
+
142
+ def local2world(v)
143
+ CP.cpBodyLocal2World(@struct.pointer,v.struct)
144
+ end
145
+
146
+ def world2local(v)
147
+ CP.cpBodyWorld2Local(@struct.pointer,v.struct)
148
+ end
149
+
150
+ def reset_forces
151
+ CP.cpBodyResetForces(@struct.pointer)
152
+ end
153
+
154
+ def apply_force(f,r)
155
+ CP.cpBodyApplyForce(@struct.pointer,f.struct,r.struct)
156
+ end
157
+
158
+ def apply_impulse(j,r)
159
+ CP.cpBodyApplyImpulse(@struct.pointer,j.struct,r.struct)
160
+ end
161
+
162
+ def update_velocity(g,dmp,dt)
163
+ CP.cpBodyUpdateVelocity(@struct.pointer,g,dmp,dt)
164
+ end
165
+
166
+ def update_position(dt)
167
+ CP.cpBodyUpdatePosition(@struct.pointer,dt)
168
+ end
169
+
170
+ end
171
+ end