I saw this question: http://gamedev.stackexchange.com/questions/4995/predicting-enemy-position-in-order-to-have-an-object-lead-its-target and followed the link in the answer to stack overflow. In the stack overflow page I used the 2nd answer, the one that is a large mathematical derivation. My situation is a little different though. My first question though is will the answer provided in the stack overflow page even work to begin with, assuming the original circumstances of moving target and stationary shooter. My situation is a little different than that situation. My target moves, the shooter moves, and the bullets from the shooter start off with the velocities in x and y added to the bullets' x and y velocities. If you are sliding to the right, the bullets will remain in front of you as you move so as long as your velocity remains constant. What I'm trying to do is to get the enemy to be able to determine where they need to shoot in order to hit the player. Unless the player and enemy is stationary, the velocity from the ship adding to the velocity of the bullets will cause a miss. I'd rather like to prevent that. I used the formula in the stack overflow answer and did what I thought were the appropriate adjustments. I've been banging at this for the last four hours and I just can't make it click. It is probably something really simple and boneheaded that I am missing (that seems to be a lot of my problems lately). -------------------------------------------------------------------------------------------- Here is the solution presented from the stack overflow answer: It boils down to solving a quadratic equation of the form: a * sqr(x) + b * x + c == 0 Note that by sqr I mean square, as opposed to square root. Use the following values: a := sqr(target.velocityX) + sqr(target.velocityY) - sqr(projectile_speed) b := 2 * (target.velocityX * (target.startX - cannon.X) + target.velocityY * (target.startY - cannon.Y)) c := sqr(target.startX - cannon.X) + sqr(target.startY - cannon.Y) Now we can look at the discriminant to determine if we have a possible solution. disc := sqr(b) - 4 * a * c If the discriminant is less than 0, forget about hitting your target -- your projectile can never get there in time. Otherwise, look at two candidate solutions: t1 := (-b + sqrt(disc)) / (2 * a) t2 := (-b - sqrt(disc)) / (2 * a) Note that if disc == 0 then t1 and t2 are equal. If there are no other considerations such as intervening obstacles, simply choose the smaller positive value. (Negative t values would require firing backward in time to use!) Substitute the chosen t value back into the target's position equations to get the coordinates of the leading point you should be aiming at: aim.X := t * target.velocityX + target.startX aim.Y := t * target.velocityY + target.startY ----------------------------------------------------------------------------------------------- Here is my code with explanation. Code without the textbookish explanation at the end. Just wanted there to be no question of what I was trying to do at each step and whether or not that step was actually doing it. First I calculate the heading directly between the enemy ship and the player: //rad2degree = 57.3248407643 heading = std::atan2( playerY - enemyY , playerX - enemyX ) * RAD2DEGREE; Next I calculate the x directional vector and the y direction vector based on the angle from the enemy to the player: //degree2rad = 0.01744444444 float xdirect = std::cos(heading * DEGREE2RAD); //get direction vectors for the shot float ydirect = std::sin(heading * DEGREE2RAD); Now I figure out the bullet's velocity in the x and y direction. //x velocity of bullet = x directional vector * speed of 30 + enemy x velocity bulletVelocityX = (xdirect * 30 + enemyVelocityX); bulletVelocityY = (ydirect * 30 + enemyVelocityY); //same as x but in y Next, I determine the speed of the bullet: bulletSpeed = std::sqrt(std::pow(bulletVelocityX,2) + std::pow(bulletVelocityY, 2); Now I determine a for the solution. cannon from the stack overflow answer is enemy in this case. The bullets originate from the enemy's location. Anywhere cannon.x or cannon.y are used, I use enemyX and enemyY instead. float a = std::pow((playerVelocityX -enemyVelocityX),2) + std::pow((playerVelocityY - enemyVelocityY),2) - std::pow(bulletSpeed,2); I subtract the enemy velocity from the player velocity because whatever velocity the enemy is moving will be the opposite in relation to the player. IE enemy moves to the right, the player is moving to the left relative to the enemy. Doubly so if the player is also moving to their right but cancels if the player is moving left at the same rate enemy is moving right. The original answer assumes the shooter is stationary, which is effectively subtracting 0 from the target's velocity. However, my shooter is also moving which will change the path of the projectiles it fires. Next I calculate b: float b = 2*( (playerVelocityX - enemyVelocityX) * (playerX - enemyX) + (playerVelocityY - enemyVelocityY) * (playerY - enemyX)); I am again taking the shooter's velocity into account in this to get the relative velocity between shooter and target. Now to calculate c: float c = std::pow((playerX - enemyX),2) + std::pow((playerY - enemyY),2); Now I find the discriminate: float disc = std::pow(b,2) - 4 * a * c; Next I determine if the descriminate is less than zero, and if not, find out which time value is smaller and use that one to compute the coordinates of where I need to point the ship in order to hit the player. if(disc >= 0) { float time1 = (-b + std::sqrt(disc) ) / 2 * a; float time2 = (-b - std::sqrt(disc) ) / 2 * a; if(time1 < time2 ) { float shootx = time1 * (playerXvelocity - xVelocity) + playerX; float shooty = time1 * (playerXvelocity - yVelocity) + playerY; heading = std::atan2( shooty, shootx) * RAD2DEGREE; } else { float shootx = time1 * (playerXvelocity - xVelocity) + playerX; float shooty = time1 * (playerXvelocity - yVelocity) + playerY; heading = std::atan2( shooty, shootx) * RAD2DEGREE; } } If the discriminate is less than zero, I just point the ship at the player and tell the ship not to shoot. The result of my code is that the ship fires in perpendicular angles, doesn't fire at all, or just picks a random angle. I don't understand it and I can't figure out where I'm going wrong. Any help would be greatly appreciated on the matter. I'm pretty much at my wits end on something that should probably be pretty simple. If necessary I could provide a video of the enemy's behavior. Thank you for your time (which was quite a bit I'm sure). Code without explanations: heading = std::atan2( playerY - enemyY , playerX - enemyX ) * RAD2DEGREE; //get angle to player float xdirect = std::cos(heading * DEGREE2RAD); //get direction vectors for the shot float ydirect = std::sin(heading * DEGREE2RAD); bulletSpeed = std::sqrt(std::pow(bulletVelocityX,2) + std::pow(bulletVelocityY, 2); float a = std::pow((playerVelocityX -enemyVelocityX),2) + std::pow((playerVelocityY - enemyVelocityY),2) - std::pow(bulletSpeed,2); float b = 2*( (playerVelocityX - enemyVelocityX) * (playerX - enemyX) + (playerVelocityY - enemyVelocityY) * (playerY - enemyX)); float c = std::pow((playerX - enemyX),2) + std::pow((playerY - enemyY),2); float disc = std::pow(b,2) - 4 * a * c; if(disc >= 0) { float time1 = (-b + std::sqrt(disc) ) / 2 * a; float time2 = (-b - std::sqrt(disc) ) / 2 * a; if(time1 < time2 ) { float shootx = time1 * (playerXvelocity - xVelocity) + playerX; float shooty = time1 * (playerXvelocity - yVelocity) + playerY; heading = std::atan2( shooty, shootx) * RAD2DEGREE; } else { float shootx = time1 * (playerXvelocity - xVelocity) + playerX; float shooty = time1 * (playerXvelocity - yVelocity) + playerY; heading = std::atan2( shooty, shootx) * RAD2DEGREE; } } else { shouldShoot = false; }