1
\$\begingroup\$

I've found this post Turret Aiming Formula on Reddit and a nice answer a while back.

The solution in the answer works great for still sanding turrets but is almost unusable for moving turrets since the speed of the turret is not applied to the bullet speed. The bullet still have the same fixed speed all the time but should be faster if the turret is moving in the same direction.

There are also a lot of answers here, but non of them respects the speed of the turret.

Is it possible do integrate turret speed into this formula or is there any other known solution?

My current code without turret speed:

Vector2f toTarget =  target.position - attacker.position;

float a = Vector2f.dot(target.velocity, target.velocity) - (bulletSpeed * bulletSpeed);
float b = 2 * Vector2f.dot(target.velocity, toTarget);
float c = Vector2f.dot(toTarget, toTarget);

float p = -b / (2 * a);
float q = sqrt((b * b) - 4 * a * c) / (2 * a);

float t1 = p - q;
float t2 = p + q;
float t;

if (t1 > t2 && t2 > 0){
    t = t2;
}
else{
    t = t1;
}

Vector2f aimSpot = target.position + target.velocity * t;

update:

the solution is to use relative velocity instead of target.velocity as suggested in the answer

relativeVelocity = target.velocity - attacker.velocity 

and then use bulletSpeed + attacker.velocity for the bullet velocity

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You could treat the turret as stationary and subtract the velocity of the turret to the target's velocity before the start of the code, provided it works for a motionless turret.

To subtract velocity vectors, if the target is moving 4 pixels left and 8 pixels up in a given time, but the turret is moving 4 pixels right and 3 pixels up in the same time, the turret could be said to be motionless and we treat movement up as positive, down as negative, left as positive and right as negative, then we have:

Target(4, 8), Turret(-4, 3)

--> Turret(0,0), Target(4-(-4), 8-3)

--> Turret(0,0), Target(8, 5)

\$\endgroup\$
2
  • \$\begingroup\$ You'll want to subtract, not add. Otherwise you actually worsen the error. (Imagine the turret and target are moving together with the same speed & direction - they are effectively stationary with respect to one another, and no target leading is required. But adding the velocities would give the target a large effective velocity, suggesting that a large amount of target leading is necessary) \$\endgroup\$
    – DMGregory
    Apr 4, 2014 at 19:13
  • \$\begingroup\$ Yes, sorry, I wasn't thinking. Edited. \$\endgroup\$ Apr 4, 2014 at 19:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .