gpt4 book ai didi

.net - XNA 沿 2D 直线运动

转载 作者:行者123 更新时间:2023-12-03 04:28:00 25 4
gpt4 key购买 nike

我编写了一个简单的 XNA 演示,其中一个 Sprite 应该以给定的速度 V(理解为直线上的距离)沿一条线移动(由两个 Vector2 点 (Ax,Ay) 和 (Bx,By) 定义)单位时间内行驶的线路)。我知道我需要使用类似于以下的方程来计算 2D 空间中的 x 和 y 位置:

dx = V*dt*cos(alpha)
dy = V*dt*sin(alpha)

但要考虑负 V(例如“向后”移动时)但我想知道我是否没有重新发明轮子,也许 xna 提供了解决方案?

最佳答案

通过从 B 中减去 A,创建从 A 点到 B 点的 2D 向量。通过将 X 和 Y 分量除以向量的长度来对其进行标准化。将 X 和 Y 分量乘以速度。将此乘以时间片(即 20/1000 表示 20 毫秒)。将这些 X 和 Y 值添加到您的位置。

示例:

A is (2, 5)  B is (1, 8)
Speed is 2 (move 2 units in one second)
Time slice is 20 milliseconds since last update

C will be (-1, 3)
Normalize C by dividing by distance (sqrt(-1 * -1 + 3 * 3) = 3.1622777)
Normalized is (-0.316277, 0.9486833) (to move 1 unit that direction)
Multiply by speed (2): (-0.632554, 1.8973666)
For movement this time period, multiply by 20 and divide by 1000:
(-0.01265108, 0.037947332), move sprite this far this frame

关于.net - XNA 沿 2D 直线运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5160002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com