Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lead Calculation

Started by arm Nov 4, 2004 at 6:20 PM 2 replies 1.1k views
Original Post
arm
arm
Hi, I'm having trouble with something that should be a simple problem. The problem is this: Given a target, its velocity, the start position for the projectile and the speed of the projectile calculate the position to shoot at to intercept the target. I had a look on the forums for the answer and found several, one was an iterative approximation and the other was an algebraic solution. The approximation works OK but the algebraic one does not (which is the one I want). The source code for the algebraic solution is given below (I tested it in matlab). All of the function parameters are 2D vectors except for bulletSpeed which is a scalar. function result = Lead(bulletOrigin, bulletSpeed, targetOrigin, targetVelocity) D = targetOrigin - bulletOrigin; E = dot(D,D); F = 2 * dot(targetVelocity, D); G = bulletSpeed * bulletSpeed - dot(targetVelocity, targetVelocity) T = (F + sqrt(F * F + 4 * G * E)) / (G * 2); result = D / T - targetSpeed; This code just does not work, it either coughs up the wrong answer or throws a divide by zero. What the hell is the formula for intercepting a target??? The solution would be much appreciated - this is driving me nuts.
jacksaccountongamedev
jacksaccountongamedev
Hello.

Here is my code for the solution described:

//starting position//bullet speed//target position//target velocityVector2D LeadTarget( Vector2D sp, float bs, Vector2D tp, Vector2D tv ){	Vector2D D = tp - sp;	float E = D.Dot( D );	float F = 2 * tv.Dot( D );	float G = bs * bs - tv.Dot( tv );	float t = ( F + sqrt( F * F + 4 * G * E) ) / ( G * 2 );	return D / t + tv;}


The notable difference is the last step:

D / T - targetSpeed;

Compared against:

D / t + tv;

The odd thing is that D / T – targetSpeed does not actually make mathematical sense – subtracting a scalar (I assume targetSpeed is the length of targetVelocity) from a vector is rather strange (to my knowledge).

Hope this helps,
Jackson Allan
LilBudyWizer
LilBudyWizer
Well, taking a simple approach, you have an equation, i.e. bo+bv*t=to+tv*t. That's a vector equation so in 2D that is actually two equations. You have three unknowns so you need another equation. That equation is (bv.x)2+(bv.y)2=bs2 where bv is the bullet velocity and bs is the bullet speed. Now to get that into a form where you can simplify it with dot products you have to solve the two linear equations for the x and y components of the bullet velocity and then substitute those into the third equation. Eventually you get it down to dot(to-bo,to-bo)+2*(tv,to-tv)*t+(dot(tv,tv)-bs2)*t2. So the problem seems to be in F since it is 2*dot(tv,to-bo) rather than 2*dot(tv,to-tv).

Oops, screwed up the derivation. It is 2*dot(tv,to-bo) so F is right. It is the last step that is wrong. You are solving BO+BV*t=TO+TV*t for BV so (TO-BO)/t+TV.

Gads, screwed up the t term as well, that should be 2*(tv,to-bo). Anyway, that's how you derive the equation. I don't think the dot products are any more than a way to simplify the equation.

[Edited by - LilBudyWizer on November 5, 2004 12:40:32 AM]
Keys to success: Ability, ambition and opportunity.
arm
arm
Cheers jack,

Thanks for that, sometimes you can stare for hours and not see that simple typo thats causing all the problems.

Back to work on my asteroids clone...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.