c# - 当给出开始点、结束点、速度和移动开始时间时,有没有更简单的方法来计算 2D 中的对象当前坐标?

标签 c# geometry linear-algebra

抱歉,几何从来不是我在学校最喜欢的科目,所以...

目前我正在编写 2D MMORPG 服务器模拟器,并希望改进我当前的坐标检测算法。

客户端向服务器发送包含起点坐标终点坐标的数据包。 服务器接收它并将movement start 变量分配给DateTime.Now。 服务器知道角色在 1 秒内可以移动多远(即速度)。 如何在他开始移动后 some 秒后计算角色位置?

因为我不擅长几何学,所以我根据行进距离的百分比编写了这个……垃圾……。 它正在工作,但计算太多。

        public ushort X;
        public ushort Y;
        public ushort TargetX;
        public ushort TargetY;

        public DateTime MovementStartTime;
        public ushort CalculatedX
        {
            get
            {
                if (this.X == this.TargetX && this.Y == this.TargetY)
                    return this.X;

                float total_dst = CF.GetEuclideanDistanceBetween(this.X, this.Y, this.TargetX, this.TargetY);
                float seconds_since_movement_start = (float)(DateTime.Now - this.MovementStartTime).TotalSeconds;
                float passed_dst = Limits.CharacterSpeed * seconds_since_movement_start;

                if (passed_dst > total_dst)
                    return this.TargetX;

                float passed_dst_in_normalized_percents = (passed_dst * 100 / total_dst) / 100;

                return (ushort)(this.X - ((this.X - this.TargetX) * passed_dst_in_normalized_percents));
            }
        }

        public ushort CalculatedY
        {
            get
            {
                if (this.X == this.TargetX && this.Y == this.TargetY)
                    return this.Y;

                float total_dst = CF.GetEuclideanDistanceBetween(this.X, this.Y, this.TargetX, this.TargetY);
                float seconds_since_movement_start = (float)(DateTime.Now - this.MovementStartTime).TotalSeconds;
                float passed_dst = Limits.CharacterSpeed * seconds_since_movement_start;

                if (passed_dst > total_dst)
                    return this.TargetY;

                float passed_dst_in_normalized_percents = (passed_dst * 100 / total_dst) / 100;

                return (ushort)(this.Y - ((this.Y - this.TargetY) * passed_dst_in_normalized_percents));
            }
        }

想法是根据它的速度确定行字符已经通过的百分比,然后将它与 start Xend X 之间的平坦距离结合起来。

也许有人知道一种不那么丑陋的方法?.. enter image description here

最佳答案

当物体以匀速运动时,X坐标对时间的依赖性为

X = X0 + Vx * t

其中 Vx 是速度的 x 分量:

Vx = CharacterSpeed * dx / sqrt(dx*dx + dy*dy) 

其中 dx, dy 是沿轴的距离。在你的情况下是这样的:

dx = this.TargetX - this.StartX
 

关于c# - 当给出开始点、结束点、速度和移动开始时间时,有没有更简单的方法来计算 2D 中的对象当前坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63871879/

相关文章:

c# - Html.EditorFor 设置文本框的初始值

geometry - ImageMagick 和饼图

Python - NumPy - 元组作为数组的元素

c# - 图像调整大小 asp.net mvc 应用程序

c# - 在 ASP.net 中从 Swift/iOS 客户端请求接收 POST 数据

javascript - 如何在three.js中弯曲圆柱体?

.net - 如何压缩角度?

computer-vision - 如何从共享相机 View 的两个基本矩阵计算相对比例?

java - 使用 EJML 的协方差

c# - ODP.NET : Parameter Types "cached" on identical CommandText