[Fix] Fixed ai.relvel(). Also introduced dot product function.

This commit is contained in:
Allanis 2014-01-22 17:31:36 +00:00
parent deeea15889
commit 84b20c9bd3
4 changed files with 24 additions and 4 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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_ {