Module: DXOpal::Sprite::Physics

Included in:
DXOpal::Sprite
Defined in:
lib/dxopal/sprite/physics.rb

Overview

Experimental Matter.js (physics engine) support

You need to load matter.js in advance to using these features.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_matter_bodyObject (readonly)

Returns the value of attribute _matter_body.



25
26
27
# File 'lib/dxopal/sprite/physics.rb', line 25

def _matter_body
  @_matter_body
end

Instance Method Details

#_move_matter_bodyObject



27
28
29
30
31
# File 'lib/dxopal/sprite/physics.rb', line 27

def _move_matter_body
  # TODO: support non-default center_x, center_y
  `Matter.Body.setPosition(#{@_matter_body},
     Matter.Vector.create(#{@x+@center_x}, #{@y+@center_y}))`
end

#_move_to_matter_body(mx, my) ⇒ Object



33
34
35
36
# File 'lib/dxopal/sprite/physics.rb', line 33

def _move_to_matter_body(mx, my)
  @x = mx - @center_x
  @y = my - @center_y
end

#physical_body=(ary) ⇒ Object

Create Matter Body and register it to the World

  • type: :rectangle, etc.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dxopal/sprite/physics.rb', line 9

def physical_body=(ary)
  raise "Call Sprite#initialize before calling physical_body=" if self.x.nil?
  type = ary[0]
  case type
  when :rectangle
    _, width, height, opts = *ary
    x = self.x + width/2
    y = self.y + height/2
    info = [width, height]
    `opts.angle = opts.angle || #{self.angle * Math::PI / 180}`
    @_matter_body = `Matter.Bodies[type](x, y, width, height, opts)`
  else
    raise "type #{type.inspect} is unknown or not supported yet"
  end
  Sprite._add_matter_body(@_matter_body, type, self, info)
end