Class: Vector2

Inherits:
Object
  • Object
show all
Defined in:
src/ruby/models/vector2.rb,
mrb_doc/models/vector2.rb

Overview

A class used to hold an x and y value, will be translated when viewed through a Camera2D object.

Constant Summary collapse

ZERO =

A Vector2 setup at 0, 0

Vector2.new(0, 0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Vector2

Creates a new instance of Vector2

Parameters:

  • x (Float)
  • y (Float)


6
7
8
9
10
# File 'mrb_doc/models/vector2.rb', line 6

def initialize(x, y)
  # mrb_Vector2_initialize
  # src/mruby_integration/models/vector2.cpp
  Vector2.new
end

Instance Attribute Details

#xFloat

Returns:

  • (Float)


5
6
7
# File 'src/ruby/models/vector2.rb', line 5

def x
  @x
end

#yFloat

Returns:

  • (Float)


5
6
7
# File 'src/ruby/models/vector2.rb', line 5

def y
  @y
end

Instance Method Details

#+(other) ⇒ Vector2

The addition operator is used for adding up two Vector2s

Parameters:

Returns:



19
20
21
22
23
24
# File 'src/ruby/models/vector2.rb', line 19

def +(other)
  Vector2.new(
    self.x + other.x,
    self.y + other.y
  )
end

#-(other) ⇒ Vector2 Also known as: difference

The addition operator is used for subtracting two Vector2s

Parameters:

Returns:



29
30
31
32
33
34
# File 'src/ruby/models/vector2.rb', line 29

def -(other)
  Vector2.new(
    self.x - other.x,
    self.y - other.y
  )
end

#==(other) ⇒ Boolean

The equality operator is used for checking if two Vector2s share the same position.

Parameters:

Returns:

  • (Boolean)


11
12
13
14
# File 'src/ruby/models/vector2.rb', line 11

def ==(other)
  self.x == other.x &&
    self.y == other.y
end

#lengthNumeric

Calculates the length of the Vector2

Returns:

  • (Numeric)


50
51
52
# File 'src/ruby/models/vector2.rb', line 50

def length
  Math.sqrt(x**2 + y**2)
end

#scale(scalar) ⇒ Vector2

Change the length of the Vector2

Parameters:

  • scalar (Numeric)

Returns:



41
42
43
44
45
46
# File 'src/ruby/models/vector2.rb', line 41

def scale(scalar)
  Vector2.new(
    self.x * scalar,
    self.y * scalar
  )
end

#to_hHash

Return the object represented by a Hash

Returns:

  • (Hash)


56
57
58
59
60
61
# File 'src/ruby/models/vector2.rb', line 56

def to_h
  {
    x: x,
    y: y,
  }
end