How can I tell Java that it's closer to 350->355->360->05 degrees instead of going all the way


Carl Olsen

I'm creating a racing game and I'm having some problems creating the AI. What I need to do is move the AI ​​from an X,Y position to another "checkpoint" at an X,Y position. Create AI driving trails by placing different invisible checkpoints around the map you want to travel to.

I can determine the direction to reach the checkpoint by calculating the difference between X,Y and me, then using tan(Deg) = Y/X, witch gives me the direction I have to start from my current position.

distanceToMoveX = checkpoint1X - this.getX();
distanceToMoveY = checkpoint1Y - this.getY();
newDirection = ((double)distanceToMoveY / distanceToMoveX) * 100;

So now I have the direction that I have to go from my current position, and I also have the direction I'm currently moving. What I want to do now is if my current direction is 350 and I want to go to 10, my AI will realize it's closer to a right turn. Right now, I can only make it turn right or left all the time.

Rodrigo Quesada

This is what is needed to select the orientation (here is an example with hardcoded values):

private int correctedDirectionCalculation(int calculation){
   return calculation >= 0 ? calculation : calculation + 360;
}

public void chooseDirection() {
    int targetDirection = 5;
    int currentDirection = 360;

    int goingRightDistance = correctedDirectionCalculation(targetDirection - currentDirection);
    int goingLeftDistance = correctedDirectionCalculation(currentDirection - targetDirection);

    System.out.println(goingLeftDistance < goingRightDistance ? "Go Left!" : "Go Right!");
}

As for the inverted value on the Y axis when rendering, I guess you just need to invert the Y value to fix it (multiply by -1).

Related


Is MySQL timing out? Java can't tell what's going on

Chapter 343 I am running this method to update SQL using DBCP connection pool: After exactly 8 passes , the method setValue stops doing this and sends no data. public static void setValue(String user, Integer id){ Connection connection = null; try

How can I tell if the user's Java code is successfully compiled?

pjcat I have a web application running on a LAMP stack that allows users to write Java code as HTML forms, but depending on whether that code compiles successfully or not, I want to do different things. The way I currently have it is that the PHP program will

How can I tell if the user's Java code is successfully compiled?

pjcat I have a web application running on a LAMP stack that allows users to write Java code as HTML forms, but depending on whether that code compiles successfully or not, I want to do different things. The way I currently have it is that the PHP program will