Tech Report — Efficient Laser Bolt Collision Checks

I’m going to depart from my usual style of blog post and exercise my right to describe some technical stuff. This week’s topic is going to be about collision detection. It’s a massive topic, but for the purposes of today’s post, I’m going to focus on a very specific portion of Gateway’s collision detection system: how it handles laser bolts colliding with other objects.

Collision detection is potentially very costly, and the amount of collision checks between objects doesn’t scale linearly. The real trick is to discard potential collisions with as many objects as quickly as possible. In my attempts to reduce how often these checks had to take place in Gateway, I focused my efforts upon the most prevalent collision-enabled objects in the game world: the laser bolts.

In my game, lasers move very fast. We can’t rely on simple distance checks to determine if a bolt hits a fighter; the ordnance may very well simply ‘jump’ over the fighter and miss it entirely.

Simple distance checks will not suffice because the bolt moves so fast that it may skip over a potential victim

Simple distance checks will not suffice because the bolt moves so fast that it may skip over a potential victim, missing it

So, instead, we cast a ray between the old and new positions of the laser bolt.

As the laser bolt moves forward, our collision system performs a ray cast between its old position and its new one to see if it hit anything in between

As the laser bolt moves forward, our collision system performs a ray cast between its old position and its new one to see if it hit anything in between

This works fine, but we’re unnecessarily testing the ray cast against objects that fall behind the bolt. So, we discard any objects from the ray cast test that aren’t in its path. This can be done with a fast vector dot-product calculation and discards half of the objects we have to test against, on average.

Ray casting is expensive, so we should only test the objects that fall in front of the laser bolt's path

Ray casting is expensive, so we should only test the objects that fall in front of the laser bolt’s path

But, we can still do better than this.

Now, we’re performing these collision checks every frame. Even though we’re discarding a good chunk of the objects in our scene fairly quickly, that can still be costly in terms of pure iteration. These checks for discarding objects also consume resources.

Let’s think about this: laser bolts move really fast. Over the life of a single bolt (about one second or two), no ship is likely to change it’s position much. In other words, the set of potential colliders for any single laser bolt is fairly static throughout its entire life. So, why not just pre-compute a list of potential colliders at the beginning of the bolt’s life? This is likely to be a very short (or empty) list, and we can re-use it for every frame of the bolt’s life until it dies. In other words, we’re only computing the list of possible collision candidates once.

This page has some great code for a fast check to see if a point lies within a cylinder in 3D space. By specifying a radius wide enough to account for the fact that certain ships may cross the path of the laser bolt, we can use this to build a list of potential colliders that will remain valid until the laser dies. This list might only ever contain just a handful of ships out of hundreds, and will save our collision engine from tons of unnecessary iteration.

When a laser bolt is first spawned, compute a small list of candidates which we may collide against, and re-use it continually throughout the laser's life

When a laser bolt is first spawned, compute a small list of candidates which we may collide against, and re-use it continually throughout the laser’s life

In the image above, we can see that only two ships are within this cylinder. For that particular laser bolt, those are the only objects we have to check for collisions against. Pretty neat.

Of course, we’ve made a number of assumptions. We assume that the laser bolt is fairly short-lived and travels very fast. Slower or long-lived ordnance would necessitate the use of a wider cylinder, if we could still use this technique at all. Additionally, weapons that don’t move in a straight line (such as heat-seeking missiles) would present a problem. But for standard laser bolts, this works just fine.

GN

Advertisements

May 18 — Where I’m At

Voice recording has begun! This is a very exciting step, as the voices for the characters I’ve imagined can finally come alive.

Now, the script is still awaiting some final editing from another source, so there may be the possibility of having to do re-takes to accommodate the changes. The reason I’m starting voice recording already is because I’m getting eager to move this game along!

Since the story is now more or less finished (barring some final editing), a lot of final decisions regarding Gateway‘s gameplay have been made. The campaign itself spans 11 complete missions, and there will also be the option of playing customized instant action games. During these missions, the player will face off against several types of one-manned fighters, a few varieties of cruisers, and even newly-added space mines.

There have also been a number of performance optimizations. Space battles can now be larger, and I feel that this is important when it comes to making the player feel immersed.

Don’t let all this talk bore you! Here are some screenshots to show you what to expect after a few more months of development.

shot-1

Three outmaneuvered alien vessels face destruction from opposing sides.

shot-2

The volcanic planet Grappley is visible behind the chaos.

The next while will be spent fine-tuning the game mechanics, eliminating obvious bugs, and integrating voices into the missions.

GN