I'm creating a racing game, in which I run into some problems while creating the AI. What I need to do is get the AI from a X,Y position to a "checkpoint" with another X,Y position. The AI driving track will be created by placing different invisible checkpoints around the map that is will go to.
I can get the direction to the checkpoint by calculating the difference in X,Y from me to it and then use tan(Deg) = Y/X, witch gives me the direction I have to go from my current position.
distanceToMoveX = checkpoint1X - this.getX();
distanceToMoveY = checkpoint1Y - this.getY();
newDirection = ((double)distanceToMoveY / distanceToMoveX) * 100;
So now I have the direction in which I have to go from my current position, and I also have a direction in which I'm currently moving. What I want to do now is get my AI to realize its closer to turn right if my current direction is 350 and I want to go to 10. As it is right now I can only get it to turn right or left all the time.
This is what you need for choosing the direction (this is an example with hardcoded values):
As for the inverted values on the Y axis when rendering, I guess you just need to invert the Y value to fix it (multiply by -1).