Shmup Devlog #7
A Splitting Laser Beam
Table of Contents
Introduction
After four posts about collisions, let’s look at something that actually uses them: the splitting laser, one of the ship’s weapons. Keep the trigger pressed down, and a beam of light shoots out from the ship and hits an enemy. When it hits, it splits into two thinner beams, and each of them goes after another enemy.

The splitting laser
The first version was a burst of bullets that spread out when they hit something. But in play, it looked more like a machine gun than a laser. So the beam now lasts as long as the trigger is held, and every frame the whole beam is calculated from the beginning: where the first part goes, what it hits, and where the split beams go from there.
You can upgrade the weapon to make it stronger. Higher levels mean more damage and more frequent splitting.
Building the Beam
The First Target
Let’s begin with the first part of the beam, the root. It starts at the ship and aims at the nearest enemy within a cone in front of it. An enemy counts if any part of its body reaches into the cone, not just the center. Why not just check the center? Because a big enemy right in front of the ship could be skipped when its center is just outside the cone.
The root picks the nearest enemy that reaches into the cone.
So why have a cone at all? Because it changes how the weapon plays. If the root could aim anywhere, it wouldn’t matter where the ship is. With the cone, the player has to move the ship to bring an enemy in front of it. The root doesn’t jump to its target, either. It turns toward it at a fixed speed, so the beam sweeps over to a new target rather than teleporting there.
Splitting on a Hit
What happens when a part of the beam hits an enemy? If the level allows another split, two new beams start at the hit point, and each one aims at the nearest enemy that hasn’t been taken yet. Unlike the root, split beams don’t have a cone. They start somewhere out in the field, not on the ship, so the player can’t steer them anyway.
At the top level, the root splits into two children, each child into two grandchildren, and each grandchild into two great-grandchildren.
Any enemy a part of the beam aims at is marked as claimed, and no other part can aim at it in the same frame. Why? Without that, the two split beams would often find the same nearest enemy, and the laser would pile up on one enemy instead of spreading out. A long enemy made of several parts, like a snake, counts as a single enemy.
Nothing Left to Aim At
What if a split beam can’t find an enemy because all of them are claimed already? The first version reflected the beam off the surface it hit, like a mirror. That looked plausible, but it sent the beam’s damage into empty space.
So now a split beam with no target isn’t created at all. Instead, its damage goes to the enemy its parent hit. In the diagram above, one of the grandchildren has only one enemy left to split toward, so the second great-grandchild’s damage lands on the grandchild’s own target. And when only one enemy is left on screen, it takes all of it.
Thinner and Weaker
Each split beam is thinner and does less damage than the one it came from. But how much less?
Every time a beam splits, the number of beams can double. Each child beam does 40% of its parent’s damage, so the two together do 80% of it, and the total damage shrinks with every generation. What about 60% per child? An earlier version used that. Then the two together did 120% of their parent, and each generation did more total damage than the one before. Splitting should spread the damage around, not multiply it.
Holding On to a Target
Since the beam is calculated from scratch every frame, a branch always aims at whatever enemy is nearest right now. That sounds fine, but what happens when two enemies fly close together? The nearest one keeps changing, and the branch moves back and forth between them, even though each frame’s choice is correct on its own.
So every branch remembers its target and keeps it as long as it’s still valid. What does “valid” mean? The same as for a new target: it’s alive, not claimed by another branch, within range, and, for the root, inside the cone. The branch only looks for the nearest enemy again when the target fails one of these checks.
The lock holds, even when another enemy comes closer.
There’s a second place where this matters: the ray itself. A beam stops at the first thing it hits. If another enemy drifts between the hit point and the locked target, the beam would hit that one instead, and switch back once it has passed. To avoid that, the ray prefers the locked target as long as it’s only a short distance behind the first thing in the way. Only an enemy that is clearly in front takes over.
Remembering a target saves time, too. Checking whether the current target is still valid is a single lookup, while searching for the nearest enemy means looking at every enemy on screen.
Putting It Together
So what does a frame of the splitting laser look like?
- The root aims at the nearest enemy that reaches into a cone in front of the ship, or keeps its current target if that one is still valid, and turns toward it at a fixed speed.
- Wherever a part of the beam hits an enemy, and the level allows it, two split beams start there and aim at the nearest enemies not yet claimed, in any direction.
- Each enemy is claimed by one part of the beam only, so the laser spreads out.
- A split beam without a target isn’t created. Its damage goes to its parent’s target instead.
- Each generation is less than half as thick and as strong as the one before, so splitting spreads the damage instead of multiplying it.