Index · Object → RefCounted → Resource → Curve2D

Curve2D

Describes a Bézier curve in 2D space.

Class Description

This class describes a Bézier curve in 2D space. It is mainly used to give a shape to a Path2D, but can be manually sampled for other purposes.

It keeps a cache of precalculated points along the curve, to speed up further calculations.

Properties

var bake_interval: float = 5.0

The distance in pixels between two adjacent cached points. Changing it forces the cache to be recomputed the next time the get_baked_points() or get_baked_length() function is called. The smaller the distance, the more points in the cache and the more memory it will consume, so use with care.

var point_count: int = 0

The number of points describing the curve.

var point_{index}/in: Vector2 = Vector2(0, 0)

The position of the control point leading to the vertex at index.

Note: index is a value in the 0 .. point_count - 1 range.

var point_{index}/out: Vector2 = Vector2(0, 0)

The position of the control point leading out of the vertex at index.

Note: index is a value in the 0 .. point_count - 1 range.

var point_{index}/position: Vector2 = Vector2(0, 0)

The position of for the vertex at index.

Note: index is a value in the 0 .. point_count - 1 range.

Inherited from Resource (4)

Methods

func add_point(position: Vector2, in: Vector2 = Vector2(0, 0), out: Vector2 = Vector2(0, 0), index: int = -1) -> void

Adds a point with the specified position relative to the curve's own position, with control points in and out. Appends the new point at the end of the point list.

If index is given, the new point is inserted before the existing point identified by index index. Every existing point starting from index is shifted further down the list of points. The index must be greater than or equal to 0 and must not exceed the number of existing points in the line. See point_count.

func clear_points() -> void

Removes all points from the curve.

func get_baked_length() -> float

Returns the total length of the curve, based on the cached points. Given enough density (see bake_interval), it should be approximate enough.

func get_baked_points() -> PackedVector2Array

Returns the cache of points as a PackedVector2Array.

func get_closest_offset(to_point: Vector2) -> float

Returns the closest offset to to_point. This offset is meant to be used in sample_baked().

to_point must be in this curve's local space.

func get_closest_point(to_point: Vector2) -> Vector2

Returns the closest point on baked segments (in curve's local space) to to_point.

to_point must be in this curve's local space.

func get_point_in(idx: int) -> Vector2

Returns the position of the control point leading to the vertex idx. The returned position is relative to the vertex idx. If the index is out of bounds, the function sends an error to the console, and returns (0, 0).

func get_point_out(idx: int) -> Vector2

Returns the position of the control point leading out of the vertex idx. The returned position is relative to the vertex idx. If the index is out of bounds, the function sends an error to the console, and returns (0, 0).

func get_point_position(idx: int) -> Vector2

Returns the position of the vertex idx. If the index is out of bounds, the function sends an error to the console, and returns (0, 0).

func remove_point(idx: int) -> void

Deletes the point idx from the curve. Sends an error to the console if idx is out of bounds.

func sample(idx: int, t: float) -> Vector2

Returns the position between the vertex idx and the vertex idx + 1, where t controls if the point is the first vertex (t = 0.0), the last vertex (t = 1.0), or in between. Values of t outside the range (0.0 <= t <= 1.0) give strange, but predictable results.

If idx is out of bounds it is truncated to the first or last vertex, and t is ignored. If the curve has no points, the function sends an error to the console, and returns (0, 0).

func sample_baked(offset: float = 0.0, cubic: bool = false) -> Vector2

Returns a point within the curve at position offset, where offset is measured as a pixel distance along the curve.

To do that, it finds the two cached points where the offset lies between, then interpolates the values. This interpolation is cubic if cubic is set to true, or linear if set to false.

Cubic interpolation tends to follow the curves better, but linear is faster (and often, precise enough).

func sample_baked_with_rotation(offset: float = 0.0, cubic: bool = false) -> Transform2D

Similar to sample_baked(), but returns Transform2D that includes a rotation along the curve, with Transform2D.origin as the point position and the Transform2D.x vector pointing in the direction of the path at that point. Returns an empty transform if the length of the curve is 0.

var baked = curve.sample_baked_with_rotation(offset)
# The returned Transform2D can be set directly.
transform = baked
# You can also read the origin and rotation separately from the returned Transform2D.
position = baked.get_origin()
rotation = baked.get_rotation()
func samplef(fofs: float) -> Vector2

Returns the position at the vertex fofs. It calls sample() using the integer part of fofs as idx, and its fractional part as t.

func set_point_in(idx: int, position: Vector2) -> void

Sets the position of the control point leading to the vertex idx. If the index is out of bounds, the function sends an error to the console. The position is relative to the vertex.

func set_point_out(idx: int, position: Vector2) -> void

Sets the position of the control point leading out of the vertex idx. If the index is out of bounds, the function sends an error to the console. The position is relative to the vertex.

func set_point_position(idx: int, position: Vector2) -> void

Sets the position for the vertex idx. If the index is out of bounds, the function sends an error to the console.

func tessellate(max_stages: int = 5, tolerance_degrees: float = 4) -> PackedVector2Array

Returns a list of points along the curve, with a curvature controlled point density. That is, the curvier parts will have more points than the straighter parts.

This approximation makes straight segments between each point, then subdivides those segments until the resulting shape is similar enough.

max_stages controls how many subdivisions a curve segment may face before it is considered approximate enough. Each subdivision splits the segment in half, so the default 5 stages may mean up to 32 subdivisions per curve segment. Increase with care!

tolerance_degrees controls how many degrees the midpoint of a segment may deviate from the real curve, before the segment has to be subdivided.

func tessellate_even_length(max_stages: int = 5, tolerance_length: float = 20.0) -> PackedVector2Array

Returns a list of points along the curve, with almost uniform density. max_stages controls how many subdivisions a curve segment may face before it is considered approximate enough. Each subdivision splits the segment in half, so the default 5 stages may mean up to 32 subdivisions per curve segment. Increase with care!

tolerance_length controls the maximal distance between two neighboring points, before the segment has to be subdivided.

Inherited from Resource (18)
func _get_rid() -> RID
func _reset_state() -> void
func _set_path_cache(path: String) -> void
func _setup_local_to_scene() -> void
func copy_from_resource(resource: Resource) -> Error
func duplicate(deep: bool = false) -> Resource
func duplicate_deep(deep_subresources_mode: Resource.DeepDuplicateMode = 1) -> Resource
func emit_changed() -> void
func get_rid() -> RID
func is_built_in() -> bool
func reset_state() -> void
func set_id_for_path(path: String, id: String) -> void
func set_path_cache(path: String) -> void
func setup_local_to_scene() -> void
func take_over_path(path: String) -> void
Inherited from RefCounted (4)
func init_ref() -> bool
func reference() -> bool
func unreference() -> bool
Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

Inherited from Resource (2)
signal changed()
Inherited from Object (2)