Public Group

# Trig Question

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Heya all, It's late and I'm having a hard time figuring out a basic trig problem. Basically, I'm trying to get an object to to follow another object on an X, Y coord system. It works fine for Quad I and IV (in traditional trig) but flips to the opposite in Quad II and III. Here is the algorithm I worte (asume all are floats and velocity is constant): positionX = x1 positionY = y1 theta = arctan( (y1 - y2) / (x1 - x2) ) deltaX = cos(theta) deltaY = sin(theta) for each tick { positionX += deltaX positionY += deltaY } Thanks in advance for any help! ~Argo [Edited by - argonaut on July 2, 2006 2:26:05 AM]

##### Share on other sites
What exactly do you mean by follow, could you please offer a bit more information. Because there are several ways to make on object 'follow' another.
Do you want the 'follower' to face the 'target' (in 2D) and move the 'follower' towards the 'target' a specified amount each frame?

##### Share on other sites
Use atan2().

From my linux manual page:

#include <math.h>double atan2(double y, double x);DESCRIPTION       The  atan2()  function calculates the arc tangent of the two vari-       ables x and y.  It is similar to calculating the arc tangent of  y       / x, except that the signs of both arguments are used to determine       the quadrant of the result.

##### Share on other sites
OK, just writing about the problem seems to have solved it.

I'm still tired, so if someone wants to explain why the original wasn't working, please feel free. However, my new algorithm is:

positionX = x1
positionY = y1
hypotenuse = sqrt( (x1- x2) ^ 2 + (y1 - y2) ^ 2)
deltaX = (x1 - x2) / hypotenuse
deltay = (y1 - y2) / hypotenuse
for each tick {
positionX += deltaX
positionY += deltaY
}

I guess what I'm not sure about at this point is simply the trig. Why couldn't I derive the angle from the arctan and simply apply it to the deltas?

Anyways, I hope this helps someone at some point :)

~Argo

##### Share on other sites
Quote:
 Original post by devin_papineauUse atan2().From my linux manual page:#include double atan2(double y, double x);DESCRIPTION The atan2() function calculates the arc tangent of the two vari- ables x and y. It is similar to calculating the arc tangent of y / x, except that the signs of both arguments are used to determine the quadrant of the result.

This is very useful information! Thanks much for posting it.

##### Share on other sites
Well as far as I can tell your algorithm should have worked assuming x1 and y1 was the position of the target and x2 and y2 was the position of the follower.

My guess as to why it wasn't working is that computers generally work in radians. 2PI = 360 degrees. So you probably need to convert theta to radians before passing it to the cos and sin functions.

##### Share on other sites
Quote:
 Original post by BoReDoM_IncWell as far as I can tell your algorithm should have worked assuming x1 and y1 was the position of the target and x2 and y2 was the position of the follower.My guess as to why it wasn't working is that computers generally work in radians. 2PI = 360 degrees. So you probably need to convert theta to radians before passing it to the cos and sin functions.

I'm not really sure. The original algorithm works in a traditional trig sense (so far as I know), but seems to have faultered because I was using the atan function in the [itex] library, instead of the atan2 function. Regardless, radians or degrees do not come into play at this point, as trig is trig and independent of radians or degrees.

To solve the error, all I did was rearrange my computations to exclude tangent from the problem. The idea hit me about 5 minutes after I posted when I remembered that Atari BASIC (my first programming language) didn't have tangent functions.

I guess I will chalk this one up to learning the libraries in more depth.

~Argo

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×