Untitled
unknown
csharp
a month ago
1.7 kB
5
Indexable
using Godot; using System; public class BezierMovement : Spatial { private float time = 0.0f; private float speed = 1.0f; // Speed of the movement private Vector3 P0 = new Vector3(0, 0, 0); // Start point private Vector3 P1 = new Vector3(5, 10, 0); // Control point private Vector3 P2 = new Vector3(10, 0, 0); // End point public override void _Process(float delta) { // Increment time time += delta * speed; // Clamp time between 0 and 1 time = Mathf.Clamp(time, 0, 1); // Calculate the position on the Bezier curve Vector3 position = QuadraticBezier(time, P0, P1, P2); // Calculate direction vector by getting the derivative of the Bezier curve Vector3 direction = QuadraticBezierDerivative(time, P0, P1, P2).Normalized(); // Create a basis (orientation) from the direction vector Basis basis = new Basis(); basis = basis.LookingAt(position + direction, Vector3.Up); // Update the object's global transform with position and orientation GlobalTransform = new Transform(basis, position); } // Function to calculate a point on a quadratic Bezier curve private Vector3 QuadraticBezier(float t, Vector3 P0, Vector3 P1, Vector3 P2) { return (1 - t) * (1 - t) * P0 + 2 * (1 - t) * t * P1 + t * t * P2; } // Function to calculate the derivative of a quadratic Bezier curve private Vector3 QuadraticBezierDerivative(float t, Vector3 P0, Vector3 P1, Vector3 P2) { return 2 * (1 - t) * (P1 - P0) + 2 * t * (P2 - P1); } }
Editor is loading...
Leave a Comment