# Moving turret aiming at moving target

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