Introduction

When a bullet or beam hits something, sparks fly off in the direction the bullet would bounce. For that, we need to know two things about the hit: where it happened (the contact point) and which way the surface faces there (the surface normal ). A surface normal is a vector of length one that points straight out of a surface.

Hit Sparks

Hit Sparks

So where do we get the normal from? The simplest guess is the direction from the object’s center to the hit point. That works for circles, but for flat faces and most convex hulls, it points the wrong way.

Luckily, the collision tests can work out the normal and the contact point along the way. So in this post, we’ll first look at how a hit turns into sparks, and then at where the normal and the contact point come from.

Using a Hit

Whose Normal Is It?

Where two shapes touch, there are two surfaces facing opposite ways. So whose normal does a collision result carry?

The rule is this: when we ask “does A collide with B?”, the normal belongs to B’s surface and points toward A. When the collision is passed on to the two objects, A gets that normal and B gets it reversed. That way, each object receives the other object’s surface, pointing back at itself, and that’s exactly what it needs to work out a bounce. For the few cases without a sensible answer, such as two circles with the same center, the normal is zero, which means “unknown”.

How a Bounce Works

Now that we have the normal, which way do the sparks go? Not straight out along the normal, but in the direction the bullet would bounce, much like a ball off a wall: the angle it comes in at equals the angle it leaves at , both measured from the normal. So a head-on hit sends the sparks straight back the way the bullet came, while a glancing hit sends them skimming away along the surface.

And what if the normal is zero? Then there’s no surface to bounce off, so the sparks simply fly back the way the bullet came.

Spawning the Sparks

The bounce direction only tells us where the burst goes as a whole. If every spark flew off exactly along it, we’d get a single line of dots instead of a burst. So we add some randomness:

  • Direction: a random angle within a 90° cone around the bounce direction, so up to 45° to either side.
  • Speed: a random value between 40 and 175 pixels per second. The slow sparks stay close to the impact and the fast ones fly farther, which makes the burst look like a spray.
  • Start position: a random point within 3 pixels of the contact point, so the sparks don’t all start on the same pixel.
  • Count: between the bullet’s damage and one and a half times that, but at least 4, so heavier hits throw more sparks.
  • Lifetime: half a second. Over that time, each spark fades from pale yellow to dark red and shrinks to half its size.

Getting a Hit

That’s the whole effect. So where do the normal and the contact point come from?

The game has two kinds of collision test (see part trois ). The discrete test checks whether two shapes overlap at the end of the frame. The swept test checks whether they touched at any point during the frame, so that fast bullets can’t pass through things (see part deux ). Both give us a normal, but each in its own way.

Normals from the Discrete Test

Let’s start with the discrete test. The game’s colliders come in three shapes: circles, axis-aligned boxes and convex hulls. Each pair of shapes has its own discrete test, and each test computes a direction along the way. That direction is the normal:

  • Box and box: the overlap is itself a small box. The normal points along its thinner side, the direction in which the two overlap least.
  • Circle and circle: the line between the two centers.
  • Circle and box: the line from the closest point on the box to the circle’s center.
  • Hull and hull (or box): the separating axis test projects both shapes onto each edge normal and looks for a gap. The axis with the smallest overlap is the normal.
  • Hull and circle: the line from the closest point on the hull to the circle’s center.

Normals from the Swept Test

What about the swept test? It looks at how the shapes came together, so its normals come from a different place:

  • Boxes: the test grows the target box by the mover’s size and casts a ray into it (with the slab method ). The normal points straight out of the face the ray enters through last.
  • Circles: the test finds the moment the two circles just touch (see Pool Hall Lessons ). The normal is the line between the centers at that moment.
  • Hulls: the test steps the shapes toward each other (conservative advancement ) until the gap closes, and measures the gap at every step. The direction of the last measured gap is the normal.

The Way Out Isn’t the Way In

But there’s one case where the two tests disagree: a fast bullet can end the frame deep inside a box, close to one of its side faces. Since the discrete test only sees the end of the frame, it reports the direction of least overlap, out through the side. That’s the shortest way out, but the bullet came in through the top.

So which result does the game use? Whenever two overlapping shapes moved relative to each other, the collision code runs the swept test anyway, to find out when contact began. So it takes the swept test’s normal and contact point whenever there is one. That way, the sparks start on the top face, where the bullet came in, and fly off as if it had bounced off that face.

Where a Hit Is Placed

Impact effects spawn at the contact point, so the contact point has to be where the shapes actually touch. For most pairs, that’s easy: for a circle and a box, the discrete test uses the closest point on the box, and for two boxes the center of their overlap. What about two circles? There, it uses the point halfway between the centers. That’s only exact for circles of the same size, but the swept test usually replaces it with the point on the surface anyway. For two convex hulls, though, the simple choices fail. A corner of one shape can be far from where the shapes touch, and the midpoint between the two centers can lie inside the larger shape.

So what can we do instead? We can borrow a standard technique from physics engines, contact clipping . It finds up to two contact points, and we average them into one. Two convex shapes touch either edge on edge or corner on edge (corner on corner is just a borderline case of corner on edge), and the technique finds out which edges are involved:

  1. On each shape, find the edge that faces the other shape most directly.
  2. Of those two edges, the one more square-on to the collision direction is the reference edge. The other is the incident edge.
  3. Cut the incident edge down to the part that lies alongside the reference edge.
  4. Keep the points of that part that are behind the reference edge. Move each one halfway back to the edge, and average them.

The swept test uses the same technique, only at the moment of impact, when the two shapes are just touching.

There’s one shortcut, though: when the swept test finds a hull hitting a circle, we go one radius from the circle’s center toward the hull. That point lies right on the circle’s surface, so it’s the contact point.

Beams Use Normals Too

What about beams? They don’t use the collision results above. Instead, they cast rays, and a ray hit carries its own normal, computed the same way: straight out of the face a ray enters a box through, along the line from a circle’s center, and, for a hull, straight out of the edge the ray crosses last on its way in. But what if a ray starts inside a box or a hull? Then it doesn’t enter through anything, so its normal is zero, and the sparks fly back along the beam.

Putting It Together

So what does it take to get sparks that fly off in the right direction?

  1. Have every collision test return a contact point and a surface normal, not just “yes, they touch”.
  2. Decide whose surface the normal belongs to, and hand each object the other object’s surface.
  3. When the swept test has a result, prefer its normal and contact point over the discrete test’s, since they show how the shapes came together.
  4. Reflect the bullet’s direction off the normal. A zero normal means “unknown”, so just reverse the direction.
  5. Spawn a burst at the contact point, spread around the bounce direction, with random speeds and a short lifetime.

Resources