Sha256: 2737f08d57cfe1908534f39a182d5e01f70c3468399c3354944d3cb1f363e6f5

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents


class AabbCollidable < CollidableShape
  attr_accessor :cw_local_points

  def setup
    @shape_type = opts[:shape]

    @cw_local_points = opts[:cw_local_points]
    @cw_local_points ||= opts[:points] || build_aabb

    @radius = opts[:radius]
    @radius ||= calculate_radius

    @old_x = actor_x
    @old_y = actor_y
  end

  def build_aabb
    w = @actor.width
    h = @actor.height
    [
      [0,0],
      [w,0],
      [w,h],
      [0,h]
    ]
  end

  def calculate_radius
    w = @actor.width
    hw = w * 0.5
    h = @actor.height
    hh = h * 0.5
    Math.sqrt(hw*hw+hh*hh)
  end

  def center_x
    actor_x + @actor.width * 0.5
  end

  def center_y
    actor_y + @actor.height * 0.5
  end

  def cw_world_points
    @cached_points ||= @cw_local_points.map{|lp| [lp[0]+actor_x,lp[1]+actor_y]}
  end

  def cw_world_lines
    return @cached_lines if @cached_lines
    lines = [] 

    hw = @actor.width * 0.5
    hh = @actor.height * 0.5
    lines = [
      [[actor_x-hw,actor_y+hh], [actor_x+hw,actor_y+hh]],
      [[actor_x+hw,actor_y+hh], [actor_x+hw,actor_y-hh]],
      [[actor_x+hw,actor_y-hh], [actor_x-hw,actor_y-hh]],
      [[actor_x-hw,actor_y-hh], [actor_x-hw,actor_y+hh]]
    ]

    @cached_lines = lines
  end

  def cw_world_edge_normals
    @cached_normals ||= [[1,0],[0,1]]
  end

  def recalculate_collidable_cache
    unless @old_x == actor_x && @old_y == actor_y
      clear_collidable_cache
      @old_x = actor_x
      @old_y = actor_y
    end
  end

  def clear_collidable_cache
    @cached_points = nil
    @cached_lines = nil
    @cached_poly_center = nil
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gamebox-0.5.5 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.5.4 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.5.2 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.5.1 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.5.0 lib/gamebox/behaviors/collidable/aabb_collidable.rb