I need to figure out whether a point is infront or behind my vehicle.
I have the vector of it's position, forward direction.
So far I have tried finding the vector perpendicular to its forward direction and then calculating what side of the line it is positioned on.
This works fine at first. But then I realised it is only calculating around the origin.
bool Car::isInfront(ngl::Vec2 _pos)
{
//http://stackoverflow.com/questions/1243614/how-do-i-calculate-the-normal-vector-of-a-line-segment
//if we define dx=x2-x1 and dy=y2-y1, then the normals are (-dy, dx) and (dy, -dx).
//create vector per to the direction of travel.
ngl::Vec2 fwdVec = m_carPhysics->getForwardVec2();
ngl::Vec2 pos = m_carPhysics->getPosVec2();
ngl::Vec2 dir = pos + fwdVec;
float dx = dir.m_x - pos.m_x;
float dy = dir.m_y - pos.m_y;
ngl::Vec2 v1(-dy, dx);
ngl::Vec2 v2(dy, -dx);
ngl::Vec2 perp = (v1) - (v2);
int turn = getLeftOrRight(_pos, perp);
if(turn == LEFT)
{
return true;
}
else
{
return false;
}
int Car::getLeftOrRight(ngl::Vec2 _dir, ngl::Vec2 _fwd)
{
//http://gamedev.stackexchange.com/questions/34536/calculating-angle-between-two-vectors-to-steer-towards-a-target
//cross product;
float val = (_dir.m_x * _fwd.m_y) - (_dir.m_y * _fwd.m_x);
if(val > 0)
{
return LEFT;
}
else
{
return RIGHT;
}
}