-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.rb
88 lines (81 loc) · 2.72 KB
/
block.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require_relative 'gameobject'
require_relative 'vector2d'
BLOCK_SIZE = 30.0
# Float and Fixnum modifications in order to
# convert to grid size easier
class Fixnum
def u
return BLOCK_SIZE * self
end
end
class Float
def u
return BLOCK_SIZE * self
end
end
EPSILON = 1.0e-6
class Block < GameObject
def initialize(relative_position)
super BLOCK_SIZE, BLOCK_SIZE, Gosu::Color::GREEN, relative_position
end
end
# Returns hash showing x-intersection type and y-intersection type.
# REVIEW needed. This method does not belong to this file.
def intersections(first, second)
def get_vertical_state(first, second)
if (first.top_edge + EPSILON) < second.bottom_edge &&
second.bottom_edge < (first.bottom_edge - EPSILON)
if (first.top_edge + EPSILON) < second.top_edge &&
second.top_edge < (first.bottom_edge - EPSILON)
return :second_in_first
else
return :intersect_top
end
elsif (first.top_edge + EPSILON) < second.top_edge &&
second.top_edge < (first.bottom_edge - EPSILON)
return :intersect_bottom
elsif second.top_edge >= first.bottom_edge - EPSILON
return :lower_side
elsif second.bottom_edge <= first.top_edge + EPSILON
return :upper_side
else
return :first_in_second
end
end
def get_horizontal_state(first, second)
if (first.left_edge + EPSILON) < second.right_edge &&
second.right_edge < (first.right_edge - EPSILON)
if (first.left_edge + EPSILON) < second.left_edge &&
second.left_edge < (first.right_edge - EPSILON)
return :second_in_first
else
return :intersect_left
end
elsif (first.left_edge + EPSILON) < second.left_edge &&
second.left_edge < (first.right_edge - EPSILON)
return :intersect_right
elsif second.right_edge <= first.left_edge + EPSILON
return :left_side
elsif second.left_edge >= first.right_edge - EPSILON
return :right_side
else
return :first_in_second
end
end
x_state = get_horizontal_state(first, second)
y_state = get_vertical_state(first, second)
dist_x = case x_state
when :intersect_left then first.left_edge - second.right_edge
when :intersect_right then first.right_edge - second.left_edge
else Float::INFINITY
end
dist_y = case y_state
when :intersect_top then first.top_edge - second.bottom_edge
when :intersect_bottom then first.bottom_edge - second.top_edge
else Float::INFINITY
end
intersection = (x_state != :left_side && x_state != :right_side) &&
(y_state != :upper_side && y_state != :lower_side)
{:x => x_state, :y => y_state, :intersect => intersection,
:dist_x => dist_x, :dist_y => dist_y}
end