[Fix] Fixed ai.relvel(). Also introduced dot product function.
This commit is contained in:
parent
deeea15889
commit
84b20c9bd3
@ -19,7 +19,7 @@ function atk_g_think()
|
|||||||
range = ai.getweaprange()
|
range = ai.getweaprange()
|
||||||
|
|
||||||
-- Shouldn't switch targets if close.
|
-- Shouldn't switch targets if close.
|
||||||
if dist > range * atk_changetarget then
|
if dist > 400 and dist > range * atk_changetarget then
|
||||||
ai.poptask()
|
ai.poptask()
|
||||||
ai.pushtask(0, "attack", enemy)
|
ai.pushtask(0, "attack", enemy)
|
||||||
end
|
end
|
||||||
|
12
src/ai.c
12
src/ai.c
@ -1113,7 +1113,9 @@ static int ai_hyperspace(lua_State* L) {
|
|||||||
/* Get the relative velocity of a pilot. */
|
/* Get the relative velocity of a pilot. */
|
||||||
static int ai_relvel(lua_State* L) {
|
static int ai_relvel(lua_State* L) {
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
|
double dot, mod;
|
||||||
Pilot* p;
|
Pilot* p;
|
||||||
|
Vec2 vv, pv;
|
||||||
|
|
||||||
LLUA_MIN_ARGS(1);
|
LLUA_MIN_ARGS(1);
|
||||||
|
|
||||||
@ -1126,7 +1128,15 @@ static int ai_relvel(lua_State* L) {
|
|||||||
return 0;
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ void vect_cadd(Vec2* v, const double x, const double y) {
|
|||||||
void vect_reflect(Vec2* r, Vec2* v, Vec2* n) {
|
void vect_reflect(Vec2* r, Vec2* v, Vec2* n) {
|
||||||
double dot;
|
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->x = v->x - ((2. * dot) * n->x);
|
||||||
r->y = v->y - ((2. * dot) * n->y);
|
r->y = v->y - ((2. * dot) * n->y);
|
||||||
r->mod = MOD(r->x, r->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! */
|
/* SOLID! */
|
||||||
/* ================ */
|
/* ================ */
|
||||||
@ -236,6 +246,5 @@ Solid* solid_create(const double mass, const double dir,
|
|||||||
/* Free an existing solid. */
|
/* Free an existing solid. */
|
||||||
void solid_free(Solid* src) {
|
void solid_free(Solid* src) {
|
||||||
free(src);
|
free(src);
|
||||||
src = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@ void vectnull(Vec2* v);
|
|||||||
double vect_angle(const Vec2* ref, const Vec2* v);
|
double vect_angle(const Vec2* ref, const Vec2* v);
|
||||||
void vect_cadd(Vec2* v, const double x, const double y);
|
void vect_cadd(Vec2* v, const double x, const double y);
|
||||||
void vect_reflect(Vec2* r, Vec2* v, Vec2* n);
|
void vect_reflect(Vec2* r, Vec2* v, Vec2* n);
|
||||||
|
double vect_dot(Vec2* a, Vec2* b);
|
||||||
|
|
||||||
/* Describe any solid in 2D space. */
|
/* Describe any solid in 2D space. */
|
||||||
typedef struct Solid_ {
|
typedef struct Solid_ {
|
||||||
|
Loading…
Reference in New Issue
Block a user