简体   繁体   English

通过3D网格绘制抛物线的算法

[英]Algorithm for drawing a parabola through a 3D grid

I am currently programming a Tactical RPG in the Godot engine which uses a Python-like language called godotscript. 我目前正在Godot引擎中编写战术RPG,该引擎使用类似Python的语言称为godotscript。 I have come to a point where I need to map the path of a projectile through space and figure out whether its path is obstructed or not. 我已经到了需要在太空中绘制弹丸路径并确定其路径是否受阻的地步。 The game space is constructed like a 3 dimensional grid of coordinates so I think some kind of line-drawing algorithm might work. 游戏空间的构建就像是3维坐标网格,因此我认为某种画线算法可能会起作用。

For a linearly traveling projectile, I used a 3D version of the Bresenham line drawing algorithm to gather a list of points the projectile passes through, then I check if any of them are obstructed. 对于线性行进的弹丸,我使用了Bresenham线描算法的3D版本来收集弹丸通过的点的列表,然后检查是否有障碍物。 It works great! 效果很好!

For a projectile that actually follows real projectile motion, I'm not quite sure what algorithm to use. 对于实际上跟随实际弹丸运动的弹丸,我不太确定要使用哪种算法。 I have an origin point, a target point, and the starting velocity of the object which should be enough to generate a function for the parabola, I'm just not sure about the algorithm for drawing its line. 我有一个原点,一个目标点和对象的起始速度,这些速度应该足以生成抛物线的函数,但我不确定画线的算法。

Any help would be greatly appreciated! 任何帮助将不胜感激!

You haven't quite generated the information to generate the parabola, but you have enough to derive it. 您还没有生成生成抛物线的信息,但是您有足够的信息来推导它。 The problem is the angle of firing, ϴ. 问题是发射角ϴ。 Normally, you have this from your game parameteres. 通常,您可以从游戏参数中获得此信息。

Since you've specified a parabola, I infer that you're ignoring air friction. 既然您已经指定了抛物线,我推断您正在忽略空气摩擦。 Assuming that you you have enough impetus to reach the target, there will be two values of ϴ that hit the target; 假设您有足够的动力达到目标,那么将有两个ϴ值达到目标; anything between those values will overshoot; 这些值之间的任何值都会超调; anything above or below that interval will fall short. 高于或低于该间隔的任何内容都将不足。

I'll assume ϴ from the game parameters and work on the parabola. 我将从游戏参数中假设and并研究抛物线。 For convenience, I'll assume the weapon at (0, 0, 0) and the target at (x, y, 0). 为了方便起见,我假设武器位于(0,0,0),目标位于(x,y,0)。 From here, the math is relatively easy. 从这里开始,数学相对容易。

The ground angle (ie compass direction) is given by φ = arctan(y/x) . 地面角度(即罗盘方向)由φ = arctan(y/x) We will decompose the projectile's velocity into x, y, and h (vertical or height) components. 我们将弹丸的速度分解为x,y和h(垂直或高度)分量。

The raw horizontal component is s = v*sin(ϴ) (s = ground speed). 原始水平分量为s = v*sin(ϴ) (s =地面速度)。 The distance from origin is a linear function of time: the ground velocity is constant. 距原点的距离是时间的线性函数:地速是恒定的。

The vertical position is the canonical formula for height of a projectile: 垂直位置是弹丸高度的标准公式:

h = 1/2 * g * t^2 + v0 * t + h0
v0 = initial upward velocity = cos(ϴ) * starting velocity.
h0 = initial height (0 in our case?)
t = time
g = gravitational force, -9.8 m / sec^2

Decomposing the ground speed as well, we get each of these as a function of time: 同样分解地面速度,我们得到每个作为时间的函数:

x = s * sin(ϴ) * sin(φ) * t
y = s * sin(ϴ) * cos(φ) * t
h = -4.9 * t^2 + cos(ϴ)*v * t

There are your 3D coordinates. 有您的3D坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM