Difficulties implementing simple working harmonic motion

Godot Version

4.3.stable

Question

I am trying to move an element using the simple equation of the harmonic motion Acos(ωt).
The problem is that while the motion direction is right, changing the variables is not working as expected.

Specifically modifying the frequency should only increase the speed of the movement, but for some reason it is affecting the oscillation amplitude to.
For example with frequency 1.0 (as in the code below) the object is oscillating between 19 and -19, but setting the frequency to 2.0 is making it float between 11 and -11 (increasing the speed as expected).

I think the function is frame-rate independent, because summing up the deltas to calculate the time variable should make it dependent by the flow of real time passage. I tried to multiply the last calculation by delta and the frequency problem remains.

Here is the code, any suggestion?

# Total time passed (t).
var time: float = 0.0
# Amplitude of the oscillation (A).
var amplitude: float = 1.0
# Frequency of the oscillation (f).
var frequency: float = 1.0
# Pulse of the oscillation (ω).
var pulse: float = 2.0 * PI * frequency
# 3D direction of the oscillation.
var oscillation_direction := Vector3(0.0, 1.0, 0.0)

# Called every frame. Update time variable and apply Acos(ωt) to the desired direction.
func _process(delta: float) -> void:
	time += delta
	position += oscillation_direction * amplitude * cos(pulse * time)

Try removing the + from your last line:

position = oscillation_direction * amplitude * cos(pulse * time)
2 Likes