[Add] Innitial attempt at a proper facing system, primarily for missiles.
This commit is contained in:
parent
26099de520
commit
17fba238d1
107
src/collision.c
107
src/collision.c
@ -2,16 +2,20 @@
|
||||
#include "log.h"
|
||||
#include "collision.h"
|
||||
|
||||
/* Collide sprite at (asx, asy) int 'at' at pos 'ap' with sprite at (bsx,bsy) */
|
||||
/*in 'bt' at 'bp' */
|
||||
/* at - Texture a. */
|
||||
/* asx - Position of x of sprite a. */
|
||||
/* asy - Position of y of sprite a. */
|
||||
/* ap - Position in space of sprite a. */
|
||||
/* bt - Texture b. */
|
||||
/* bsx - Position of x of sprite b. */
|
||||
/* bsy - Position of y of sprite b. */
|
||||
/* bp - Position in space of sprite b. */
|
||||
/**
|
||||
* @brief Collide sprite at (asx, asy) in 'at' at pos 'ap' with sprite at (bsx,bsy)
|
||||
* in 'bt' at 'bp'
|
||||
*
|
||||
* @param[in] at - Texture a.
|
||||
* @param[in] asx - Position of x of sprite a.
|
||||
* @param[in] asy - Position of y of sprite a.
|
||||
* @param[in] ap - Position in space of sprite a.
|
||||
* @param[in] bt - Texture b.
|
||||
* @param[in] bsx - Position of x of sprite b.
|
||||
* @param[in] bsy - Position of y of sprite b.
|
||||
* @param[in] bp - Position in space of sprite b.
|
||||
* @return 1 on collision, 0 else.
|
||||
*/
|
||||
int CollideSprite(const glTexture* at, const int asx, const int asy,
|
||||
const Vec2* ap, const glTexture* bt,
|
||||
const int bsx, const int bsy, const Vec2* bp) {
|
||||
@ -65,3 +69,86 @@ int CollideSprite(const glTexture* at, const int asx, const int asy,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculate collision path corrections.
|
||||
*
|
||||
* @param[in] p - Position of object that's trying to collide.
|
||||
* @param[in] v - Velocity of object that's trying to collide.
|
||||
* @param[in] tp - Position of the target to collide into.
|
||||
* @param[in] tv - Velocity of the target to collide into.
|
||||
* @param[in] limit - Accuracy limit.
|
||||
* @return Direction object will need to adjust velocity to for collision.
|
||||
*/
|
||||
/*
|
||||
* Doesn't work as expected, needs to be fixed to allow missiles to use
|
||||
* the physics engine instead of hacking their velocities.
|
||||
*/
|
||||
#if 0
|
||||
double CollidePath(const Vec2* p, Vec2* v,
|
||||
const Vec2* tp, Vec2* tv, double limit) {
|
||||
|
||||
double mx, my; /* Position modifiers. */
|
||||
double t, tt; /* Time to target. */
|
||||
Vec2 test, ttest; /* Test vector and target test vector. */
|
||||
double offset; /* Direction to face and error it produces. */
|
||||
double mod, moddir; /* Modifier for iteration. */
|
||||
int i;
|
||||
|
||||
/* Test vector. */
|
||||
vect_cset(&test, tp->x - p->x, tp->y - p->y); /* Start byt straight line. */
|
||||
t = VMOD(test) / VMOD(*v); /* d=v*t ==> t=d/v. */
|
||||
|
||||
/* Target test vector. */
|
||||
vectnull(&ttest); /* Starting from itself. */
|
||||
tt = VMOD(ttest) / VMOD(*tv);
|
||||
|
||||
/* Special case object isn't moving. */
|
||||
if(VMOD(*v) < 1e-6)
|
||||
return VANGLE(test);
|
||||
|
||||
/* Target faster than object. */
|
||||
if(VMOD(*v) < VMOD(*tv)) {
|
||||
vect_reflect(&test, v, tv);
|
||||
return VANGLE(test);
|
||||
}
|
||||
|
||||
/* Loop until error is minimal. */
|
||||
offset = tt - t;
|
||||
moddir = 1.; /* Start off by position increments. */
|
||||
mod = 10.; /* Start off by a 10 second jump. */
|
||||
i = 0;
|
||||
while(FABS(offset) > limit) {
|
||||
if(i > 100) break;
|
||||
|
||||
/* Calculate position modifiers. */
|
||||
if(offset < -mod/2.) /* tt>t ==> major overshot. */
|
||||
moddir = -1.;
|
||||
else if(offset < 0.) { /* tt>t ==> overshot. */
|
||||
/* Invert direction and half. */
|
||||
moddir = -1.;
|
||||
mod = mod/2.;
|
||||
}
|
||||
else if(offset > mod) /* tt<t ==> undershot. */
|
||||
moddir = 1.; /* Make sure it's positive. */
|
||||
else if(offset > 0.) { /* tt<t ==> minor undershot. */
|
||||
/* Positive and shrink step. */
|
||||
moddir = 1.;
|
||||
mod = FABS(mod)/2.;
|
||||
}
|
||||
mx = tv->x * mod * moddir;
|
||||
my = tv->y * mod * moddir;
|
||||
|
||||
/* Increment test vectors. */
|
||||
vect_cadd(&test, mx, my);
|
||||
vect_cadd(&ttest, mx, my);
|
||||
|
||||
/* Compare results. */
|
||||
t = VMOD(test) / VMOD(*v);
|
||||
tt = VMOD(ttest) / VMOD(*tv);
|
||||
offset = tt - t;
|
||||
|
||||
i++;
|
||||
}
|
||||
return VANGLE(test);
|
||||
}
|
||||
#endif
|
||||
|
@ -2,7 +2,12 @@
|
||||
#include "opengl.h"
|
||||
#include "physics.h"
|
||||
|
||||
/* Return 1 if collision is detected. */
|
||||
int CollideSprite(const glTexture* at, const int asx, const int asy,
|
||||
const Vec2* ap, const glTexture* bt,
|
||||
const int bsx, const int bsy, const Vec2* bp);
|
||||
|
||||
/* Get direction to face the target. */
|
||||
/*double CollidePath(const Vec2* p, Vec2* v,
|
||||
const Vec2* tp, Vec2* tv, const double limit);*/
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user