Hi, I have a projectile and a target.
Both objects have: Vector2D velocity and Vector2D position. How do I find the predicted position just using the target's position and velocity, together with the projectile speed?

## All 8 Replies

Assuming the velocity doesn't change, the position after time t is just
current position + velocity * t

You don't need speed as well because velocity, by definition, includes speed (velocity is speed and direction).

what if the velocity of the target change? Also, is there a solution that does not include a clock?
I've tried that solution and my projectile is always behind.

If you want a predicted position for moving things then you must have some kind of clock, because the predicted position will change over time.
If the velocity of the target changes then it becomes harder. Is the change predictable (eg following a trajectory under gravity) or does it change unpredictably (eg performing avoidance tactics)?

I tried using a clock. the problem now is the whole thing turns to 'pursuit'. The projectile is not doing a straight trajectory and instead turning to the heading of the target.

Are you able to predict the changes in the target's velocity?

I did it so the velocity is constant.

In that (simple) case you have two equations for the position at time t Pt, given the initial position p and the velocity
pt1 = p1 + v1.t
pt2 = p2 + v2.t

Ifthe two obects are going to collide then their positions must be the same at the same time, ie
pt1 = pt2 = p1 + v1.t = p2 + v2.t

now you can just solve those simultaneous equations to find the point of collision (pt1 or pt2) and the time at which they collide (t).
If there is no solution then it means the two objects will pass by without colliding.

Hi, sorry it took me too long to get back to the problem....
I get what you're saying.. but I have to use time = distance / velocity.
So, Distance1 / Velocity1 = Distance2 / Velocity2
then sqrt((x - x1)^2 + (y - y1)^2) / Velocity1 = sqrt((x - x2)^2 + (y - y2)^2) / Velocity1
I just have to find (x, y).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.