0

See my image below:

enter image description here

The black-right-arrow is my ship and the red circle my enemy (radius 1.2). Both are in movement without acceleration. I need kill the red circle with precision, what type of calculations can I use with only linear velocity, position, and radius data?

Note: The bullet has 0.5s of lifetime and the speed 40 and radius 0.2

I'm trying to implement this in C#.

public static Vector3 CalculatePreciseShootDirection(Vector3 shipPosition, Vector3 shipVelocity,
                                                         Vector3 targetPosition, Vector3 targetVelocity,
                                                         float bulletLifetime, float bulletSpeed)
        {
            Vector3 shipFuturePosition = shipPosition + bulletLifetime * shipVelocity;
            Vector3 targetFuturePosition = targetPosition + bulletLifetime * targetVelocity;
            Vector3 relativeDisplacement = targetFuturePosition - shipFuturePosition;
            float timeToTarget = Vector3Magnitude(relativeDisplacement) / bulletSpeed;
            Vector3 adjustedTargetPosition = targetPosition + timeToTarget * targetVelocity;
            Vector3 shootingDirection = adjustedTargetPosition - shipPosition;
            float shootingDistance = Vector3Magnitude(shootingDirection);
            float maxDistance = bulletLifetime * bulletSpeed;
            if (shootingDistance <= maxDistance)
            {
                return Vector3.Normalize(shootingDirection);
            }
            else
            {
                return Vector3.Zero;
            }
        }
        
        private static float Vector3Magnitude(Vector3 v)
        {
            return (float)Math.Sqrt(v.X * v.X + v.Y * v.Y + v.Z * v.Z);
        }
PerduGames
  • 1,108
  • 8
  • 19
  • Step 1: rewrite the velocity of the red dot as the velocity _relative_ to your ship. The dot's position over time is now a line, and you can find the segment on that line that can be hit by a bullet given the values you stated. – Mike 'Pomax' Kamermans Jul 31 '23 at 18:01
  • If you aim at the center of the target, the radius information is useless, isn't it ? – Yves Daoust Jul 31 '23 at 19:05
  • Is the speed of the bullet absolute (always 20, in any direction) or to be added to the speed of the ship as in the real world ? – Yves Daoust Jul 31 '23 at 19:19
  • yes, all speed are constant, without acceleration. – PerduGames Jul 31 '23 at 20:05
  • @Mike'Pomax'Kamermans I don't understanding, can you explain – PerduGames Jul 31 '23 at 20:06
  • The radius doesn't matter at all - you aim at the center anyway. I think your issue is that currently you always assume the full bullet lifetime .. what if we can intercept the target already before that? – derHugo Aug 01 '23 at 07:45
  • This is a duplicate of [this question](https://stackoverflow.com/q/11369616/3871028) And there you can get the formulation used – Ripi2 Aug 01 '23 at 19:45
  • see [Projectile Aim Prediction with Target Acceleration and Bullet Deceleration Varying with Angle](https://stackoverflow.com/a/71817221/2521214) and [C++ intersection time of 2 bullets](https://stackoverflow.com/a/71808916/2521214) for some inspiration – Spektre Aug 08 '23 at 09:00

1 Answers1

1

You need to add both velocities together and set that as the initial speed of the projectile.

like so:

projectile speed = (X= V1.x+V2.x, Y= V1.y+V2.y)