Compute target lead

This is a discussion on Compute target lead within the Game Programming forums, part of the General Programming Boards category; Cool ....

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

  2. #17
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    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
    Code:
    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.
    Code:
    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).
    Code:
    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.

    Code:
    (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. http://www.dandongs.com/

  3. #18
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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:

    Code:
    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.
    Code:
    //try
    //{
    	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
    Location
    Canada
    Posts
    8,048
    Just a little SDL program I made a while back which involves these sorts of calculations: http://dwks.theprogrammingsite.com/m...332814f.tar.gz
    (Or http://dwks.theprogrammingsite.com/m....0-332814f.zip)

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

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 2 of 2 FirstFirst 12
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, 11:46 AM
  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, 09:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21