Sha256: cc72b25b12b6311d1ff84e6879cd5ef6509db573286c3dd74bccf60a4e78cb04

Contents?: true

Size: 1.59 KB

Versions: 8

Compression:

Stored size: 1.59 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] || []
    @cw_world_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

8 entries across 8 versions & 1 rubygems

Version Path
gamebox-0.4.1 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.4.0 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.4.0.rc11 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.4.0.rc5 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.4.0.rc4 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.4.0.rc3 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.4.0.rc2 lib/gamebox/behaviors/collidable/aabb_collidable.rb
gamebox-0.4.0.rc1 lib/gamebox/behaviors/collidable/aabb_collidable.rb