module DXOpal::Sprite::Physics

Experimental Matter.js (physics engine) support

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

Attributes

_matter_body[R]

Public Instance Methods

_move_matter_body() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Create Matter Body and register it to the World

  • type: :rectangle, etc.

# 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