• Godot 运动的像素点


    一条直线上运动

    using Godot;
    using System;
    
    public class Line : Node2D
    {
        [Export]
        //Pixel/s
        public float v = 100;
        [Export]
        public Vector2 direction = new Vector2(2, 1);
        private Rect2 screenRect;
        private Vector2 Point = new Vector2(1, 1);
        private Color color = new Color("60C5F1");
        /*
    	x,y,len
    	b = x / y, x = yb, len = vt
    	sqrt(y^2(b^2+1)) = len
    	y =sqrt(len^2/(b^2+1)) = len/sqrt(b^2+1) = vt/sqrt(b^2+1)
    	 */
        private float x1;
        private float y1;
    
        public Line()
        {
            UpdateDirection();
        }
    
        public override void _Ready()
        {
            screenRect = GetViewport().GetVisibleRect();
        }
    
        private void UpdateDirection()
        {
            if (direction.x == 0)
            {
                x1 = 0;
                y1 = v;
                return;
            }
            else if (direction.y == 0)
            {
                y1 = 0;
                x1 = v;
            }
    
            var b = direction.x / direction.y;
            y1 = (direction.y > 0 ? 1 : -1) * v / Mathf.Sqrt(Mathf.Pow(b, 2) + 1);
            x1 = (direction.x > 0 ? 1 : -1) * b * v / Mathf.Sqrt(Mathf.Pow(b, 2) + 1);
        }
    
        public override void _Process(float delta)
        {
            var y = y1 * delta;
            var x = x1 * delta;
            Point.x += x;
            Point.y += y;
    
            Update();
            if (!screenRect.HasPoint(Point))
            {
                direction = -direction;
                UpdateDirection();
            }
        }
    
        private void _on_Line_draw()
        {
            DrawCircle(Point, 2, color);
        }
    }
    
    

    圆形上运动

    using Godot;
    using System;
    
    public class Circle : Node2D
    {
        private Vector2 start = new Vector2(200, 200);
        private Vector2 current = new Vector2(300, 200);
        [Export]
        //Pixel/s
        public float v = 100;
        //radius Pixel
        private float r = 100;
        //radian = arc length/radius = v*t / radius
        private float v_r_ratio;
        private float rad = 0;
        private Color color = new Color("60C5F1");
        private float maxrad = 2 * Mathf.Pi;
    
        public Circle()
        {
            v_r_ratio = v / r;
        }
    
        public override void _Process(float delta)
        {
            if (rad > maxrad)
            {
                rad -= maxrad;
            }
    
            rad += delta * v_r_ratio;
            var y = r * Mathf.Sin(rad);
            var x = r * Mathf.Cos(rad);
    
            current.x = start.x + x;
            current.y = start.y + y;
    
            Update();
        }
    
        private void _on_Circle_draw()
        {
            DrawCircle(current, 2, color);
        }
    }
    
    
  • 相关阅读:
    个人总结08
    npm快速入门
    Activity简介
    SELinux
    正则表达式学习笔记(二)表达式的匹配原理
    git学习笔记(一)
    使用VSFTPD传输文件
    正则表达式学习笔记(一)正则表达式入门
    Linux基础(一)磁盘分区
    Shell脚本笔记(九)数组
  • 原文地址:https://www.cnblogs.com/naergaga/p/13953318.html
Copyright © 2020-2023  润新知