Thread: Compute target lead

  1. #16
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Zagreb, Croatia
    Cool .
    Arduino rocks!

  2. #17
    Amazingly beautiful user.
    Join Date
    Jul 2005
    If you knew I'd have to kill you
    My friend solved a similar problem in 2-d for turrets firing at moving targets.
    You have a few equations for the motion of the bullet
    bulletX(t) = initialX + bulletVelocityX * t
    bulletY(t) = initialY + bulletVelocityY * t
    sqrt((bulletVelocityX - firingShipVelocityX)^2 + (bulletVelocityY - firingVelocityY)^2) = bulletVelocity
    You also have a couple for the predicted motion of a target.
    targetX(t) = initialTargetX + targetXVelocity * t
    targetY(t) = initialTargetY + targetYVelocity * t
    You can now solve X and Y seperately (with bulletVelocityX/Y and t being unknowns).
    initialTargetX + targetXVelocity * t = initialX + bulletVelocityX * t
    t = (initialX - initialTargetX) / (targetXVelocity - bulletVelocityX)
    initialTargetY + targetYVelocity * t = initialY + bulletVelocityY * t
    t = (initialY - initialTargetY) / (targetYvelocity - bulletvelocityY)
    Now you have two equations, one which tells you what time the x-pos would intersect given bulletXVelocity, and one given Y.

    (initialX - initialTargetX) / (targetXVelocity - bulletVelocityX) = (initialY - initialTargetY) / (targetYVelocity - bulletVelocityY)
    So if you fill in an estimate X velocity for the bullet, you get back the Y velocity you'd want to fire it at and vice versa. Of course, you want the net velocity of the bullet to be the firing speed of the gun, minus whatever speed the ship is already moving it. We couldn't find a way to solve this part in to one equation, but you can iterately test X velocities till you find something that satisfies your velocity constraint, something ALA newtons method gets fairly accurate in 3-4 iterations.

    Hope that's helpful.
    Programming Your Mom.

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    I'd describe the path of the laser and the path of the object parametrically, then solve the following three equations simultaneously for t, laser_vel_x, and laser_vel_y:

    target_x + target_vel_x * t = laser_x + ( laser_vel_x + source_vel_x ) * t
    target_y + target_vel_y * t = laser_y + ( laser_vel_y + source_vel_y ) * t
    laser_vel_x * laser_vel_x + laser_vel_y * laser_vel_y = laser_speed * laser_speed
    Having found t, which you are not directly interested in (it is the time it will take for the laser to reach the target), and the two laser velocities, you back-substitute to find the intersection point. This point is where you should put the reticle.

    The math is identical in three dimensions, you just have a fourth equation to solve.

    (And I don't mean solving these equations at run-time via some iterative method. The solution exists in closed form. Due to eq 3, the solution will involve taking a square root at some point. And in some cases, the radicand of the square root will be negative, which indicates that it is IMPOSSIBLE to hit the target -- for instance, it is moving away from you faster than the laser can travel)
    Last edited by brewbuck; 10-01-2009 at 12:18 PM.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Just a little SDL program I made a while back which involves these sorts of calculations:

    There's a comment at the top of the source which explains what's going on.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to compute the variable type range
    By webofunni in forum C Programming
    Replies: 1
    Last Post: 08-04-2009, 01:28 AM
  2. Compute without Limit - support for fractions and 2G Digits!
    By etlam in forum Projects and Job Recruitment
    Replies: 14
    Last Post: 02-07-2008, 12:46 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. WANTED: Lead Programmer
    By fluid_hres in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-10-2006, 07:25 PM
  5. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM