Hello devs, I’ve recently been messing around with sine waves and ran into a bit of a problem. I’ve been trying to create a bobbing effect that can smoothly transition from one speed to another, but I can’t figure out exactly how
Lets say im trying to create a buoy bounce up and down using:
--psuedocode
local Frequency = 1
local Phase = 0
Bouy.Position.Y= math.sin(time() * frequency + Phase )
To speed it up, im lerping Frequency from one number to another, but this causes jittering as the sine wave expands from the origin, meaning the Y value would appear to slow down or even reverse. For example:
To swtich from Red to blue at tick() = 2, the Y value would switch from going down to going up.
My best guess as to solve this problem is to set the phase of sinewave to some number so that the point of expansion is on tick(), but that might just cause the motion to freeze until it reaches its target frequency, I don’t know. How would you smoothly translate from one frequency to another?
local phase = 0
local sine = 0
local frequency = 2
RunService.RenderStepped:Connect(function(delta)
phase += delta * frequency
sine = math.sin(phase)
end)
There is of course a better way to do this but I hope this makes sense.
You increment the frequency slowly to make it go faster, this should in theory prevent weird jittery effects and whatnot.
This won’t help because there is nothing to compensate for the change in frequency. Just to be clear, the phase is an offset in a sine wave and also the b in:
The idea behind it is that you increase the offset every step to smoothly interpolate from slow to fast.
If you interpolate from a low frequency to a high frequency, the 2 individual sine waves will mix.
I suggest you try this code I provided on a part first and increase the offset per tick to see what happens.
I’ve had this problem before and you have to add a single offset to the phase, you can figure it out using some algebra and the two wave functions’ frequencies