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.

Examples:

Basic usage

delta = 1 / 60.0 # Assume 60 frames per second
player_position = Vector2[7, 8]
player_velocity = Vector2[2, 2]

player_position += player_velocity * delta

puts player_position.x # => 7.0333...
puts player_position.y # => 8.0333...

Constant Summary collapse

ZERO =

A Vector2 setup at 0, 0.

Vector2.new(x: 0, y: 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:) ⇒ Vector2

Creates a new instance of Vector2.

Examples:

Basic usage

position = Vector2.new(x: 16, y: 32)

Parameters:

  • x (Float)
  • y (Float)


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

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

Instance Attribute Details

#xFloat Also known as: width

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/vector2.rb', line 3

def x
  @x
end

#yFloat Also known as: height

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/vector2.rb', line 3

def y
  @y
end

Class Method Details

.[](x, y) ⇒ Vector2

A short form way to create new Vector2 objects.

Examples:

Basic usage

position = Vector2[10, 12]

puts position.x # => 10
puts position.y # => 12

Parameters:

  • x (Float)
  • y (Float)

Returns:



28
29
30
# File 'src/ruby/models/vector2.rb', line 28

def self.[](x, y)
  new(x: x, y: y)
end

.mock_return(x: 0, y: 0) ⇒ String

A method used to generate the mock data for Raylib.

Examples:

Basic usage

Taylor::Raylib.mock_call(
  "GetMousePosition",
  Vector2.mock_return(x: 1, y: 2)
)

Parameters:

  • x (Float) (defaults to: 0)
  • y (Float) (defaults to: 0)

Returns:

  • (String)


259
260
261
# File 'src/ruby/models/vector2.rb', line 259

def self.mock_return(x: 0, y: 0)
  [x, y].map(&:to_s).join(" ")
end

Instance Method Details

#*(other) ⇒ Vector2 Also known as: scale

Scale the Vector2 by the scalar.

Examples:

Basic usage

vector = Vector2[2, 4]
vector *= 3

puts vector.x # => 6
puts vector.y # => 12

Parameters:

  • other (Numeric)

Returns:



138
139
140
141
142
143
# File 'src/ruby/models/vector2.rb', line 138

def *(other)
  Vector2.new(
    x: x * other,
    y: y * other
  )
end

#+(other) ⇒ Vector2

The addition operator is used for adding up two Vector2 objects or a Vector2 and a Numeric.

Examples:

Basic usage

vector_1 = Vector2[1, 2]
vector_2 = Vector2[3, 4]

vector_both = vector_1 + vector_2

puts vector_both.x # => 4
puts vector_both.y # => 6

bigger_vector = vector_1 + 5

puts bigger_vector.x # => 6
puts bigger_vector.y # => 7

Parameters:

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'src/ruby/models/vector2.rb', line 68

def +(other)
  case other
  when Vector2
    Vector2.new(
      x: x + other.x,
      y: y + other.y
    )

  when Numeric
    Vector2.new(
      x: x + other,
      y: y + other
    )

  else
    raise ArgumentError, "can only add Numeric and Vector2"
  end
end

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

The addition operator is used for subtracting two Vector2 objects or a Vector2 and a Numeric.

Examples:

Basic usage

vector_1 = Vector2[3, 4]
vector_2 = Vector2[2, 1]

vector_both = vector_1 - vector_2

puts vector_both.x # => 1
puts vector_both.y # => 3

smaller_vector = vector_1 - 1

puts smaller_vector.x # => 2
puts smaller_vector.y # => 3

Parameters:

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'src/ruby/models/vector2.rb', line 106

def -(other)
  case other
  when Vector2
    Vector2.new(
      x: x - other.x,
      y: y - other.y
    )

  when Numeric
    Vector2.new(
      x: x - other,
      y: y - other
    )

  else
    raise ArgumentError, "can only subtract Numeric and Vector2"
  end
end

#/(other) ⇒ Vector2

Divide the Vector2 by the value.

Examples:

Basic usage

vector = Vector2[2, 3]
vector /= 2

puts vector.x # => 1
puts vector.y # => 1.5

Parameters:

  • other (Numeric)

Returns:



158
159
160
161
162
163
# File 'src/ruby/models/vector2.rb', line 158

def /(other)
  Vector2.new(
    x: x / other,
    y: y / other
  )
end

#==(other) ⇒ Boolean

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

Examples:

Basic usage

puts Vector2[3, 4] == Vector2[3, 4] # => true

puts Vector2[3, 4] == Vector2[4, 3] # => false

Parameters:

Returns:

  • (Boolean)


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

def ==(other)
  return super unless other.is_a?(Vector2)

  x == other.x &&
    y == other.y
end

#drawnil

Sets the corresponding pixel to the passed in Colour.

Examples:

Basic usage

pixel = Vector2.new(x: 16, y: 32)
pixel.draw

Returns:

  • (nil)


25
26
27
28
29
# File 'mrb_doc/models/vector2.rb', line 25

def draw
  # mrb_Vector2_draw
  # src/mruby_integration/models/vector2.cpp
  nil
end

#inspectString

Returns a string representation of the Vector2 that's useful for debugging.

Examples:

Basic usage

vector = Vector2[6, 8]

puts vector.inspect # => #<Vector2:0x102bd20 x:6.0 y:8.0>

p vector # => #<Vector2:0x102bd20 x:6.0 y:8.0>

Returns:

  • (String)


244
245
246
# File 'src/ruby/models/vector2.rb', line 244

def inspect
  "#<Vector2:0x#{object_id.to_s(16)} x:#{x} y:#{y}>"
end

#lengthNumeric

Calculates the length of the Vector2.

Examples:

Basic usage

vector = Vector2[3, 4]

puts vector.length # => 5

Returns:

  • (Numeric)


173
174
175
# File 'src/ruby/models/vector2.rb', line 173

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

#overlaps?(other) ⇒ Boolean

Returns true if the Vector2 is contained inside this Rectangle or is on the border of.

Examples:

Basic usage

position = Vector2.new(x: 5, y: 5)
hitbox = Rectangle.new(x: 10, y: 10, width: 10, height: 10)

puts position.overlaps?(hitbox)
# => false

# The position changes to inside the rectangle...
position = Vector2.new(x: 15, y: 15)

puts position.overlaps?(hitbox)
# => true

# The position changes to the corner of the rectangle...
position = Vector2.new(x: 10, y: 10)

puts position.overlaps?(hitbox)
# => true

# The position changes to far outside rectangle...
position = Vector2.new(x: 100, y: 100)

puts position.overlaps?(hitbox)
# => false

Parameters:

  • other (Object#overlaps?)

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

    If passed anything other than a Vector2



228
229
230
231
232
# File 'src/ruby/models/vector2.rb', line 228

def overlaps?(other)
  raise ArgumentError, "Must respond to #overlaps?" unless other.respond_to?(:overlaps?)

  other.overlaps?(self)
end

#to_hHash

Return the object represented by a Hash.

Examples:

Basic usage

vector = Vector2[6, 8]

p vector.to_h

# => {
#      x: 6,
#      y: 8
#    }

Returns:

  • (Hash)


190
191
192
193
194
195
# File 'src/ruby/models/vector2.rb', line 190

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