Class: Moon::SAT::Polygon

Inherits:
Object show all
Defined in:
lib/moon/packages/physics/sat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pos = Moon::Vector2.zero, points = []) ⇒ Polygon

Returns a new instance of Polygon.


18
19
20
21
22
23
24
25
# File 'lib/moon/packages/physics/sat.rb', line 18

def initialize(pos = Moon::Vector2.zero, points = [])
  @position = pos
  @angle = 0
  @offset = Moon::Vector2.zero
  @points = []
  @normals = []
  self.points = points
end

Instance Attribute Details

#angleInteger

Returns:


10
11
12
# File 'lib/moon/packages/physics/sat.rb', line 10

def angle
  @angle
end

#calc_pointsArray<Moon::Vector2> (readonly)

Returns:


16
17
18
# File 'lib/moon/packages/physics/sat.rb', line 16

def calc_points
  @calc_points
end

#normalsArray<Moon::Vector2> (readonly)

Returns:


14
15
16
# File 'lib/moon/packages/physics/sat.rb', line 14

def normals
  @normals
end

#offsetMoon::Vector2

Returns:


8
9
10
# File 'lib/moon/packages/physics/sat.rb', line 8

def offset
  @offset
end

#pointsArray<Moon::Vector2>

Returns:


12
13
14
# File 'lib/moon/packages/physics/sat.rb', line 12

def points
  @points
end

#positionMoon::Vector2

Returns:


6
7
8
# File 'lib/moon/packages/physics/sat.rb', line 6

def position
  @position
end

Instance Method Details

#aabbObject


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/moon/packages/physics/sat.rb', line 79

def aabb
  xmn = @calc_points[0].x
  ymn = @calc_points[0].y
  xmx = @calc_points[0].x
  ymx = @calc_points[0].y

  @calc_points.each_with_index do |p, i|
    if p.x < xmn
      xmn = p.x
    elsif p.x > xmx
      xmx = p.x
    end

    if p.y < ymn
      ymn = p.y
    elsif p.y > ymx
      ymx = p.y
    end
  end

  Box.new(position + [xmn, ymn], xmx - xmn, ymx - ymn).to_polygon
end

#rotate(angle) ⇒ Object


68
69
70
# File 'lib/moon/packages/physics/sat.rb', line 68

def rotate(angle)
  dup.rotate!(angle)
end

#rotate!(angle) ⇒ Object


61
62
63
64
65
66
# File 'lib/moon/packages/physics/sat.rb', line 61

def rotate!(angle)
  # in case this gets too slow, a rotate! method could be implemented
  # to modify the vector in place.
  points.map { |p| p.rotate(angle) }
  recalc
end

#translate(x, y) ⇒ Object


72
73
74
75
76
77
# File 'lib/moon/packages/physics/sat.rb', line 72

def translate(x, y)
  points.each do |p|
    p.x += x
    p.y += y
  end
end