Thread: Compute target lead

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Compute target lead

    I'm trying to compute target lead so I can give the player a reticle to aim for when firing at the enemies in my game.

    Here are my calculations but something does not seem right:

    Code:
    //Create vector from current position to target
    D3DXVECTOR3 toTarget = target - position;
    
    //Calc distance to target
    float distance = D3DXVec3Length(&toTarget);
    
    //Calc final laser speed
    float finalSpeed = Ship.GetSpeed() + LaserCannon.GetSpeed();
    
    //Calc time to the target
    float timeToTarget = finalSpeed / distance;
    
    //Calc 3D lead position
    D3DXVECTOR3 leadPosition = (target.velocityVector * target.Speed * timeToTarget * frameDelta);
    After this I project the leadPosition and arrive at the final screen coords. I then draw the image for the lead reticle or pipper at this 2D point.

    Does this look right?

    My thought was since the laser moves at a set speed and since I'm treating the laser as a projectile I should add the laser speed and ship speed. Then I find out how long it will take to trave the distance from the current position to the target's current position. I use that time to estimate the target's new position at the time I just computed by using the target's velocity and speed and the computed time and then finally taking into account the frameDelta to put the entire system into the right units for the game.

    The example is a very simplified version of what is in the game. I took out a lot of resource manager calls to make it easier to read.
    Last edited by VirtualAce; 09-24-2009 at 07:17 PM.

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