I’m trying to use math.sin() to spin a wheel, problem is that it doesn’t go back to 0 and I don’t know why. It’s even harder because I still haven’t learned cos, sin and tan at school yet and there aren’t any documentations nor forums helping me understand how to use cos and sin in studio. I’ve understand cos and sin now but I have a problem of making it work in roblox, any help?
function module.SpinWheel(Wheel)
for i = 1,100 do
local f = math.sin(math.pi*((3.5/100)/100)*i)
Wheel.PrimaryPart.CFrame *= CFrame.new() * CFrame.Angles(0,math.rad(f*100), 0)
print(f)
task.wait()
end
end
the sin function is periodic, meaning after some time it repeats, this isn’t a bad thing on it’s own, but sin is wave:
sin stands for sine, so this is a sine wave. A sine wave might make your wheel go back and forth like it’s on a spring instead of spin like an actual wheel.
You don’t need sine at all the way you’re using it. You already have the angle you want the wheel to be rotated at.
function module.SpinWheel(Wheel)
-- will rotate 100 degrees after 100 frames (1.667 seconds at 60 fps)
for i = 1,100 do
Wheel.PrimaryPart.CFrame *= CFrame.new() * CFrame.Angles(0,math.rad(f), 0)
print(f)
task.wait()
end
end
If you want to use sinusoidals to spin a wheel, you can make use of the sinulsodal squared (range is 0 <= x <= 1). Each period is pi radians. You can divide this by 2 to get the range where it smoothly transitions from 0 to 1.
function module.SpinWheel(Wheel)
for i = 1, 100 do
local percent = math.pow(math.sin(math.pi * i / 200), 2)
Wheel.PrimaryPart.Orientation = Vector3.new(0, percent * 360, 0)
task.wait()
end
end
As a side note, you shouldn’t be using for loops with a yielding function to spin a wheel. You should be using a connection to the runservice heartbeat and using the elapsed time to calculate the new position of the wheel.
it works but not the way I want. It’s way too fast and just moves backwards halfway. All I want is to just make the wheel move like you would spin a wheel, it get’s fast and then gets slowly less. That’s why I used sine to try to make it work
If you want a wheel which starts at a maximum speed and slows to 0:
function module.SpinWheel(Wheel)
local speed = 1
for i = 1, 100 do
local theta = 1-math.sin(i/100 * math.pi/2)
Wheel.PrimaryPart.CFrame *= CFrame.Angles(0, speed*theta, 0)
print(speed*theta)
task.wait()
end
end
We want to restrict sin to between 0 and pi/2 (90 degrees)
This way sin will start at 0 and end at 1
So 1-sin will start at 1 and end at 0
So the change in angle will start at speed x 1 and end at speed x 0
i/100 starts at ~0 (1/100) and ends at 1 (100/100)
So i/100 * math.pi/2 starts at 0 and ends at math.pi/2
1-math.sin(i/100 * math.pi/2) is similar to math.cos((100-i)/100 * math.pi/2)