/// <summary> /// Algorithm to predict the position of the target /// </summary> public static bool GetPredictedTargetPosition(MyGunBase gun, MyEntity shooter, MyEntity target, out Vector3 predictedPosition, out float timeToHit, float shootDelay = 0) { Debug.Assert(target != null && target.PositionComp != null, "Null target!"); Debug.Assert(shooter != null && shooter.PositionComp != null, "Null shooter!"); if (target == null || target.PositionComp == null || shooter == null || shooter.PositionComp == null) { predictedPosition = Vector3.Zero; timeToHit = 0; return false; } Vector3 targetPosition = target.PositionComp.WorldAABB.Center; Vector3 muzzlePosition = gun.GetMuzzleWorldPosition(); Vector3 toTarget = targetPosition - muzzlePosition; Vector3 targetVelocity = Vector3.Zero; if (target.Physics != null) { targetVelocity = target.Physics.LinearVelocity; } Vector3 shooterVelocity = Vector3.Zero; if (shooter.Physics != null) { shooterVelocity = shooter.Physics.LinearVelocity; } Vector3 diffVelocity = targetVelocity - shooterVelocity; float projectileSpeed = GetProjectileSpeed(gun); float a = diffVelocity.LengthSquared() - projectileSpeed * projectileSpeed; float b = 2 * Vector3.Dot(diffVelocity, toTarget); float c = toTarget.LengthSquared(); float p = -b / (2 * a); float q = (float)Math.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; } t = t + shootDelay; predictedPosition = targetPosition + diffVelocity * t; Vector3 bulletPath = predictedPosition - muzzlePosition; timeToHit = bulletPath.Length() / projectileSpeed; return true; }
Vector3 GetSmokePosition() { return(m_gunBase.GetMuzzleWorldPosition() - WorldMatrix.Forward * 0.5f); }
/// <summary> /// Algorithm to predict the position of the target /// </summary> public static bool GetPredictedTargetPosition(MyGunBase gun, MyEntity shooter, MyEntity target, out Vector3 predictedPosition, out float timeToHit, float shootDelay = 0) { Debug.Assert(target != null && target.PositionComp != null, "Null target!"); Debug.Assert(shooter != null && shooter.PositionComp != null, "Null shooter!"); if (target == null || target.PositionComp == null || shooter == null || shooter.PositionComp == null) { predictedPosition = Vector3.Zero; timeToHit = 0; return(false); } Vector3 targetPosition = target.PositionComp.WorldAABB.Center; Vector3 muzzlePosition = gun.GetMuzzleWorldPosition(); Vector3 toTarget = targetPosition - muzzlePosition; Vector3 targetVelocity = Vector3.Zero; if (target.Physics != null) { targetVelocity = target.Physics.LinearVelocity; } Vector3 shooterVelocity = Vector3.Zero; if (shooter.Physics != null) { shooterVelocity = shooter.Physics.LinearVelocity; } Vector3 diffVelocity = targetVelocity - shooterVelocity; float projectileSpeed = GetProjectileSpeed(gun); float a = diffVelocity.LengthSquared() - projectileSpeed * projectileSpeed; float b = 2 * Vector3.Dot(diffVelocity, toTarget); float c = toTarget.LengthSquared(); float p = -b / (2 * a); float q = (float)Math.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; } t = t + shootDelay; predictedPosition = targetPosition + diffVelocity * t; Vector3 bulletPath = predictedPosition - muzzlePosition; timeToHit = bulletPath.Length() / projectileSpeed; return(true); }