Class: CP::Arbiter

Inherits:
Object
  • Object
show all
Defined in:
lib/chipmunk-ffi/arbiter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ Arbiter

Returns a new instance of Arbiter.



29
30
31
32
33
34
35
# File 'lib/chipmunk-ffi/arbiter.rb', line 29

def initialize(ptr)
  @struct = ArbiterStruct.new(ptr)
  @shapes = nil
  
  # Temporary workaround for a bug in chipmunk, fixed in r342.
  @struct.num_contacts = 0 if @struct.contacts.null?
end

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



28
29
30
# File 'lib/chipmunk-ffi/arbiter.rb', line 28

def struct
  @struct
end

Instance Method Details

#aObject



78
79
80
# File 'lib/chipmunk-ffi/arbiter.rb', line 78

def a
  self.shapes[0]
end

#bObject



82
83
84
# File 'lib/chipmunk-ffi/arbiter.rb', line 82

def b
  self.shapes[1]
end

#eObject



86
87
88
# File 'lib/chipmunk-ffi/arbiter.rb', line 86

def e
  @struct.e
end

#e=(new_e) ⇒ Object



89
90
91
# File 'lib/chipmunk-ffi/arbiter.rb', line 89

def e=(new_e)
  @struct.e = new_e
end

#each_contactObject



100
101
102
103
104
# File 'lib/chipmunk-ffi/arbiter.rb', line 100

def each_contact
  (0...@struct.num_contacts).each do |index|
    yield CP.cpArbiterGetPoint(@struct, index), CP.cpArbiterGetNormal(@struct, index)
  end
end

#first_contact?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/chipmunk-ffi/arbiter.rb', line 37

def first_contact?
  # CP.cpArbiterIsFirstContact(@struct.pointer).nonzero?
  @struct.first_col.nonzero?
end

#impulse(with_friction = false) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/chipmunk-ffi/arbiter.rb', line 52

def impulse with_friction = false
  if with_friction
    Vec2.new CP.cpArbiterTotalImpulseWithFriction(@struct.pointer)
  else
    Vec2.new CP.cpArbiterTotalImpulse(@struct.pointer)
  end
end

#normal(index = 0) ⇒ Object

Raises:

  • (IndexError)


47
48
49
50
# File 'lib/chipmunk-ffi/arbiter.rb', line 47

def normal(index = 0)
  raise IndexError unless (0...@struct.num_contacts).include? index
  Vec2.new CP.cpArbiterGetNormal(@struct.pointer, index)
end

#point(index = 0) ⇒ Object

Raises:

  • (IndexError)


42
43
44
45
# File 'lib/chipmunk-ffi/arbiter.rb', line 42

def point(index = 0)
  raise IndexError unless (0...@struct.num_contacts).include? index
  Vec2.new CP.cpArbiterGetPoint(@struct.pointer, index)
end

#shapesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chipmunk-ffi/arbiter.rb', line 60

def shapes
  return @shapes if @shapes

  swapped = @struct.swapped_col.nonzero?
  arba = swapped ? @struct.b : @struct.a
  arbb = swapped ? @struct.a : @struct.b

  as = ShapeStruct.new(arba)
  a_obj_id = as.data.get_long 0
  rb_a = ObjectSpace._id2ref a_obj_id

  bs = ShapeStruct.new(arbb)
  b_obj_id = bs.data.get_long 0
  rb_b = ObjectSpace._id2ref b_obj_id

  @shapes = [ rb_a, rb_b ]
end

#uObject



93
94
95
# File 'lib/chipmunk-ffi/arbiter.rb', line 93

def u
  @struct.u
end

#u=(new_u) ⇒ Object



96
97
98
# File 'lib/chipmunk-ffi/arbiter.rb', line 96

def u=(new_u)
  @struct.u = new_u
end