Trajectory Prediction

So, I’ve been working on a game on-and-off, but have been trying to implement one key feature.
But first, the game. (, most updated in the last post) It has no plot yet, so don’t get excited. I have yet to draw up organized plans for exactly what this is supposed to become. You are in a spacecraft, and start out near your space station. Eventually, you’ll be able to land in the space station, but I haven’t worked that out or worried about it yet. There is a turret on top of the station, and one remote turret near the station, which are on your side. An enemy is there, too! You have to kill it. (I haven’t told it to die yet, since I can’t target it all too well…)
So, my problem is targeting. I would like to have the the turrets target not the ship, but an empty that is in front of the ship, its location set by the formula in the attached image. The formula was found in Google Books Since the scene has no gravity, I can omit the (1/2)GyT2 section, which, if implemented, would be on the Ez function, anyway. I am assuming these variables stand for:
E(x,y,z): End location, on X, Y, and Z planes.
S(x,y,z): Start location, on X, Y, and Z planes.
v(x,y,z): Velocity, on X, Y, and Z planes.
t: Trajectory.

What I don’t know how to do is how to implement this into Python and use it to calculate where the object will end in relation to the distance between enemy and turret, thus creating an accurate targeting system.

Any ideas, or other criticisms and comments on the game in general are welcome!


maybe this thread will help:

Alright, here’s something for you. I’ll explain it, then explain the problem.

The basic idea is that each turret aims towards the “collision point” on a sphere. The radius of the sphere is dependent upon the velocity of the projectile and the time elapsed since the projectile was fired (time from now).

On the other hand, the targets each have their own velocities (which are assumed to be constant) which can be extrapolated into a line (basically those equations you put up).

The key point here is that the time from now until the collision must be the same for both the projectile and the target, and the projectile and target must end up in the same location. The targeting script I’ve written extrapolates the target’s path (by incrimenting t) until it finds that this (roughly) lies on the sphere with a center at the turret and a radius a function of time. That position is where the turret aims.

I set up this file so it tries to be modular. Each turret runs the same copy of the targeting script, and automatically aims towards the “aim” object with the same property value. So the turret with property turret = 1 aims towards the aimer with property aim = 1, not aim = 0. Also the turrets auto-target the nearest object with the “target” property.

The Problem:

In order to predict the location of the targets at some time in the future, the script must know the target’s velocities. However, the functions getLinearVelocity(), getVelocity() always return [0, 0, 0] (when in fact all the test targets have velocities of [0.3, 0, 0]). Thus the turret does not “lead” the target.

This can be verified as the only problem by uncommenting line 23. This line manually tells the targeting script that the targets velocity is [0.3, 0, 0] and the turrets now “lead” the targets perfectly.



turrets.blend (174 KB)

Well, so this is not really possible on objects with changing velocities, is your point, right?
I guess I won’t be doing that, then. Either way, thanks for the help!

No, that’s not my point. The functions required to make this work properly aren’t working for some reason. Its most likely an error on my part, but I can’t figure out what it is.

No problem. It was an interesting question… which is why I just went and “made” the function myself. Here’s a version that works.


turrets.blend (179 KB)

Hey, thanks again! Mind if I use this script for the game?

No problem. I suppose it’d be nice if you’d credit me as the author of the script, but I don’t care what you do with it.