Naev

Module vec2

Represents a 2D vector in Lua.

This module allows you to manipulate 2D vectors. Usage is generally as follows:

 myvec = vec2.new( 3, 2 ) -- myvec is now (3,2)
 myvec:add( 5, 3 ) -- myvec is now (8,5)
 myvec = myvec * 3 -- myvec is now (24,15)
 yourvec = vec2.new( 5, 2 ) -- yourvec is now (5,2)
 myvec = myvec - yourvec -- my_vec is now (19,13)
 

To call members of the metatable always use:

 vector:function( param )
 

Functions

__tostring (v) Converts a vector to a string.
add (v, x, y) Adds two vectors or a vector and some cartesian coordinates.
angle (v) Gets the angle of the vector.
clone (v) Creates a clone of a vector.
collideCircleLine (center, radius, p1, p2) Computes the intersection of a line segment and a circle.
collideLineLine (s1, e1, s2, e2) Sees if two line segments collide.
cross (a, b) Cross product of two vectors.
dist (v[, v2=vec2.new()]) Gets the distance from the Vec2.
dist2 (v[, v2=vec2.new()]) Gets the squared distance from the Vec2 (saves a sqrt())
div (v, mod) Divides a vector by a number.
dot (a, b) Dot product of two vectors.
get (v) Gets the cartesian positions of the vector.
mod (v) Gets the modulus of the vector.
mul (v, mod) Multiplies a vector by a number.
new ([x=0[, y=x]]) Creates a new vector.
newP ([m=0[, a=0]]) Creates a new vector using polar coordinates.
normalize (v[, n=1]) Normalizes a vector.
polar (v) Gets polar coordinates of a vector.
set (v, x, y) Sets the vector by cartesian coordinates.
setP (v, m, a) Sets the vector by polar coordinates.
sub (v, x, y) Subtracts two vectors or a vector and some cartesian coordinates.


Functions

__tostring (v)
Converts a vector to a string.

Parameters:

  • v Vector Vector to convert to as string.

Returns:

    string String version of v.
add (v, x, y)
Adds two vectors or a vector and some cartesian coordinates.

If x is a vector it adds both vectors, otherwise it adds cartesian coordinates to the vector.

Parameters:

  • v Vector Vector getting stuff added to.
  • x number or Vec2 X coordinate or vector to add to.
  • y number or nil Y coordinate or nil to add to.

Returns:

    Vec2 The result of the vector operation.

Usage:

  • my_vec = my_vec + your_vec
    
  • my_vec:add( your_vec )
    
  • my_vec:add( 5, 3 )
    
angle (v)
Gets the angle of the vector.

Parameters:

  • v Vec2 Vector to get angle of.

Returns:

    number The angle of the vector.
clone (v)
Creates a clone of a vector.

Parameters:

  • v Vec2 Vector to clone.

Returns:

    Vec2 A clone of v.
collideCircleLine (center, radius, p1, p2)
Computes the intersection of a line segment and a circle.

Parameters:

  • center Vector Center of the circle.
  • radius number Radius of the circle.
  • p1 Vector First point of the line segment.
  • p2 Vector Second point of the line segment.

Returns:

  1. Vector or nil First point of collision or nil if no collision.
  2. Vector or nil Second point of collision or nil if single-point collision.
collideLineLine (s1, e1, s2, e2)
Sees if two line segments collide.

Parameters:

  • s1 Vec2 Start point of the first segment.
  • e1 Vec2 End point of the first segment.
  • s2 Vec2 Start point of the second segment.
  • e2 Vec2 End point of the second segment.

Returns:

    integer 0 if they don't collide, 1 if they collide on a point, 2 if they are parallel, and 3 if they are coincident.
cross (a, b)
Cross product of two vectors.

Parameters:

  • a Vec2 First vector.
  • b Vec2 Second vector.

Returns:

    number The cross product.
dist (v[, v2=vec2.new()])
Gets the distance from the Vec2.

Parameters:

  • v Vec2 Vector to act as origin.
  • v2 Vec2 Vector to get distance from, uses origin (0,0) if not set. (default vec2.new())

Returns:

    number The distance calculated.

Usage:

  • my_vec:dist() -- Gets length of the vector (distance from origin).
    
  • my_vec:dist( your_vec ) -- Gets distance from both vectors (your_vec -
    my_vec).
    
dist2 (v[, v2=vec2.new()])
Gets the squared distance from the Vec2 (saves a sqrt())

Parameters:

  • v Vec2 Vector to act as origin.
  • v2 Vec2 Vector to get squared distance from, uses origin (0,0) if not set. (default vec2.new())

Returns:

    number The distance calculated.

Usage:

  • my_vec:dist2() -- Gets squared length of the vector (distance squared
     from origin).
    
  • my_vec:dist2( your_vec ) -- Gets squared distance from both vectors
    (your_vec - my_vec)^2.
    
div (v, mod)
Divides a vector by a number.

Parameters:

  • v Vec2 Vector to divide.
  • mod number Amount to divide by.

Returns:

    Vec2 The result of the vector operation.

Usage:

  • my_vec = my_vec / 3
    
  • my_vec:div(3)
    
dot (a, b)
Dot product of two vectors.

Parameters:

  • a Vec2 First vector.
  • b Vec2 Second vector.

Returns:

    number The dot product.
get (v)
Gets the cartesian positions of the vector.

Parameters:

  • v Vec2 Vector to get position of.

Returns:

  1. number X position of the vector.
  2. number Y position of the vector.

Usage:

    x,y = my_vec:get()
    
mod (v)
Gets the modulus of the vector.

Parameters:

  • v Vec2 Vector to get modulus of.

Returns:

    number The modulus of the vector.
mul (v, mod)
Multiplies a vector by a number.

Parameters:

  • v Vec2 Vector to multiply.
  • mod number Amount to multiply by.

Returns:

    Vec2 The result of the vector operation.

Usage:

  • my_vec = my_vec * 3
    
  • my_vec:mul( 3 )
    
new ([x=0[, y=x]])
Creates a new vector.

Parameters:

  • x number If set, the X value for the new vector. (default 0)
  • y number If set, the Y value for the new vector. (default x)

Returns:

    Vec2 The new vector.

Usage:

  • vec2.new( 5, 3 ) -- creates a vector at (5,3)
    
  • vec2.new() -- creates a vector at (0,0)
    
newP ([m=0[, a=0]])
Creates a new vector using polar coordinates.

Parameters:

  • m number If set, the modulus for the new vector. (default 0)
  • a number If set, the angle for the new vector, in radians. (default 0)

Returns:

    Vec2 The new vector.

Usage:

  • vec2.newP( 1000, math.pi/2 ) -- creates a vector at (0,1000)
    
  • vec2.newP() -- creates a vector at (0,0)
    
normalize (v[, n=1])
Normalizes a vector.

Parameters:

  • v Vec2 Vector to normalize.
  • n number Length to normalize the vector to. (default 1)

Returns:

    Vec2 Normalized vector.
polar (v)
Gets polar coordinates of a vector.

The angle is in radians.

Parameters:

  • v Vec2 Vector to get polar coordinates of.

Returns:

  1. number The modulus of the vector.
  2. number The angle of the vector.

Usage:

    modulus, angle = my_vec:polar()
    
set (v, x, y)
Sets the vector by cartesian coordinates.

Parameters:

  • v Vec2 Vector to set coordinates of.
  • x number X coordinate to set.
  • y number Y coordinate to set.

Usage:

    my_vec:set(5, 3) -- my_vec is now (5,3)
    
setP (v, m, a)
Sets the vector by polar coordinates.

Parameters:

  • v Vec2 Vector to set coordinates of.
  • m number Modulus to set.
  • a number Angle to set, in radians.

Usage:

    my_vec:setP( 1, 90 ) -- my_vec is now (0,1)
    
sub (v, x, y)
Subtracts two vectors or a vector and some cartesian coordinates.

If x is a vector it subtracts both vectors, otherwise it subtracts cartesian coordinates to the vector.

Parameters:

  • v Vec2 Vector getting stuff subtracted from.
  • x number or Vec2 X coordinate or vector to subtract.
  • y number or nil Y coordinate or nil to subtract.

Returns:

    Vec2 The result of the vector operation.

Usage:

  • my_vec = my_vec - your_vec
    
  • my_vec:sub( your_vec )
    
  • my_vec:sub( 5, 3 )
    
generated by LDoc 1.5.0 Last updated 2025-05-18 01:00:15