对推力从当前速度矢量到目标矢量的平滑变化进行编程

发布于 2024-08-03 19:13:39 字数 679 浏览 10 评论 0原文

TL;dr:“我不确定如何计算一个矢量与另一个矢量之间的推力平滑过渡。”

我正在编写一个简单的游戏,敌人在开放空间(没有墙壁)中追逐玩家。我正在计算敌人的x& y 速度独立,如果它们朝玩家的方向移动,则加速它们;如果它们走错方向,则快速减慢它们(例如 EnemyVelocity.x > 0 & player.x < heavenly.x,然后 EnemyVelocity.x > 0 x - 2.)

虽然尝试躲避敌人的游戏玩法相当有趣,但我希望让敌人使用适当的物理行为。我目前正在做的是让敌人根据他们和玩家之间的角度设置他们的推力(想想宇宙飞船),并让他们的推力加速到最大速度(计算 EnemyVelocity 三角形的 c 边)。一旦发生这种情况,我不确定让推力自行调整的最佳方法。如果我没有保留最大速度,敌人会很好地加速,但很容易超过玩家,然后需要很长时间才能获得足够的动量返回玩家的方向。

我想要发生的是让敌人在接近玩家的路上不断调整他们的速度,瞄准他们所在的任何地方(我不希望他们预测你会在哪里)。然后,当他们想念球员时,我想要同样的推力和压力。加速度公式来重新调整它们的速度并将它们发送回玩家。

我认为这将涉及两个向量:一个是敌人当前正在行进的向量,另一个是敌人想要行进的向量(将把他们直接带到玩家那里的向量)。我不知道如何计算一个矢量与另一个矢量之间的推力的平滑过渡。

任何提示、公式或问题将不胜感激!谢谢堆栈溢出。

TL;dr: "I am not sure how to calculate a smooth transition of thrust between one vector and another."

I am programming a simple game where an enemy chases after the player in an open space (no walls). I was calculating the enemy's x & y velocities independently, accelerating them if they were taking them in the direction of the player and quickly slowing them if they were going the wrong way (e.g. EnemyVelocity.x > 0 & player.x < enemy.x, then EnemyVelocity.x - 2.)

While the gameplay is decently fun trying to dodge the enemy, it is my desire to have the enemy behave using proper physics. What I am currently doing is have the enemy set their thrust (think a spaceship) based on the angle between them and the player, and have their thrust accelerate up to a max speed (calculating side c of the EnemyVelocity triangle). Once that happens, I'm not sure the best way to have the thrust adjust itself. If I leave no max speed, the enemy accelerates nicely but will easily overshoot the player, and then take a long time to get enough momentum heading back in the player's direction.

What I'd like to happen is have the enemy constantly adjust their velocity on the way to the player, targeting wherever they are at (I don't want them to predict where you will be). Then, when they miss the player, I'd like for the same thrust & acceleration formulas to readjust their velocity and send them back at the player.

I'm thinking this will involve two vectors: one where the enemy is currently traveling, and one where the enemy wants to travel (the vector that will take them straight to the player). I am not sure how to calculate a smooth transition of thrust between one vector and another.

Any tips, formulas or questions will be much appreciated! Thank you Stack Overflow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浅语花开 2024-08-10 19:13:39

这一切都回到牛顿方程:

F = m * a
s = s_o + v * t + a * t^2 / 2
v = v_o + a * t

在这种情况下,F 是力(推力),a 是加速度,m 是质量船的。 s 是当前位置,s_o 是原始位置,v 是速度,t 是当前位置时间。

当然,这是沿着一条直线,所以如果你想转换为二维或三维,你必须做一些数学运算。 Fsva 都是向量,这意味着它们的方向同样重要。从技术上讲,t 也是一个向量,但由于时间通常只向一个方向移动,因此我们不必担心这一点。

2d:
F^2 = F_x^2 + F_y^2 (use Pythagorean theorem to split force into components)
F_x = m * a_x
F_y = m * a_y
s_x = s_o_x + v_x * t + a_x * t^2 / 2
s_y = s_o_y + v_y * t + a_y * t^2 / 2
v_x = v_o_x + a_x * t
v_y = v_o_y + a_y * t

3d:
F^2 = F_x^2 + F_y^2 + F_z^2 (surprisingly, this works)
F_x = m * a_x
F_y = m * a_y
F_z = m * a_z
s_x = s_o_x + v_x * t + a_x * t^2 / 2
s_y = s_o_y + v_y * t + a_y * t^2 / 2
s_z = s_o_z + v_z * t + a_z * t^2 / 2
v_x = v_o_x + a_x * t
v_y = v_o_y + a_y * t
v_z = v_o_z + a_z * t

现在,为了将您的速度调整到玩家的方向,您需要一个固定的总力 (F) 来更改您当前朝向玩家的速度。在物理学中,事情不会立即发生,但你的目标应该是最小化变化发生的时间(“t”)。

这将为您提供一个由您的当前位置 ((s_o_x,s_o_y)(s_o_x,s_o_y,s_o_z)) 和对手的当前位置或您的目标位置 ( (s_x,s_y)(s_x,s_y,s_z)),用于您的目标速度(忽略加速度)。

v_x = (s_x - s_o_x) / t
v_y = (s_y - s_o_y) / t

v_x = (s_x - s_o_x) / t
v_y = (s_y - s_o_y) / t
v_z = (s_z - z_o_y) / t

我们可以用这个代替我们的其他方程:

(s_x - s_o_x) / t = v_o_x + a_x * t
(s_y - s_o_y) / t = v_o_y + a_y * t

(s_x - s_o_x) / t = v_o_x + a_x * t
(s_y - s_o_y) / t = v_o_y + a_y * t
(s_z - z_o_y) / t = v_o_z + a_z * t

然后我们求解加速度(这与力有关,这就是我们要计算的)。

(s_x - s_o_x) / t^2 - v_o_x / t = a_x
(s_y - s_o_y) / t^2 - v_o_y / t = a_y

(s_x - s_o_x) / t^2 - v_o_x / t = a_x
(s_y - s_o_y) / t^2 - v_o_y / t = a_y
(s_z - z_o_y) / t^2 - v_o_z / t = a_z

将其代入力方程:

F_x = m * (s_x - s_o_x) / t^2 - m * v_o_x / t
F_y = m * (s_y - s_o_y) / t^2 - m * v_o_y / t

F_x = m * (s_x - s_o_x) / t^2 - m * v_o_x / t
F_y = m * (s_y - s_o_y) / t^2 - m * v_o_y / t
F_z = m * (s_z - z_o_y) / t^2 - m * v_o_z / t

现在求解 t

t = (-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x
t = (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y

t = (-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x
t = (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y
t = (-m * v_o_z +/- sqrt(m^2 * v_o_z^2 - 4 * F_z * m * (s_z - s_o_z))) / 2 / F_z

时间应该收敛,因此时间将相等!这为我们提供了每个坐标(平面和球体)的方程组。请注意,有多个可能的值,但有些涉及虚数,因此您必须消除这些解决方案:

(-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x
= (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y
F^2 = F_x^2 + F_y^2

(-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x
= (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y
= (-m * v_o_z +/- sqrt(m^2 * v_o_z^2 - 4 * F_z * m * (s_z - s_o_z))) / 2 / F_z
F^2 = F_x^2 + F_y^2 + F_z^2

求解 (F_x,F_y)(F_x,F_y,F_z)< /code> 坐标,你就得到了你需要的力量。

如果您有任何疑问或发现我的数学错误,请告诉我。

It all comes back to Newton's equations:

F = m * a
s = s_o + v * t + a * t^2 / 2
v = v_o + a * t

In this case F is the force (thrust), a is the acceleration, and m is the mass of the ship. s is the current location, s_o is the original location, v is the velocity, and t is the current time.

Of course this is along a straight line, so if you want to convert to two or three dimensions you'll have to do some math. F, s, v, and a are all vectors, meaning that their direction is equally important. Technically t is also a vector, but since time generally only goes one direction, we don't have to worry about that.

2d:
F^2 = F_x^2 + F_y^2 (use Pythagorean theorem to split force into components)
F_x = m * a_x
F_y = m * a_y
s_x = s_o_x + v_x * t + a_x * t^2 / 2
s_y = s_o_y + v_y * t + a_y * t^2 / 2
v_x = v_o_x + a_x * t
v_y = v_o_y + a_y * t

3d:
F^2 = F_x^2 + F_y^2 + F_z^2 (surprisingly, this works)
F_x = m * a_x
F_y = m * a_y
F_z = m * a_z
s_x = s_o_x + v_x * t + a_x * t^2 / 2
s_y = s_o_y + v_y * t + a_y * t^2 / 2
s_z = s_o_z + v_z * t + a_z * t^2 / 2
v_x = v_o_x + a_x * t
v_y = v_o_y + a_y * t
v_z = v_o_z + a_z * t

Now to adjust your velocity to the direction of the player, you've got a fixed total force (F) in order to change your current velocity toward the player. In physics things don't happen instantaneously, but your goal should be to minimize the time in which the change happens ('t').

This gives you an equation in terms of your current location ((s_o_x,s_o_y) or (s_o_x,s_o_y,s_o_z)) and your opponent's current location or your target location ((s_x,s_y) or (s_x,s_y,s_z)), for your target velocity (ignores acceleration).

v_x = (s_x - s_o_x) / t
v_y = (s_y - s_o_y) / t

v_x = (s_x - s_o_x) / t
v_y = (s_y - s_o_y) / t
v_z = (s_z - z_o_y) / t

We can substitute this for our other equation:

(s_x - s_o_x) / t = v_o_x + a_x * t
(s_y - s_o_y) / t = v_o_y + a_y * t

(s_x - s_o_x) / t = v_o_x + a_x * t
(s_y - s_o_y) / t = v_o_y + a_y * t
(s_z - z_o_y) / t = v_o_z + a_z * t

We then solve for the acceleration (this is related to the force, which is what we are trying to calculate).

(s_x - s_o_x) / t^2 - v_o_x / t = a_x
(s_y - s_o_y) / t^2 - v_o_y / t = a_y

(s_x - s_o_x) / t^2 - v_o_x / t = a_x
(s_y - s_o_y) / t^2 - v_o_y / t = a_y
(s_z - z_o_y) / t^2 - v_o_z / t = a_z

Plug this into the force equation:

F_x = m * (s_x - s_o_x) / t^2 - m * v_o_x / t
F_y = m * (s_y - s_o_y) / t^2 - m * v_o_y / t

F_x = m * (s_x - s_o_x) / t^2 - m * v_o_x / t
F_y = m * (s_y - s_o_y) / t^2 - m * v_o_y / t
F_z = m * (s_z - z_o_y) / t^2 - m * v_o_z / t

Now solve for t:

t = (-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x
t = (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y

t = (-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x
t = (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y
t = (-m * v_o_z +/- sqrt(m^2 * v_o_z^2 - 4 * F_z * m * (s_z - s_o_z))) / 2 / F_z

The times should converge, so the times will be equal! This gives us a system of equations for each coordinate (plane and sphere). Note that there are multiple possible values, but some involve imaginary numbers so you'll have to eliminate those solutions:

(-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x
= (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y
F^2 = F_x^2 + F_y^2

(-m * v_o_x +/- sqrt(m^2 * v_o_x^2 - 4 * F_x * m * (s_x - s_o_x))) / 2 / F_x
= (-m * v_o_y +/- sqrt(m^2 * v_o_y^2 - 4 * F_y * m * (s_y - s_o_y))) / 2 / F_y
= (-m * v_o_z +/- sqrt(m^2 * v_o_z^2 - 4 * F_z * m * (s_z - s_o_z))) / 2 / F_z
F^2 = F_x^2 + F_y^2 + F_z^2

Solve for the (F_x,F_y) or (F_x,F_y,F_z) coordinates and you've got the force you need.

Let me know if you have any questions or if you find mistakes in my math.

碍人泪离人颜 2024-08-10 19:13:39

您可以通过确保速度(而不是推力)的平滑变化来获得您想要的效果。这样,如果敌人超过玩家,它可以立即反转其加速度,从而减慢其速度并最终反转其行进方向。

您可以通过在每次迭代期间更改速度来实现此目的,更改的幅度取决于敌人到玩家的距离:

while (game_in_progress)
{
    // Distance from enemy to player.  The larger the
    // distance, the greater the acceleration will be.
    delta.x = player.x - enemy.x
    delta.y = player.y - enemy.y

    // Accelerate by changing velocity based on distance,
    // where 'scale' is sufficiently small. (Limit v to
    // some maximum if you choose; likely to be unnecessary.)
    v.x += delta.x * scale
    v.y += delta.y * scale

    // Update the enemy's position.
    enemy.x += v.x
    enemy.y += v.y
}

通过独立计算 xy 值,您可以省去处理矢量、角度和联立方程的麻烦。

同样,通过认识到加速度(推力)只是速度的变化,而速度的变化又是位置的变化,您可以仅使用简单的代数而不是微积分来创建离散时间模拟。

玩得开心!

You may get the effect you want by ensuring a smooth change in velocity, rather than thrust. That way, if the enemy overshoots the player, it can immediately reverse its acceleration, which will slow it down and eventually reverse its direction of travel.

You can accomplish this by changing the velocity during each iteration, by a small amount that's based on the distance from the enemy to the player:

while (game_in_progress)
{
    // Distance from enemy to player.  The larger the
    // distance, the greater the acceleration will be.
    delta.x = player.x - enemy.x
    delta.y = player.y - enemy.y

    // Accelerate by changing velocity based on distance,
    // where 'scale' is sufficiently small. (Limit v to
    // some maximum if you choose; likely to be unnecessary.)
    v.x += delta.x * scale
    v.y += delta.y * scale

    // Update the enemy's position.
    enemy.x += v.x
    enemy.y += v.y
}

By calculating the x and y values independently, you can save yourself the headache of dealing with vectors, angles, and simultaneous equiations.

Similarly, by recognizing that acceleration (thrust) is simply a change in velocity, which in turn is a change in position, you can create a discrete-time simulation using only simple algebra instead of calculus.

Have fun!

提赋 2024-08-10 19:13:39

你需要用正确的物理术语来思考。您有一个速度,并且想要添加一个加速度。这就是它的全部内容 - 加速度是速度的逐渐变化,它将把敌人拉向玩家,允许其超调,减速(或转向),然后返回玩家。

加速度的测量方式为 d(速度)/时间。您希望在任何时间点向玩家加速,因此每个间隔(秒、百分之一秒或您选择的任何时间)都需要将敌人和玩家之间的向量乘以某个常数,添加到您的速度中。

Velocity = Velocity + c * (Player-Enemy vector)

常数 c 将取决于您想要向玩家加速的速度,以及您更新速度的频率。

如果您想“限制”敌人的最大速度,使其不会无限期地继续增加速度,您也可以这样做。

Velocity = Velocity * (Maximum magniture / |Velocity|)

编辑:为了进一步澄清,添加速度只是意味着添加分量向量。其中

Vx = Vx + c * Ax
Vy = Vy + c * Ay

V 是速度,A 是加速度。大小的测量方式为 sqrt(Vx^2 + Vy^2),即直角三角形的斜边。所以如果你想让敌人的最大速度为m,

Vx = Vx * ( m / sqrt(Vx^2 + Vy^2)
Vy = Vy * ( m / sqrt(Vx^2 + Vy^2)

You need to think in proper physics terms. You have a velocity, and you want to add an acceleration. That's all there is to it - the acceleration is a gradual change in velocity that will draw the enemy towards to player, allow it to overshoot, slow down (or turn) and then head back towards the player.

Acceleration is measured as d(velocity)/time. You want to accelerate towards the player at any point in time, so every interval (second, hundredth of a second or whatever you choose) you need to add the vector between enemy and player, multiplied by some constant, to your velocity.

Velocity = Velocity + c * (Player-Enemy vector)

Constant c will depend on how fast you want to accelerate towards the player, and how often you are updating your velocity.

If you want to 'cap' the maximum speed of the enemy so that it doesn't continue to increase the magnitude of its velocity indefinitely, you can also do so.

Velocity = Velocity * (Maximum magniture / |Velocity|)

EDIT: to clarify further, adding a Velocity simply means adding the component vectors. So

Vx = Vx + c * Ax
Vy = Vy + c * Ay

where V is velocity and A is acceleration. Magnitude is measured as sqrt(Vx^2 + Vy^2), ie the hypotenuse of a right triangle. So if you want maximum speed of the enemy to be m,

Vx = Vx * ( m / sqrt(Vx^2 + Vy^2)
Vy = Vy * ( m / sqrt(Vx^2 + Vy^2)
她如夕阳 2024-08-10 19:13:39

不久前我写了一个简单的小行星游戏,其中有一艘“盟友”飞船,它会追捕小行星并为你射击。基本上它发现了最近的小行星,然后开始平稳地转向它并追赶它。遗憾的是我不再有代码了,但如果没记错的话,我想我每转都会稍微转动一下船,然后如果小行星很远我就会加速,但如果它很近我会尝试匹配小行星的速度。实际上这很酷,并且至少涉及代数。

最好的方法是采用 2 个弧度值并在它们之间进行 lerp,处理环绕。 (必要时可以添加或减去 2pi)。然后将其转换为单位向量。随后将其乘以您希望船舶加速的速度,就可以了!

I wrote a simple asteroids game a while back that had an "ally" ship which would hunt down asteroids and shoot at them for you. Basically it found the closest asteroid then started smoothly turning towards it and going after it. Sadly i don't have the code anymore but if memory serves, i think i tured the ship a little each turn, then if the asteroid was far away i accelerated but if it was close i tried to match the asteroid's speed. It was pretty cool actually, and a minimum of algebra involved.

The best way to do it would be to take 2 radian values and lerp between them, handling wrapping. (perhaps by adding or subtracting 2pi where necessary). Then convert it to a unit vector. Subsequently multiply that by the speed you want the ship to accelerate, and there you go!

江南月 2024-08-10 19:13:39

一种简单的方法(不是正确的物理方法)是计算敌人的“所需速度”,然后调整敌人当前的速度,注意其顶部的任何限制或最小速度。

例如,在我编写的一个小 2d 游戏中(http://wordwarvi.sourceforge.net)有“热寻导弹。”如果导弹停在半空中掉头,看起来就很奇怪。所以我所做的如下:我计算朝向玩家的“所需速度”。这只是通过“相似三角形”来完成的。我在 X 和 Y 中找到到玩家的距离,以较大者为准,我将“所需(x 或 y)速度设置为最大可能值,然后缩放另一个速度以适应“相似三角形”。 ,这只是“所需速度”,而不是当前速度,我采用当前速度并将其缓慢(每帧一点点)调整到“所需”速度(尽管所需速度也会按帧重新计算)。在 vx 和 vy 上设置最小值以防止它们在半空中停止

,但它工作正常(没有人抱怨它们太简单、太难或太不切实际。)

编辑:在重新阅读问题时,我的答案可能不是你想要的。

One simple way (not proper physics) is to calculate your enemy's "desired velocity" and then adjust enemy's currently velocity towards that, minding whatever limits on top, or minimum speed it has.

For instance, in a little 2d game I wrote (http://wordwarvi.sourceforge.net) there are "heat seeking missiles." It looks pretty weird if the missiles stop in midair to turn around. So what I did was as follows: I calculate a "desired velocity" which is towards the player. This is just done by "similar triangles". I find the distance to the player in X, and in Y, and whichver is greater, I make the "desired (x or y) velocity be the maximum possible, and then scale the other one to fit the "similar triangle." Note, that's just the "desired velocity," not the current velocity. I take the current velocity and adjust it slowly (by a little bit per frame) towards the "desired" velocity (though the desired velocity is recalculated per frame as well,) mindimg minimums on vx and vy to keep them from stopping mid-air.

Dumb algorithm, but it works ok (nobody's complained that they are too easy, too hard, or too unrealistic.)

Edit: on re-reading the question, my answer is probably not what you're after.

巾帼英雄 2024-08-10 19:13:39

我已经专业地解决过这样的问题,我建议您从简单的版本开始并逐步完善。在尝试下一步之前,请确保每一步都获得预期的行为。

  1. 导引头在一维原点处寻找固定目标。没错,是一维。它可以在 x 轴上来回推力,并试图达到 x=0。推进器没有节流阀(像固体火箭一样),但导引头可以将其指向任一方向。如果你正确编程,导引头将在 x=0 附近振荡,每次都会超调。
  2. 相同,但目标在 x=0 以外的地方是静止的。只需使 x 相对,而不是绝对(也就是说,搜索者关心 x 的差异,而不是目标的 x)。
  3. 现在目标正在移动(或跳跃)。搜寻者应该能够跟随它。振荡会根据目标的移动方式而增大或缩小——你会明白我的意思。
  4. 现在是二维的。导引头始终直接向目标推力,这意味着您必须通过简单的三角函数将推力分为 x 和 y 分量。如果移动目标,导引头可能会进入围绕它的轨道。
  5. 回到一维和固定目标,但现在导引头正在尝试交会,而不是飞越。这是最难的部分。目标是让距离和速度同时变为零,不超调,所以导引头必须了解自己的制动能力。当 x 小于 v^2/2a 时,导引头必须反向推力,远离目标,以便减速并与目标相遇。当导引头非常接近目标时,允许其停止推进是件好事。
  6. 目标再次开始移动。这很容易;只是使 x 和 v 相对,而不是绝对。
  7. 多个维度。这非常容易; x、y 和 z 部分是独立的。

现在,如果您想要一个不能立即转弯但必须弯曲的导引头,事情就会变得棘手......

I've solved problems like this professionally, and I advise you to start with simple versions and work up. Make sure you get expected behavior at each step before you try the next.

  1. The seeker seeks a stationary target at the origin in one dimension. That's right, one dimension. It can thrust back and forth on the x-axis and it's trying to get to x=0. The thruster has no throttle (like a solid rocket) but the seeker can point it in either direction. If you program this right, the seeker will oscillate around x=0, overshooting every time.
  2. The same, but the target is stationary somewhere other than x=0. Just make x relative, not absolute (that is, the seeker cares about the difference in x, not the target's x).
  3. Now the target is moving (or jumping). The seeker should be able to follow it around. The oscillation will grow or shrink depending on how the target moves -- you'll see what I mean.
  4. Now two dimensions. The seeker always thrusts directly toward the target, which means you must divide the thrust into x and y components by simple trig. If you move the target, the seeker may go into orbit around it.
  5. Back to one dimension and a stationary target, but now the seeker is trying a rendezvous, not a flyby. This is the hard part. The goal is to have distance and velocity become zero at the same time, no overshooting, so the seeker must know its own braking ability. When x is less than v^2/2a, the seeker must reverse thrust, thrusting away from the target in order to slow down and meet it. It's nice to allow the seeker to stop thrusting when it's very close to the target.
  6. The target starts moving again. This is easy; just make x and v relative, not absolute.
  7. Multiple dimensions. This is remarkably easy; the x, y, and z parts are independent.

Now if you want a seeker that can't turn on a dime, but must curve around, things get tricky...

爱她像谁 2024-08-10 19:13:39

只需一些提示即可轻松正确地完成此操作。 1)使用向量是最简单、最通用的,而不是把所有内容都写两三次。 2)如果你控制力(实际上是加速度,因为 A=F/质量),然后动态地改变速度和位置,事情看起来就会正确。

真实运动的基本循环看起来像这样(其中 CAP 是向量,dt 是你的时间步长):

while (motion) {
   A = get_acceleration(X, V, A, X_target, V_targer, A_target)
   V += A*dt       // V is the integral of A
   X += V*dt       // X is the integral of V
}

实际上,这就是动态进化的情况。

然后你需要决定如何确定你的加速度,即编写get_acceleration。这里有很多选择取决于多种因素,而现实生活中的追逐者会采用多种策略。例如,如果相对于您的质量有很大的推力(即高加速度),您可能只想径直冲向目标;但如果你的质量相对于你的推力很大,你可能想要进行拦截航向。如果您想在接近目标时减速,则可以在 |X-X_target| 变小时(即它们接近)和/或它们的速度接近时反转加速。此外,阻尼可以帮助物体不振荡,为此,请在加速度中添加一个术语,例如 -c*(V-V_target)。我建议你尝试一下这些,直到你得到与你想要的物理外观和感觉相匹配的东西。

There are just a few pointers to get this right and easy. 1) it's easiest and most general to work with vectors rather that writing everything two or three times. 2) things will look right if you control the force (which is effectively the acceleration since A=F/mass) and then dynamically evolve the velocity and position.

Your basic loop for realistic motion looks like (where the CAPs are vectors and dt is your timestep):

while (motion) {
   A = get_acceleration(X, V, A, X_target, V_targer, A_target)
   V += A*dt       // V is the integral of A
   X += V*dt       // X is the integral of V
}

And really, this is about it for you're dynamic evolution.

Then you need to decide how to determine your acceleration, i.e. write get_acceleration. There are a number of options here that depend on multiple factors, and real-life chasers employ multiple strategies. For example, if you have a lot of thrust relative to your mass (i.e. high acceleration) you probably just want to head straight at the target; but if you have a lot of mass relative to your thrust you probably want to make an interception course. If you want to slow down as you approach the target, you could reverse the acceleration when |X-X_target| becomes small (i.e. they get close) and/or their velocities are close. Also, damping can help things not oscillate, and for this, add a term to the acceleration something like -c*(V-V_target). I suggest you play around with these until you get something that matches the physical look and feel that you're aiming for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文