#### Archived

This topic is now archived and is closed to further replies.

# Vector prediction

This topic is 5887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m in the midst of a 3D Asteroids game (http://www.4bitterguys.com/michael/asteroids - source available) and am working on a heads-up-display for my cockpit which places a green rectangle around an object and a red rectangle where the pilot should aim for if he wants to hit the asteroid with a laser, like this: Now, when a laser is fired, its speed is set as the ship''s current speed + 2 units. So, I have the following variables at my disposal: sS - Speed of the spaceship (scalar) vS - Vector of the spaceship''s movement (unit length vector) pS - Position of the spaceship in world coordinates (vector) sA - Speed of the asteroid (scalar) vA - Vector of the asteroid''s movement (unit length vector) pA - Position of the asteroid (vector) What I want to find is: pT - Predicted position of the asteroid by the time a laser arrives to that point. Currently I''m using a simple "distance = speed / time" ratio multiplier but this makes the assumption that the asteroid is not getting closer or further from your ship, which is bad. I can almost *taste* the calculus involved (the force tells me it will involve a derivative) but I just can''t quite get my head around it - it''s been awhile since I did maths at university! Any help would be greatly appreciated!

##### Share on other sites
in case you didnt know distance=speed*time

with proper substituion(not sure about speeling) u can solve equations to get the time and hence the distance (desplacemt from postion to new)

##### Share on other sites
Hahaha, sorry. d=s/t was a typo, I''ve meant d=s*t

But the problem I have still stands.

Things are easy if you assume that the asteroid is travelling in a perfect orbit around the spaceship, because then you can calculate the time it will take the laser to travel to the asteroid (seeing as it will be the same value no matter how far the asteroid travels, since it will always be the same distance from the ship) and then you can substitute it back in to find out how far the asteroid actually will have travelled in that time.

The tricky part is if you allow the asteroid to be moving on any straight-line trajectory, since to calculate the laser''s travel time, you need to know how far the asteroid will have travelled, and to calculate how far the asteroid will have travelled, you need the laser''s travel time

I think it''s a simultaneous equation but I can''t work out how to solve it. Again, any help from anyone would be greatly appreciated!!!

##### Share on other sites
There was a similar thread about a month ago that was about shooting a moving target, and knowing at what angle you must shoot. Look at the archives.

Here's my take on the problem:

First of all, the speed of the ship is not really useful, because all we really care about is the speed of the laser (btw, isn't setting the speed of the laser, ie.: a light beam, to speed of the ship + 2 in contradiction with relativity? whatever)

Speed of the laser: sL (vector)

We want to know the position where the laser and asteroid will collide, and when: (I'm using only vectors: sA is also a vector --- don't use your conventions/optimizations when posting here)

P = pS + sL * tP = pA + sA * tpS + sL * t = pA + sA * t  (first equation)pS - pA = (sA - sL) * tt = pS - pA    -------    sA - sL

You have to express sL's direction as a function of pS, pA and sA.

Hmm... I'm sorry, but I should be working now and I don't feel like finishing this problem (I didn't think it would be that long), although I can outline what I think is the solution. This may look like a 3D problem, but it really is 2D because you only have to solve a triangle. Its sides are pA - pS, P - pS and P - pA. You can find the angle between P - pA and pA - pS with the dot product. Using this angle and the law of cosines, you can find a second equation. Since you have two unknowns in sL' vector (remember, it's a 2D problem; you have to toy with the equations), then you should be able to solve your problem with these two equations.

If I have more time today, I'll finish it myself, but right now, I don't. Sorry.

Cédric

[edited by - cedricl on May 2, 2002 9:45:25 AM]

##### Share on other sites
Thanks a lot Cédric. I've spent some time going over the problem.

I'm still confused, but your post DID clear a lot up.

Since what we're dealing with is a triangle in 2D, we can work out the length of line pA - pS, and also the angle between lines pA - pS and p - pA. From here we still have 2 unknowns (as you mentioned in your post). How do we go from here to a solveable equation? The law of cosines?

Sorry to bring it back on you again, I'm tearing my hair out trying to figure it out!

[edited by - a1studmuffin on May 2, 2002 10:26:14 AM]

##### Share on other sites
No problem! Actually, if you can solve the problem by yourself, all the better! I'd rather answer short questions than write the answer completely.

Again, I'm not sure that you will be able to solve analytically with my approach, but we can try

When I wrote my original post, I thought that the x and y components of the vector would be our two unknowns. That was wrong. Our two unknowns are theta (the angle between pA - pS and P - pS) and t. There are two useful laws to deal with scalene triangles:

If A,B,C represent the angles of a triangle and a,b,c its sides (a is opposed to A), then

a² = b² + c² - 2bc*cosA(similar for b and c)anda / sinA = b / sinB = c / sinC

Using these two laws, try having nothing but known quantities and the variables theta and t in your two equations. The law of cosines is useful if you know only one of the angles of your triangle. In your problem, you know the angle between P - pA and pA - pS. If you have problems solving, post again and we might be able to find a solution.

I think you might have misunderstood me when I said that the problem was 2D (and what I said wasn't entirely clear either). We're not going to deal with vector components here. The reason why the problem is "2D" is that the vector P - pS is in the same plane as sA, pA and pS, so it's kinda 2D because we're in a plane.

Cédric

[edited by - cedricl on May 2, 2002 11:10:51 AM]

##### Share on other sites
quote:
Original post by a1studmuffin

So, I have the following variables at my disposal:

sS - Speed of the spaceship (scalar)
vS - Vector of the spaceship''s movement (unit length vector)
pS - Position of the spaceship in world coordinates (vector)
sA - Speed of the asteroid (scalar)
vA - Vector of the asteroid''s movement (unit length vector)
pA - Position of the asteroid (vector)

What I want to find is:

pT - Predicted position of the asteroid by the time a laser arrives to that point.

If you write the asteroids motion as a set of parametric equations, of the form x = A(t-t0) + B, (A is a column vector (matrix) of velocities and B a column vector of intial displacements (at t0) then you can predict the position of the asteroid at any time t by simple substitution.

Furthermore, if you write out a similar set of equations for the bullet (based on the spacecrafts motion) then you can solve the system for the time of intersection of the two paths.

Cheers,

Timkin

##### Share on other sites
Oh, one tiny aesthetic point about your HUD. You might consider adding a thin line between the centre of the green box and the centre of the red diamond. Why? Because this line represents the projection of the objects path into the 2-D visual plane of the viewer and helps with targeting when the object is not moving linearly (i.e., the line moves as well). Combat HUDs use this method to give additional information to the pilot.

Of course, if your asteroids are always moving linearly, then you could ignore it.

Cheers,

Timkin

##### Share on other sites
I don''t think that simple substitution will work.

In the soluitnos above, ppl use the laser vector like they know what it is, when all you know is its magnitude.

There''s about 10 different solutions on an old thread called "leading a target". Just go there. look it up right now.

##### Share on other sites
Thanks Timkin and _SHO_NUFF, I've just looked up the older thread (http://www.gamedev.net/community/forums/topic.asp?topic_id=88047) and was reading through it, and it almost looks to me like the recursive approximation function that was suggested there will be as fast (if not faster) and a lot easier (from a programmer's perspective) than calculating the exact intersection point.

quote:
Original post by TerranFury
Here it is (A = target; B = bullet; k = bullet speed). Just solve for each variable sequentially and plug it into the next equation.

D = Ainitpos-Binitpos
E = D * D
F = 2(Avelocity * D)
G = k2 - (Avelocity * Avelocity)
t = (F + sqrt(F2 + 4GE))/(2G)
Bvelocity = D/t + Avelocity

What I don't understand about this solution is what is a vector and what is a vector length? There's no indication of |Ainitpos| for length of the vector and Ainitpos for the actual vector, so it makes it a little confusing to interpret. Any demystification would be appreciated :-)

Also Timkin, in regards to your suggestion of a thin line between the green and red rectangles, while my asteroids are travelling in a straight line, I think your suggestion is still a worthy idea and I'll probably implement it since when the screen has about 4-5 asteroids and a few martian spaceships on it, things can get a bit cluttered Not to mention the martian ships, just realised they move all over the place. Is the connecting line normally curved to indicate the projected trajectory of the targetted object? Time to do some military research

[edited by - a1studmuffin on May 3, 2002 3:53:20 AM]

• 16
• 11
• 10
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999748
×