[java] Let's calculate the angle of a right triangle

Started by
3 comments, last by WiLD2 17 years, 8 months ago
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:

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);
		}
}

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.
---Real programmers don't comment their code. It was hard enough to write, it should be hard to understand!
Advertisement
I'm a bit confused as to what you're trying to do here. If you simply want to move the agent towards the destination, then the math is a little different.

Pseudocode:
// givenVector agentSrc;Vector agentDest;Vector diff;float speed;// movediff = agentDest - agentSrc;diff.normalize();agentSrc += diff * speed;


[Edited by - Funkapotamus on August 15, 2006 2:11:46 PM]
What I've done is calculate the angle between two points. Within computer science (at least within java and c), the quadrants of a 2 dimensional space are flipped. The angles are counter-intuitive in that when starting from 0 degrees where the 1st and 4th quadrants touch, a positive degree increases downward and vice-versa.

I'm getting a little beside myself here. I've calculated the angle between my two points; for the values I've given the angle is approximately 27 degrees. What should happen is my 'newX' value should return some X and the same for my 'newY' value to return some point along the hypotenuse between my starting and end destination. It should be as simple as that, however my agent isn't even wandering along the straight path between the start and end points. He's wandering off up and to the right, rather than along the path which would take him up and to the left. He does end up at the end destination (as it's a torroidal space) but not how I'd have expected him to.

I guess I'm not really expecting help, then. What I've done should be correct. I was maybe wondering if other people have had similar problems with trigonometry and what might be done to prevent such 'features'.

I thought there might have been something wrong from within my 'sintable' so I changed that piece of code into one that uses the Math. library
newX = xnorm(myX + (int)(distTravel * Math.cos(Math.toRadians(degTravel))));newY = ynorm(myY + (int)(distTravel * Math.sin(Math.toRadians(degTravel))));

But still no luck, same thing as before.
---Real programmers don't comment their code. It was hard enough to write, it should be hard to understand!
"my agent isn't even wandering along the straight path between the start and end points"

If you use the equation I provided, he should move along a straight path. If you fear that you are returning an improper angle, try this:

public int vectorToAngle(Vector src, Vector dest) {  int quadrant = -1;  if(dest.x == src.x && dest.y == src.y)     return 0;  else if(dest.x >= src.x && dest.y >= src.y)     quadrant = 0;  else if(dest.x <= src.x && dest.y >= src.y)     quadrant = 3;  else if(dest.x <= src.x && dest.y <= src.y)     quadrant = 2;  else     quadrant = 1;  int angle = (int)Math.toDegrees(Math.atan((Math.abs(dest.y - src.y) / Math.abs(dest.x - src.x))));  return angle + quadrant * 90;}
Thanks for the replies, funk*

I managed to find my problem yesterday, it required me to recognize when my destination point was less than that of my current point along the X axis. Here's what has seemed to have fixed it:
			double sinDiff = Math.sin(Math.toRadians(diffY/disp));			double theta = Math.asin(Math.toDegrees(sinDiff));			double degTravel = Math.toDegrees(theta);						degTravel = degTravel*(-1);						if (destination[0] < myX) degTravel = (180-degTravel);


Thanks for the help.
---Real programmers don't comment their code. It was hard enough to write, it should be hard to understand!

This topic is closed to new replies.

Advertisement