What do math.sin and math.cos really do?

Hello! I wknder what do math.sin and math.cos actually do? I know what they are but i dont know how they are used in scripting.
Please explain to me, and thanks!

1 Like

It doesn’t have a “use” in scripting like keywords do. It’s there to provide mathematical functions developers can use to create varying things. There are an immeasurable number of things you can do with these two functions. For example, game design, sinusoidals can be used to create animations, interpolation styles (easing styles), derive angles, and derive side lengths.

You will learn about the uses when you’re taught trig in highschool.

3 Likes

They function in scripting the same way they function in mathematics; some code may use sine and cosine waves to do something specific (like creating circles).

local pi = math.pi
local x = math.cos(pi)

print(x) --> -1 because the X Coordinate for PI on the unit circle is -1

1 Like

they are the functions sin(x) and cos(x)

Can u explain to me why these are used to move a part from left to right and from right to left such as camera bobbing,… ?

Sine and Cosine create “waves” that go along a consistent pattern:


Camera bobbing uses these values to make the player’s camera move back and forth every frame (as you can see: the waves go up and down between a specific value, and these values are used to move the camera either left or right).

It’s more complex than this though, so I recommend looking more into sine and cosine before proceeding

4 Likes

The trigonometric sine and cosine functions (math.sin, math.cos) are functions that represent the ratio between the x and y values on the unit circle where sin(theta) = y/x. Let me explain visually.

File:Trigo-unitcircle-animation.gif

The diameter of the circle is 2, this is why sin and cos always return numbers between -1, and 1, because the origin of the circle is at zero. The line on the x axis is cosine, the line alternating sizes going up and down is sine.

sin would give the y of the angle (a), and cos would be the x. In Luau, the parameters are taken in radians, if you pass degrees like 135, you will definitely get unexpected results because they will be interpreted as radians. Use the math.rad(degree) function to translate degrees to radians.

A good use for these, as someone mentioned earlier in the thread, you can get any point on a circle (radius of 1). The points coords would be (math.cos(angle), math.sin(angle)).

1 Like

Is cos the length between x and the center and is sin the length between y and the center?

Yes, if cos(x) = 1 then x is a value that’s 1 forward from the center on the x axis.
Likewise, if sin(y) = 1 then y is a value that’s one above the center on the y axis.

cos = x axis
sin = y axis

Last question, can I put x as a number that is not 0 to 360?

Yes, but it’s just give the same result as its remainder from dividing by 360

So for example, math.sin(450) is equal to math.sin(90)

Thank you guys so much, now I understand math.cos and math.sin. My brain isnt braining anymore. (I actually understand them now)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.