Class: Vector2
- Inherits:
-
Object
- Object
- Vector2
- 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
-
#+(other) ⇒ Vector2
The addition operator is used for adding up two Vector2s.
-
#-(other) ⇒ Vector2
(also: #difference)
The addition operator is used for subtracting two Vector2s.
-
#==(other) ⇒ Boolean
The equality operator is used for checking if two Vector2s share the same position.
-
#initialize(x, y) ⇒ Vector2
constructor
Creates a new instance of Vector2.
-
#length ⇒ Numeric
Calculates the length of the Vector2.
-
#scale(scalar) ⇒ Vector2
Change the length of the Vector2.
-
#to_h ⇒ Hash
Return the object represented by a Hash.
Constructor Details
Instance Attribute Details
#x ⇒ Float
5 6 7 |
# File 'src/ruby/models/vector2.rb', line 5 def x @x end |
#y ⇒ 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
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
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.
11 12 13 14 |
# File 'src/ruby/models/vector2.rb', line 11 def ==(other) self.x == other.x && self.y == other.y end |
#length ⇒ Numeric
Calculates the length of the Vector2
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
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_h ⇒ Hash
Return the object represented by a Hash
56 57 58 59 60 61 |
# File 'src/ruby/models/vector2.rb', line 56 def to_h { x: x, y: y, } end |