Gamedev Tutorial: Trigonometry Fundamentals – Sine and Cosine

Gamedev Tutorial: Trigonometry Fundamentals – Sine and Cosine

Right here is the Chinese language translation of this submit.



Trigonometry is a really important constructing block to an enormous portion of recreation math. That’s why I’ve chosen this subject for the primary tutorial of my new Gamedev Tutorials collection. Having a stable understanding of fundamental of trigonometry can go a good distance for recreation improvement. It’s used extensively in recreation drawback fixing.

On this tutorial you’ll study:

  • A geometrical interpretation of two fundamental trigonometric capabilities: sine & cosine.
  • The comparability of two totally different angle models: levels & radians.
  • Some fundamental properties of sine & cosine.
  • The right way to transfer and prepare issues in a round style:
  • The right way to transfer issues in a spiral style:
  • The right way to create easy harmonic movement:
  • The right way to create damped spring movement:
  • The right way to create pendulum movement:
  • The right way to generate hovering movement:

Geometric Interpretation of Sine & Cosine

Let’s take a look at the unit circle, a circle with a radius of 1 centered on the origin.

Now decide a degree on the circle. The road phase between this level and the origin varieties an angle (theta) between it and the X axis (constructive X path).

What’s proven right here is definitely a strategy to geometrically specific the two fundamental trigonometric capabilities: (sine of theta) and (cosine of theta). The coordinates of this level are precisely .

So, to recap, the 2 trigonometry operate and are, respectively, the Y and X coordinates of a degree on the unit circle, the place is the angle from the X axis (constructive X path) to the road phase between the purpose and the origin.

Since and are capabilities, the correct notation ought to embody parenthesis across the enter: and , however many individuals and literature simply ignore the parenthesis and write them as and .

and are capabilities that take one single enter (an angle) and output a single worth between -1 and 1. If you concentrate on it, this output vary is smart: the X and Y coordinates of a degree on the of the unit circle can by no means go outdoors of the vary.

If the angle will increase at a continuing charge, we will plot the worth of the purpose’s X and Y coordinates individually over time.

If we examine the plots side-by-side within the type of angle-vs-value, we will see they’re the identical periodic curve in a wave-like form, however offset by a fourth from one another. 

The interval of those capabilities is , so offers the identical worth as . This is smart, as a result of rotating an additional previous would carry us again to the identical angle.

Levels v.s. Radians

The angle handed into the trigonometric capabilities could be in two totally different models: levels and radians. Most individuals are aware of levels and its upper-little-circle notation. For example, the best angle (90 levels) is written as . Do beware that is just not the identical as . If the diploma notation is just not current, the angle’s unit is definitely thought to be radians.

is equal to (pi) radians, the place is the well-known mathematical fixed, “the ratio of a circle’s circumference to its diameter”, roughly equal to three.14. Therefore, an angle of 1 radian is roughly , virtually . As a sanity verify, coming into on an engineering calculator (with angle models set to radians) will give us roughly , which is certainly near .

Listed here are some widespread degree-to-radian mappings:


In Unity, the and capabilities are known as by way of Mathf.Sin and Mathf.Cos, respectively. Beware that these capabilities take enter in radians, so if you wish to compute , don’t write:

// that is really cosine of 45 radians!
float cos45Deg = Mathf.Cos(45.0f);

45 radians is about . A full revolution of is equal to no rotation in any respect. divided by offers us a the rest of , which is the equal angle of and is totally different from .

To compute , write this as a substitute:

// covert to radians
float cos45Deg = Mathf.Cos(45.0f * Mathf.PI / 180.0f);

Or use constants that assist convert between levels and radians.

float cos45Deg = Mathf.Cos(45.0f * Mathf.Deg2Rad);

In instruments just like the Unity editor, expressing angles in levels is extra person pleasant, as a result of most individuals can instantly image what a angle appears to be like like. Nonetheless, within the context of math and programming, many individuals, myself included, choose sticking with radians.

One helpful factor about radians is that it trivializes calculating arc size from a given radius and angle. Let’s say we need to calculate the size of an arc of , or radians, from a circle of radius 2.

If computed utilizing levels, first the entire circumference is calculated utilizing the components , after which it’s multiplied by the ratio of out of :


When utilizing radians, the arc size components is solely radius instances angle in radians:


The circle’s circumference components agrees properly with the arc size components in radians. Since one full circle is mainly an arc with an angle of radians, the size of such arc is , precisely the identical because the circle’s circumference components.

Fundamental Properties of Sine & Cosine

Now let’s take a look at some fundamental properties of sine & cosine that may turn out to be useful in future mathematical derivations.

Since are coordinates of a degree on the unit circle, the purpose’s distance from the origin is all the time 1, whatever the angle . The Pythagorean theorem states that the space of the purpose from the origin is . From there we will get this id (equation that’s all the time true):


The squares of and are written as and , respectively. Folks write them that approach most likely as a result of they’re too lazy to jot down and .

Recall the side-by-side comparability of the sine and cosine plots.

You may see that the cosine curve mainly is the sine curve shifted to the left by , or radians. This implies we will get these identities that convert between the 2:


Shifting in Circles & Spirals

Now that we’ve seen that are 2D coordinates of a degree on the unit circle, we will begin enjoying with some fundamental round movement in Unity.

The code under strikes an object round a circle at a continuing charge: = 
  new Vector3
    Radius * Mathf.Cos(Price * Time.time), 
    Radius * Mathf.Sin(Price * Time.time), 

The code under strikes 12 objects round a circle at a continuing charge, and the objects are equally spaced out across the circle:

float baseAngle = Price * Time.time + angleOffset;
for (int i = zero; i < 12; ++i)

Combining round movement with motion within the Z path, we will create a spiral movement in 3D: = 
  new Vector3
    Radius * Mathf.Cos(Price * Time.time),
    Radius * Mathf.Sin(Price * Time.time),
    ZSpeed * Time.time

Easy Harmonic Movement (S.H.M.)

We’ve seen this plot of cosine versus angle:

What if we plug cosine into the X coordinate of an object?

float x = Mathf.Cos(Price * Time.time); = Vector3(x, zero.0f, zero.0f);

That is what we get:

This sort of oscillating movement that matches a sine-shaped curve, a.okay.a. sinusoid, is named easy harmonic movement, or S.H.M.

Since begins at zero, the thing’s X coordinate begins at . If we use , the X coordinate would begin at .

The enter angle handed in to the sine and cosine capabilities are known as the part. Sometimes, if the part handed in is a continuing a number of of time, many individuals write it as , the place (omega) is known as the angular frequency (in radians per second), and is the time. For instance, would produce a easy harmonic movement that oscillates one full cycle each second.

What if we scale this movement by an exponentially reducing issue?

float s = Mathf.Pow(zero.5f, Decay * Time.time);
float x = Mathf.Cos(Price * Time.time); = Vector3(s * x, zero.0f, zero.0f);

Now the thing strikes in a damped spring movement:

Pendulum Movement

As an alternative of plugging a sinusoid into an object’s X coordinate, what if we plug it into the angle for the round movement instance above?

float baseAngle = 1.5f * Mathf.PI; // 270 levels
float halfAngleRange = zero.25f * mathf.PI; // 45 levels
float c = Mathf.Cos(Price * Time.time);
float angle = halfAngleRange * c + baseAngle; = 
  new Vector3
    Radius * Mathf.Cos(angle), 
    Radius * Mathf.Sin(angle), 

The article now strikes in a pendulum movement:

We will deal with this because the round movement’s angle being in a easy harmonic movement.

Hovering Movement

As a bonus instance, right here is UFO Bunny, a personality from Boing Package, my bouncy VFX extension for Unity.

We will apply staggered easy harmonic movement to her X, Y, and Z coordinates individually.

Vector3 hover = 
  new Vector3
    RadiusX * Mathf.Sin(RateX * Time.time + OffsetX), 
    RadiusY * Mathf.Sin(RateY * Time.time + OffsetY), 
    RadiusZ * Mathf.Sin(RateZ * Time.time + OffsetZ)
  ); = basePosition + hover;

And this creates a hovering movement.

And the hover offset can be utilized to compute a tilt rotation. That is past the scope of this tutorial, so I’ll simply go away the code and outcomes right here.

obj.rework.rotation = 
  * Quaternion.FromToRotation
      -hover + three.0f * Vector3.up


That’s it!

We've got seen how  and could be geometrically outlined as coordinates of a degree on the unit circle.

Additionally, we've seen the distinction between the 2 angle models: levels and radians.

Lastly, we now know how one can strikes issues in circles and spirals, in addition to oscillating issues in easy harmonic movement, damped spring movement, pendulum movement, and hove movement.

I hope this tutorial has helped you get a greater understanding of the two fundamental trigonometric capabilities: sine & cosine.

Within the subsequent tutorial, I'll introduce one extra fundamental trigonometric operate: tangent, in addition to speak about extra purposes of all these three capabilities.

Till then!

For those who’ve loved this tutorial and wish to see extra, please contemplate supporting me on Patreon. By doing so, you too can get updates on future tutorials. Thanks!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.