# Attempts to ease frequency and amplitude of a sine wave lead to unexpected results

4.2.2

So, I have a line 2D node setup as such:

The idea is its a sperm cell (Im making a game about reproduction ,which is pretty weird in concept but I’m having a neat time) anyway I put a sine wave on the sperm cell to make its movement look more realistic and I think it looks alright however I want it to react to your change in velocity and to do that I am using move_toward

here is the code:

``````extends Line2D

var distance = 2
var SPEED = 300
var FRICTION = 12
var velocity = Vector2.ZERO
var truePoints = []
var sinedPoints = []
var x = 0
var amplitude = 0
var amplitudeScale = 100
var freqScale = 1.6

# Export the curve to be editable in the inspector
@export var amplitude_curve: Curve

var pos = 0
for i in range(150):
var initial_pos = get_global_mouse_position() + Vector2(-distance, 0) * pos
truePoints.append(initial_pos)
sinedPoints.append(initial_pos)  # Initialize sinedPoints
pos += 1

func Constrain(point, anchor, distance):
return ((point - anchor).normalized() * distance) + anchor

func _process(delta):
# Movement code
x += 3 * delta
if (get_global_mouse_position() - points[0]).length() > 20:
velocity = (get_global_mouse_position() - points[0]).normalized() * SPEED * delta
else:
velocity.x = move_toward(velocity.x, 0, FRICTION * delta)
velocity.y = move_toward(velocity.y, 0, FRICTION * delta)
points[0] += velocity
\$Area2D.global_position = points[0]

if velocity.length() == 0:
amplitudeScale = move_toward(amplitudeScale,10,10 * delta)
freqScale = move_toward(freqScale,0.5,0.8 * delta)
elif velocity.length() > 0:
amplitudeScale = move_toward(amplitudeScale,100,10 * delta)
freqScale = move_toward(freqScale,2.6,0.8 * delta)

# Move all points along with mouse, do not yet apply
truePoints[0] = points[0]
sinedPoints[0] = points[0]
for pos in range(1, len(truePoints)):
truePoints[pos] = Constrain(truePoints[pos], truePoints[pos - 1], distance)

# Add sine wave with dynamic amplitude from the curve
for sinpos in range(0, len(sinedPoints)):
var angleVector = Vector2.ZERO
if sinpos > 0:
angleVector = (truePoints[sinpos] - truePoints[sinpos - 1]).normalized()

amplitude = amplitude_curve.sample(float(sinpos)/len(sinedPoints)) * amplitudeScale
sinedPoints[sinpos] = truePoints[sinpos] + Vector2(-angleVector.y, angleVector.x) * sin(sinpos * 0.02 + x * freqScale) * amplitude

# Update points with sinedPoints
for pos in range(1, len(points)):
#set_point_position(pos, Constrain(sinedPoints[pos], sinedPoints[pos - 1], distance))
set_point_position(pos, sinedPoints[pos])

``````

So here is the issue, it does not work. At all. In fact it does almost the opposite. Amplitude works fine but frequency goes nuts while its easing between however the final values, when reached look as expected. Why does it go so fast when its an inbetween value, I’m really at a loss for ideas. If my description of the issue is inadequate you can run it for yourself and I’ll attatch a video.

quite surprising on what you are trying to make, ngl

try change 100 and 10 to be 100.0 and 10.0, not directly tell godot it’s a float might messed it up
also printing each value to check what’s wrong is ideal to debug yourself

I did try printing the values, pretty much what you’d expect values themselves ease just fine. I do like the idea of triple checking floating point values that sounds promising. As for the idea, yeah I get it. It really just came from me messing around with constraints and thinking “i could totally make this worm look like sperm” so i did