Class: PolygonCollidable
Instance Attribute Summary collapse
#actor, #opts, #radius
Instance Method Summary
collapse
#actor_x, #actor_y, #initialize, #width
Instance Attribute Details
#cw_local_points ⇒ Object
Returns the value of attribute cw_local_points.
4
5
6
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 4
def cw_local_points
@cw_local_points
end
|
Instance Method Details
#calculate_radius ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 19
def calculate_radius
local_avg = cw_local_points.inject([0,0]) {|s, (x,y)| s[0] += x; s[1]+=y; s}.map {|x| x / cw_local_points.size}
max_dist = 0
cw_local_points.each do |lp|
x_dist = local_avg[0]-lp[0]
y_dist = local_avg[1]-lp[1]
dist = Math.sqrt(x_dist*x_dist + y_dist*y_dist)
max_dist = dist if dist > max_dist
end
max_dist
end
|
31
32
33
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 31
def center_x
poly_center[0]
end
|
35
36
37
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 35
def center_y
poly_center[1]
end
|
#clear_collidable_cache ⇒ Object
79
80
81
82
83
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 79
def clear_collidable_cache
@cached_points = nil
@cached_lines = nil
@cached_poly_center = nil
end
|
#cw_world_edge_normals ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 59
def cw_world_edge_normals
return @cached_normals if @cached_normals
@cached_normals = cw_world_lines.map do |edge|
v = edge[1][0]-edge[0][0], edge[1][1]-edge[0][1]
[-v[1],v[0]]
end
@cached_normals
end
|
#cw_world_lines ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 47
def cw_world_lines
return @cached_lines if @cached_lines
lines = []
cw_world_points.each_cons(2) do |a,b|
lines << [a,b]
end
lines << [cw_world_points[-1],cw_world_points[0]]
@cached_lines = lines
end
|
#cw_world_points ⇒ Object
43
44
45
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 43
def cw_world_points
@cached_points ||= @cw_local_points.map{|lp| [lp[0]+actor_x,lp[1]+actor_y]}
end
|
#poly_center ⇒ Object
39
40
41
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 39
def poly_center
@cached_poly_center ||= cw_world_points.inject([0,0]) {|s, (x,y)| s[0] += x; s[1]+=y; s}.map {|x| x / cw_world_points.size}
end
|
#recalculate_collidable_cache ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 71
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
|
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/gamebox/behaviors/collidable/polygon_collidable.rb', line 6
def setup
@shape_type = opts[:shape]
@cw_local_points = opts[:cw_local_points]
@cw_local_points ||= opts[:points]
@radius = opts[:radius]
@radius ||= calculate_radius
@old_x = actor_x
@old_y = actor_y
end
|