Sine waves problems

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:


Red is sin(x), Blue is sin(0.6x)

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?

Thanks in advance! :grin:

2 Likes

are both waves a whole different formula? or is it the same equation with different values?

1 Like

they are both sine waves, just have different terms.

so instead of it being like this:

you want it like this la is it? :

image

2 Likes

You could try doing this instead.

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.

2 Likes

Yea, that’s exactly what i’m looking for.

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:

sin(ax+b)

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.

1 Like

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

2 Likes

Thank you so much!

The problem was instead of changing the sine wave to be smooth on a fixed x-interval, change the x interval itself to the wanted speed.

2 Likes

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