Help in math.atan2

im a developer but i dont know much about math

can anyone explain math.atan2 in simple terms

I don’t know off the top of my head but there are some great teachers on YouTube.

Through-out your whole life, unless you’re a nerd and use it in every use case, you’ll need something to refresh your memory. Like old homework or a Video on YouTube. Don’t let someone tell you you’re an idiot because you don’t know something off the Top of your head.

2 Likes

Hi! This post is a little long read, so bear with me, but I’ll try to explain as best as I can without using math jargons.

math.atan2(y, x) is generally a shorthand for math.atan(y/x), and in math, atan is the inverse tangent. For example, if tan(45deg) = 1, then you can also say atan(1) = 45deg, you can think of them like opposite relationships. I will give a quick example on where it’s useful:

Let’s say you have two points in your ScreenGui, and you want to connect them with a line using a Frame like this:

To do this the only things you need to change is the position, size and rotation of the frame. The position and size is easy, for position you just put it in the middle of two points, and the size you can just use the handy-dandy distance formula! The rotation is the hard part here, how would we deal with that?

For this let’s draw a right triangle:

We have the x and y lengths, what we want to find is the θ symbol, which is our rotation here!

According to the formula tan(θ) = y/x,
we can instead reverse this relationship to get atan(y/x) = θ, which is atan2(y, x) = θ. The difference between them is atan2 will help you deal with the case when x=0 and you get division zero.

So using that we can finally make our perfect line! In here, I calculated the rotation here to be 14.664.

To briefly summarize, although it is not widely used and you definitely don’t need to know it, it is still quite important sometimes :slight_smile: , you can use it to make cool mouse trails if you want lol.

1 Like

Correction: atan2() gives you angles within -180° and +180° whereas atan() only gives you angles between -90° and +90°

5 Likes

i know how math.atan2 works now

1 Like