8
\$\begingroup\$

I'm new to game development in some phases and I want to understand everything I do (at least most of it) so I've started developing in MonoGames framework (XNA) and I saw a function like this.

rocket.angle = System.Math.Atan2(rocket.rocketdirection.X , -rocket.rocketdirection.Y);

I didn't understand this function (I was following a tutorial) and dove deeper into understanding sin, cos and tan. Now I have some understanding over it but one thing is unclear to me.

enter image description here

This was shown to me in a way to show how you calculate sin, cos and tan and I didn't understand it.

  • Why is the circle represented as 1,1 and -1,-1?
  • And why is Y = sin and X = cos?
  • And through this tan = cos/sin?

Can somebody explain this picture in a better way, in a way that is relatively easy to understand?

\$\endgroup\$
7
  • 5
    \$\begingroup\$ This is not a game development question. You should ask this on math.stackexchange.com \$\endgroup\$ Commented Feb 12, 2017 at 20:41
  • \$\begingroup\$ Is there a way to transfer this question ? \$\endgroup\$
    – Bojje
    Commented Feb 12, 2017 at 21:13
  • 4
    \$\begingroup\$ @StephaneHockenhull I believe that math people would get too much carried away and make the answer to this overly complicated. I think here it's better to have an answer from a game dev perspective. \$\endgroup\$
    – Vaillancourt
    Commented Feb 13, 2017 at 2:14
  • 1
    \$\begingroup\$ The short answer is "convention" ;) Unless otherwise specified, we generally agree to measure angles in the xy plane by going counter-clockwise from the positive x axis. If we had a different convention, say we wanted to measure clockwise from the positive y axis (like a clock) then we'd say x = sin(angle) and y = cos(theta) (and in fact, sometimes in gamedev that's exactly what we'll do, if we need an angle to start from a different axis for a particular scenario) \$\endgroup\$
    – DMGregory
    Commented Feb 13, 2017 at 4:39
  • \$\begingroup\$ You use atan2 incorrectly, the parameters are in y, x order. \$\endgroup\$
    – Bálint
    Commented Feb 13, 2017 at 8:52

2 Answers 2

20
\$\begingroup\$

I will try to answer briefly.

  1. Why is the circle represented as 1,1 and -1,1?

This thing is usually called Unit Circle, because its radius equals to 1. How it is relative to trigonometry you're trying to solve? Let's see.

  1. Why is Y = sin and X = cos?

Well, let's look at the picture I've made five minutes ago (sorry, it's not the best my work). http://prnt.sc/e7we2d

You can mention at the picture the point B with coordinates \$X, Y\$ and the constructed triangle \$ABC\$. First of all, this is the right triangle. So we can continue our mind work.

The definition of sine states: \$sin(φ)\$ is the ratio of the length of the opposite to angle \$φ\$ side and the length of the hypotenuse. For our example \$sin(\angle BAC) = \frac{BC}{AB}\$ because \$BC\$ is opposite to \$\angle BAC\$ and \$AB\$ is simply hypotenuse.

As you can see,

a) \$BC\$ equates to \$y\$.

b) \$AB\$ equals to 1 (because it is a Unit Circle and \$AB\$ is a radius).

c) \$\frac{BC}{AB} = \frac{y}{1} = y\$, so \$sin(\angle BAC) = y\$.

The same logic easily can be applied for cos but this time we will talk not about opposite side to the angle, but adjacent side. In other words, \$cos(φ)\$ is a ratio of the length of the adjacent to angle \$φ\$ side to the length of the hypotenuse.

In the same way we see that \$cos(\angle BAC) = \frac{AC}{AB} = \frac{x}{1} = x\$.

  1. How we can see that \$tan = \frac{sin}{cos}\$?

Well, it is the easiest part. The \$tan(φ)\$ of the angle \$φ\$ is the ratio of the length of the opposite to the angle \$φ\$ side and the length of the adjacent to the angle \$φ\$ side.

For our example \$tan(\angle BAC) = \frac{BC}{AC} = \frac{y}{x}\$. I hope this is a clear starting explanation.

Further reading about the unit circle

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Your explanation hit it on the nail; however, the mixed usage of x as the angle and as the X position is confusing. I'd recommend you use theta or phi (respectively θ, φ). This prevents confusion between angular coordinates and euclidean coordinates. \$\endgroup\$ Commented Feb 13, 2017 at 23:12
  • \$\begingroup\$ @KareemElashmawy you're right, thank you very much! \$\endgroup\$ Commented Feb 14, 2017 at 8:31
11
\$\begingroup\$

When you ask Why is sin = Y and cosine = X? I read your question as Why is the function that gives me the Y axis called "sin" and why is the function that gives me the X axis called "cosine"?, so I'll start by answering that...

At its heart, it is a naming convention. If you have a unit circle with the common trigonometric functions...

Unit circle with sin, sec, tan, cos, csc and cot

... you'll see that all the function that go to the Y axis are "co" something (cosine, cosecant, cotangent).

Looking at the same unit circle you will find that cos(θ) and sin(θ) will give the X and Y coordinates respectively for the point on the unit circle that is at θ angle from the X axis.

These functions where historically defined in terms of circles, in fact they come from the Sanskrit Jyā (sine) and koti-jyā (cosine), which where the names of those functions used by Indian mathematicians circa 500 AD. They were later translated to Latin, and from there to English. But, you don't care about that, you care about making games (or solving math problems, or whatever)!


Why is the circle represented as 1,1 and -1,-1?

It is a unit circle, that means that it is a circle with radius = 1, and thus it reaches down to -1 and up to 1. The reason why they use radius = 1 is because this makes it easy to scale the functions to a different radius.

And why is Y = sin and X = cos?

You may wonder why they aren't the other way around. Well, they had to be one way or the other, and whichever it was you would be asking the same... so the answer is: for historic reasons.

And through this tan = cos/sin?

No, this is WRONG. Actually tan(x) = sin(x) / cos(x).

I hope to demostrate this in an intuitive manner. To start let's take the 0BC triangle from the image above:

0BC triangle

I have added cos in the triangle for ease of reference (sec was removed because we don't need it).

We can see this triangle is a Right Triangle, and also that it contains another smaller Right Triangle. Furthermore, both triangles have one angle with the value θ, therfore they are Similar Triangles. Which means that we can map one triangle over the other by only scaling, rotating and translating (moving). This can be seen in the following animation.

Similar Triangles animation

In the animation we used rotation and scaling, neither of these transformations changes the angles of the triangles.

Pay attention to what segment was mapped over what segment during the transformation. In particular:

  • The magenta segment (tan(θ)) was mapped over the black segment (1).
  • The olive segment (sin(θ)) was mapped over the green segment (cos(θ))

Now, let's see what does that mean.

First tan(θ) went to 1. Meaning that

scaling_factor * tan(θ) = 1
=>
scaling_factor = 1 / tan(θ)

So the scaling factor is 1 / tan(θ).

Second, sin(θ) went to cos(θ). Meaning that:

scaling_factor * sin(θ) = cos(θ)

By replacing the scaling factor we have:

1 / tan(θ) * sin(θ) = cos(θ)
=>
sin(θ) / tan(θ) = cos(θ)
=>
sin(θ) = cos(θ) * tan(θ)
=>
sin(θ) / cos(θ) = tan(θ)

And that's what we were set to demostrate.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .