作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个简单的 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/
我编写了一个简单的 XNA 演示,其中一个 Sprite 应该以给定的速度 V(理解为直线上的距离)沿一条线移动(由两个 Vector2 点 (Ax,Ay) 和 (Bx,By) 定义)单位时间内行驶的
我是一名优秀的程序员,十分优秀!