Using Curve2D to draw a Bezier Curve

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

I am trying to use a Curve2D to draw a Bezier Curve exactly like the documentation:

I found an example of manually calculating all of the points in a Bezier curve, but I’d like to use Curve2D properly so I can take advantage of the Tessellate function which as I understand it will give me a reduced number of points for areas of minimal curvature (the manual example is here: https://github.com/JohnMeadow1/GodotGeometryElements/tree/master/final/bezier_curve)

I am having trouble with adding points to the Curve2D - I believe the mistake is in the “in” and “out” arguments. Can someone help me figure out how I can just get nice control nodes and define a Bezier curve properly so I can tessellate (so I use the Godot functionality of Curve2D properly!)

I have a (very simple) project with 4 Position2D nodes added to it, and the following script:

Cubic Bezier Project (Simple)

extends Node2D

onready var curve = Curve2D.new()

func _ready():
	# https://docs.godotengine.org/en/3.2/tutorials/math/beziers_and_curves.html
	var control0 = $P1.position-$P0.position
	var control1 = $P3.position-$P0.position
	curve.add_point($P0.position,control0,control1)

func _process(delta):
	$LineP0P1.points = Array()
	$LineP2P3.points = Array()
	$LineP0P1.add_point($P0.position)
	$LineP0P1.add_point($P1.position)
	$LineP2P3.add_point($P2.position)
	$LineP2P3.add_point($P3.position)
	
func _draw():
	# Draw the handles
	draw_circle($P0.position,5.0,Color(1.0,0,0,1.0))
	draw_circle($P1.position,5.0,Color(0,1.0,0,1.0))
	draw_circle($P2.position,5.0,Color(0,1.0,0,1.0))
	draw_circle($P3.position,5.0,Color(1.0,0,0,1.0))
	var curve_pts = curve.tessellate()
	print(curve_pts)

[Edit] Where I’m going with this is having movable control and end points, which can interact with another node when it is hovering over them, and that light up when the mouse is near the bezier curve (so the user knows it is selectable). Any shove in the right direction is greatly appreciated. Thanks everyone!

:bust_in_silhouette: Reply From: pouing

You can define the Bezier curve by adding the two points P0 and P3, with “outer” param as (0,0) and “inner” param to be the control points:

 func _ready():
    # https://docs.godotengine.org/en/3.2/tutorials/math/beziers_and_curves.html
    var control0 = $P1.position-$P0.position
    var control1 = $P2.position-$P3.position
    curve.add_point($P0.position, Vector2.ZERO, control0)
    curve.add_point($P3.position, control1, Vector2.ZERO)

Also note the sign change for control1.