Godot中如何获得多边形旋转后的位置

Godot 中如何获得多边形旋转后的顶点位置

01. 前言

我们在进行多边形处理的时候可能会涉及旋转。
我们希望知道旋转后的各个顶点的坐标。

02. 场景布置

画布中有一个多边形。

我们在根节点添加脚本,来访问对应的多边形。

03. 如何获得多边形旋转后的顶点位置?

using Godot;
using System;
using System.Collections.Generic;
using System.Linq;

public partial class Node2D : Godot.Node2D
{

    private Polygon2D _polygon;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        _polygon = GetNode<Polygon2D>("Polygon2D");
    }

    public void _on_button_pressed()
    {
        _polygon.Rotate(30 * Mathf.Pi / 180f);
        QueueRedraw();
    }

    public override void _Draw()
    {
        base._Draw();

        var transform = _polygon.GetTransform();
        foreach (var item in _polygon.Polygon)
        {
            var transformedPos = transform.BasisXform(item);
            var point = _polygon.Position + transformedPos;
            DrawCircle(point, 10, Colors.Red);
        }
        
    }

}

请注意 _Draw 的部分。

posted @ 2024-07-01 16:30  fanbal  阅读(48)  评论(0)    收藏  举报