在 Godot 中创建圆弧路径

在 Godot 中创建圆弧路径

注意是圆弧路径,不是圆弧图形。

1. 就是这个

使用这个脚本挂载 Path2D上吧!。


using Godot;
using System;

public partial class Arc : Path2D
{
    [Export]
    public float StartAngle = 10;

    [Export]
    public float RangeAngle = 45;

    [Export]
    public float Radius = 100;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        CreateArc();
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {

    }

    private void CreateArc()
    {
        var alpha = StartAngle * MathF.PI / 180;
        var beta = (RangeAngle+StartAngle) * MathF.PI / 180;

        var h = 4 / 3 * ((1 - MathF.Cos((beta - alpha) / 2)) / MathF.Sin((beta - alpha) / 2));

        //Console.WriteLine(h);

        var a = new Vector2(MathF.Cos(alpha), MathF.Sin(alpha)) * Radius;
        var b = new Vector2(MathF.Cos(beta), MathF.Sin(beta)) * Radius;

        var ad = new Vector2(-MathF.Sin(alpha), MathF.Cos(alpha)) * h * Radius;
        var bc = new Vector2(MathF.Sin(beta), -MathF.Cos(beta)) * h * Radius;

        var d = a + ad;
        var c = b +bc;

        var str = $"M 0 0 L {a.X} {a.Y} C {d.X} {d.Y} {c.X} {c.Y} {b.X} {b.Y} Z";

        GD.Print(str);

        Curve.ClearPoints();
        //Curve.AddPoint(Vector2.Zero);
        Curve.AddPoint(a, @out:ad);
        Curve.AddPoint(b, @in:bc);
    }
}

结果就是这样的。

posted @ 2024-06-14 14:41  fanbal  阅读(54)  评论(0)    收藏  举报