Original Post
I'm trying to make a stationary structure fire a bullet that intercepts a moving target.
At one point I had this code:
It wasn't accurate enough, so I found this:
I figured that xAimPoint would tell me where I'd need to shoot in order to hit the moving target. So I took xAimPoint and did this:
The bullet is still not accurate enough. I'm passing in these values to CalculateAimPoint:
I'm guessing it's got something to do with fInterceptSpeed? I know this is a really vague post, but that's because it's my last resort before I PAY someone to fix it for me. :)
Cheers.
At one point I had this code:
PointF Movement::intercept(const PointF& shooter, float bullet_speed,const PointF& target, const PointF& target_velocity){ float a = bullet_speed * bullet_speed - target_velocity.Get_Dot_Product(target_velocity); float b = -2 * target_velocity.Get_Dot_Product(target - shooter); float c = -(target - shooter).Get_Dot_Product(target - shooter); return target + target_velocity * Maths::largest_root_of_quadratic_equation(a, b, c);}It wasn't accurate enough, so I found this:
bool CalculateAimPoint(const Vector& xP0, const Vector& xV0, const Vector& xP1, float fInterceptSpeed, Vector& xAimPoint){ Vector dP = xP0 - xP1; float a = xV0 * xV0 - (fInterceptSpeed * fInterceptSpeed); float b = 2.0f * (dP) * xV0; float c = dP * dP; float d = b*b - 4 * a * c; if (d < 0.0f) { return false; } d = (float) Maths::Sqrt(d); float t0 = (-b - d) / (2.0f * a); float t1 = (-b + d) / (2.0f * a); float t; if (t0 > t1) { float c = t0; t0 = t1; t1 = c; } if (t1 < 0.0f) { return false; } if (t0 >= 0.0f) { t = t0; } else { t = t1; } xAimPoint = xP0 + xV0 * t; return true;}I figured that xAimPoint would tell me where I'd need to shoot in order to hit the moving target. So I took xAimPoint and did this:
dir = Movement::get_dir_towards(src, xAimPoint);PointF Movement::get_dir_towards(const PointF& source, const PointF& target){ const float cos_a = (target.y - source.y) / sqrt(pow(target.x - source.x, 2) + pow(target.y - source.y, 2)); const float sin_a = (target.x - source.x) / sqrt(pow(target.x - source.x, 2) + pow(target.y - source.y, 2)); return PointF(sin_a, cos_a);}The bullet is still not accurate enough. I'm passing in these values to CalculateAimPoint:
PointF xP0 = target_pos;PointF xV0 = target.Get_Direction() * target_speed;PointF xP1 = source_pos;float fInterceptSpeed = projectile_speed + /* source speed would go here, but we're not moving */0;I'm guessing it's got something to do with fInterceptSpeed? I know this is a really vague post, but that's because it's my last resort before I PAY someone to fix it for me. :)
Cheers.
