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:
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.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);
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.