Sign in to follow this  

How does tan work exactly?

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this