Thank you for visiting Blazing Games

Follow that Mouse

There is a much more efficient way of finding the angle between where the mouse is pointing and the coordinates of the player. When I was writing my angle routines, I was relying on the trigonometry overview in my math books. Had I read through the section on polar coordinates I might have made my task much simpler. In fact, using the polar coordinate version of atan in java could also be done.

The version of my find angle function that is used in the game is the one I created using the traditional version of the atan function, which returns a result that only covers a small portion of the circle. To get the correct coordinate, the results have to be adjusted. Lets quickly go through the code.

The first thing we do is find the tangent of the angle, making sure to take the special case of a vertical line into account.

Next we take the arc tangent of the tangent we calculated to find the angle in radians. This angle is converted into degrees.

Finally, we use the x and y coordinates to determine which quadrant of the circle that the angle belongs and make sure the returned result is correct.

I should point out that as this was written for java and adjusted for flash, the angle for 0 is different so 90 degrees have to be added. To take advantage of polar coordinates to re-write this function, you could reduce the function down to the following.

Previous Page
1 2 3 4 5 6 7

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2004-2006 Blazing Games Inc. All Rights Reserved.