So as the name suggests, I have trouble finding where Sin, Cos, and Tan are usable. I have taken trig class and I know the unit circle, the trignometric functions and what they do. I just can’t find where they’d be applicable and how I’d use them in code.
sin, cos can be used with an elapsed time variable to make a part move in a sine/cosine wave pattern (tick()
would also work)
Example code: (this will move the part in a circle)
local RunService = game:GetService("RunService")
local Sine = 0
local Part = script.Parent
local PartCFrame = Part.CFrame
RunService.Heartbeat:Connect(function(DeltaTime: number)
Sine += DeltaTime
Part.CFrame = PartCFrame * CFrame.new(math.sin(Sine) * 5, 0, math.cos(Sine) * 5)
end)
So *5 really just amplifies the movement, it has no other use, correct?
math.sin() returns a value between -1 and 1, so therefore multiplication by 5 just makes it move by 5 studs. I can also think of an example with the Rotation property of GUIs, you can use it to smoothly make it turn left to right.
Is there a specific reason why math.sin() comes before math.cos() in the new cframe?
Late reply, but ill gave a few:
Moving objects in a wave pattern is one of them for sine/cosine:
local amplitude = 5
local frequency = 2 * math.pi / 60
for i = 1, 360 do
local y = amplitude * math.sin(frequency * i)
object.Position = Vector3.new(object.Position.X, y, object.Position.Z)
task.wait()
end
For tangent, you can calculate the horizontal field of view of a camera:
function calculateHorizontalFOV(verticalFOV, aspectRatio)
local verticalFOVInRadians = math.rad(verticalFOV)
local tanOfHalfVerticalFOV = math.tan(verticalFOVInRadians / 2)
local tanOfHalfHorizontalFOV = tanOfHalfVerticalFOV * aspectRatio
local horizontalFOV = 2 * math.deg(math.atan(tanOfHalfHorizontalFOV))
return horizontalFOV
end
local verticalFOV = 60
local aspectRatio = 16/9
local horizontalFOV = calculateHorizontalFOV(verticalFOV, aspectRatio)
print(horizontalFOV)
Using the inverse tangent can help find the angle between two points:
function calculateAngle(x1, y1, x2, y2)
local deltaY = y2 - y1
local deltaX = x2 - x1
local angle = math.atan(deltaY / deltaX)
local angleInDegrees = math.deg(angle)
return angleInDegrees
end
local x1, y1 = 1, 2
local x2, y2 = 4, 6
local angle = calculateAngle(x1, y1, x2, y2)
print(angle)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.