Class: Ruby2D::Vertices

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/vertices.rb

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width, height, rotate, crop: nil, flip: nil) ⇒ Vertices

Returns a new instance of Vertices.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby2d/vertices.rb', line 9

def initialize(x, y, width, height, rotate, crop: nil, flip: nil)
  @flip = flip
  @x = x
  @y = y
  @width = width.to_f
  @height = height.to_f

  if @flip == :horizontal || @flip == :both
    @x = @x + @width
    @width = -@width
  end

  if @flip == :vertical || @flip == :both
    @y = y + @height
    @height = -@height
  end

  @rotate = rotate
  @rx = @x + (@width / 2.0)
  @ry = @y + (@height / 2.0)
  @crop = crop
end

Instance Method Details

#coordinatesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby2d/vertices.rb', line 32

def coordinates
  if @rotate == 0
    x1, y1 = @x,          @y;           # Top left
    x2, y2 = @x + @width, @y;           # Top right
    x3, y3 = @x + @width, @y + @height; # Bottom right
    x4, y4 = @x,          @y + @height; # Bottom left
  else
    x1, y1 = rotate(@x,          @y);           # Top left
    x2, y2 = rotate(@x + @width, @y);           # Top right
    x3, y3 = rotate(@x + @width, @y + @height); # Bottom right
    x4, y4 = rotate(@x,          @y + @height); # Bottom left
  end

  [ x1, y1, x2, y2, x3, y3, x4, y4 ]
end

#texture_coordinatesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby2d/vertices.rb', line 48

def texture_coordinates
  if @crop.nil?
    tx1 = 0.0; ty1 = 0.0 # Top left
    tx2 = 1.0; ty2 = 0.0 # Top right
    tx3 = 1.0; ty3 = 1.0 # Bottom right
    tx4 = 0.0; ty4 = 1.0 # Bottom left
  else
    tx1 = @crop[:x] / @crop[:image_width].to_f; ty1 = @crop[:y] / @crop[:image_height].to_f # Top left
    tx2 = tx1 + (@crop[:width] / @crop[:image_width].to_f); ty2 = ty1                       # Top right
    tx3 = tx2; ty3 = ty1 + (@crop[:height] / @crop[:image_height].to_f)                     # Botttom right
    tx4 = tx1; ty4 = ty3                                                                    # Bottom left
  end

  [ tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4 ]
end