Skip to content

Commit

Permalink
Added DampedRotarySpring
Browse files Browse the repository at this point in the history
  • Loading branch information
philomory authored and shawn42 committed Apr 10, 2010
1 parent 1f2462d commit 929947e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/chipmunk-ffi/constraint.rb
Expand Up @@ -44,4 +44,5 @@ def self.included(other)
require 'chipmunk-ffi/constraints/ratchet_joint'
require 'chipmunk-ffi/constraints/gear_joint'
require 'chipmunk-ffi/constraints/simple_motor'
require 'chipmunk-ffi/constraints/damped_rotary_spring'
end
43 changes: 43 additions & 0 deletions lib/chipmunk-ffi/constraints/damped_rotary_spring.rb
@@ -0,0 +1,43 @@
module CP

callback :cpDampedRotarySpringTorqueFunc, [:pointer, CP_FLOAT], CP_FLOAT

class DampedRotarySpringStruct < NiceFFI::Struct
layout(:constraint, ConstraintStruct,
:rest_angle, CP_FLOAT,
:stiffness, CP_FLOAT,
:damping, CP_FLOAT,
:spring_torque_func, :cpDampedRotarySpringTorqueFunc,
:dt, CP_FLOAT,
:target_wrn, CP_FLOAT,
:i_sum, CP_FLOAT)
end

func :cpDampedRotarySpringNew, [:pointer, :pointer, CP_FLOAT, CP_FLOAT, CP_FLOAT], :pointer

class DampedRotarySpring
include Constraint
struct_accessor DampedRotarySpringStruct, :rest_angle, :damping, :stiffness
def initialize(a_body, b_body,rest_angle, stiffness, damping)
@body_a, @body_b = a_body, b_body
@struct = DampedRotarySpringStruct.new(CP.cpDampedRotarySpringNew(
a_body.struct.pointer,b_body.struct.pointer, rest_angle, stiffness, damping))
end


# FIXME: Ideally, we'd prefer to pass DampedSprings, rather than DampedSpringStructs,
# to the user's lambda; or, better still, pass no spring at all, and allow them to refer
# to self. However, this means using wrapper procs in both the getter and the setter; in
# the case where the user takes a lambda recieved from a reader and supplies it to a writer;
# Each time this happens, we get a more deeply nested chain of lambdas.
def spring_torque_func
@struct.spring_torque_func
end

def spring_torque_func=(l)
@spring_torque_lambda = l # Keep the lambda from being GCed
@struct.spring_torque_func = @spring_torque_lambda
end
end
end

46 changes: 46 additions & 0 deletions spec/constraint_spec.rb
Expand Up @@ -312,4 +312,50 @@
joint.rate.should == -2.0
end
end

describe 'DampedRotarySpring class' do
it 'can be created' do
boda = Body.new 90, 46
bodb = Body.new 9, 6
CP::DampedRotarySpring.new(boda,bodb,3,4,5)
end

it 'can get and set its rest angle' do
boda = CP::Body.new 90, 46
bodb = CP::Body.new 9, 6
joint = CP::DampedRotarySpring.new(boda,bodb,3,4,5)
joint.rest_angle.should == 3.0
joint.rest_angle = 1
joint.rest_angle.should == 1.0
end

it 'can get and set its stiffness' do
boda = CP::Body.new 90, 46
bodb = CP::Body.new 9, 6
joint = CP::DampedRotarySpring.new(boda,bodb,3,4,5)
joint.stiffness.should == 4.0
joint.stiffness = 1
joint.stiffness.should == 1.0
end

it 'can get and set its damping' do
boda = CP::Body.new 90, 46
bodb = CP::Body.new 9, 6
joint = CP::DampedRotarySpring.new(boda,bodb,3,4,5)
joint.damping.should == 5.0
joint.damping = 1
joint.damping.should == 1.0
end

it 'can get and set its spring torque function' do
boda = CP::Body.new 90, 46
bodb = CP::Body.new 9, 6
joint = CP::DampedRotarySpring.new(boda,bodb,3,4,5)
joint.spring_torque_func.call(joint.struct,1.0).should == -8.0
joint.spring_torque_func = lambda {|spring,float| float + 1.0 }
joint.spring_torque_func.call(joint.struct,1.0).should == 2.0
end
end


end

0 comments on commit 929947e

Please sign in to comment.