28static int vectorL_add__( lua_State *L );
30static int vectorL_sub__( lua_State *L );
32static int vectorL_mul__( lua_State *L );
34static int vectorL_div__( lua_State *L );
36static int vectorL_unm( lua_State *L );
56 {
"add", vectorL_add__ },
58 {
"sub", vectorL_sub__ },
60 {
"mul", vectorL_mul__ },
62 {
"div", vectorL_div__ },
63 {
"__unm", vectorL_unm},
121 return (
vec2*) lua_touserdata(L,ind);
133 return (
vec2*) lua_touserdata(L,ind);
134 luaL_typerror(L, ind, VECTOR_METATABLE);
149 luaL_getmetatable(L, VECTOR_METATABLE);
150 lua_setmetatable(L, -2);
165 if (lua_getmetatable(L,ind)==0)
167 lua_getfield(L, LUA_REGISTRYINDEX, VECTOR_METATABLE);
170 if (lua_rawequal(L, -1, -2))
193 if (!lua_isnoneornil(L,1))
194 x = luaL_checknumber(L,1);
198 if (!lua_isnoneornil(L,2))
199 y = luaL_checknumber(L,2);
203 vec2_cset( &v, x, y );
224 if (!lua_isnoneornil(L,1))
225 m = luaL_checknumber(L, 1);
229 if (!lua_isnoneornil(L,2))
230 a = luaL_checknumber(L, 2);
234 vec2_pset( &v, m, a );
262 char buf[STRMAX_SHORT];
264 snprintf( buf,
sizeof(buf),
"vec2( %g, %g )", v->x, v->y );
265 lua_pushstring(L, buf);
290 if (lua_isnumber(L,1)) {
291 x = y = lua_tonumber(L,1);
305 x = luaL_checknumber(L,2);
306 if (!lua_isnoneornil(L,3))
307 y = luaL_checknumber(L,3);
314 vec2_cset( &vout, v1->
x + x, v1->y + y );
319static int vectorL_add__( lua_State *L )
334 x = luaL_checknumber(L,2);
335 if (!lua_isnoneornil(L,3))
336 y = luaL_checknumber(L,3);
342 vec2_cset( v1, v1->x + x, v1->y + y );
380 x = luaL_checknumber(L,2);
381 if (!lua_isnoneornil(L,3))
382 y = luaL_checknumber(L,3);
388 vec2_cset( &vout, v1->
x - x, v1->y - y );
392static int vectorL_sub__( lua_State *L )
407 x = luaL_checknumber(L,2);
408 if (!lua_isnoneornil(L,3))
409 y = luaL_checknumber(L,3);
415 vec2_cset( v1, v1->x - x, v1->y - y );
435 if (lua_isnumber(L,1)) {
436 double d = lua_tonumber(L,1);
438 vec2_cset( &vout, v->
x *
d, v->y *
d );
441 if (lua_isnumber(L,2)) {
443 double d = lua_tonumber(L,2);
444 vec2_cset( &vout, v->
x *
d, v->y *
d );
449 vec2_cset( &vout, v1->
x * v2->x, v1->y * v2->y );
457static int vectorL_mul__( lua_State *L )
460 if (lua_isnumber(L,2)) {
461 double mod = luaL_checknumber(L,2);
462 vec2_cset( v1, v1->x * mod, v1->y * mod );
466 vec2_cset( v1, v1->x * v2->x, v1->y * v2->y );
489 if (lua_isnumber(L,2)) {
490 double mod = lua_tonumber(L,2);
491 vec2_cset( &vout, v1->
x / mod, v1->y / mod );
495 vec2_cset( &vout, v1->
x / v2->x, v1->y / v2->y );
501static int vectorL_div__( lua_State *L )
504 if (lua_isnumber(L,2)) {
505 double mod = lua_tonumber(L,2);
506 vec2_cset( v1, v1->x / mod, v1->y / mod );
510 vec2_cset( v1, v1->x / v2->x, v1->y / v2->y );
516static int vectorL_unm( lua_State *L )
520 vec2_cset( &vout, -vin->
x, -vin->
y );
537 lua_pushnumber( L, a->x*b->x + a->y*b->y );
553 lua_pushnumber( L, a->x*b->y - a->y*b->x );
571 lua_pushnumber(L, v1->x);
572 lua_pushnumber(L, v1->y);
591 lua_pushnumber(L, VMOD(*v1));
592 lua_pushnumber(L, VANGLE(*v1));
613 x = luaL_checknumber(L,2);
614 y = luaL_checknumber(L,3);
616 vec2_cset( v1, x, y );
637 m = luaL_checknumber(L,2);
638 a = luaL_checknumber(L,3);
640 vec2_pset( v1, m, a );
661 if (!lua_isnoneornil(L,2)) {
663 dist = vec2_dist(v1, v2);
666 dist = vec2_odist(v1);
669 lua_pushnumber(L, dist);
690 if (!lua_isnoneornil(L,2)) {
692 dist2 = vec2_dist2(v1, v2);
695 dist2 = vec2_odist2(v1);
698 lua_pushnumber(L, dist2);
711 lua_pushnumber(L, VMOD(*v));
724 lua_pushnumber(L, VANGLE(*v));
738 double n = luaL_optnumber(L,2,1.);
739 double m = n/
MAX(VMOD(*v),1e-6);
764 lua_pushinteger( L, ret );
782 const vec2 *center, *p1, *p2;
787 radius = luaL_checknumber( L, 2 );
int CollideLineCircle(const vec2 *p1, const vec2 *p2, const vec2 *cc, double cr, vec2 crash[2])
Checks to see if a line collides with a circle.
int CollideLineLine(double s1x, double s1y, double e1x, double e1y, double s2x, double s2y, double e2x, double e2y, vec2 *crash)
Checks to see if two lines collide.
Header file with generic functions and naev-specifics.
static int vectorL_distance2(lua_State *L)
Gets the squared distance from the Vec2 (saves a sqrt())
static int vectorL_collideCircleLine(lua_State *L)
Computes the intersection of a line segment and a circle.
static int vectorL_collideLineLine(lua_State *L)
Sees if two line segments collide.
static const luaL_Reg vector_methods[]
static int vectorL_mod(lua_State *L)
Gets the modulus of the vector. Lua function parameter: Vec2 v Vector to get modulus of....
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_set(lua_State *L)
Sets the vector by cartesian coordinates.
static int vectorL_div(lua_State *L)
Divides a vector by a number.
static int vectorL_setP(lua_State *L)
Sets the vector by polar coordinates.
static int vectorL_angle(lua_State *L)
Gets the angle of the vector. Lua function parameter: Vec2 v Vector to get angle of....
static int vectorL_normalize(lua_State *L)
Normalizes a vector. Lua function parameter: Vec2 v Vector to normalize. Lua function parameter:[opt=...
static int vectorL_mul(lua_State *L)
Multiplies a vector by a number.
int nlua_loadVector(nlua_env env)
Loads the vector metatable.
static int vectorL_copy(lua_State *L)
Copies a vector.
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
static int vectorL_add(lua_State *L)
Adds two vectors or a vector and some cartesian coordinates.
static int vectorL_newP(lua_State *L)
Creates a new vector using polar coordinates.
static int vectorL_tostring(lua_State *L)
Converts a vector to a string.
static int vectorL_get(lua_State *L)
Gets the cartesian positions of the vector.
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
static int vectorL_sub(lua_State *L)
Subtracts two vectors or a vector and some cartesian coordinates.
static int vectorL_distance(lua_State *L)
Gets the distance from the Vec2.
static int vectorL_polar(lua_State *L)
Gets polar coordinates of a vector.
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
static int vectorL_new(lua_State *L)
Creates a new vector.