Adventures in Aimimg


I wanted some enemies to be able to predict the player's movements and shoot where they will be when the bullet reaches them. This is what is known as predictive aiming, and I knew it would involve a lot of math but I didn't exactly know how to go about doing it at first.

My first idea was to predict where the player would be when the bullet would get to the position the player is currently in. This is not an exact solution, but I thought if I could iterate this process with the output of the last time as an input I could approach the correct position. Unfortunately, iterating like this only caused the aim to go much wider with each iteration. Clearly this was not the way to go.

After searching on Google I came across what is generally considered to be the correct solution to this problem: triangulating the correct position using the cosine law. In short, this involves solving a quadratic equation with the distance from the enemy to the player, the player's current speed, and the bullet's speed all as inputs. However, when I first implemented it the bullets would go off in even wider directions when the player was moving, and even go backwards if the player was moving towards the enemy.

The problem turned out to be that the length of one of the sides of the triangle was not the output of the quadratic equation as the tutorial I was watching said, but its square root. Fixing this caused the new enemy's aim to be almost perfect, being slightly off in some cases. I believe I can fix this by increasing the size of the bullet's hitbox.

Get Drone Wars

Leave a comment

Log in with itch.io to leave a comment.