PDA

View Full Version : Polar Coordinates



Forkaster
04-10-2009, 04:29 PM
Hi,

I am working on a project (game) for college using OpenGL. To sum it up, the player controls an agent that navigates trough a terrain searching for keys. He has the help of a key detector to help him find the keys.

My problem is in the implementation of the key detector, it is supposed to tell the distance and the direction of the nearest key. In order to do this i think that using polar coordinates is the best choice. The thing is that the zero-angle of the polar coordinate must coincide with the agent's orientation.

But hoe can I say that in code (using C, BTW)?

I currently have this code:


double directionKey(Key key)
{
double aux1 = key.pos_x - agent.pos_x;
double aux2 = key.pos_z - agent.pos_z;

return atan(aux2/aux1) * RAD;
}

Rosario Leonardi
04-12-2009, 12:41 PM
With a subtraction. :-|

Also, don't use atan, use atan2 (or check division by zero).