I'm trying to move a square from it's original position to the coordinates of my mouse when I click. The code I have somewhat works, but the square does not go directly to the mouse click. It goes diagonally a bit off, then it goes to the mouse click, as shown in the picture. I tried to make it a straight movement path but I could not think of a way to do so. I'm pretty sure the error is something to do with the 1st method below.
________X ← the mouse click
/
/ ↑
/ ← = movement path
/
/
_____/
| | ← character
|___|
Here are the 3 methods involved so far (pls dont criticize my code too much)
//int x and int y are the positions of the mouse, champion = the character
public static Champion calculateChampionMovement(int x, int y, Champion champion) {
if (x != champion.x || y != champion.y) {
//x and y dist = the distance between the character and the cursor
int xDist = x - champion.x;
int yDist = y - champion.y;
//the angle
plrAngle = Math.atan2(yDist, xDist) * 180 / Math.PI;
//the speed of the character on the x and y axis
//(character diagonally moves at the speed of "champion.speed")
plrXSpeed = champion.speed * Math.cos(plrAngle * Math.PI / 180);
plrYSpeed = champion.speed * Math.sin(plrAngle * Math.PI / 180);
//calls the method below that actually moves the character
playerMain.champion = (Champion) Entity.moveChampions(x, y, champion, champion.speed, plrXSpeed, plrYSpeed);
champion.moving = true;
}
return playerMain.champion;
}
And the 2nd one...
//called by the method above
public static Entity moveChampions(int x, int y, Champion champion, float speed, double xSpeed, double ySpeed) {
//if the distance between the character on the x and y axis is not
//exactly divisible by "speed", then this helps the character stop.
if (Math.abs(x - champion.x) <= speed) {
champion.x = x;
}
if (Math.abs(y - champion.y) <= speed) {
champion.y = y;
}
//stops the character
if (x == champion.x && y == champion.y) {
champion.moving = false;
}
//moves the character
if (champion.moving) {
champion.x += xSpeed;
champion.y += ySpeed;
}
return champion;
}
The last method calls "calculateChampionMovement" and "moveChampions", and it moves the character while "moving" is true
public static void buttonTest() {
if (RIGHTCLICK == true) {
//mouse x and y positions
cursorClickX = (int) (mapX + MOUSE_X);
cursorClickY = (int) (mapY + MOUSE_Y);
//first method (setup the x and y speed)
playerMain.champion = PlayerMain.testMainChampionMove(cursorClickX, cursorClickY, playerMain.champion);
// if character is already moving
} else if (playerMain.champion.moving == true) {
//move the character
playerMain.champion = (Champion) Entity.moveChampions(cursorClickX, cursorClickY, playerMain.champion, champAsdf.speed, plrXSpeed, plrYSpeed);
}
}
hi riot games pls dont sue me im too young anyways