Android : Inconsistent movement speed based on angles

on Saturday, August 9, 2014


I have a 120x120 box that I am using as a touchpad for an android game. You touch and/or drag within the box, and the angle between the touch and the center of the box is used to create a normalized vector to move my player object. It works fine except for one thing, when I go down, left or both, the player moves slower than when I go up, right or both.


I determine the angle with the following method:



private double getAngle(int xLoc, int yLoc) {
double angle = Math.atan2(yLoc - yCenter, xLoc - xCenter);
return angle;
}


Then I call the move method in the player class, which changes the speedX and speedY to follow the same angle:



speedX = moveSpeed * Math.cos(angle);
speedY = moveSpeed * Math.sin(angle);


The player location is then updated:



xPosition += speedX;
yPosition += speedY;

0 comments:

Post a Comment