What's up, guys.
I want to make my enemy patrol around some area and I don't like the code I've written.
My function is:
void AI::patrol( glm::vec3 startPoint, glm::vec3 endPoint, float speed )
{
//Find out on which direction to move towards.
glm::vec3 movementDirection = glm::normalize( endPoint - startPoint );
//Move the player to that direction with a certain speed.
mainCam.setCurrentPosition( mainCam.getCurrentPosition() + movementDirection*speed );
/*Now the problem is that I need to somehow say: If the enemy is already at the
end of the route, turn in the opposite direction.
If the game was one-dimensional, I could just compare startPoint and endPoint, find
the biggest one, and check if I'm between the two points. But it's not.
The only way that comes to mind is to do the same thing that I do for 1D, but do it 3 times for 3D.
This is to find the biggest x,y,z and the smallest x,y and z
among the two vectors and check if I'm between them (as the code below ) */
glm::vec3 pos = mainCam.getCurrentPosition() //too much writing. stupid getters.
if( pos.x < findMax( startPoint.x, endPoint.x ) && pos.x > findMin( startPoint.x, endPoint.x ) &&
pos.y < findMax( startPoint.y, endPoint.y ) && pos.y > findMin( startPoint.y, endPoint.y ) &&
pos.z < findMax( startPoint.z, endPoint.z ) && pos.z > findMin( startPoint.z, endPoint.z )
{
//keep walking;
}
else movementDirection *= -1;
}
This is kind of messed up, because I can get some kind of floating-point errors.
Is there another way of doing this?