# Programming/math question, please see inside (if you're a math wizard)

Asked by Zyx (4142) February 25th, 2013

I’m writing a tower defence game because I love tower defence games but I ran into some serious problems when I decided my projectiles needed to lead the target perfectly. So we have a moving target on the screen and a tower which would normally fire bullets but in my game the bullets orbit the tower until they are ready to be fired. That means I can’t fire them as soon as they’re in range of the tower, I have to wait until the bullet faces in the right direction to intercept the target.

Ok, so I have 3 objects: A, B and C.

A and C are in motion, B is stationary. A is in orbit of B and the path of C is known. A can escape from its orbit instantly and if it does it drifts in a straight line. How do I find the first possible moment at which the target and the bullet could collide?

Observing members: 0 Composing members: 0

I wonder if ray-casting strategies would be appropriate here, since it’s like trying to calculate when one object eclipses another.

gorillapaws (19978)

The other way I can think of is to divide the tower into a clock face with say 12 points around the clock face. Do a simple distance calculation from each of the clockface points to the target object and figure which point is closest. When the orbiting object hits that clockface point, have it fire off in a line towards where the target will be.

gorillapaws (19978)

Good but not perfect… I’ll consider it (and might have no choice but to use it).

I also noticed the last question should be phrased “How do I find the first possible moment at which A and C could collide?”.

EDIT: Also I don’t really know what raycasting is. I’ll see what wikipedia has to say.

Zyx (4142)

Math wizard time!

Most of this answer is pretty messy, but it is a work in progress. I don’t have enough time right now to work out a full solution.

Assuming purely linear flight, the line of travel for the target is line p.
The radius of the orbit is R.
The speed of the projectile is Y.
The speed of the target is X.
The flight time is T.
The distance from the center of the circle and the interception point is D.
Distance from projectile to intercept is F.
Distance from target to intercept is E.

F = Y*T = sqrt(D^2 + R^2)
E = X*T

E/F = X/Y

PhiNotPi (12609)

Is C traveling in a straight line? Am I right in assuming that A’s orbit around B is circular and that when it leaves the orbit, it travels tangentially to the orbit? Can A’s speed be adjusted after it leaves the orbit. If not, that would mean that A could leave its orbit and cross the path of C but not hit it because it arrives too early or too late.

What did you end up doing?

gorillapaws (19978)

or