二维抛物线抛射体
我正在寻求创建一个弹丸的基本 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pointy 的答案很好地总结了如何在给定初始轨迹的情况下模拟对象的运动(其中轨迹被认为是方向和速度,或者在组合向量)。
然而,您在问题中说过(如果我没理解错的话)您想要确定仅知道原点
O
和确定初始轨迹>目标P
的预期点。坏消息是,在实践中,对于任何特定的
P
,都有无数条抛物线轨迹可以让您从O
到达那里。角度和速度是相互依赖的。如果我们平移所有内容,使 O 位于原点(即 [0, 0]),则:
则任意时间点
(t)
的位置(x, y)
> 是:因此,您得到了影响
,但有三个未知数(
t
、s_x
和s_y
)和两个联立方程。如果修正其中之一,就足以求解方程。FWIW,修复
s_x
或s_y
相当于修复speed
或angle
,那只是简单的三角学。有些组合当然是不可能的 - 如果速度太低或角度太高,射弹将在到达目标之前撞击地面。
注意:这假设位置是连续评估的。根据 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 targetP
.The bad news is that in practise for any particular
P
there's an infinite number of parabolic trajectories that will get you there fromO
. The angle and speed are interdependent.If we translate everything so that O is at the origin (i.e. [0, 0]) then:
Then the position
(x, y)
at any point in time(t)
is:so at impact you've got
but you have three unknowns (
t
,s_x
ands_y
) and two simultaneous equations. If you fix one of those, that should be sufficient to solve the equations.FWIW, fixing
s_x
ors_y
is equivalent to fixing eitherspeed
orangle
, 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.
我不是物理学家,所以我所能做的就是告诉您一种基于非常简单过程的方法。
现在您需要解决的一个细微差别是“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.
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.