Can someone help me understand math.sin and math.cos from a person that doesn't know what it is?

All I know is that math.sin(90) returns 1 for some reason

math.sin and math.cos are both functions used in mathematics and are from a topic known as trigonometry - these functions take in an angle as the input and return a value between -1 and 1.

To understand what these functions actually do, lets consider a circle with a radius of 1 unit as such:

If we drew a line from the origin to any point on that circle, the length would be one. Now, lets choose a random radi and draw a triangle from it.

Lets give these lines some values. First, we know the radius has a length of 1, so we’ll label that as 1. Next, let’s call our green line A and the purple line B. At the moment, we have a triangle of angle 45 degrees, and A and B are the same size. Lets move our radius now so that it parallel to the x-axis and see what happens.

Now our triangle looks less like a triangle and more like a straight line. As we aren’t moving upwards at any point, our purple line (B) must be 0, and our green line (A) must be equal to the radius, or otherwise 1. Lets change the radi one last time, with this one being parallel to our y axis.

Now our angle is 90 degrees, our triangle is once again more like a line, with our A line being equal to 0, and our B line being equal to the radius (or otherwise 1). Lets use all this information and see what we can deduce.

As our radius rotates around the circle, the values of A and B change based on the angle our triangle makes. For example, when we have 0 degrees, A = 1 and B = 0. At 45 degrees, A and B are equal, and at 90 degrees, A = 0 and B = 1. From this, we will say our lengths A and B are some function of the angle - lets go back to our original triangle and label the sides with some new function names.

Here, we’ve labelled our angle as a sign known as θ (pronounced theta). Our horizontal length is known as cos and takes the parameter θ (typically written as cos(θ) ), and our vertical length is known as sin(θ).

Lets make a graph representing the range of values sin and cos can take, based on the angle they make (which will be between 0 and 360 degrees)

When we plot our sin graph we get this:

And when we plot our cos graph we get this:

They look pretty similar, and that’s because they’re both what we refer to as sinusoidal waves, which are extremely important in all things science and maths. The main difference is that our cos graph does not pass through the origin, whereas the sin graph does; cos is also a symmetrical graph.

Let’s wrap up by stating one of the most important features of sin and cos in reference to the original triangle. Using pythagoras’ theorem, we can derive an identity which is extremely important to the field of trigonometry. As we know a² + b² = c², we can apply this to our triangle where a = cos θ, b = sin θ and c = 1

(cos θ)² + (sin θ)² = (1)²
cos² θ + sin² θ = 1


Thank you, but how would I go about using this in roblox studio, for example if I wanted to make the head look toward the cameras lookvector, how would I do that?

1 Like

You would need to match the head match the cameras rotation matrix or do some manipulation of the attachments, however I believe that may be a bit complicated - I’d recommend finding some sort of tutorial on youtube.

1 Like

use CFrame.LookAt
no complicated math needed

With linear algebra we can use a type of structure known as a matrix, to perform rotation transformations on vectors, which in this case is a list of three real numbers. To rotate the vector to any given direction, you will need three (or two) angles of rotation, that you can probably just get by casting a threedimensional line to the object that you want to look at. This is basically how rotation is defined in 3D engines.

In the image below, three matrices, each representing rotation around one axis, x, y and z, are multiplied to get one 3x3 matrix that represents the combined rotation as a function of all three angles.

Image from wikipedia.

In roblox, you obviously don’t need to do these algorithms, as they are already built in to the API.

Brief explanation I know, but let me know if you have any questions.

1 Like