Trying to Graph a sin wave with Node2D

Godot Version



So I am trying to graph a sin wave, however, whenever I run my project the line starts but abruptly cuts off. I have tried increasing the number of points I am using, but it doesn’t seem to affect anything. making the node bigger simply enlarges the whole thing, and does not show more of the line.


extends Line2D

var amplitude = 25
var frequency = .2
var varPoints = 1500

func _draw():
	var step = 2 * 3.141592 / varPoints
	var prev_point = Vector2(0, amplitude * sin(0))

	for i in range(1, varPoints):
		var x = i * step
		var y = amplitude * sin(x * frequency)
		var current_point = Vector2(x, y)
		draw_line(prev_point, current_point, Color(1, 1, 1), 2)
		prev_point = current_point

You do realize that you’re telling it to only ever draw one cycle, right?

This means the first point is at zero and the last point is at tau, which is a single sine cycle.
If you want more cycles, just multiply by the number of cycles you want. Possibly 1/frequency in your case.

1 Like

Thanks, I have been scratching my head for a while. I am a bit rusty on the formulas and just looked them up. I thought it was an issue with the node it’s self.

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