Naev

Module vec2

Represents a 2D vector in Lua.

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

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

To call members of the metatable always use:

 vector:function( param )
 

Functions

new ([x=0[, y=x]]) Creates a new vector.
newP ([m=0[, a=0]]) Creates a new vector using polar coordinates.
__tostring (v) Converts a vector to a string.
add (v, x, y) Adds two vectors or a vector and some cartesian coordinates.
sub (v, x, y) Subtracts two vectors or a vector and some cartesian coordinates.
mul (v, mod) Multiplies a vector by a number.
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.
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.
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())
mod (v) Gets the modulus of the vector.
angle (v) Gets the angle of the vector.
normalize (v) Normalizes a vector.
collideLineLine (s1, e1, s2, e2) Sees if two line segments collide.
collideCircleLine (center, radius, p1, p2) Computes the intersection of a line segment and a circle.


Functions

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, 90 ) -- creates a vector at (0,1000)
    
  • vec2.newP() -- creates a vector at (0,0)
    
__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 )
    
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 )
    
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 )
    
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:

    Vec2 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()
    
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)
    
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.
    
mod (v)
Gets the modulus of the vector.

Parameters:

  • v Vec2 Vector to get modulus of.

Returns:

    number The modulus of the vector.
angle (v)
Gets the angle of the vector.

Parameters:

  • v Vec2 Vector to get angle of.

Returns:

    number The angle of the vector.
normalize (v)
Normalizes a vector.

Parameters:

  • v Vec2 Vector to normalize.

Returns:

    Vec2 Normalized vector.
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.
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.
generated by LDoc 1.4.6 Last updated 2023-02-06 13:16:19