27static int vectorL_add__( lua_State *L );
29static int vectorL_sub__( lua_State *L );
31static int vectorL_mul__( lua_State *L );
33static int vectorL_div__( lua_State *L );
52 {
"add", vectorL_add__ },
54 {
"sub", vectorL_sub__ },
56 {
"mul", vectorL_mul__ },
58 {
"div", vectorL_div__ },
115 return (
vec2*) lua_touserdata(L,ind);
127 return (
vec2*) lua_touserdata(L,ind);
128 luaL_typerror(L, ind, VECTOR_METATABLE);
143 luaL_getmetatable(L, VECTOR_METATABLE);
144 lua_setmetatable(L, -2);
159 if (lua_getmetatable(L,ind)==0)
161 lua_getfield(L, LUA_REGISTRYINDEX, VECTOR_METATABLE);
164 if (lua_rawequal(L, -1, -2))
187 if (!lua_isnoneornil(L,1))
188 x = luaL_checknumber(L,1);
192 if (!lua_isnoneornil(L,2))
193 y = luaL_checknumber(L,2);
197 vec2_cset( &v, x, y );
218 if (!lua_isnoneornil(L,1))
219 m = luaL_checknumber(L, 1);
223 if (!lua_isnoneornil(L,2))
224 a = luaL_checknumber(L, 2);
228 vec2_pset( &v, m, a );
242 char buf[STRMAX_SHORT];
244 snprintf( buf,
sizeof(buf),
"vec2( %g, %g )", v->x, v->y );
245 lua_pushstring(L, buf);
270 if (lua_isnumber(L,1)) {
271 x = y = lua_tonumber(L,1);
285 x = luaL_checknumber(L,2);
286 if (!lua_isnoneornil(L,3))
287 y = luaL_checknumber(L,3);
294 vec2_cset( &vout, v1->
x + x, v1->y + y );
299static int vectorL_add__( lua_State *L )
314 x = luaL_checknumber(L,2);
315 if (!lua_isnoneornil(L,3))
316 y = luaL_checknumber(L,3);
322 vec2_cset( v1, v1->x + x, v1->y + y );
359 x = luaL_checknumber(L,2);
360 if (!lua_isnoneornil(L,3))
361 y = luaL_checknumber(L,3);
367 vec2_cset( &vout, v1->
x - x, v1->y - y );
371static int vectorL_sub__( lua_State *L )
386 x = luaL_checknumber(L,2);
387 if (!lua_isnoneornil(L,3))
388 y = luaL_checknumber(L,3);
394 vec2_cset( v1, v1->x - x, v1->y - y );
414 if (lua_isnumber(L,1)) {
415 double d = lua_tonumber(L,1);
417 vec2_cset( &vout, v->
x *
d, v->y *
d );
420 if (lua_isnumber(L,2)) {
422 double d = lua_tonumber(L,2);
423 vec2_cset( &vout, v->
x *
d, v->y *
d );
428 vec2_cset( &vout, v1->
x * v2->x, v1->y * v2->y );
436static int vectorL_mul__( lua_State *L )
442 if (lua_isnumber(L,2)) {
443 double mod = luaL_checknumber(L,2);
444 vec2_cset( v1, v1->x * mod, v1->y * mod );
448 vec2_cset( v1, v1->x * v2->x, v1->y * v2->y );
473 if (lua_isnumber(L,2)) {
474 double mod = lua_tonumber(L,2);
475 vec2_cset( &vout, v1->
x / mod, v1->y / mod );
479 vec2_cset( &vout, v1->
x / v2->x, v1->y / v2->y );
485static int vectorL_div__( lua_State *L )
491 if (lua_isnumber(L,2)) {
492 double mod = lua_tonumber(L,2);
493 vec2_cset( v1, v1->x / mod, v1->y / mod );
497 vec2_cset( v1, v1->x / v2->x, v1->y / v2->y );
516 lua_pushnumber( L, a->x*b->x + a->y*b->y );
535 lua_pushnumber(L, v1->x);
536 lua_pushnumber(L, v1->y);
555 lua_pushnumber(L, VMOD(*v1));
556 lua_pushnumber(L, VANGLE(*v1));
577 x = luaL_checknumber(L,2);
578 y = luaL_checknumber(L,3);
580 vec2_cset( v1, x, y );
601 m = luaL_checknumber(L,2);
602 a = luaL_checknumber(L,3);
604 vec2_pset( v1, m, a );
628 if (!lua_isnoneornil(L,2))
635 dist = vec2_odist(v1);
637 dist = vec2_dist(v1, v2);
640 lua_pushnumber(L, dist);
664 if (!lua_isnoneornil(L,2))
671 dist2 = vec2_odist2(v1);
673 dist2 = vec2_dist2(v1, v2);
676 lua_pushnumber(L, dist2);
689 lua_pushnumber(L, VMOD(*v));
702 lua_pushnumber(L, VANGLE(*v));
740 lua_pushinteger( L, ret );
758 vec2 *center, *p1, *p2, crash[2];
762 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_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 return parameter: Vec2 N...
static int vectorL_mul(lua_State *L)
Multiplies a vector by a number.
int nlua_loadVector(nlua_env env)
Loads the vector metatable.
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.