naev 0.11.5
nlua_vec2.c File Reference

Handles the Lua vector handling bindings. More...

#include "nlua_vec2.h"
#include "log.h"
#include "nluadef.h"
#include "collision.h"

Go to the source code of this file.

Functions

static int vectorL_new (lua_State *L)
 Creates a new vector.
 
static int vectorL_newP (lua_State *L)
 Creates a new vector using polar coordinates.
 
static int vectorL_copy (lua_State *L)
 Copies a vector.
 
static int vectorL_tostring (lua_State *L)
 Converts a vector to a string.
 
static int vectorL_add__ (lua_State *L)
 
static int vectorL_add (lua_State *L)
 Adds two vectors or a vector and some cartesian coordinates.
 
static int vectorL_sub__ (lua_State *L)
 
static int vectorL_sub (lua_State *L)
 Subtracts two vectors or a vector and some cartesian coordinates.
 
static int vectorL_mul__ (lua_State *L)
 
static int vectorL_mul (lua_State *L)
 Multiplies a vector by a number.
 
static int vectorL_div__ (lua_State *L)
 
static int vectorL_div (lua_State *L)
 Divides a vector by a number.
 
static int vectorL_unm (lua_State *L)
 
static int vectorL_dot (lua_State *L)
 Dot product of two vectors.
 
static int vectorL_cross (lua_State *L)
 Cross product of two vectors.
 
static int vectorL_get (lua_State *L)
 Gets the cartesian positions of the vector.
 
static int vectorL_polar (lua_State *L)
 Gets polar coordinates of a vector.
 
static int vectorL_set (lua_State *L)
 Sets the vector by cartesian coordinates.
 
static int vectorL_setP (lua_State *L)
 Sets the vector by polar coordinates.
 
static int vectorL_distance (lua_State *L)
 Gets the distance from the Vec2.
 
static int vectorL_distance2 (lua_State *L)
 Gets the squared distance from the Vec2 (saves a sqrt())
 
static int vectorL_mod (lua_State *L)
 Gets the modulus of the vector. Lua function parameter: Vec2 v Vector to get modulus of. Lua return parameter: number The modulus of the vector.
 
static int vectorL_angle (lua_State *L)
 Gets the angle of the vector. Lua function parameter: Vec2 v Vector to get angle of. Lua return parameter: number The angle of the vector.
 
static int vectorL_normalize (lua_State *L)
 Normalizes a vector. Lua function parameter: Vec2 v Vector to normalize. Lua function parameter:[opt=1] number n Length to normalize the vector to. Lua return parameter: Vec2 Normalized vector.
 
static int vectorL_collideLineLine (lua_State *L)
 Sees if two line segments collide.
 
static int vectorL_collideCircleLine (lua_State *L)
 Computes the intersection of a line segment and a circle.
 
int nlua_loadVector (nlua_env env)
 Loads the vector metatable.
 
vec2lua_tovector (lua_State *L, int ind)
 Represents a 2D vector in Lua.
 
vec2luaL_checkvector (lua_State *L, int ind)
 Gets vector at index making sure type is valid.
 
vec2lua_pushvector (lua_State *L, vec2 vec)
 Pushes a vector on the stack.
 
int lua_isvector (lua_State *L, int ind)
 Checks to see if ind is a vector.
 

Variables

static const luaL_Reg vector_methods []
 

Detailed Description

Handles the Lua vector handling bindings.

These bindings control the spobs and systems.

Definition in file nlua_vec2.c.

Function Documentation

◆ lua_isvector()

int lua_isvector ( lua_State * L,
int ind )

Checks to see if ind is a vector.

Parameters
LLua state to check.
indIndex position to check.
Returns
1 if there is a vector at index position.

Definition at line 161 of file nlua_vec2.c.

◆ lua_pushvector()

vec2 * lua_pushvector ( lua_State * L,
vec2 vec )

Pushes a vector on the stack.

Parameters
LLua state to push vector onto.
vecVector to push.
Returns
Vector just pushed.

Definition at line 145 of file nlua_vec2.c.

◆ lua_tovector()

vec2 * lua_tovector ( lua_State * L,
int ind )

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)
Represents a 2d vector.
Definition vec2.h:32

To call members of the metatable always use:

vector:function( param )

Lua module: vec2

Gets vector at index.

Parameters
LLua state to get vector from.
indIndex position of vector.
Returns
The vec2 at ind.

Definition at line 119 of file nlua_vec2.c.

◆ luaL_checkvector()

vec2 * luaL_checkvector ( lua_State * L,
int ind )

Gets vector at index making sure type is valid.

Parameters
LLua state to get vector from.
indIndex position of vector.
Returns
The vec2 at ind.

Definition at line 130 of file nlua_vec2.c.

◆ nlua_loadVector()

int nlua_loadVector ( nlua_env env)

Loads the vector metatable.

Parameters
envEnvironment to load the vector metatable into.
Returns
0 on success.

Definition at line 86 of file nlua_vec2.c.

◆ vectorL_add()

static int vectorL_add ( lua_State * L)
static

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.

Lua usage parameter: my_vec = my_vec + your_vec Lua usage parameter: my_vec:add( your_vec ) Lua usage parameter: my_vec:add( 5, 3 )

Lua function parameter: Vector v Vector getting stuff added to. Lua function parameter: number|Vec2 x X coordinate or vector to add to. Lua function parameter: number|nil y Y coordinate or nil to add to. Lua return parameter: Vec2 The result of the vector operation.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: add

Definition at line 285 of file nlua_vec2.c.

◆ vectorL_add__()

static int vectorL_add__ ( lua_State * L)
static

Definition at line 319 of file nlua_vec2.c.

◆ vectorL_angle()

static int vectorL_angle ( lua_State * L)
static

Gets the angle of the vector. Lua function parameter: Vec2 v Vector to get angle of. Lua return parameter: number The angle of the vector.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: angle

Definition at line 721 of file nlua_vec2.c.

◆ vectorL_collideCircleLine()

static int vectorL_collideCircleLine ( lua_State * L)
static

Computes the intersection of a line segment and a circle.

Lua function parameter: Vector center Center of the circle. Lua function parameter: number radius Radius of the circle. Lua function parameter: Vector p1 First point of the line segment. Lua function parameter: Vector p2 Second point of the line segment. Lua return parameter: Vector|nil First point of collision or nil if no collision. Lua return parameter: Vector|nil Second point of collision or nil if single-point collision.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: collideCircleLine

Definition at line 780 of file nlua_vec2.c.

◆ vectorL_collideLineLine()

static int vectorL_collideLineLine ( lua_State * L)
static

Sees if two line segments collide.

Lua function parameter: Vec2 s1 Start point of the first segment. Lua function parameter: Vec2 e1 End point of the first segment. Lua function parameter: Vec2 s2 Start point of the second segment. Lua function parameter: Vec2 e2 End point of the second segment. Lua return parameter: 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.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: collideLineLine

Definition at line 756 of file nlua_vec2.c.

◆ vectorL_copy()

static int vectorL_copy ( lua_State * L)
static

Copies a vector.

Lua function parameter: Vec2 v Vector to copy. Lua return parameter: Vec2 A copy of v.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: copy

Definition at line 246 of file nlua_vec2.c.

◆ vectorL_cross()

static int vectorL_cross ( lua_State * L)
static

Cross product of two vectors.

Lua function parameter: Vec2 a First vector. Lua function parameter: Vec2 b Second vector. Lua return parameter: number The cross product.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: cross

Definition at line 549 of file nlua_vec2.c.

◆ vectorL_distance()

static int vectorL_distance ( lua_State * L)
static

Gets the distance from the Vec2.

Lua usage parameter: my_vec:dist() – Gets length of the vector (distance from origin). Lua usage parameter: my_vec:dist( your_vec ) – Gets distance from both vectors (your_vec - my_vec).

Lua function parameter: Vec2 v Vector to act as origin. Lua function parameter:[opt=vec2.new()] Vec2 v2 Vector to get distance from, uses origin (0,0) if not set. Lua return parameter: number The distance calculated.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: dist

Definition at line 655 of file nlua_vec2.c.

◆ vectorL_distance2()

static int vectorL_distance2 ( lua_State * L)
static

Gets the squared distance from the Vec2 (saves a sqrt())

Lua usage parameter: my_vec:dist2() – Gets squared length of the vector (distance squared from origin). Lua usage parameter: my_vec:dist2( your_vec ) – Gets squared distance from both vectors (your_vec - my_vec)^2.

Lua function parameter: Vec2 v Vector to act as origin. Lua function parameter:[opt=vec2.new()] Vec2 v2 Vector to get squared distance from, uses origin (0,0) if not set. Lua return parameter: number The distance calculated.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: dist2

Definition at line 684 of file nlua_vec2.c.

◆ vectorL_div()

static int vectorL_div ( lua_State * L)
static

Divides a vector by a number.

Lua usage parameter: my_vec = my_vec / 3 Lua usage parameter: my_vec:div(3)

Lua function parameter: Vec2 v Vector to divide. Lua function parameter: number mod Amount to divide by. Lua return parameter: Vec2 The result of the vector operation.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: div

Definition at line 485 of file nlua_vec2.c.

◆ vectorL_div__()

static int vectorL_div__ ( lua_State * L)
static

Definition at line 501 of file nlua_vec2.c.

◆ vectorL_dot()

static int vectorL_dot ( lua_State * L)
static

Dot product of two vectors.

Lua function parameter: Vec2 a First vector. Lua function parameter: Vec2 b Second vector. Lua return parameter: number The dot product.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: dot

Definition at line 533 of file nlua_vec2.c.

◆ vectorL_get()

static int vectorL_get ( lua_State * L)
static

Gets the cartesian positions of the vector.

Lua usage parameter: x,y = my_vec:get()

Lua function parameter: Vec2 v Vector to get position of. Lua return parameter: number X position of the vector. Lua return parameter: number Y position of the vector.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: get

Definition at line 567 of file nlua_vec2.c.

◆ vectorL_mod()

static int vectorL_mod ( lua_State * L)
static

Gets the modulus of the vector. Lua function parameter: Vec2 v Vector to get modulus of. Lua return parameter: number The modulus of the vector.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: mod

Definition at line 708 of file nlua_vec2.c.

◆ vectorL_mul()

static int vectorL_mul ( lua_State * L)
static

Multiplies a vector by a number.

Lua usage parameter: my_vec = my_vec * 3 Lua usage parameter: my_vec:mul( 3 )

Lua function parameter: Vec2 v Vector to multiply. Lua function parameter: number mod Amount to multiply by. Lua return parameter: Vec2 The result of the vector operation.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: mul

Definition at line 431 of file nlua_vec2.c.

◆ vectorL_mul__()

static int vectorL_mul__ ( lua_State * L)
static

Definition at line 457 of file nlua_vec2.c.

◆ vectorL_new()

static int vectorL_new ( lua_State * L)
static

Creates a new vector.

Lua usage parameter: vec2.new( 5, 3 ) – creates a vector at (5,3) Lua usage parameter: vec2.new() – creates a vector at (0,0)

Lua function parameter:[opt=0] number x If set, the X value for the new vector. Lua function parameter:[opt=x] number y If set, the Y value for the new vector. Lua return parameter: Vec2 The new vector.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: new

Definition at line 188 of file nlua_vec2.c.

◆ vectorL_newP()

static int vectorL_newP ( lua_State * L)
static

Creates a new vector using polar coordinates.

Lua usage parameter: vec2.newP( 1000, 90 ) – creates a vector at (0,1000) Lua usage parameter: vec2.newP() – creates a vector at (0,0)

Lua function parameter:[opt=0] number m If set, the modulus for the new vector. Lua function parameter:[opt=0] number a If set, the angle for the new vector, in radians. Lua return parameter: Vec2 The new vector.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: newP

Definition at line 219 of file nlua_vec2.c.

◆ vectorL_normalize()

static int vectorL_normalize ( lua_State * L)
static

Normalizes a vector. Lua function parameter: Vec2 v Vector to normalize. Lua function parameter:[opt=1] number n Length to normalize the vector to. Lua return parameter: Vec2 Normalized vector.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: normalize

Definition at line 735 of file nlua_vec2.c.

◆ vectorL_polar()

static int vectorL_polar ( lua_State * L)
static

Gets polar coordinates of a vector.

The angle is in radians.

Lua usage parameter: modulus, angle = my_vec:polar()

Lua function parameter: Vec2 v Vector to get polar coordinates of. Lua return parameter: number The modulus of the vector. Lua return parameter: number The angle of the vector.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: polar

Definition at line 588 of file nlua_vec2.c.

◆ vectorL_set()

static int vectorL_set ( lua_State * L)
static

Sets the vector by cartesian coordinates.

Lua usage parameter: my_vec:set(5, 3) – my_vec is now (5,3)

Lua function parameter: Vec2 v Vector to set coordinates of. Lua function parameter: number x X coordinate to set. Lua function parameter: number y Y coordinate to set.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: set

Definition at line 606 of file nlua_vec2.c.

◆ vectorL_setP()

static int vectorL_setP ( lua_State * L)
static

Sets the vector by polar coordinates.

Lua usage parameter: my_vec:setP( 1, 90 ) – my_vec is now (0,1)

Lua function parameter: Vec2 v Vector to set coordinates of. Lua function parameter: number m Modulus to set. Lua function parameter: number a Angle to set, in radians.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: setP

Definition at line 630 of file nlua_vec2.c.

◆ vectorL_sub()

static int vectorL_sub ( lua_State * L)
static

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.

Lua usage parameter: my_vec = my_vec - your_vec Lua usage parameter: my_vec:sub( your_vec ) Lua usage parameter: my_vec:sub( 5, 3 )

Lua function parameter: Vec2 v Vector getting stuff subtracted from. Lua function parameter: number|Vec2 x X coordinate or vector to subtract. Lua function parameter: number|nil y Y coordinate or nil to subtract. Lua return parameter: Vec2 The result of the vector operation.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: sub

Definition at line 364 of file nlua_vec2.c.

◆ vectorL_sub__()

static int vectorL_sub__ ( lua_State * L)
static

Definition at line 392 of file nlua_vec2.c.

◆ vectorL_tostring()

static int vectorL_tostring ( lua_State * L)
static

Converts a vector to a string.

Lua function parameter: Vector v Vector to convert to as string. Lua return parameter: string String version of v.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: __tostring

Definition at line 260 of file nlua_vec2.c.

◆ vectorL_unm()

static int vectorL_unm ( lua_State * L)
static

Definition at line 516 of file nlua_vec2.c.

Variable Documentation

◆ vector_methods

const luaL_Reg vector_methods[]
static
Initial value:
= {
{ "new", vectorL_new },
{ "newP", vectorL_newP },
{ "copy", vectorL_copy },
{ "__tostring", vectorL_tostring },
{ "__add", vectorL_add },
{ "add", vectorL_add__ },
{ "__sub", vectorL_sub },
{ "sub", vectorL_sub__ },
{ "__mul", vectorL_mul },
{ "mul", vectorL_mul__ },
{ "__div", vectorL_div },
{ "div", vectorL_div__ },
{ "__unm", vectorL_unm},
{ "dot", vectorL_dot },
{ "cross", vectorL_cross },
{ "get", vectorL_get },
{ "polar", vectorL_polar },
{ "set", vectorL_set },
{ "setP", vectorL_setP },
{ "dist", vectorL_distance },
{ "dist2", vectorL_distance2 },
{ "mod", vectorL_mod },
{ "angle", vectorL_angle },
{ "normalize", vectorL_normalize },
{ "collideLineLine", vectorL_collideLineLine },
{ "collideCircleLine", vectorL_collideCircleLine },
{0,0}
}
static int vectorL_distance2(lua_State *L)
Gets the squared distance from the Vec2 (saves a sqrt())
Definition nlua_vec2.c:684
static int vectorL_collideCircleLine(lua_State *L)
Computes the intersection of a line segment and a circle.
Definition nlua_vec2.c:780
static int vectorL_collideLineLine(lua_State *L)
Sees if two line segments collide.
Definition nlua_vec2.c:756
static int vectorL_mod(lua_State *L)
Gets the modulus of the vector. Lua function parameter: Vec2 v Vector to get modulus of....
Definition nlua_vec2.c:708
static int vectorL_dot(lua_State *L)
Dot product of two vectors.
Definition nlua_vec2.c:533
static int vectorL_cross(lua_State *L)
Cross product of two vectors.
Definition nlua_vec2.c:549
static int vectorL_set(lua_State *L)
Sets the vector by cartesian coordinates.
Definition nlua_vec2.c:606
static int vectorL_div(lua_State *L)
Divides a vector by a number.
Definition nlua_vec2.c:485
static int vectorL_setP(lua_State *L)
Sets the vector by polar coordinates.
Definition nlua_vec2.c:630
static int vectorL_angle(lua_State *L)
Gets the angle of the vector. Lua function parameter: Vec2 v Vector to get angle of....
Definition nlua_vec2.c:721
static int vectorL_normalize(lua_State *L)
Normalizes a vector. Lua function parameter: Vec2 v Vector to normalize. Lua function parameter:[opt=...
Definition nlua_vec2.c:735
static int vectorL_mul(lua_State *L)
Multiplies a vector by a number.
Definition nlua_vec2.c:431
static int vectorL_copy(lua_State *L)
Copies a vector.
Definition nlua_vec2.c:246
static int vectorL_add(lua_State *L)
Adds two vectors or a vector and some cartesian coordinates.
Definition nlua_vec2.c:285
static int vectorL_newP(lua_State *L)
Creates a new vector using polar coordinates.
Definition nlua_vec2.c:219
static int vectorL_tostring(lua_State *L)
Converts a vector to a string.
Definition nlua_vec2.c:260
static int vectorL_get(lua_State *L)
Gets the cartesian positions of the vector.
Definition nlua_vec2.c:567
static int vectorL_sub(lua_State *L)
Subtracts two vectors or a vector and some cartesian coordinates.
Definition nlua_vec2.c:364
static int vectorL_distance(lua_State *L)
Gets the distance from the Vec2.
Definition nlua_vec2.c:655
static int vectorL_polar(lua_State *L)
Gets polar coordinates of a vector.
Definition nlua_vec2.c:588
static int vectorL_new(lua_State *L)
Creates a new vector.
Definition nlua_vec2.c:188

Vector metatable methods.

Definition at line 50 of file nlua_vec2.c.