How can I offset baked points in curve 2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Suleymanov

I’m trying to put trees along the line but with an offset to the left and right. I figured out to get the positions of the baked points and instantiate trees on them. But how would I properly offset them?

:bust_in_silhouette: Reply From: Zylann

You need to get the “normal” of the curve at the position you are sampling, somehow.

Maybe sample_baked_with_rotation can give you that info?

It gives a 2D transform which has an origin, x, and y axes. y will be parallel to the curve, and x will be perpendicular (the normal). So if you need to offset your trees, use the normal axis and multiply it by the amount you want the tree offset from.

Animated example:

extends Path2D

func _process(delta):
	queue_redraw()

func _draw():
	var count := 100
	var offset := 20.0 * (0.5 + 0.5 * sin(Time.get_ticks_msec() / 200.0))
	var length := curve.get_baked_length()
	for i in count:
		var t := length * float(i) / count
		var trans = curve.sample_baked_with_rotation(t)
		draw_circle(trans.origin + trans.x * offset * sin(t), 5.0, Color(0,1,0))