Class Curve2D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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.
public class Curve2D : Resource, IDisposable
- Inheritance
-
Curve2D
- Implements
- Inherited Members
Constructors
Curve2D()
public Curve2D()
Properties
BakeInterval
The distance in pixels between two adjacent cached points. Changing it forces the cache to be recomputed the next time the GetBakedPoints() or GetBakedLength() function is called. The smaller the distance, the more points in the cache and the more memory it will consume, so use with care.
public float BakeInterval { get; set; }
Property Value
PointCount
The number of points describing the curve.
public int PointCount { get; set; }
Property Value
_Data
public Dictionary _Data { get; set; }
Property Value
Methods
AddPoint(Vector2, Vector2?, Vector2?, int)
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 PointCount.
public void AddPoint(Vector2 position, Vector2? @in = null, Vector2? @out = null, int index = -1)
Parameters
position
Vector2in
Vector2?If the parameter is null, then the default value is
new Vector2(0, 0)
.out
Vector2?If the parameter is null, then the default value is
new Vector2(0, 0)
.index
int
ClearPoints()
Removes all points from the curve.
public void ClearPoints()
GetBakedLength()
Returns the total length of the curve, based on the cached points. Given enough density (see BakeInterval), it should be approximate enough.
public float GetBakedLength()
Returns
GetBakedPoints()
Returns the cache of points as a Vector2[].
public Vector2[] GetBakedPoints()
Returns
- Vector2[]
GetClosestOffset(Vector2)
Returns the closest offset to toPoint
. This offset is meant to be used in SampleBaked(float, bool).
toPoint
must be in this curve's local space.
public float GetClosestOffset(Vector2 toPoint)
Parameters
toPoint
Vector2
Returns
GetClosestPoint(Vector2)
Returns the closest point on baked segments (in curve's local space) to toPoint
.
toPoint
must be in this curve's local space.
public Vector2 GetClosestPoint(Vector2 toPoint)
Parameters
toPoint
Vector2
Returns
GetPointIn(int)
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)
.
public Vector2 GetPointIn(int idx)
Parameters
idx
int
Returns
GetPointOut(int)
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)
.
public Vector2 GetPointOut(int idx)
Parameters
idx
int
Returns
GetPointPosition(int)
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)
.
public Vector2 GetPointPosition(int idx)
Parameters
idx
int
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
RemovePoint(int)
Deletes the point idx
from the curve. Sends an error to the console if idx
is out of bounds.
public void RemovePoint(int idx)
Parameters
idx
int
Sample(int, float)
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
) 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)
.
public Vector2 Sample(int idx, float t)
Parameters
Returns
SampleBaked(float, bool)
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).
public Vector2 SampleBaked(float offset = 0, bool cubic = false)
Parameters
Returns
SampleBakedWithRotation(float, bool)
Similar to SampleBaked(float, bool), but returns Transform2D that includes a rotation along the curve, with Transform2D.origin
as the point position, Transform2D.x
as the sideways vector, and Transform2D.y
as the forward vector. Returns an empty transform if the length of the curve is 0
.
var baked = curve.sample_baked_with_rotation(offset)
# This will rotate and position the node with the up direction pointing along the curve.
position = baked.get_origin()
rotation = baked.get_rotation()
# Alternatively, not preserving scale.
transform = baked * Transform2D.FLIP_Y
# To match the rotation of PathFollow2D, not preserving scale.
transform = Transform2D(baked.y, baked.x, baked.origin)
public Transform2D SampleBakedWithRotation(float offset = 0, bool cubic = false)
Parameters
Returns
Samplef(float)
Returns the position at the vertex fofs
. It calls Sample(int, float) using the integer part of fofs
as idx
, and its fractional part as t
.
public Vector2 Samplef(float fofs)
Parameters
fofs
float
Returns
SetPointIn(int, Vector2)
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.
public void SetPointIn(int idx, Vector2 position)
Parameters
SetPointOut(int, Vector2)
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.
public void SetPointOut(int idx, Vector2 position)
Parameters
SetPointPosition(int, Vector2)
Sets the position for the vertex idx
. If the index is out of bounds, the function sends an error to the console.
public void SetPointPosition(int idx, Vector2 position)
Parameters
Tessellate(int, float)
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.
maxStages
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!
toleranceDegrees
controls how many degrees the midpoint of a segment may deviate from the real curve, before the segment has to be subdivided.
public Vector2[] Tessellate(int maxStages = 5, float toleranceDegrees = 4)
Parameters
Returns
- Vector2[]
TessellateEvenLength(int, float)
Returns a list of points along the curve, with almost uniform density. maxStages
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!
toleranceLength
controls the maximal distance between two neighboring points, before the segment has to be subdivided.
public Vector2[] TessellateEvenLength(int maxStages = 5, float toleranceLength = 20)
Parameters
Returns
- Vector2[]