二维抛物线抛射体

发布于 2024-10-21 03:45:10 字数 306 浏览 7 评论 0原文

我正在寻求创建一个弹丸的基本 Javascript 实现,该弹丸遵循抛物线弧(或接近抛物线的弧线)到达特定点。我对复杂的数学不是特别精通,并且花了几天时间阅读有关该问题的材料。不幸的是,看到数学解决方案对我来说毫无用处。理想情况下,我正在寻找伪代码(甚至现有的示例代码)来尝试理解它。我发现的一切似乎只提供了问题的部分解决方案。

实际上,我希望模拟箭从一个位置(弓的位置)到另一个位置的飞行。我已经通过在每个逻辑间隔更新射弹的速度来模拟重力对射弹的影响。我现在想要弄清楚的是如何找出正确的轨迹/角度来发射我的箭,以便在最短的时间内到达我的目标。

任何帮助将不胜感激。

I'm looking to create a basic Javascript implementation of a projectile that follows a parabolic arc (or something close to one) to arrive at a specific point. I'm not particularly well versed when it comes to complex mathematics and have spent days reading material on the problem. Unfortunately, seeing mathematical solutions is fairly useless to me. I'm ideally looking for pseudo code (or even existing example code) to try to get my head around it. Everything I find seems to only offer partial solutions to the problem.

In practical terms, I'm looking to simulate the flight of an arrow from one location (the location of the bow) to another. I have already simulated the effects of gravity on my projectile by updating its velocity at each logic interval. What I'm now looking to figure out is exactly how I figure out the correct trajectory/angle to fire my arrow at in order to reach my target in the shortest time possible.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

只为一人 2024-10-28 03:45:10

Pointy 的答案很好地总结了如何在给定初始轨迹的情况下模拟对象的运动(其中轨迹被认为是方向速度,或者在组合向量)。

然而,您在问题中说过(如果我没理解错的话)您想要确定仅知道原点O确定初始轨迹>目标P的预期点。

坏消息是,在实践中,对于任何特定的 P ,都有无数条抛物线轨迹可以让您从 O 到达那里。角度和速度是相互依赖的。

如果我们平移所有内容,使 O 位于原点(即 [0, 0]),则:

T_x = P_x - O_x            // the X distance to travel
T_y = P_y - O_y            // the Y distance to travel

s_x = speed * cos(angle)   // the X speed
s_y = speed * sin(angle)   // the Y speed

则任意时间点 (t) 的位置 (x, y) > 是:

x = s_x * t
y = s_y * t - 0.5 * g * (t ^ 2)

因此,您得到了影响

T_x = s_x * t
T_y = -0.5 * g * (t ^ 2) + s_y * t

,但有三个未知数(ts_xs_y)和两个联立方程。如果修正其中之一,就足以求解方程。

FWIW,修复s_xs_y相当于修复speedangle,那只是简单的三角学。

有些组合当然是不可能的 - 如果速度太低或角度太高,射弹将在到达目标之前撞击地面。

注意:这假设位置是连续评估的。根据 Pointy 的回答和您自己对如何模拟运动的描述,它与时间以离散增量流逝时发生的情况不太相符。不过,如果您足够频繁地重新计算位置(即每秒 10 次),它应该足够准确。

Pointy's answer is a good summary of how to simulate the movement of an object given an initial trajectory (where a trajectory is considered to be a direction, and a speed, or in combination a vector).

However you've said in the question (if I've read you correctly) that you want to determine the initial trajectory knowing only the point of origin O and the intended point of target P.

The bad news is that in practise for any particular P there's an infinite number of parabolic trajectories that will get you there from O. The angle and speed are interdependent.

If we translate everything so that O is at the origin (i.e. [0, 0]) then:

T_x = P_x - O_x            // the X distance to travel
T_y = P_y - O_y            // the Y distance to travel

s_x = speed * cos(angle)   // the X speed
s_y = speed * sin(angle)   // the Y speed

Then the position (x, y) at any point in time (t) is:

x = s_x * t
y = s_y * t - 0.5 * g * (t ^ 2)

so at impact you've got

T_x = s_x * t
T_y = -0.5 * g * (t ^ 2) + s_y * t

but you have three unknowns (t, s_x and s_y) and two simultaneous equations. If you fix one of those, that should be sufficient to solve the equations.

FWIW, fixing s_x or s_y is equivalent to fixing either speed or angle, that bit is just simple trigonometry.

Some combinations are of course impossible - if the speed is too low or the angle too high the projectile will hit the ground before reaching the target.

NB: this assumes that position is evaluated continuously. It doesn't quite match what happens when time passes in discrete increments, per Pointy's answer and your own description of how you're simulating motion. If you recalculate the position sufficiently frequently (i.e. 10s of times per second) it should be sufficiently accurate, though.

寒尘 2024-10-28 03:45:10

我不是物理学家,所以我所能做的就是告诉您一种基于非常简单过程的方法。

  1. 你的“箭头”有一个“x”和“y”坐标,以及“vx”和“vy”速度。箭头的初始位置是最初的“x”和“y”。初始的“vx”是箭头的水平速度,初始的“vy”是垂直速度(确实是速度,但这些只是文字)。从概念上讲,这两个值取决于弓箭手射箭时使用的角度。
  2. 您将通过离散时间间隔的离散计算来模拟时间的进展。您不必担心“平滑”轨迹弧的方程。因此,您将运行一个计时器并每 100 毫秒(或您想要的任何时间间隔)计算更新的位置。
  3. 在每个时间间隔,您将把“vx”添加到“x”,将“vy”添加到“y”。 (因此,请注意,“vx”和“vy”的初始选择与您选择的时间间隔有关。)您还需要更新“vx”和“vy”以反映效果重力和(如果你愿意的话)风。如果“vx”没有改变,你基本上是在模拟在月球上射箭:-)但是“vy”会因为重力而改变。该变化应该是在每个时间间隔上减去一个恒定量。称之为“delta vy”,您将不得不根据您想要的效果进行修改以获得正确的值。 (从数学角度来说,“vy”就像一阶导数的“y”分量,而“delta vy”值是二阶导数。)
  4. 因为每次都向“vy”添加少量,所以增量当箭头在屏幕上移动时,变化会累加起来,正确模拟“重力彩虹”。

现在您需要解决的一个细微差别是“vy”的符号。 “vy”的初始符号应与“delta vy”相反。哪个应该是正值,哪个应该是负值取决于坐标网格与屏幕的关系。

编辑 - 请参阅@Alnitak的答案,了解与您的问题实际密切相关的内容。

I'm not a physicist so all I can do is tell you an approach based on really simple process.

  1. Your "arrow" has an "x" and "y" coordinate, and "vx" and "vy" velocities. The initial position of the arrow is the initial "x" and "y". The initial "vx" is the horizontal speed of the arrow, and the initial "vy" is the vertical speed (well velocity really but those are just words). The values of those two, conceptually, depend on the angle your bowman will use when shooting the arrow off.
  2. You're going to be simulating the progression of time with discrete computations at discrete time intervals. You don't have to worry about the equations for "smooth" trajectory arcs. Thus, you'll run a timer and compute updated positions every 100 milliseconds (or whatever interval you want).
  3. At each time interval, you're going to add "vx" to "x" and "vy" to "y". (Thus, note that the initial choice of "vx" and "vy" is bound up with your choice of time interval.) You'll also update "vx" and "vy" to reflect the effect of gravity and (if you feel like it) wind. If "vx" doesn't change, you're basically simulating shooting an arrow on the moon :-) But "vy" will change because of gravity. That change should be a constant amount subtracted on each time interval. Call that "delta vy", and you'll have to tinker with things to get the values right based on the effect you want. (Math-wise, "vy" is like the "y" component of the first derivative, and the "delta vy" value is the second derivative.)
  4. Because you're adding a small amount to "vy" every time, the incremental change will add up, correctly simulating "gravity's rainbow" as your arrow moves across the screen.

Now a nuance you'll need to work out is the sign of "vy". The initial sign of "vy" should be the opposite of "delta vy". Which should be positive and which should be negative depends on how the coordinate grid relates to the screen.

edit — See @Alnitak's answer for something actually germane to your question.

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