chipmunk-ffi 0.1.0 → 0.2.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.1.0
1
+ 0.2.0
data/chipmunk-ffi.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chipmunk-ffi}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.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-04}
12
+ s.date = %q{2009-12-16}
13
13
  s.description = %q{FFI bindings for chipmunk physics lib.}
14
14
  s.email = %q{shawn42@gmail.com}
15
15
  s.files = [
@@ -19,12 +19,23 @@ Gem::Specification.new do |s|
19
19
  "lib/chipmunk-ffi.rb",
20
20
  "lib/chipmunk-ffi/bb.rb",
21
21
  "lib/chipmunk-ffi/body.rb",
22
+ "lib/chipmunk-ffi/constraint.rb",
23
+ "lib/chipmunk-ffi/constraints/damped_spring.rb",
24
+ "lib/chipmunk-ffi/constraints/gear_joint.rb",
25
+ "lib/chipmunk-ffi/constraints/groove_joint.rb",
26
+ "lib/chipmunk-ffi/constraints/pin_joint.rb",
27
+ "lib/chipmunk-ffi/constraints/pivot_joint.rb",
28
+ "lib/chipmunk-ffi/constraints/ratchet_joint.rb",
29
+ "lib/chipmunk-ffi/constraints/rotary_limit_joint.rb",
30
+ "lib/chipmunk-ffi/constraints/simple_motor.rb",
31
+ "lib/chipmunk-ffi/constraints/slide_joint.rb",
22
32
  "lib/chipmunk-ffi/core.rb",
23
33
  "lib/chipmunk-ffi/shape.rb",
24
34
  "lib/chipmunk-ffi/space.rb",
25
35
  "lib/chipmunk-ffi/vec2.rb",
26
36
  "spec/bb_spec.rb",
27
37
  "spec/body_spec.rb",
38
+ "spec/constraint_spec.rb",
28
39
  "spec/core_spec.rb",
29
40
  "spec/shape_spec.rb",
30
41
  "spec/space_spec.rb",
@@ -40,6 +51,7 @@ Gem::Specification.new do |s|
40
51
  s.test_files = [
41
52
  "spec/bb_spec.rb",
42
53
  "spec/body_spec.rb",
54
+ "spec/constraint_spec.rb",
43
55
  "spec/core_spec.rb",
44
56
  "spec/shape_spec.rb",
45
57
  "spec/space_spec.rb",
data/lib/chipmunk-ffi.rb CHANGED
@@ -33,8 +33,7 @@ module CP
33
33
  CP_FLOAT = :double
34
34
 
35
35
  end
36
- libs = %w{vec2 core bb body shape space}
37
- # Init_cpConstraint();
36
+ libs = %w{vec2 core bb body shape space constraint}
38
37
  $: << File.dirname(__FILE__)
39
38
  libs.each do |lib|
40
39
  require "chipmunk-ffi/#{lib}"
@@ -1,15 +1,12 @@
1
1
  module CP
2
2
 
3
3
  callback :cpBodyVelocityFunc, [:pointer, Vect.by_value, CP_FLOAT, CP_FLOAT], :void
4
- callback :cpBodyPostitionFunc, [:pointer, CP_FLOAT], :void
4
+ callback :cpBodyPositionFunc, [:pointer, CP_FLOAT], :void
5
5
 
6
6
  class BodyStruct < NiceFFI::Struct
7
7
  layout(
8
- # :bodyVelocityFunc, :cpBodyVelocityFunc,
9
- # :bodyPositionFunc, :cpBodyPositionFunc,
10
- # TODO not sure if :pointer is right here...
11
- :bodyVelocityFunc, :pointer,
12
- :bodyPositionFunc, :pointer,
8
+ :bodyVelocityFunc, :cpBodyVelocityFunc,
9
+ :bodyPositionFunc, :cpBodyPositionFunc,
13
10
  :m, CP_FLOAT,
14
11
  :m_inv, CP_FLOAT,
15
12
  :i, CP_FLOAT,
@@ -22,6 +19,8 @@ module CP
22
19
  :t, CP_FLOAT,
23
20
  :rot, Vect,
24
21
  :data, :pointer,
22
+ :v_limit, CP_FLOAT,
23
+ :w_limit, CP_FLOAT,
25
24
  :v_bias, Vect,
26
25
  :w_bias, CP_FLOAT
27
26
  )
@@ -79,8 +78,6 @@ module CP
79
78
  end
80
79
  def p=(new_p)
81
80
  @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
81
  self
85
82
  end
86
83
  alias :pos :p
@@ -0,0 +1,30 @@
1
+ module CP
2
+ callback :cpConstraintPreStepFunction, [:pointer, CP_FLOAT, CP_FLOAT], :void
3
+ callback :cpConstraintApplyImpulseFunction, [:pointer], :void
4
+ callback :cpConstraintGetImpulseFunction, [:pointer], CP_FLOAT
5
+
6
+ class ConstraintClassStruct < NiceFFI::Struct
7
+ layout(:pre_step, :cpConstraintPreStepFunction,
8
+ :apply_impluse, :cpConstraintApplyImpulseFunction,
9
+ :getImpulse, :cpConstraintGetImpulseFunction)
10
+ end
11
+
12
+ class ConstraintStruct < NiceFFI::Struct
13
+ layout(:klass, :pointer,
14
+ :a, :pointer,
15
+ :b, :pointer,
16
+ :max_force, CP_FLOAT,
17
+ :max_bias, CP_FLOAT,
18
+ :data, :pointer)
19
+ end
20
+
21
+ require 'chipmunk-ffi/constraints/pin_joint'
22
+ require 'chipmunk-ffi/constraints/slide_joint'
23
+ require 'chipmunk-ffi/constraints/pivot_joint'
24
+ require 'chipmunk-ffi/constraints/groove_joint'
25
+ require 'chipmunk-ffi/constraints/damped_spring'
26
+ require 'chipmunk-ffi/constraints/rotary_limit_joint'
27
+ require 'chipmunk-ffi/constraints/ratchet_joint'
28
+ require 'chipmunk-ffi/constraints/gear_joint'
29
+ require 'chipmunk-ffi/constraints/simple_motor'
30
+ end
@@ -0,0 +1,30 @@
1
+ module CP
2
+
3
+ class DampedSpringStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :anchr1, Vect,
6
+ :anchr2, Vect,
7
+ :rest_length, CP_FLOAT,
8
+ :stiffness, CP_FLOAT,
9
+ :damping, CP_FLOAT,
10
+ :damped_spring_force_fun, :pointer,
11
+ :dt, CP_FLOAT,
12
+ :target_vrn, CP_FLOAT,
13
+ :r1, Vect,
14
+ :r2, Vect,
15
+ :n_mass, CP_FLOAT,
16
+ :n, Vect)
17
+ end
18
+
19
+ func :cpDampedSpringNew, [:pointer, :pointer, Vect.by_value, Vect.by_value, CP_FLOAT, CP_FLOAT, CP_FLOAT], :pointer
20
+
21
+ class DampedSpring
22
+ attr_reader :struct
23
+ def initialize(a_body, b_body, anchr_one, anchr_two,
24
+ rest_length, stiffness, damping)
25
+ @struct = DampedSpringStruct.new(CP.cpDampedSpringNew(
26
+ a_body.struct.pointer,b_body.struct.pointer,anchr_one.struct,anchr_two.struct,
27
+ rest_length, stiffness, damping))
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ module CP
2
+
3
+ class GearJointStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :phase, CP_FLOAT,
6
+ :ratio, CP_FLOAT,
7
+ :ratio_inv, CP_FLOAT,
8
+ :i_sum, CP_FLOAT,
9
+ :bias, CP_FLOAT,
10
+ :j_acc, CP_FLOAT,
11
+ :j_max, CP_FLOAT)
12
+ end
13
+ func :cpGearJointNew, [:pointer, :pointer, CP_FLOAT, CP_FLOAT], :pointer
14
+
15
+ class GearJoint
16
+ attr_reader :struct
17
+ def initialize(a_body, b_body, phase, ratio)
18
+ @struct = GearJointStruct.new(CP.cpGearJointNew(
19
+ a_body.struct.pointer,b_body.struct.pointer,phase, ratio))
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ module CP
2
+
3
+ class GrooveJointStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :grv_n, Vect,
6
+ :grv_a, Vect,
7
+ :grv_b, Vect,
8
+ :anchr2, Vect,
9
+ :grv_tn, Vect,
10
+ :clamp, CP_FLOAT,
11
+ :r1, Vect,
12
+ :r2, Vect,
13
+ :k1, Vect,
14
+ :k2, Vect,
15
+ :j_acc, Vect,
16
+ :j_max_length, CP_FLOAT,
17
+ :bias, Vect)
18
+ end
19
+
20
+ func :cpGrooveJointNew, [:pointer, :pointer, Vect.by_value, Vect.by_value, Vect.by_value], :pointer
21
+
22
+ class GrooveJoint
23
+ attr_reader :struct
24
+ def initialize(a_body, b_body, groove_a, groove_b, anchr2)
25
+ @struct = GrooveJointStruct.new(CP.cpGrooveJointNew(
26
+ a_body.struct.pointer,b_body.struct.pointer,groove_a.struct,groove_b.struct,anchr2.struct))
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module CP
2
+
3
+ class PinJointStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :anchr1, Vect,
6
+ :anchr2, Vect,
7
+ :dist, CP_FLOAT,
8
+ :r1, Vect,
9
+ :r2, Vect,
10
+ :n, Vect,
11
+ :n_mass, CP_FLOAT,
12
+ :jn_acc, CP_FLOAT,
13
+ :jn_max, CP_FLOAT,
14
+ :bias, CP_FLOAT)
15
+ end
16
+ func :cpPinJointNew, [:pointer, :pointer, Vect.by_value, Vect.by_value], :pointer
17
+
18
+ class PinJoint
19
+ attr_reader :struct
20
+ def initialize(a_body, b_body, anchr_one, anchr_two)
21
+ @struct = PinJointStruct.new(CP.cpPinJointNew(
22
+ a_body.struct.pointer,b_body.struct.pointer,anchr_one.struct,anchr_two.struct))
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ module CP
2
+
3
+ class PivotJointStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :anchr1, Vect,
6
+ :anchr2, Vect,
7
+ :r1, Vect,
8
+ :r2, Vect,
9
+ :k1, Vect,
10
+ :k2, Vect,
11
+ :j_acc, Vect,
12
+ :j_max, CP_FLOAT,
13
+ :bias, CP_FLOAT)
14
+ end
15
+ func :cpPivotJointNew, [:pointer, :pointer, Vect.by_value], :pointer
16
+ func :cpPivotJointNew2, [:pointer, :pointer, Vect.by_value, Vect.by_value], :pointer
17
+
18
+ class PivotJoint
19
+ attr_reader :struct
20
+ def initialize(a_body, b_body, anchr_one, anchr_two=nil)
21
+ @struct = if anchr_two.nil?
22
+ PivotJointStruct.new(CP.cpPivotJointNew(
23
+ a_body.struct.pointer,b_body.struct.pointer,anchr_one.struct))
24
+ else
25
+ PivotJointStruct.new(CP.cpPivotJointNew2(
26
+ a_body.struct.pointer,b_body.struct.pointer,anchr_one.struct,anchr_two.struct))
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ module CP
2
+
3
+ class RatchetJointStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :angle, CP_FLOAT,
6
+ :phase, CP_FLOAT,
7
+ :ratchet, CP_FLOAT,
8
+ :i_sum, CP_FLOAT,
9
+ :bias, CP_FLOAT,
10
+ :j_acc, CP_FLOAT,
11
+ :j_max, CP_FLOAT)
12
+ end
13
+ func :cpRatchetJointNew, [:pointer, :pointer, CP_FLOAT, CP_FLOAT], :pointer
14
+
15
+ class RatchetJoint
16
+ attr_reader :struct
17
+ def initialize(a_body, b_body, phase, ratchet)
18
+ @struct = RatchetJointStruct.new(CP.cpRatchetJointNew(
19
+ a_body.struct.pointer,b_body.struct.pointer,phase,ratchet))
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module CP
2
+
3
+ class RotaryLimitJointStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :min, CP_FLOAT,
6
+ :max, CP_FLOAT,
7
+ :i_sum, CP_FLOAT,
8
+ :bias, CP_FLOAT,
9
+ :j_acc, CP_FLOAT,
10
+ :j_max, CP_FLOAT)
11
+ end
12
+ func :cpRotaryLimitJointNew, [:pointer, :pointer, CP_FLOAT, CP_FLOAT], :pointer
13
+
14
+ class RotaryLimitJoint
15
+ attr_reader :struct
16
+ def initialize(a_body, b_body, min, max)
17
+ @struct = RotaryLimitJointStruct.new(CP.cpRotaryLimitJointNew(
18
+ a_body.struct.pointer,b_body.struct.pointer,min,max))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module CP
2
+
3
+ class SimpleMotorStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :rate, CP_FLOAT,
6
+ :i_sum, CP_FLOAT,
7
+ :j_acc, CP_FLOAT,
8
+ :j_max, CP_FLOAT)
9
+ end
10
+ func :cpSimpleMotorNew, [:pointer, :pointer, CP_FLOAT], :pointer
11
+
12
+ class SimpleMotor
13
+ attr_reader :struct
14
+ def initialize(a_body, b_body, rate)
15
+ @struct = SimpleMotorStruct.new(CP.cpSimpleMotorNew(
16
+ a_body.struct.pointer,b_body.struct.pointer,rate))
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ module CP
2
+
3
+ class SlideJointStruct < NiceFFI::Struct
4
+ layout(:constraint, ConstraintStruct,
5
+ :anchr1, Vect,
6
+ :anchr2, Vect,
7
+ :min, CP_FLOAT,
8
+ :max, CP_FLOAT,
9
+ :r1, Vect,
10
+ :r2, Vect,
11
+ :n, Vect,
12
+ :n_mass, CP_FLOAT,
13
+ :jn_acc, CP_FLOAT,
14
+ :jn_max, CP_FLOAT,
15
+ :bias, CP_FLOAT)
16
+ end
17
+
18
+ func :cpSlideJointNew, [:pointer, :pointer, Vect.by_value, Vect.by_value, CP_FLOAT, CP_FLOAT], :pointer
19
+
20
+ class SlideJoint
21
+ attr_reader :struct
22
+ def initialize(a_body, b_body, anchr_one, anchr_two, min, max)
23
+ @struct = SlideJointStruct.new(CP.cpSlideJointNew(
24
+ a_body.struct.pointer,b_body.struct.pointer,anchr_one.struct,anchr_two.struct,min,max))
25
+ end
26
+ end
27
+ end
@@ -46,4 +46,8 @@ module CP
46
46
  }
47
47
  cpMomentForPoly(m, verts.size, mem_pointer, offset.struct)
48
48
  end
49
+
50
+ func :cpInitChipmunk, [], :void
51
+ cpInitChipmunk
52
+
49
53
  end
@@ -32,7 +32,8 @@ module CP
32
32
  :data, :pointer,
33
33
  :collision_type, :uint,
34
34
  :group, :uint,
35
- :layers, :int
35
+ :layers, :uint,
36
+ :hash_value, :size_t
36
37
  )
37
38
  end
38
39
  class SegmentQueryInfoStruct < NiceFFI::Struct
@@ -52,7 +53,6 @@ module CP
52
53
  attr_reader :struct
53
54
 
54
55
  def body
55
- # Body.new BodyStruct.new(@struct.body)
56
56
  @body
57
57
  end
58
58
  def body=(new_body)
@@ -110,11 +110,26 @@ module CP
110
110
  @struct.u = new_u
111
111
  end
112
112
 
113
+ def data
114
+ @struct.data
115
+ end
116
+ def data=(new_data)
117
+ @struct.data = new_data
118
+ end
119
+
113
120
  def surface_v
114
121
  Vec2.new @struct.surface_v
115
122
  end
116
123
  def surface_v=(new_sv)
117
- @struct.surface_v = new_sv.struct
124
+ @struct.surface_v.pointer.put_bytes 0, new_sv.struct.to_bytes, 0,Vect.size
125
+ end
126
+
127
+ def set_data_pointer
128
+ mem = FFI::MemoryPointer.new(:ulong)
129
+ mem.put_ulong 0, object_id
130
+ # this is needed to prevent data corruption by GC
131
+ @shape_pointer = mem
132
+ @struct.data = mem
118
133
  end
119
134
 
120
135
  def self.reset_id_counter
@@ -127,6 +142,7 @@ module CP
127
142
  @body = body
128
143
  ptr = CP.cpCircleShapeNew body.struct.pointer, rad, offset_vec.struct
129
144
  @struct = ShapeStruct.new ptr
145
+ set_data_pointer
130
146
  end
131
147
  end
132
148
  class Segment
@@ -135,6 +151,7 @@ module CP
135
151
  @body = body
136
152
  ptr = CP.cpSegmentShapeNew body.struct.pointer, v1.struct, v2.struct, r
137
153
  @struct = ShapeStruct.new ptr
154
+ set_data_pointer
138
155
  end
139
156
  end
140
157
  class Poly
@@ -152,6 +169,7 @@ module CP
152
169
  }
153
170
  ptr = CP.cpPolyShapeNew body.struct.pointer, verts.size, mem_pointer, offset_vec.struct
154
171
  @struct = ShapeStruct.new ptr
172
+ set_data_pointer
155
173
  end
156
174
  end
157
175
  end
@@ -1,16 +1,37 @@
1
1
  module CP
2
+ callback :cpCollisionBeginFunc, [:pointer,:pointer,:pointer], :int
3
+ callback :cpCollisionPreSolveFunc, [:pointer,:pointer,:pointer], :int
4
+ callback :cpCollisionPostSolveFunc, [:pointer,:pointer,:pointer], :int
5
+ callback :cpCollisionSeparateFunc, [:pointer,:pointer,:pointer], :int
6
+
2
7
  class CollisionHandlerStruct < NiceFFI::Struct
3
8
  layout(
4
9
  :a, :uint,
5
10
  :b, :uint,
6
- :begin, :pointer,
7
- :pre_solve, :pointer,
8
- :post_solve, :pointer,
9
- :separate, :pointer,
11
+ :begin, :cpCollisionBeginFunc,
12
+ :pre_solve, :cpCollisionPreSolveFunc,
13
+ :post_solve, :cpCollisionPostSolveFunc,
14
+ :separate, :cpCollisionSeparateFunc,
10
15
  :data, :pointer
11
16
  )
12
17
  end
13
18
 
19
+ class ArbiterStruct < NiceFFI::Struct
20
+ layout(
21
+ :num_contacts, :int,
22
+ :contacts, :pointer,
23
+ :a, :pointer,
24
+ :b, :pointer,
25
+ :e, CP_FLOAT,
26
+ :u, CP_FLOAT,
27
+ :surf_vr, Vect.by_value,
28
+ :stamp, :int,
29
+ :handler, :pointer,
30
+ :swapped_col, :char,
31
+ :first_col, :char
32
+ )
33
+ end
34
+
14
35
  class SpaceStruct < NiceFFI::Struct
15
36
  layout( :iterations, :int,
16
37
  :elastic_iterations, :int,
@@ -24,7 +45,7 @@ module CP
24
45
  :contact_set, :pointer,
25
46
  :constraints, :pointer,
26
47
  :coll_func_set, :pointer,
27
- :default_handler, :pointer,
48
+ :default_handler, CollisionHandlerStruct.by_value,
28
49
  :post_step_callbacks, :pointer
29
50
  )
30
51
  def self.release(ptr)
@@ -32,11 +53,6 @@ module CP
32
53
  end
33
54
  end
34
55
 
35
- callback :cpCollisionBeginFunc, [:pointer,:pointer,:pointer], :int
36
- callback :cpCollisionPreSolveFunc, [:pointer,:pointer,:pointer], :int
37
- callback :cpCollisionPostSolveFunc, [:pointer,:pointer,:pointer], :int
38
- callback :cpCollisionSeparateFunc, [:pointer,:pointer,:pointer], :int
39
-
40
56
 
41
57
  func :cpSpaceNew, [], :pointer
42
58
  func :cpSpaceFreeChildren, [:pointer], :void
@@ -57,12 +73,13 @@ module CP
57
73
  func :cpSpaceResizeStaticHash, [:pointer,CP_FLOAT,:int], :void
58
74
 
59
75
 
60
- # TODO seems like a broken name here
61
- func :cpSpaceSetDefaultCollisionPairFunc, [:pointer, :uint, :uint,
76
+ func :cpSpaceSetDefaultCollisionHandler, [:pointer, :uint, :uint,
62
77
  :pointer, :pointer, :pointer, :pointer, :pointer], :void
63
78
  func :cpSpaceAddCollisionHandler, [:pointer, :uint, :uint,
64
- :pointer, :pointer, :pointer, :pointer, :pointer], :void
79
+ :cpCollisionBeginFunc, :cpCollisionPreSolveFunc, :cpCollisionPostSolveFunc, :cpCollisionSeparateFunc, :pointer], :void
65
80
  func :cpSpaceRemoveCollisionHandler, [:pointer, :uint, :uint], :void
81
+
82
+
66
83
  class Space
67
84
  attr_reader :struct
68
85
  def initialize
@@ -72,6 +89,7 @@ module CP
72
89
  @bodies = []
73
90
  @constraints = []
74
91
  @blocks = {}
92
+ @callbacks = {}
75
93
  end
76
94
 
77
95
  def iterations
@@ -99,36 +117,101 @@ module CP
99
117
  Vec2.new @struct.gravity
100
118
  end
101
119
  def gravity=(v)
102
- @struct.gravity = v.struct
120
+ @struct.gravity.pointer.put_bytes 0, v.struct.to_bytes, 0,Vect.size
103
121
  end
104
122
 
105
123
  def add_collision_func(a,b,&block)
106
- # TODO huh?
107
124
  beg = nil
108
- pre = nil
125
+ pre = Proc.new do |arb_ptr,space_ptr,data_ptr|
126
+ begin
127
+ arb = ArbiterStruct.new(arb_ptr)
128
+
129
+ swapped = arb.swapped_col == 0 ? false : true
130
+ arba = swapped ? arb.b : arb.a
131
+ arbb = swapped ? arb.a : arb.b
132
+
133
+ as = ShapeStruct.new(arba)
134
+ a_obj_id = as.data.get_ulong 0
135
+ rb_a = ObjectSpace._id2ref a_obj_id
136
+
137
+ bs = ShapeStruct.new(arbb)
138
+ b_obj_id = bs.data.get_ulong 0
139
+ rb_b = ObjectSpace._id2ref b_obj_id
140
+
141
+ block.call rb_a, rb_b
142
+ 1
143
+ rescue Exception => ex
144
+ puts ex.message
145
+ puts ex.backtrace
146
+ 0
147
+ end
148
+ end
109
149
  post = nil
110
150
  sep = nil
111
- data = block
151
+ data = nil
112
152
  a_id = a.object_id
113
153
  b_id = b.object_id
114
- # CP.cpSpaceAddCollisionHandler(@struct.pointer, a_id, b_id,
115
- # beg,pre,post,sep,data)
116
- @blocks[[a_id,b_id]] = block
154
+ CP.cpSpaceAddCollisionHandler(@struct.pointer, a_id, b_id,
155
+ beg,pre,post,sep,data)
156
+ @blocks[[a_id,b_id]] = pre
117
157
  nil
118
158
  end
119
159
 
120
160
  def remove_collision_func(a,b)
121
161
  a_id = a.object_id
122
162
  b_id = b.object_id
123
- # CP.cpSpaceRemoveCollisionHandler(@struct.pointer, a_id, b_id)
163
+ CP.cpSpaceRemoveCollisionHandler(@struct.pointer, a_id, b_id)
124
164
  @blocks.delete [a_id,b_id]
125
165
  nil
126
166
  end
127
167
 
128
168
  def set_default_collision_func(&block)
169
+ raise "Not Implmented yet"
129
170
  @blocks[:default] = block
130
171
  end
131
172
 
173
+ def wrap_collision_callback(a,b,type,handler)
174
+ callback = Proc.new do |arb_ptr,space_ptr,data_ptr|
175
+ arb = ArbiterStruct.new(arb_ptr)
176
+
177
+ swapped = arb.swapped_col == 0 ? false : true
178
+ arba = swapped ? arb.b : arb.a
179
+ arbb = swapped ? arb.a : arb.b
180
+
181
+ as = ShapeStruct.new(arba)
182
+ a_obj_id = as.data.get_ulong 0
183
+ rb_a = ObjectSpace._id2ref a_obj_id
184
+
185
+ bs = ShapeStruct.new(arbb)
186
+ b_obj_id = bs.data.get_ulong 0
187
+ rb_b = ObjectSpace._id2ref b_obj_id
188
+
189
+ ret = handler.send type, rb_a, rb_b
190
+ if ret
191
+ 1
192
+ else
193
+ 0
194
+ end
195
+ end
196
+ @callbacks[[a,b,type]] = [handler,callback]
197
+ callback
198
+ end
199
+
200
+ # handler should have methods [beg,pre,post,sep] defined
201
+ def add_collision_handler(a,b,handler)
202
+ a_id = a.object_id
203
+ b_id = b.object_id
204
+
205
+ beg = handler.respond_to?(:begin) ? wrap_collision_callback(a, b, :begin, handler) : nil
206
+ pre = handler.respond_to?(:pre) ? wrap_collision_callback(a, b, :pre, handler) : nil
207
+ post = handler.respond_to?(:post) ? wrap_collision_callback(a, b, :post, handler) : nil
208
+ sep = handler.respond_to?(:sep) ? wrap_collision_callback(a, b, :sep, handler) : nil
209
+ data = nil
210
+
211
+ CP.cpSpaceAddCollisionHandler(@struct.pointer,
212
+ a_id, b_id, beg,pre,post,sep,data)
213
+ end
214
+
132
215
  def add_shape(shape)
133
216
  CP.cpSpaceAddShape(@struct.pointer, shape.struct.pointer)
134
217
  @active_shapes << shape
@@ -190,7 +273,7 @@ module CP
190
273
  end
191
274
 
192
275
  def step(dt)
193
- # CP.cpSpaceStep @struct.pointer, dt
276
+ CP.cpSpaceStep @struct.pointer, dt
194
277
  end
195
278
 
196
279
  def shape_point_query(*args)
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__)+'/spec_helper'
2
+ describe 'Constraints in chipmunk' do
3
+ describe 'PinJoint class' do
4
+ it 'can be created' do
5
+ boda = Body.new 90, 46
6
+ bodb = Body.new 9, 6
7
+ CP::PinJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
8
+ end
9
+ end
10
+
11
+ describe 'SlideJoint class' do
12
+ it 'can be created' do
13
+ boda = Body.new 90, 46
14
+ bodb = Body.new 9, 6
15
+ CP::SlideJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,4,6)
16
+ end
17
+ end
18
+
19
+ describe 'PivotJoint class' do
20
+ it 'can be created' do
21
+ boda = Body.new 90, 46
22
+ bodb = Body.new 9, 6
23
+ CP::PivotJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
24
+ end
25
+ end
26
+
27
+ describe 'GrooveJoint class' do
28
+ it 'can be created' do
29
+ boda = Body.new 90, 46
30
+ bodb = Body.new 9, 6
31
+ CP::GrooveJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,ZERO_VEC_2)
32
+ end
33
+ end
34
+
35
+ describe 'DampedSpring class' do
36
+ it 'can be created' do
37
+ boda = Body.new 90, 46
38
+ bodb = Body.new 9, 6
39
+ CP::DampedSpring.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2,3,4,5)
40
+ end
41
+ end
42
+
43
+ describe 'RotaryLimitJoint class' do
44
+ it 'can be created' do
45
+ boda = Body.new 90, 46
46
+ bodb = Body.new 9, 6
47
+ CP::RotaryLimitJoint.new(boda,bodb,Math::PI,Math::PI/2)
48
+ end
49
+ end
50
+
51
+ describe 'RatchetJoint class' do
52
+ it 'can be created' do
53
+ boda = Body.new 90, 46
54
+ bodb = Body.new 9, 6
55
+ CP::RatchetJoint.new(boda,bodb,3,4)
56
+ end
57
+ end
58
+
59
+ describe 'GearJoint class' do
60
+ it 'can be created' do
61
+ boda = Body.new 90, 46
62
+ bodb = Body.new 9, 6
63
+ CP::GearJoint.new(boda,bodb,1,2)
64
+ end
65
+ end
66
+
67
+ describe 'SimpleMotor class' do
68
+ it 'can be created' do
69
+ boda = Body.new 90, 46
70
+ bodb = Body.new 9, 6
71
+ CP::SimpleMotor.new(boda,bodb,2)
72
+ end
73
+ end
74
+ end
data/spec/shape_spec.rb CHANGED
@@ -34,7 +34,14 @@ describe 'ShapeStruct in chipmunk' do
34
34
  it 'can get its layers' do
35
35
  bod = CP::Body.new 90, 76
36
36
  s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
37
- s.layers.should == -1
37
+ # he sets layers to -1 on an unsigned int
38
+ s.layers.should == 2**32-1
39
+ end
40
+
41
+ it 'can get its group' do
42
+ bod = CP::Body.new 90, 76
43
+ s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
44
+ s.struct.group.should == 0
38
45
  end
39
46
 
40
47
  it 'can get its col type' do
@@ -45,6 +52,26 @@ describe 'ShapeStruct in chipmunk' do
45
52
  s.collision_type.should == :foo
46
53
  s.struct.collision_type.should == :foo.object_id
47
54
  end
55
+
56
+ it 'can get its sensor'
57
+ it 'can get its u' do
58
+ bod = CP::Body.new 90, 76
59
+ s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
60
+ s.u.should be_close(0,0.001)
61
+ end
62
+ it 'can get its surf vec'
63
+
64
+ it 'can get its data' do
65
+ bod = CP::Body.new 90, 76
66
+ s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
67
+ s.data.read_int.should == s.object_id
68
+ end
69
+
70
+ it 'can get its klass' do
71
+ bod = CP::Body.new 90, 76
72
+ s = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
73
+ ShapeClassStruct.new(s.struct.klass).type.should == :circle_shape
74
+ end
48
75
  end
49
76
  describe 'Segment class' do
50
77
  it 'can be created' do
data/spec/space_spec.rb CHANGED
@@ -8,4 +8,101 @@ describe 'Shape in chipmunk' do
8
8
  s.iterations = 9
9
9
  s.iterations.should == 9
10
10
  end
11
+ it 'can set its gravity' do
12
+ s = CP::Space.new
13
+ s.gravity = vec2(4,5)
14
+ s.gravity.x.should == 4
15
+ s.gravity.y.should == 5
16
+ end
17
+
18
+ it 'can have a shape added to it' do
19
+ s = CP::Space.new
20
+ bod = CP::Body.new 90, 76
21
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
22
+ s.add_shape shapy
23
+
24
+ end
25
+
26
+ it 'can have old style callbacks' do
27
+ space = CP::Space.new
28
+ bod = CP::Body.new 90, 76
29
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
30
+ shapy.collision_type = :foo
31
+
32
+ bod_one = CP::Body.new 90, 76
33
+ shapy_one = CP::Shape::Circle.new bod_one, 40, CP::ZERO_VEC_2
34
+ shapy_one.collision_type = :bar
35
+ space.add_shape shapy
36
+ space.add_shape shapy_one
37
+
38
+ called = false
39
+ space.add_collision_func :foo, :bar do |a,b|
40
+ a.should_not be_nil
41
+ b.should_not be_nil
42
+ called = true
43
+ 1
44
+ end
45
+
46
+ space.step 1
47
+ called.should be_true
48
+ end
49
+
50
+ class CollisionHandler
51
+ attr_reader :begin_called
52
+ def begin(a,b)
53
+ @begin_called = [a,b]
54
+ end
55
+ end
56
+
57
+ it 'can have new style callbacks' do
58
+ ch = CollisionHandler.new
59
+
60
+ space = CP::Space.new
61
+ bod = CP::Body.new 90, 76
62
+ shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
63
+ shapy.collision_type = :foo
64
+
65
+ bod_one = CP::Body.new 90, 76
66
+ shapy_one = CP::Shape::Circle.new bod_one, 40, CP::ZERO_VEC_2
67
+ shapy_one.collision_type = :bar
68
+ space.add_shape shapy
69
+ space.add_shape shapy_one
70
+
71
+ space.add_collision_handler :foo, :bar, ch
72
+
73
+ space.step 1
74
+
75
+ ch.begin_called[0].should == shapy
76
+ ch.begin_called[1].should == shapy_one
77
+ end
78
+
79
+ it 'can have lots of shapes no GC corruption' do
80
+ space = CP::Space.new
81
+
82
+ bods = []
83
+ shapes = []
84
+ 5.times do |i|
85
+ bods[i] = CP::Body.new(90, 76)
86
+ shapes[i] = CP::Shape::Circle.new(bods[i], 40, CP::ZERO_VEC_2)
87
+ shapes[i].collision_type = "bar#{i}".to_sym
88
+ space.add_shape(shapes[i])
89
+ space.add_body(bods[i])
90
+ end
91
+
92
+ GC.start
93
+
94
+ space.step 1
95
+ end
96
+
97
+ it 'can have constraints added' do
98
+ space = CP::Space.new
99
+
100
+ boda = Body.new 90, 46
101
+ bodb = Body.new 9, 6
102
+ pj = CP::PinJoint.new(boda,bodb,ZERO_VEC_2,ZERO_VEC_2)
103
+
104
+ space.add_constraint pj
105
+ end
106
+
107
+
11
108
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn Anderson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-04 00:00:00 -05:00
12
+ date: 2009-12-16 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -47,12 +47,23 @@ files:
47
47
  - lib/chipmunk-ffi.rb
48
48
  - lib/chipmunk-ffi/bb.rb
49
49
  - lib/chipmunk-ffi/body.rb
50
+ - lib/chipmunk-ffi/constraint.rb
51
+ - lib/chipmunk-ffi/constraints/damped_spring.rb
52
+ - lib/chipmunk-ffi/constraints/gear_joint.rb
53
+ - lib/chipmunk-ffi/constraints/groove_joint.rb
54
+ - lib/chipmunk-ffi/constraints/pin_joint.rb
55
+ - lib/chipmunk-ffi/constraints/pivot_joint.rb
56
+ - lib/chipmunk-ffi/constraints/ratchet_joint.rb
57
+ - lib/chipmunk-ffi/constraints/rotary_limit_joint.rb
58
+ - lib/chipmunk-ffi/constraints/simple_motor.rb
59
+ - lib/chipmunk-ffi/constraints/slide_joint.rb
50
60
  - lib/chipmunk-ffi/core.rb
51
61
  - lib/chipmunk-ffi/shape.rb
52
62
  - lib/chipmunk-ffi/space.rb
53
63
  - lib/chipmunk-ffi/vec2.rb
54
64
  - spec/bb_spec.rb
55
65
  - spec/body_spec.rb
66
+ - spec/constraint_spec.rb
56
67
  - spec/core_spec.rb
57
68
  - spec/shape_spec.rb
58
69
  - spec/space_spec.rb
@@ -89,6 +100,7 @@ summary: FFI bindings for chipmunk physics lib.
89
100
  test_files:
90
101
  - spec/bb_spec.rb
91
102
  - spec/body_spec.rb
103
+ - spec/constraint_spec.rb
92
104
  - spec/core_spec.rb
93
105
  - spec/shape_spec.rb
94
106
  - spec/space_spec.rb