简体   繁体   中英

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. 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.

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. 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. For convenience, I'll assume the weapon at (0, 0, 0) and the target at (x, y, 0). From here, the math is relatively easy.

The ground angle (ie compass direction) is given by φ = arctan(y/x) . We will decompose the projectile's velocity into x, y, and h (vertical or height) components.

The raw horizontal component is s = v*sin(ϴ) (s = ground speed). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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