Original Post
Alrighty, here's my problem. I have an agent on a 2-dimensional space. This agent has a current location (myX and myY) and he has a destination (destination[0] and destination[1] for X and Y respectively). I'm trying to calculate the angle between these two points for use with the agents velocity, distTravel. I've managed to do so, but with bugs. Particularly for the values: myX -> 250 myY -> 250 dest[0] -> 52 dest[1] -> 149 (These numbers were generated randomly and is a case I found that does not work properly). Here's my code: And what this should do for the values posted is send the agent moving toward a North-Western (top-left) direction. However, it decides to send the agent moving East-North-East until it wraps around the torroidal space and THEN it moves toward the destination in a North-East fashion. Can anyone see what might be going on with my math that is telling my agent to move East toward the destination? For destinations that are to the East of the agent it works properly. Thank you.
public void move()
{
int newX, newY,
diffX, diffY;
diffX = (destination[0]-myX);
diffY = (destination[1]-myY);
int xSq = diffX*diffX,
ySq = diffY*diffY;
double disp = Math.sqrt(xSq+ySq);
if (disp > 0.0)
{
double sinDiff = Math.sin(Math.toRadians(diffY/disp));
double theta = Math.asin(Math.toDegrees(sinDiff));
double degTravel = Math.toDegrees(theta);
while (degTravel < 0) degTravel += 360;
while (degTravel > 360) degTravel -= 360;
do {
newX = xnorm(myX + (int)(distTravel * sinTable.getCosFromDeg(degTravel)));
newY = ynorm(myY + (int)(distTravel * sinTable.getSinFromDeg(degTravel)));
} while (myWorld.getObjectAtX$Y(newX,newY)!= null);
// Remove this agent from the current location
myWorld.putObject$atX$Y(null,myX,myY);
// Update the new position and put the agent there
myX = newX;
myY = newY;
myWorld.putObject$atX$Y(this, myX,myY);
}
}