In our last post we added a laser to our spaceship that shot asteroids. It was a very powerful laser, because it would shoot absolutely everything in its path, effectively shooting through all the asteroids in one go. Often in a game, you don’t want this: you want to shoot only the first thing that the laser hits. We can implement this by continuing our maths from last post.

Our last post finished with this quadratic equation:

The variable of interest there was , which is, in effect, a distance along the line. We have a direction vector for the laser (the pink arrow below), and to make up the line for the laser, we effectively lay an infinite amount of these arrows end-to-end, like in the diagram below. The variable keeps track of how many of these pink arrows we are from the origin of the line (the centre of the spaceship):

So if we actually finish calculating the value for where each asteroid is hit by the line, then the asteroid that we hit with the lowest associated positive value is the first one along the line of the laser (in this diagram, 1.7):

Note that we don’t want to allow negative values, because then we would shoot asteroids that were behind the ship! So, back to our maths equation — we saw last post that a quadratic can be rearranged to:

Where, for us:

The quadratic equation gives two solutions: this will be the value of scalar where the line enters the circle, and a second solution for the value of scalar where the line exits the circle (think about it: an infinite straight line can’t enter a circle without leaving it again). We don’t need to worry about which is which, we are just interested in the lowest scalar value that we find, as that will be the nearest one. All this comes out in the code that we need:

Asteroid nearest = null; double nearestScalar = 1000; //huge value for (Asteroid asteroid : (List<Asteroid>)getWorld().getObjects(Asteroid.class)) { double distX = getX() - asteroid.getX(); double distY = getY() - asteroid.getY(); double a = dirX * dirX + dirY * dirY; double b = 2 * distX * dirX + 2 * distY * dirY; double c = distX * distX + distY * distY - asteroid.getRadius() * asteroid.getRadius(); if (b * b - 4 * a * c >= 0) { double scalarA = (-b + Math.sqrt(b * b - 4 * a * c)) / (2*a); double scalarB = (-b - Math.sqrt(b * b - 4 * a * c)) / (2*a); if (scalarA >= 0 && scalarA < nearestScalar) { nearestScalar = scalarA; nearest = asteroid; } if (scalarB >= 0 && scalarB < nearestScalar) { nearestScalar = scalarB; nearest = asteroid; } } } if (nearest != null) { if (nearest.hit(30)) { destroyedAsteroid(); } }

So there it is — the quadratic formula actually implemented and used in code. You can have a play with the scenario (and download the full source) on the Greenfoot site.

## Leave a Reply