Module: CollisionAABB

Defined in:
lib/collision/collision_aabb.rb

Class Method Summary collapse

Class Method Details

.bounce(item, other, goal) ⇒ Object



16
# File 'lib/collision/collision_aabb.rb', line 16

def self.bounce(item, other, goal) end

.cross(item, other, goal) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/collision/collision_aabb.rb', line 18

def self.cross(item, other, goal)
  x, y = goal.to_a

  if x != 0
    goal.x *= -1
    item.position.x += x
  end

  if y != 0
    goal.y *= -1
    item.position.y += y
  end

  item
end

.is_overlaps?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/collision/collision_aabb.rb', line 12

def self.is_overlaps?(a, b)
  was_vertical_aligned?(a, b) && was_horizontal_aligned?(a, b)
end

.push(item, other, goal) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/collision/collision_aabb.rb', line 60

def self.push(item, other, goal)
  x, y = goal.to_a

  if x != 0
    if x > 0
      other.right = item.left
    elsif x < 0
      other.left = item.right
    end
  end

  if y != 0
    if y > 0
      other.bottom = item.top
    elsif y < 0
      other.top = item.bottom
    end
  end

  item
end

.slide(item, other, goal) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/collision/collision_aabb.rb', line 36

def self.slide(item, other, goal)
  x, y = goal.to_a

  if x != 0
    item.position.x += x
    if x > 0
      item.right = other.left
    elsif x < 0
      item.left = other.right
    end
  end

  if y != 0
    item.position.y += y
    if y > 0
      item.bottom = other.top
    elsif y < 0
      item.top = other.bottom
    end
  end

  item
end

.touch(item, other, goal) ⇒ Object



34
# File 'lib/collision/collision_aabb.rb', line 34

def self.touch(item, other, goal) end

.was_horizontal_aligned?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/collision/collision_aabb.rb', line 4

def self.was_horizontal_aligned?(a, b)
  a.left < b.right && a.right > b.left
end

.was_vertical_aligned?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/collision/collision_aabb.rb', line 8

def self.was_vertical_aligned?(a, b)
  a.top < b.bottom && a.bottom > b.top
end