I saw this question: 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 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, and the shooter moves. Also, and the bullets from the shooter start off with the velocities in x and yshooter's velocity is added to the bullets' x and y velocities, i. If you aree. bullets fired while sliding to the right, the bullets will remain in front of you as you move so as long as yourhave a greater velocity remains constanttoward the right.
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 Using the linked SO solution, unless the player and enemy isare stationary, the velocity from the ship adding to the velocity of the bulletsdifference 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 How can I just can't make it click. It is probably something really simple and boneheadedprevent 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. It boils down to solving a quadratic equation of the form:
Note that by sqrsqr
I mean square, as opposed to square root. Use the following values:
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, after being corrected by Sam Hocevar (thank you again for your help!). It still doesn't work. For some reason it never enters the section of code inside the if(disc >= 0) (obviously because it is always less than zero but...). However, if I plug the numbers from my game log on the enemy and player positions and velocities it outputs a valid firing solution. I have looked at the code side by side a couple of times now and I can't find any differences. There has got to be something simple I'm missing here. If someone else could look at this code and determine what is going on here I'd appreciate it. I know it's not going through that section because if it were, shouldShoot
would become true and the enemy would be blasting away at the player.
This section calls the function in question, CalculateShootHeading()
if(shouldMove)
{
UseEngines();
}
x += xVelocity;
y += yVelocity;
CalculateShootHeading();
if(shouldShoot)
{
ShootWeapons();
}
UpdateWeapons();
This is CalculateShootHeading(). This is inside the enemy class so x and y are the enemy's x and y and the same with velocity. One output from my game log gives Player X = 2108, Player Y = -180.956, Player X velocity = 10.9949, Player Y Velocity = -6.26017, Enemy X = 1988.31, Enemy Y = -339.051, Enemy X velocity = 1.81666, Enemy Y velocity = -9.67762, 0 enemy projectiles. The output from the console tester is Bullet position = 2210.49, -239.313 and Player Position = 2210.49, -239.313. This doesn't make any sense. The only thing that could be different is the code or the input into my function in the game and I've checked that and I don't think that it is wrong as it's updated before this and never changed.
float const bulletSpeed = 30.f;
float const dx = playerX - x;
float const dy = playerY - y;
float const vx = playerXVelocity - xVelocity;
float const vy = playerYVelocity - yVelocity;
float const a = vx * vx + vy * vy - bulletSpeed * bulletSpeed;
float const b = 2.f * (vx * dx + vy * dy);
float const c = dx * dx + dy * dy;
float const disc = b * b - 4.f * a * c;
shouldShoot = false;
if (disc >= 0.f)
{
float t0 = (-b - std::sqrt(disc)) / (2.f * a);
float t1 = (-b + std::sqrt(disc)) / (2.f * a);
if (t0 < 0.f || (t1 < t0 && t1 >= 0.f))
{
t0 = t1;
}
if (t0 >= 0.f)
{
float shootx = vx + dx / t0;
float shooty = vy + dy / t0;
heading = std::atan2(shooty, shootx) * RAD2DEGREE;
}
shouldShoot = true;
}