0 votes

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

func _process(delta):
    $LineP0P1.points = Array()
    $LineP2P3.points = Array()

func _draw():
    # Draw the handles
    var curve_pts = curve.tessellate()

[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!

in Engine by (17 points)

1 Answer

0 votes

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.

by (68 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.