Class: Vector2

Inherits:
Object
  • Object
show all
Defined in:
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)


11
12
13
14
15
# File 'mrb_doc/models/vector2.rb', line 11

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 'mrb_doc/models/vector2.rb', line 5

def x
  @x
end

#yFloat

Returns:

  • (Float)


5
6
7
# File 'mrb_doc/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:



42
43
44
45
46
47
48
# File 'mrb_doc/models/vector2.rb', line 42

def +(other)
  # src/mruby_integration/models/vector2.cpp
  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:



53
54
55
56
57
58
59
# File 'mrb_doc/models/vector2.rb', line 53

def -(other)
  # src/mruby_integration/models/vector2.cpp
  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)


33
34
35
36
37
# File 'mrb_doc/models/vector2.rb', line 33

def ==(other)
  # src/mruby_integration/models/vector2.cpp
  self.x == other.x &&
    self.y == other.y
end

#lengthNumeric

Calculates the length of the Vector2

Returns:

  • (Numeric)


76
77
78
79
# File 'mrb_doc/models/vector2.rb', line 76

def length
  # src/mruby_integration/models/vector2.cpp
  Math.sqrt(x**2 + y**2)
end

#scale(scalar) ⇒ Vector2

Change the length of the Vector2

Parameters:

  • scalar (Numeric)

Returns:



66
67
68
69
70
71
72
# File 'mrb_doc/models/vector2.rb', line 66

def scale(scalar)
  # src/mruby_integration/models/vector2.cpp
  Vector2.new(
    self.x * scalar,
    self.y * scalar
  )
end

#to_hHash

Return the object represented by a Hash

Returns:

  • (Hash)


83
84
85
86
87
88
89
# File 'mrb_doc/models/vector2.rb', line 83

def to_h
  # src/mruby_integration/models/vector2.cpp
  {
    x: x,
    y: y,
  }
end