Vec2

January 7, 2026 ยท View on GitHub

GMR Docs > Types > Vec2

Vec2

2D vector for positions and directions.

Table of Contents

Instance Methods

#initialize

Create a new Vec2 with optional x and y values.

Parameters:

NameTypeDescription
xFloatThe x component (default: 0)
yFloatThe y component (default: 0)

Returns: Vec2 - The new vector

Example:

Vec2.new(100, 200)    # (100, 200)

#x

Get the x component.

Returns: Float - The x value

Example:

x = vec.x

#y

Get the y component.

Returns: Float - The y value

Example:

y = vec.y

#+

Add two vectors, returning a new Vec2.

Parameters:

NameTypeDescription
otherVec2The vector to add

Returns: Vec2 - A new vector with the sum

Example:

result = Vec2.new(1, 2) + Vec2.new(3, 4)  # Vec2(4, 6)

#-

Subtract two vectors, returning a new Vec2.

Parameters:

NameTypeDescription
otherVec2The vector to subtract

Returns: Vec2 - A new vector with the difference

Example:

result = Vec2.new(5, 5) - Vec2.new(2, 1)  # Vec2(3, 4)

#*

Multiply vector by a scalar, returning a new Vec2.

Parameters:

NameTypeDescription
scalarFloatThe scalar value

Returns: Vec2 - A new scaled vector

Example:

result = Vec2.new(2, 3) * 2.0  # Vec2(4, 6)

#/

Divide vector by a scalar, returning a new Vec2.

Parameters:

NameTypeDescription
scalarFloatThe scalar value (must not be zero)

Returns: Vec2 - A new scaled vector

Example:

result = Vec2.new(10, 20) / 2.0  # Vec2(5, 10)

#to_s

Convert to a string representation.

Returns: String - String in format "Vec2(x, y)"

Example:

puts Vec2.new(1, 2).to_s  # "Vec2(1.00, 2.00)"

#to_a

A 3D vector with x, y, and z components. Used for 3D positions, colors (RGB), and other 3-component values. Supports arithmetic operations.

Returns: Array<Float> - Array containing [x, y]

Example:

# 3D position for parallax layers
  class ParallaxLayer
    def initialize(texture, depth)


Back to Types | Documentation Home