naev 0.11.5
nlua_vec2.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
12#include <lauxlib.h>
13
14#include "naev.h"
17#include "nlua_vec2.h"
18
19#include "log.h"
20#include "nluadef.h"
21#include "collision.h"
22
23/* Vector metatable methods */
24static int vectorL_new( lua_State *L );
25static int vectorL_newP( lua_State *L );
26static int vectorL_copy( lua_State *L );
27static int vectorL_tostring( lua_State *L );
28static int vectorL_add__( lua_State *L );
29static int vectorL_add( lua_State *L );
30static int vectorL_sub__( lua_State *L );
31static int vectorL_sub( lua_State *L );
32static int vectorL_mul__( lua_State *L );
33static int vectorL_mul( lua_State *L );
34static int vectorL_div__( lua_State *L );
35static int vectorL_div( lua_State *L );
36static int vectorL_unm( lua_State *L );
37static int vectorL_dot( lua_State *L );
38static int vectorL_cross( lua_State *L );
39static int vectorL_get( lua_State *L );
40static int vectorL_polar( lua_State *L );
41static int vectorL_set( lua_State *L );
42static int vectorL_setP( lua_State *L );
43static int vectorL_distance( lua_State *L );
44static int vectorL_distance2( lua_State *L );
45static int vectorL_mod( lua_State *L );
46static int vectorL_angle( lua_State *L );
47static int vectorL_normalize( lua_State *L );
48static int vectorL_collideLineLine( lua_State *L );
49static int vectorL_collideCircleLine( lua_State *L );
50static const luaL_Reg vector_methods[] = {
51 { "new", vectorL_new },
52 { "newP", vectorL_newP },
53 { "copy", vectorL_copy },
54 { "__tostring", vectorL_tostring },
55 { "__add", vectorL_add },
56 { "add", vectorL_add__ },
57 { "__sub", vectorL_sub },
58 { "sub", vectorL_sub__ },
59 { "__mul", vectorL_mul },
60 { "mul", vectorL_mul__ },
61 { "__div", vectorL_div },
62 { "div", vectorL_div__ },
63 { "__unm", vectorL_unm},
64 { "dot", vectorL_dot },
65 { "cross", vectorL_cross },
66 { "get", vectorL_get },
67 { "polar", vectorL_polar },
68 { "set", vectorL_set },
69 { "setP", vectorL_setP },
70 { "dist", vectorL_distance },
71 { "dist2", vectorL_distance2 },
72 { "mod", vectorL_mod },
73 { "angle", vectorL_angle },
74 { "normalize", vectorL_normalize },
75 { "collideLineLine", vectorL_collideLineLine },
76 { "collideCircleLine", vectorL_collideCircleLine },
77 {0,0}
78};
86int nlua_loadVector( nlua_env env )
87{
88 nlua_register(env, VECTOR_METATABLE, vector_methods, 1);
89 return 0;
90}
91
119vec2* lua_tovector( lua_State *L, int ind )
120{
121 return (vec2*) lua_touserdata(L,ind);
122}
130vec2* luaL_checkvector( lua_State *L, int ind )
131{
132 if (lua_isvector(L,ind))
133 return (vec2*) lua_touserdata(L,ind);
134 luaL_typerror(L, ind, VECTOR_METATABLE);
135 return NULL;
136}
137
145vec2* lua_pushvector( lua_State *L, vec2 vec )
146{
147 vec2 *v = (vec2*) lua_newuserdata(L, sizeof(vec2));
148 *v = vec;
149 luaL_getmetatable(L, VECTOR_METATABLE);
150 lua_setmetatable(L, -2);
151 return v;
152}
153
161int lua_isvector( lua_State *L, int ind )
162{
163 int ret;
164
165 if (lua_getmetatable(L,ind)==0)
166 return 0;
167 lua_getfield(L, LUA_REGISTRYINDEX, VECTOR_METATABLE);
168
169 ret = 0;
170 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
171 ret = 1;
172
173 lua_pop(L, 2); /* remove both metatables */
174 return ret;
175}
176
188static int vectorL_new( lua_State *L )
189{
190 vec2 v;
191 double x, y;
192
193 if (!lua_isnoneornil(L,1))
194 x = luaL_checknumber(L,1);
195 else
196 x = 0.;
197
198 if (!lua_isnoneornil(L,2))
199 y = luaL_checknumber(L,2);
200 else
201 y = x;
202
203 vec2_cset( &v, x, y );
204 lua_pushvector(L, v);
205 return 1;
206}
207
219static int vectorL_newP( lua_State *L )
220{
221 vec2 v;
222 double m, a;
223
224 if (!lua_isnoneornil(L,1))
225 m = luaL_checknumber(L, 1);
226 else
227 m = 0.;
228
229 if (!lua_isnoneornil(L,2))
230 a = luaL_checknumber(L, 2);
231 else
232 a = 0.;
233
234 vec2_pset( &v, m, a );
235 lua_pushvector(L, v);
236 return 1;
237}
238
246static int vectorL_copy( lua_State *L )
247{
248 const vec2 *v = luaL_checkvector(L,1);
249 lua_pushvector( L, *v );
250 return 1;
251}
252
260static int vectorL_tostring( lua_State *L )
261{
262 char buf[STRMAX_SHORT];
263 const vec2 *v = luaL_checkvector(L,1);
264 snprintf( buf, sizeof(buf), "vec2( %g, %g )", v->x, v->y );
265 lua_pushstring(L, buf);
266 return 1;
267}
268
285static int vectorL_add( lua_State *L )
286{
287 vec2 vout, *v1;
288 double x, y;
289
290 if (lua_isnumber(L,1)) {
291 x = y = lua_tonumber(L,1);
292 v1 = luaL_checkvector(L,2);
293 }
294 else {
295 /* Get self. */
296 v1 = luaL_checkvector(L,1);
297
298 /* Get rest of parameters. */
299 if (lua_isvector(L,2)) {
300 const vec2 *v2 = lua_tovector(L,2);
301 x = v2->x;
302 y = v2->y;
303 }
304 else {
305 x = luaL_checknumber(L,2);
306 if (!lua_isnoneornil(L,3))
307 y = luaL_checknumber(L,3);
308 else
309 y = x;
310 }
311 }
312
313 /* Actually add it */
314 vec2_cset( &vout, v1->x + x, v1->y + y );
315 lua_pushvector( L, vout );
316
317 return 1;
318}
319static int vectorL_add__( lua_State *L )
320{
321 vec2 *v1;
322 double x, y;
323
324 /* Get self. */
325 v1 = luaL_checkvector(L,1);
326
327 /* Get rest of parameters. */
328 if (lua_isvector(L,2)) {
329 const vec2 *v2 = lua_tovector(L,2);
330 x = v2->x;
331 y = v2->y;
332 }
333 else {
334 x = luaL_checknumber(L,2);
335 if (!lua_isnoneornil(L,3))
336 y = luaL_checknumber(L,3);
337 else
338 y = x;
339 }
340
341 /* Actually add it */
342 vec2_cset( v1, v1->x + x, v1->y + y );
343 lua_pushvector( L, *v1 );
344
345 return 1;
346}
347
364static int vectorL_sub( lua_State *L )
365{
366 const vec2 *v1;
367 vec2 vout;
368 double x, y;
369
370 /* Get self. */
371 v1 = luaL_checkvector(L,1);
372
373 /* Get rest of parameters. */
374 if (lua_isvector(L,2)) {
375 const vec2 *v2 = lua_tovector(L,2);
376 x = v2->x;
377 y = v2->y;
378 }
379 else {
380 x = luaL_checknumber(L,2);
381 if (!lua_isnoneornil(L,3))
382 y = luaL_checknumber(L,3);
383 else
384 y = x;
385 }
386
387 /* Actually add it */
388 vec2_cset( &vout, v1->x - x, v1->y - y );
389 lua_pushvector( L, vout );
390 return 1;
391}
392static int vectorL_sub__( lua_State *L )
393{
394 vec2 *v1;
395 double x, y;
396
397 /* Get self. */
398 v1 = luaL_checkvector(L,1);
399
400 /* Get rest of parameters. */
401 if (lua_isvector(L,2)) {
402 const vec2 *v2 = lua_tovector(L,2);
403 x = v2->x;
404 y = v2->y;
405 }
406 else {
407 x = luaL_checknumber(L,2);
408 if (!lua_isnoneornil(L,3))
409 y = luaL_checknumber(L,3);
410 else
411 y = x;
412 }
413
414 /* Actually add it */
415 vec2_cset( v1, v1->x - x, v1->y - y );
416 lua_pushvector( L, *v1 );
417 return 1;
418}
419
431static int vectorL_mul( lua_State *L )
432{
433 vec2 vout;
434
435 if (lua_isnumber(L,1)) {
436 double d = lua_tonumber(L,1);
437 const vec2 *v = luaL_checkvector(L,2);
438 vec2_cset( &vout, v->x * d, v->y * d );
439 }
440 else {
441 if (lua_isnumber(L,2)) {
442 const vec2 *v = luaL_checkvector(L,1);
443 double d = lua_tonumber(L,2);
444 vec2_cset( &vout, v->x * d, v->y * d );
445 }
446 else {
447 const vec2 *v1 = luaL_checkvector(L,1);
448 const vec2 *v2 = luaL_checkvector(L,2);
449 vec2_cset( &vout, v1->x * v2->x, v1->y * v2->y );
450 }
451 }
452
453 /* Actually add it */
454 lua_pushvector( L, vout );
455 return 1;
456}
457static int vectorL_mul__( lua_State *L )
458{
459 vec2 *v1 = luaL_checkvector(L,1);
460 if (lua_isnumber(L,2)) {
461 double mod = luaL_checknumber(L,2);
462 vec2_cset( v1, v1->x * mod, v1->y * mod );
463 }
464 else {
465 const vec2 *v2 = luaL_checkvector(L,2);
466 vec2_cset( v1, v1->x * v2->x, v1->y * v2->y );
467 }
468
469 /* Actually add it */
470 lua_pushvector( L, *v1 );
471 return 1;
472}
473
485static int vectorL_div( lua_State *L )
486{
487 vec2 vout;
488 const vec2 *v1 = luaL_checkvector(L,1);
489 if (lua_isnumber(L,2)) {
490 double mod = lua_tonumber(L,2);
491 vec2_cset( &vout, v1->x / mod, v1->y / mod );
492 }
493 else {
494 const vec2 *v2 = luaL_checkvector(L,2);
495 vec2_cset( &vout, v1->x / v2->x, v1->y / v2->y );
496 }
497
498 lua_pushvector( L, vout );
499 return 1;
500}
501static int vectorL_div__( lua_State *L )
502{
503 vec2 *v1 = luaL_checkvector(L,1);
504 if (lua_isnumber(L,2)) {
505 double mod = lua_tonumber(L,2);
506 vec2_cset( v1, v1->x / mod, v1->y / mod );
507 }
508 else {
509 const vec2 *v2 = luaL_checkvector(L,2);
510 vec2_cset( v1, v1->x / v2->x, v1->y / v2->y );
511 }
512
513 lua_pushvector( L, *v1 );
514 return 1;
515}
516static int vectorL_unm( lua_State *L )
517{
518 vec2 vout;
519 const vec2 *vin = luaL_checkvector(L,1);
520 vec2_cset( &vout, -vin->x, -vin->y );
521 lua_pushvector( L, vout );
522 return 1;
523}
524
533static int vectorL_dot( lua_State *L )
534{
535 const vec2 *a = luaL_checkvector(L,1);
536 const vec2 *b = luaL_checkvector(L,2);
537 lua_pushnumber( L, a->x*b->x + a->y*b->y );
538 return 1;
539}
540
549static int vectorL_cross( lua_State *L )
550{
551 const vec2 *a = luaL_checkvector(L,1);
552 const vec2 *b = luaL_checkvector(L,2);
553 lua_pushnumber( L, a->x*b->y - a->y*b->x );
554 return 1;
555}
556
567static int vectorL_get( lua_State *L )
568{
569 const vec2 *v1 = luaL_checkvector(L,1);
570 /* Push the vector. */
571 lua_pushnumber(L, v1->x);
572 lua_pushnumber(L, v1->y);
573 return 2;
574}
575
588static int vectorL_polar( lua_State *L )
589{
590 const vec2 *v1 = luaL_checkvector(L,1);
591 lua_pushnumber(L, VMOD(*v1));
592 lua_pushnumber(L, VANGLE(*v1));
593 return 2;
594}
595
606static int vectorL_set( lua_State *L )
607{
608 vec2 *v1;
609 double x, y;
610
611 /* Get parameters. */
612 v1 = luaL_checkvector(L,1);
613 x = luaL_checknumber(L,2);
614 y = luaL_checknumber(L,3);
615
616 vec2_cset( v1, x, y );
617 return 0;
618}
619
630static int vectorL_setP( lua_State *L )
631{
632 vec2 *v1;
633 double m, a;
634
635 /* Get parameters. */
636 v1 = luaL_checkvector(L,1);
637 m = luaL_checknumber(L,2);
638 a = luaL_checknumber(L,3);
639
640 vec2_pset( v1, m, a );
641 return 0;
642}
643
655static int vectorL_distance( lua_State *L )
656{
657 double dist;
658 const vec2 *v1 = luaL_checkvector(L,1);
659
660 /* Get rest of parameters. */
661 if (!lua_isnoneornil(L,2)) {
662 const vec2 *v2 = luaL_checkvector(L,2);
663 dist = vec2_dist(v1, v2);
664 }
665 else
666 dist = vec2_odist(v1);
667
668 /* Return the distance. */
669 lua_pushnumber(L, dist);
670 return 1;
671}
672
684static int vectorL_distance2( lua_State *L )
685{
686 double dist2;
687 const vec2 *v1 = luaL_checkvector(L,1);
688
689 /* Get rest of parameters. */
690 if (!lua_isnoneornil(L,2)) {
691 const vec2 *v2 = luaL_checkvector(L,2);
692 dist2 = vec2_dist2(v1, v2);
693 }
694 else
695 dist2 = vec2_odist2(v1);
696
697 /* Return the distance. */
698 lua_pushnumber(L, dist2);
699 return 1;
700}
701
708static int vectorL_mod( lua_State *L )
709{
710 const vec2 *v = luaL_checkvector(L,1);
711 lua_pushnumber(L, VMOD(*v));
712 return 1;
713}
714
721static int vectorL_angle( lua_State *L )
722{
723 const vec2 *v = luaL_checkvector(L,1);
724 lua_pushnumber(L, VANGLE(*v));
725 return 1;
726}
727
735static int vectorL_normalize( lua_State *L )
736{
737 vec2 *v = luaL_checkvector(L,1);
738 double n = luaL_optnumber(L,2,1.);
739 double m = n/MAX(VMOD(*v),1e-6);
740 v->x *= m;
741 v->y *= m;
742 lua_pushvector(L, *v);
743 return 1;
744}
745
756static int vectorL_collideLineLine( lua_State *L )
757{
758 const vec2 *s1 = luaL_checkvector(L,1);
759 const vec2 *e1 = luaL_checkvector(L,2);
760 const vec2 *s2 = luaL_checkvector(L,3);
761 const vec2 *e2 = luaL_checkvector(L,4);
762 vec2 crash;
763 int ret = CollideLineLine( s1->x, s1->y, e1->x, e1->y, s2->x, s2->y, e2->x, e2->y, &crash );
764 lua_pushinteger( L, ret );
765 lua_pushvector( L, crash );
766 return 2;
767}
768
780static int vectorL_collideCircleLine( lua_State *L )
781{
782 const vec2 *center, *p1, *p2;
783 vec2 crash[2];
784 double radius;
785
786 center = luaL_checkvector( L, 1 );
787 radius = luaL_checknumber( L, 2 );
788 p1 = luaL_checkvector( L, 3 );
789 p2 = luaL_checkvector( L, 4 );
790
791 int cnt = CollideLineCircle( p1, p2, center, radius, crash );
792 if (cnt>0)
793 lua_pushvector( L, crash[0] );
794 if (cnt>1)
795 lua_pushvector( L, crash[1] );
796
797 return cnt;
798}
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.
Definition collision.c:952
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.
Definition collision.c:455
Header file with generic functions and naev-specifics.
#define MAX(x, y)
Definition naev.h:39
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 const luaL_Reg vector_methods[]
Definition nlua_vec2.c:50
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
int nlua_loadVector(nlua_env env)
Loads the vector metatable.
Definition nlua_vec2.c:86
static int vectorL_copy(lua_State *L)
Copies a vector.
Definition nlua_vec2.c:246
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
Definition nlua_vec2.c:161
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
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
Definition nlua_vec2.c:130
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
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
Definition nlua_vec2.c:119
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition nlua_vec2.c:145
static int vectorL_new(lua_State *L)
Creates a new vector.
Definition nlua_vec2.c:188
static const double d[]
Definition rng.c:273
Represents a 2d vector.
Definition vec2.h:32
double y
Definition vec2.h:34
double x
Definition vec2.h:33