Here is a solution for calculating a weapon's linear velocity against a moving target in order to lead the target

I am creating a space game so gravity is not a factor but I wanted to calculate a weapon’s linear velocity when it’s fired against a moving target to ensure that the AI weapon leads the target with relative accuracy and ensuring the code is as simple as possible. With @pspeed’s help I solved the calculations and figured I would share the code for anyone who needs it.

static Vector3f calculateWeaponLinearVelocity(float weaponVelocity, Vector3f weaponPosition, Vector3f targetPosition,
		Vector3f targetLinearVelocity)
	float targetDistance = weaponPosition.subtract(targetPosition).length();
	float time = targetDistance / weaponVelocity;
	Vector3f finalTargetPosition = targetPosition.add(targetLinearVelocity.mult(time));
	return finalTargetPosition.subtractLocal(weaponPosition).normalizeLocal().mult(weaponVelocity);

Note: before we both get slammed about “accuracy”, we do realize that this is only a guess since it doesn’t take the prediction of where the ship will be at time ‘t’ into account. But this math is simpler.

1 Like

That is correct. This is a BEST GUESS with super fast math.

Slower but perfect solution:

result from last = direct vector to target

  1. aim at target with result from last,
  2. calculate time to target for bullet
  3. readjust the targets position by predicting new position after time to target
  4. result from last = adjusted aim vector for now better predicted position
  5. now start from 1 again to improve accuracy further.

do this for around 2-5 loops based on weapon type and required accuracy. (more loops for stuff like sniper, for a minigun 1 might be enough with the inherent spread)

This can btw be improved witha heuristic to work for non linear weapons, like accelerating rockets, self targeting ammonution ect. basically just probe a few different shoot scenarios based on the heuristic and continue from the one with the best min distance between flightpath and target.

Yes an iterative approach is more accurate but for my fire and forget missiles in a space game the single iteration approach is simple fast and accurate enough since your targets is a space ship.

Sure it should work most of the time fine enough.

1 Like

Could you try this one? Found it on an online paper but i am too lazy to write a test :wink: It should take the ships movement into account

    static Vector3f calculateWeaponLinearVelocityTrue(float weaponVelocity, Vector3f weaponPosition, Vector3f targetPosition,
                                                      Vector3f targetLinearVelocity) {
        Vector3f tmp = targetPosition.clone();
        float impactTime = tmp.subtractLocal(weaponPosition).length() / (weaponVelocity - targetLinearVelocity.length());
        return tmp.normalizeLocal().multLocal(weaponVelocity);

Awesome! I have a very weird system in place for this thing and will be gladly replacing it with something that makes sense :smiley:

1 Like