gpt4 book ai didi

algorithm - 在基于旋转的方向上移动时限制 x 和 y 跟踪的对象的最高速度

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:30:19 25 4
gpt4 key购买 nike

假设我有一些可以 360 度旋转的角色,我正在跟踪它的 x 和 y 位置。

我正在使用 sincos 来确定它的运动:

x += speed * cos(rotation)
y += speed * sin(rotation)

这很好用,但我想我想要一个基于加速的系统。因此,我不是提高速度,而是提高加速度,然后提高速度:

x_velocity += speed * cos(rotation)
y_velocity += speed * sin(rotation)
x += x_velocity
y += y_velocity

但是,我不希望这个角色能够无限加速,因此我想限制它的最高速度。现在,我可以这样做:

if x_velocity > maxspeed then x_velocity = maxspeed
if y_velocity > maxspeed then y_velocity = maxspeed

但这并不完美,因为这意味着如果您沿完美对角线移动,您的移动速度比沿基本方向移动快 44%。

所以我使用勾股定理,但我马上遇到了问题。

if sqrt(x_velocity^2 + y_velocity^2) > maxspeed then ???

有解决办法吗?我不能像以前那样简单地将它定义为等于 maxspeed,但我也不能将所有速度完全分配给一个轴,因为那样会改变方向。如何确定将其限制在的速度?

最佳答案

只需使用整体速度

 velocity = Min(max_velocity, velocity + speed) //really acceleration instead of speed
x_velocity = velocity * cos(rotation)
y_velocity = velocity * sin(rotation)
x += x_velocity
y += y_velocity

如果你必须保持 x_velocity 和 y_velocity(出于某种奇怪的原因),那么你可以通过它的保持方向的组件来限制整体速度:

... update x_velocity, y_velocity
vel = sqrt(x_velocity^2 + y_velocity^2)
if vel > max_velocity then
x_velocity = x_velocity * max_velocity / vel
y_velocity = y_velocity * max_velocity / vel

关于algorithm - 在基于旋转的方向上移动时限制 x 和 y 跟踪的对象的最高速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40904237/

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