Jump to content
  • Advertisement
Sign in to follow this  

How does tan work exactly?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I've been looking for a way to get a sprite to rotate and face my cursor, and I came across this:


You wan't a sprite to point towards the mouse pointer?

If that is the case you would probably need a algorithm to calculate it. It would depend on the location of the sprite as much as of the mouse pointer.

Let´s say the rotation 0 is straight up then that should be the case when it has the same X as mouse and Y is bigger.

The math here would be, assuming:

pos is position of sprite
mouse is position of mouse

DeltaY = mouse.Y - pos.Y
DeltaX = mouse.X - pos.X

rotation = tan^-1(DeltaY / DeltaX)


Now I understand that it works, but I'm wondering if one of you fine people can help me understand why it works. I'm not that great at  math, but I take every chance I can to improve. I need to understand the concept behind tan, and inverse tan, so I can apply it to any other problems I might face that would require it.


Thanks smile.png


Just to clarify, I know what tan is. Tan = opposite / adjacent. I know you feed tan the angle, and it returns o/a but why is o/a needed? What is it used for? and I don't know what inverse tan is at all.

Edited by littletray26

Share this post

Link to post
Share on other sites

ok, so tan(angle x) = opp / adj

In the image below, tan(x) = 2/1


tan^-1 is the inverse of tan, so if you have the angle, tan(angle) gives you the ratio, and if you have the ratio, tan^-1(ratio) gives you the angle.

In the image below, tan^-1(2/1) = ~1.107 rads (~63.5 deg)





So you have the setup in the image below. Subtracting the position of the image from the mouse gives you the length of the two sides. You then divide them to get the ratio, and pass that to tan^-1 to get the angle (in radians).




The sin and cos formulas work the same but are used when you have the hypotenuse and one of the shorter sides.


There's also sin^-1 and cos^-1, they are also known as arcsine, arccosine, and arctan (asin/acos/atan).

Share this post

Link to post
Share on other sites
It should be mentioned that the slope doesn't contain the information as to which side the mouse is on, right or left. So when you take the atan(slope) you'll get the same answer for two opposite sides. Then there is the issue of dividing by zero if the line is vertical. There is a function atan2 to which you pass the two coordinates before dividing them and that one does return the correct angle in all cases.

The paragraph above doesn't mean that I condone using angles for these kinds of things. You should use vectors (the way Aressera described), or complex numbers (which are almost the same thing but make it easier to do rotations).

Share this post

Link to post
Share on other sites

First you get some sun, then you get a tan :)

Tsk, tsk. Don't you know that puns about trigonometry are a sin? Why? 'Cos I said so!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!