From 84b20c9bd3c5209c2fd349b23f61da08485806e7 Mon Sep 17 00:00:00 2001 From: Allanis Date: Wed, 22 Jan 2014 17:31:36 +0000 Subject: [PATCH] [Fix] Fixed ai.relvel(). Also introduced dot product function. --- scripts/ai/include/attack_generic.lua | 2 +- src/ai.c | 12 +++++++++++- src/physics.c | 13 +++++++++++-- src/physics.h | 1 + 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/scripts/ai/include/attack_generic.lua b/scripts/ai/include/attack_generic.lua index d382598..1635e11 100644 --- a/scripts/ai/include/attack_generic.lua +++ b/scripts/ai/include/attack_generic.lua @@ -19,7 +19,7 @@ function atk_g_think() range = ai.getweaprange() -- Shouldn't switch targets if close. - if dist > range * atk_changetarget then + if dist > 400 and dist > range * atk_changetarget then ai.poptask() ai.pushtask(0, "attack", enemy) end diff --git a/src/ai.c b/src/ai.c index f85a63b..e62cd37 100644 --- a/src/ai.c +++ b/src/ai.c @@ -1113,7 +1113,9 @@ static int ai_hyperspace(lua_State* L) { /* Get the relative velocity of a pilot. */ static int ai_relvel(lua_State* L) { unsigned int id; + double dot, mod; Pilot* p; + Vec2 vv, pv; LLUA_MIN_ARGS(1); @@ -1126,7 +1128,15 @@ static int ai_relvel(lua_State* L) { return 0; } - lua_pushnumber(L, vect_dist(&cur_pilot->solid->vel, &p->solid->vel)); + /* Get te projection of target on current velocity. */ + vect_cset(&vv, p->solid->vel.x - cur_pilot->solid->vel.x, + p->solid->vel.y - cur_pilot->solid->vel.y); + vect_cset(&pv, p->solid->pos.x - cur_pilot->solid->pos.x, + p->solid->pos.y - cur_pilot->solid->pos.y); + dot = vect_dot(&pv, &vv); + mod = MAX(VMOD(pv), 1.); /* Avoid /0. */ + + lua_pushnumber(L, dot / mod); return 1; } diff --git a/src/physics.c b/src/physics.c index 99dc5ec..c6ee382 100644 --- a/src/physics.c +++ b/src/physics.c @@ -78,7 +78,7 @@ void vect_cadd(Vec2* v, const double x, const double y) { void vect_reflect(Vec2* r, Vec2* v, Vec2* n) { double dot; - dot = (v->x*n->x) + (v->y*n->y); + dot = vect_dot(v, n); r->x = v->x - ((2. * dot) * n->x); r->y = v->y - ((2. * dot) * n->y); r->mod = MOD(r->x, r->y); @@ -86,6 +86,16 @@ void vect_reflect(Vec2* r, Vec2* v, Vec2* n) { } +/** + * @brief Vector dot product. + * @param a Vector 1 for dot product. + * @param b Vector 2 for dot product. + * @return Dot product of vectors. + */ +double vect_dot(Vec2* a, Vec2* b) { + return a->x * b->x + a->y * b->y; +} + /* ================ */ /* SOLID! */ /* ================ */ @@ -236,6 +246,5 @@ Solid* solid_create(const double mass, const double dir, /* Free an existing solid. */ void solid_free(Solid* src) { free(src); - src = NULL; } diff --git a/src/physics.h b/src/physics.h index 7e89674..5d557b5 100644 --- a/src/physics.h +++ b/src/physics.h @@ -34,6 +34,7 @@ void vectnull(Vec2* v); double vect_angle(const Vec2* ref, const Vec2* v); void vect_cadd(Vec2* v, const double x, const double y); void vect_reflect(Vec2* r, Vec2* v, Vec2* n); +double vect_dot(Vec2* a, Vec2* b); /* Describe any solid in 2D space. */ typedef struct Solid_ {