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.
@@ -14,9 +14,9 @@ Install
14
14
 
15
15
  You will need:
16
16
 
17
- * Ruby 1.8.6+ or JRuby 1.4+
18
- * Chipmunk > 5.0 (svn right now... http://code.google.com/p/chipmunk-physics/ )
19
- * FFI > 0.6.0 (git right now... http://github.com/ffi/ffi/ )
17
+ * Ruby 1.8.6+ or JRuby 1.5+
18
+ * Chipmunk > 5.2
19
+ * FFI > 0.6.0
20
20
 
21
21
  Then...
22
22
 
@@ -34,3 +34,4 @@ Author
34
34
  ------
35
35
 
36
36
  Shawn Anderson <shawn42+chipmunk@gmail.com>
37
+ Philomory http://github.com/philomory
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chipmunk-ffi}
8
- s.version = "1.0.0"
8
+ s.version = "1.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{2010-01-04}
12
+ s.date = %q{2010-04-10}
13
13
  s.description = %q{FFI bindings for chipmunk physics lib.}
14
14
  s.email = %q{shawn42@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "lib/chipmunk-ffi/bb.rb",
26
26
  "lib/chipmunk-ffi/body.rb",
27
27
  "lib/chipmunk-ffi/constraint.rb",
28
+ "lib/chipmunk-ffi/constraints/damped_rotary_spring.rb",
28
29
  "lib/chipmunk-ffi/constraints/damped_spring.rb",
29
30
  "lib/chipmunk-ffi/constraints/gear_joint.rb",
30
31
  "lib/chipmunk-ffi/constraints/groove_joint.rb",
@@ -38,6 +39,7 @@ Gem::Specification.new do |s|
38
39
  "lib/chipmunk-ffi/shape.rb",
39
40
  "lib/chipmunk-ffi/space.rb",
40
41
  "lib/chipmunk-ffi/space_hash.rb",
42
+ "lib/chipmunk-ffi/struct_accessor.rb",
41
43
  "lib/chipmunk-ffi/unsafe.rb",
42
44
  "lib/chipmunk-ffi/vec2.rb",
43
45
  "spec/bb_spec.rb",
@@ -56,7 +58,7 @@ Gem::Specification.new do |s|
56
58
  s.rdoc_options = ["--charset=UTF-8"]
57
59
  s.require_paths = ["lib"]
58
60
  s.rubyforge_project = %q{chipmunk-ffi}
59
- s.rubygems_version = %q{1.3.5}
61
+ s.rubygems_version = %q{1.3.6}
60
62
  s.summary = %q{FFI bindings for chipmunk physics lib.}
61
63
  s.test_files = [
62
64
  "spec/bb_spec.rb",
@@ -1,3 +1,4 @@
1
+ require 'rubygems'
1
2
  require 'nice-ffi'
2
3
 
3
4
  module CP
@@ -14,13 +15,17 @@ module CP
14
15
  end
15
16
 
16
17
  end
17
- load_library "chipmunk", CP::LOAD_PATHS
18
+
19
+ defined?(::CP_EXACT_PATH) ?
20
+ load_library(::CP_EXACT_PATH) :
21
+ load_library("chipmunk", CP::LOAD_PATHS)
22
+
18
23
  def self.cp_static_inline(func_sym, args, ret)
19
24
  func_name = "_#{func_sym}"
20
25
  attach_variable func_name, :pointer
21
26
  const_func_name = func_sym.to_s.upcase
22
27
 
23
- func = FFI::Function.new(ret, args, FFI::Pointer.new(self.send(func_name)), :convention => :default )
28
+ func = FFI::Function.new(ret, args, FFI::Pointer.new(self.send(func_name).to_i), :convention => :default )
24
29
  const_set const_func_name, func
25
30
 
26
31
  instance_eval <<-METHOD
@@ -5,8 +5,8 @@ module CP
5
5
 
6
6
  class BodyStruct < NiceFFI::Struct
7
7
  layout(
8
- :bodyVelocityFunc, :cpBodyVelocityFunc,
9
- :bodyPositionFunc, :cpBodyPositionFunc,
8
+ :velocity_func, :cpBodyVelocityFunc,
9
+ :position_func, :cpBodyPositionFunc,
10
10
  :m, CP_FLOAT,
11
11
  :m_inv, CP_FLOAT,
12
12
  :i, CP_FLOAT,
@@ -54,9 +54,12 @@ module CP
54
54
  when 2
55
55
  ptr = CP.cpBodyNew(*args)
56
56
  @struct = BodyStruct.new ptr
57
+ set_data_pointer
57
58
  else
58
59
  raise "wrong number of args for Body, got #{args.size}, but expected 2"
59
60
  end
61
+ set_default_velocity_lambda
62
+ set_default_position_lambda
60
63
  end
61
64
 
62
65
  def m
@@ -194,5 +197,60 @@ module CP
194
197
  CP.cpBodyUpdatePosition(@struct.pointer,dt)
195
198
  end
196
199
 
200
+ def velocity_func
201
+ @user_level_velocity_lambda
202
+ end
203
+
204
+ def velocity_func=(l)
205
+ @user_level_velocity_lambda = l
206
+
207
+ # We keep the lambda in an ivar to keep it from being GCed
208
+ @body_velocity_lambda = Proc.new do |body_ptr,g,dmp,dt|
209
+ body_struct = BodyStruct.new(body_ptr)
210
+ obj_id = body_struct.data.get_long(0)
211
+ body = ObjectSpace._id2ref(obj_id)
212
+ l.call(body,g,dmp,dt)
213
+ end
214
+ @struct.velocity_func = @body_velocity_lambda
215
+ end
216
+
217
+ def position_func
218
+ @user_level_position_lambda
219
+ end
220
+
221
+ def position_func=(l)
222
+ @user_level_position_lambda = l
223
+
224
+ # We keep the lambda in an ivar to keep it from being GCed
225
+ @body_position_lambda = Proc.new do |body_ptr,dt|
226
+ body_struct = BodyStruct.new(body_ptr)
227
+ obj_id = body_struct.data.get_long(0)
228
+ body = ObjectSpace._id2ref(obj_id)
229
+ l.call(body,dt)
230
+ end
231
+ @struct.position_func = @body_position_lambda
232
+ end
233
+
234
+ private
235
+ def set_data_pointer
236
+ mem = FFI::MemoryPointer.new(:long)
237
+ mem.put_long 0, object_id
238
+ # this is needed to prevent data corruption by GC
239
+ @body_pointer = mem
240
+ @struct.data = mem
241
+ end
242
+
243
+ def set_default_velocity_lambda
244
+ @user_level_velocity_lambda = Proc.new do |body,g,dmp,dt|
245
+ body.update_velocity(g,dmp,dt)
246
+ end
247
+ end
248
+
249
+ def set_default_position_lambda
250
+ @user_level_position_lambda = Proc.new do |body,dt|
251
+ body.update_position(dt)
252
+ end
253
+ end
254
+
197
255
  end
198
256
  end
@@ -1,3 +1,5 @@
1
+ require 'chipmunk-ffi/struct_accessor'
2
+
1
3
  module CP
2
4
  callback :cpConstraintPreStepFunction, [:pointer, CP_FLOAT, CP_FLOAT], :void
3
5
  callback :cpConstraintApplyImpulseFunction, [:pointer], :void
@@ -14,9 +16,32 @@ module CP
14
16
  :a, :pointer,
15
17
  :b, :pointer,
16
18
  :max_force, CP_FLOAT,
19
+ :bias_coef, CP_FLOAT,
17
20
  :max_bias, CP_FLOAT,
18
21
  :data, :pointer)
19
22
  end
23
+
24
+ module Constraint
25
+ attr_reader :body_a, :body_b, :struct
26
+ [:max_force,:bias_coef,:max_bias].each do |sym|
27
+ define_method(sym) { struct.constraint[sym] }
28
+ define_method("#{sym}=") {|val| struct.constraint[sym] = val.to_f }
29
+ end
30
+
31
+ def self.included(other)
32
+ super
33
+ other.class_eval { extend StructAccessor }
34
+ end
35
+
36
+ def set_data_pointer
37
+ mem = FFI::MemoryPointer.new(:long)
38
+ mem.put_long 0, object_id
39
+ # this is needed to prevent data corruption by GC
40
+ @constraint_pointer = mem
41
+ @struct.constraint.data = mem
42
+ end
43
+
44
+ end
20
45
 
21
46
  require 'chipmunk-ffi/constraints/pin_joint'
22
47
  require 'chipmunk-ffi/constraints/slide_joint'
@@ -27,4 +52,5 @@ module CP
27
52
  require 'chipmunk-ffi/constraints/ratchet_joint'
28
53
  require 'chipmunk-ffi/constraints/gear_joint'
29
54
  require 'chipmunk-ffi/constraints/simple_motor'
55
+ require 'chipmunk-ffi/constraints/damped_rotary_spring'
30
56
  end
@@ -0,0 +1,58 @@
1
+ module CP
2
+
3
+ callback :cpDampedRotarySpringTorqueFunc, [:pointer, CP_FLOAT], CP_FLOAT
4
+
5
+ class DampedRotarySpringStruct < NiceFFI::Struct
6
+ layout(:constraint, ConstraintStruct,
7
+ :rest_angle, CP_FLOAT,
8
+ :stiffness, CP_FLOAT,
9
+ :damping, CP_FLOAT,
10
+ :spring_torque_func, :cpDampedRotarySpringTorqueFunc,
11
+ :dt, CP_FLOAT,
12
+ :target_wrn, CP_FLOAT,
13
+ :i_sum, CP_FLOAT)
14
+ end
15
+
16
+ func :cpDampedRotarySpringNew, [:pointer, :pointer, CP_FLOAT, CP_FLOAT, CP_FLOAT], :pointer
17
+
18
+ class DampedRotarySpring
19
+ include Constraint
20
+ struct_accessor DampedRotarySpringStruct, :rest_angle, :damping, :stiffness
21
+ def initialize(a_body, b_body,rest_angle, stiffness, damping)
22
+ @body_a, @body_b = a_body, b_body
23
+ @struct = DampedRotarySpringStruct.new(CP.cpDampedRotarySpringNew(
24
+ a_body.struct.pointer,b_body.struct.pointer, rest_angle, stiffness, damping))
25
+ set_data_pointer
26
+ set_initial_torque_proc
27
+ end
28
+
29
+ def spring_torque_func
30
+ @user_level_torque_lambda
31
+ end
32
+
33
+ def spring_torque_func=(l)
34
+ @user_level_torque_lambda = l
35
+
36
+ # We keep the lambda in an ivar to keep it from being GCed
37
+ @spring_torque_lambda = Proc.new do |spring_ptr,angle|
38
+ spring_struct = DampedRotarySpringStruct.new(spring_ptr)
39
+ obj_id = spring_struct.constraint.data.get_long(0)
40
+ spring = ObjectSpace._id2ref(obj_id)
41
+ l.call(spring,angle)
42
+ end
43
+ @struct.spring_torque_func = @spring_torque_lambda
44
+ end
45
+
46
+ private
47
+ def set_initial_torque_proc
48
+ ffi_func = @struct.spring_torque_func
49
+ @user_level_torque_lambda ||= Proc.new do |spring, angle|
50
+ ffi_func.call(spring.struct,angle)
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ # Alias for compatibility with chipmunk C-Ruby bindings.
58
+ CP::Constraint::DampedRotarySpring = CP::DampedRotarySpring
@@ -1,5 +1,7 @@
1
1
  module CP
2
2
 
3
+ callback :cpDampedSpringForceFunc, [:pointer, CP_FLOAT], CP_FLOAT
4
+
3
5
  class DampedSpringStruct < NiceFFI::Struct
4
6
  layout(:constraint, ConstraintStruct,
5
7
  :anchr1, Vect,
@@ -7,7 +9,7 @@ module CP
7
9
  :rest_length, CP_FLOAT,
8
10
  :stiffness, CP_FLOAT,
9
11
  :damping, CP_FLOAT,
10
- :damped_spring_force_fun, :pointer,
12
+ :spring_force_func, :cpDampedSpringForceFunc,
11
13
  :dt, CP_FLOAT,
12
14
  :target_vrn, CP_FLOAT,
13
15
  :r1, Vect,
@@ -19,12 +21,44 @@ module CP
19
21
  func :cpDampedSpringNew, [:pointer, :pointer, Vect.by_value, Vect.by_value, CP_FLOAT, CP_FLOAT, CP_FLOAT], :pointer
20
22
 
21
23
  class DampedSpring
22
- attr_reader :struct
24
+ include Constraint
25
+ struct_accessor DampedSpringStruct, :anchr1, :anchr2, :rest_length, :damping, :stiffness
23
26
  def initialize(a_body, b_body, anchr_one, anchr_two,
24
27
  rest_length, stiffness, damping)
28
+ @body_a, @body_b = a_body, b_body
25
29
  @struct = DampedSpringStruct.new(CP.cpDampedSpringNew(
26
30
  a_body.struct.pointer,b_body.struct.pointer,anchr_one.struct,anchr_two.struct,
27
31
  rest_length, stiffness, damping))
32
+ set_data_pointer
33
+ set_initial_force_proc
34
+ end
35
+
36
+ def spring_force_func
37
+ @user_level_force_lambda
38
+ end
39
+
40
+ def spring_force_func=(l)
41
+ @user_level_force_lambda = l
42
+
43
+ # We keep the lambda in an ivar to keep it from being GCed
44
+ @spring_force_lambda = Proc.new do |spring_ptr,dist|
45
+ spring_struct = DampedSpringStruct.new(spring_ptr)
46
+ obj_id = spring_struct.constraint.data.get_long(0)
47
+ spring = ObjectSpace._id2ref(obj_id)
48
+ l.call(spring,dist)
49
+ end
50
+ @struct.spring_force_func = @spring_force_lambda
51
+ end
52
+
53
+ private
54
+ def set_initial_force_proc
55
+ ffi_func = @struct.spring_force_func
56
+ @user_level_force_lambda ||= Proc.new do |spring, dist|
57
+ ffi_func.call(spring.struct,dist)
58
+ end
28
59
  end
29
60
  end
30
61
  end
62
+
63
+ # Alias for compatibility with chipmunk C-Ruby bindings.
64
+ CP::Constraint::DampedSpring = CP::DampedSpring
@@ -11,12 +11,24 @@ module CP
11
11
  :j_max, CP_FLOAT)
12
12
  end
13
13
  func :cpGearJointNew, [:pointer, :pointer, CP_FLOAT, CP_FLOAT], :pointer
14
+ func :cpGearJointSetRatio, [:pointer, CP_FLOAT], :void
14
15
 
15
16
  class GearJoint
16
- attr_reader :struct
17
+ include Constraint
18
+ struct_accessor GearJointStruct, :phase
19
+ struct_reader GearJointStruct, :ratio
17
20
  def initialize(a_body, b_body, phase, ratio)
21
+ @body_a, @body_b = a_body, b_body
18
22
  @struct = GearJointStruct.new(CP.cpGearJointNew(
19
23
  a_body.struct.pointer,b_body.struct.pointer,phase, ratio))
20
24
  end
25
+
26
+ def ratio=(val)
27
+ CP.cpGearJointSetRatio(@struct.pointer,val)
28
+ end
29
+
21
30
  end
22
31
  end
32
+
33
+ # Alias for compatibility with chipmunk C-Ruby bindings.
34
+ CP::Constraint::GearJoint = CP::GearJoint
@@ -20,10 +20,15 @@ module CP
20
20
  func :cpGrooveJointNew, [:pointer, :pointer, Vect.by_value, Vect.by_value, Vect.by_value], :pointer
21
21
 
22
22
  class GrooveJoint
23
- attr_reader :struct
23
+ include Constraint
24
+ struct_accessor GrooveJointStruct, :anchr2
24
25
  def initialize(a_body, b_body, groove_a, groove_b, anchr2)
26
+ @body_a, @body_b = a_body, b_body
25
27
  @struct = GrooveJointStruct.new(CP.cpGrooveJointNew(
26
28
  a_body.struct.pointer,b_body.struct.pointer,groove_a.struct,groove_b.struct,anchr2.struct))
27
29
  end
28
30
  end
29
31
  end
32
+
33
+ # Alias for compatibility with chipmunk C-Ruby bindings.
34
+ CP::Constraint::GrooveJoint = CP::GrooveJoint
@@ -16,10 +16,15 @@ module CP
16
16
  func :cpPinJointNew, [:pointer, :pointer, Vect.by_value, Vect.by_value], :pointer
17
17
 
18
18
  class PinJoint
19
- attr_reader :struct
19
+ include Constraint
20
+ struct_accessor PinJointStruct, :anchr1, :anchr2, :dist
20
21
  def initialize(a_body, b_body, anchr_one, anchr_two)
22
+ @body_a, @body_b = a_body, b_body
21
23
  @struct = PinJointStruct.new(CP.cpPinJointNew(
22
24
  a_body.struct.pointer,b_body.struct.pointer,anchr_one.struct,anchr_two.struct))
23
25
  end
24
26
  end
25
27
  end
28
+
29
+ # Alias for compatibility with chipmunk C-Ruby bindings.
30
+ CP::Constraint::PinJoint = CP::PinJoint
@@ -16,8 +16,10 @@ module CP
16
16
  func :cpPivotJointNew2, [:pointer, :pointer, Vect.by_value, Vect.by_value], :pointer
17
17
 
18
18
  class PivotJoint
19
- attr_reader :struct
19
+ include Constraint
20
+ struct_accessor PivotJointStruct, :anchr1, :anchr2
20
21
  def initialize(a_body, b_body, anchr_one, anchr_two=nil)
22
+ @body_a, @body_b = a_body, b_body
21
23
  @struct = if anchr_two.nil?
22
24
  PivotJointStruct.new(CP.cpPivotJointNew(
23
25
  a_body.struct.pointer,b_body.struct.pointer,anchr_one.struct))
@@ -28,3 +30,6 @@ module CP
28
30
  end
29
31
  end
30
32
  end
33
+
34
+ # Alias for compatibility with chipmunk C-Ruby bindings.
35
+ CP::Constraint::PivotJoint = CP::PivotJoint
@@ -13,10 +13,15 @@ module CP
13
13
  func :cpRatchetJointNew, [:pointer, :pointer, CP_FLOAT, CP_FLOAT], :pointer
14
14
 
15
15
  class RatchetJoint
16
- attr_reader :struct
16
+ include Constraint
17
+ struct_accessor RatchetJointStruct, :angle, :phase, :ratchet
17
18
  def initialize(a_body, b_body, phase, ratchet)
19
+ @body_a, @body_b = a_body, b_body
18
20
  @struct = RatchetJointStruct.new(CP.cpRatchetJointNew(
19
21
  a_body.struct.pointer,b_body.struct.pointer,phase,ratchet))
20
22
  end
21
23
  end
22
24
  end
25
+
26
+ # Alias for compatibility with chipmunk C-Ruby bindings.
27
+ CP::Constraint::RatchetJoint = CP::RatchetJoint
@@ -12,10 +12,15 @@ module CP
12
12
  func :cpRotaryLimitJointNew, [:pointer, :pointer, CP_FLOAT, CP_FLOAT], :pointer
13
13
 
14
14
  class RotaryLimitJoint
15
- attr_reader :struct
15
+ include Constraint
16
+ struct_accessor RotaryLimitJointStruct, :min, :max
16
17
  def initialize(a_body, b_body, min, max)
18
+ @body_a, @body_b = a_body, b_body
17
19
  @struct = RotaryLimitJointStruct.new(CP.cpRotaryLimitJointNew(
18
20
  a_body.struct.pointer,b_body.struct.pointer,min,max))
19
21
  end
20
22
  end
21
23
  end
24
+
25
+ # Alias for compatibility with chipmunk C-Ruby bindings.
26
+ CP::Constraint::RotaryLimitJoint = CP::RotaryLimitJoint