Class: AabbCollidable

Inherits:
CollidableShape show all
Defined in:
lib/gamebox/behaviors/collidable/aabb_collidable.rb

Instance Attribute Summary collapse

Attributes inherited from CollidableShape

#actor, #opts, #radius

Instance Method Summary collapse

Methods inherited from CollidableShape

#actor_x, #actor_y, #initialize, #width

Constructor Details

This class inherits a constructor from CollidableShape

Instance Attribute Details

#cw_local_pointsObject

Returns the value of attribute cw_local_points.



4
5
6
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 4

def cw_local_points
  @cw_local_points
end

Instance Method Details

#build_aabbObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 19

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

#calculate_radiusObject



30
31
32
33
34
35
36
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 30

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

#center_xObject



38
39
40
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 38

def center_x
  actor_x + @actor.width * 0.5
end

#center_yObject



42
43
44
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 42

def center_y
  actor_y + @actor.height * 0.5
end

#clear_collidable_cacheObject



78
79
80
81
82
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 78

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

#cw_world_edge_normalsObject



66
67
68
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 66

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

#cw_world_linesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 50

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

#cw_world_pointsObject



46
47
48
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 46

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

#recalculate_collidable_cacheObject



70
71
72
73
74
75
76
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 70

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

#setupObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 6

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