How would you use math.cos() and math.sin() in a script? Whats the point of it?
Math.cos() and math.sin() are type of math functions which are commonly used in Trigonometry. For example, if you would like to find an angle or side length of a right-angled triangle, you would use this function with a specific formula.
There also is math.tan() which is related in Trigonometry.
If I am wrong, may someone please correct me. Hope you find what you need here
As @urgentnotice has said, there are formulas like sine = adjacent over hypotenuse.
math.cos and math.sin are the sine and cosines in math that you learn in trigonometry which in my country is required in 10th grade. I only know the introductory cuz i just finished 10th grade.
Basically you insert the ratio of the corresponding side lengths of a triangle that each function uses,
for example cosine delta would be the ratio between the adjacent side over the hypotenuse
and sine delta would be adjacent over hypotenuse
these are the sides in a triangle
This is just the most basic stuff apparently later on you can create graphs based of these relations
Also each angle has their own pair of opposite and adjacent sides
The image here only shows the set corresponding to angle Q
Well actually the hypotenuse will remain the same my bad but each angle will have their own pair of opposite and adjacent angles
Can you make an example script using math.sin() or math.cos()
I never really understood the in-depths of it. But I can definitely tell you how to use it.
In games, sin and cos can be used to rotate things, or just get a wave.
This video here shows it really well, and here is a basic equation for something such as camera bobbing. The basic equation of time * speed and multiplied by amplitude works for both cosine and sine, that’s the only equation you will really ever need. As I said, watch the video below.
-- Deltatime comes from the heartbeat connection of RunService.
local time += deltaTime
local CameraY = math.sin(time * speed) * amplitude
Enter the angle in radians, to get your value.
Usually, trigonometric functions are used in calculations where triangles are involved. For example, you want rotate a part around the origin.
local pos = part.Position
local angle = math.rad(30)
part.Position = Vector3.new(pos.X * math.cos(angle) - pos.Z * math.sin(angle), pos.Y, pos.Z * math.cos(angle) + pos.X * math.sin(angle))
As @LxckyDev has explained, you would use sine and cosine for specific equations. For example, you would use sine for finding the opposite side using the hypotenuse and an angle.
For example: Length * math.sin(angle)
I know you probably don’t want to, but learning math for game development is a must.
This video is good at explaining everything you need to know.
yeah mb i mixed it up you would insert the angle to get the ratio between the side lengths but that isnt really applicable in game. Graphs as LuckyEcho showed would be much more applicable though I have no knowledge of them
There’s many applications to be had with sin
and cos
functions in game developments. They can look like a rather abstract topic, but the properties of these functions have many uses.
For example, here’s a ball rotating around another with a radius:
Code
local cos = math.cos
local sin = math.sin
local angle = 0
local radius = 10
while true do
local circlePoint = Vector3.new(radius * cos(angle), 0, radius * sin(angle))
local sunPos = workspace.Sun.Position
local finalPos = sunPos + circlePoint
workspace.Mars.Position = finalPos
angle = angle + math.rad(5)
task.wait(0.05)
end
Because a sine wave oscillates over time, we can use that to create an oscillating motion
Code
local part = script.Parent
local position = part.Position
while true do
part.Position = Vector3.new(
position.X,
math.sin(time()) + 5, -- offset added
position.Z
)
task.wait()
end
Additionally, almost anywhere you see calculations dealing with angles, you can expect trigonometry to be present. I’d say it’s a very important area of knowledge to have as a game developer.