Class: AabbCollidable
- Inherits:
-
CollidableShape
- Object
- CollidableShape
- AabbCollidable
- Defined in:
- lib/gamebox/behaviors/collidable/aabb_collidable.rb
Instance Attribute Summary collapse
-
#cw_local_points ⇒ Object
Returns the value of attribute cw_local_points.
Attributes inherited from CollidableShape
Instance Method Summary collapse
- #build_aabb ⇒ Object
- #calculate_radius ⇒ Object
- #center_x ⇒ Object
- #center_y ⇒ Object
- #clear_collidable_cache ⇒ Object
- #cw_world_edge_normals ⇒ Object
- #cw_world_lines ⇒ Object
- #cw_world_points ⇒ Object
- #recalculate_collidable_cache ⇒ Object
- #setup ⇒ Object
Methods inherited from CollidableShape
#actor_x, #actor_y, #initialize, #width
Constructor Details
This class inherits a constructor from CollidableShape
Instance Attribute Details
#cw_local_points ⇒ Object
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_aabb ⇒ Object
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_radius ⇒ Object
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_x ⇒ Object
38 39 40 |
# File 'lib/gamebox/behaviors/collidable/aabb_collidable.rb', line 38 def center_x actor_x + @actor.width * 0.5 end |
#center_y ⇒ Object
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_cache ⇒ Object
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_normals ⇒ Object
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_lines ⇒ Object
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_points ⇒ Object
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_cache ⇒ Object
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 |
#setup ⇒ Object
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 |