Collision FilteringJames Golding on | Tags designfeatureslearning
Choosing what collides is obviously very important, but it can be tricky, and it’s a problem that we have spent quite a while discussing while developing UE4. The system we have can seem a little complex at first, but it is very powerful and consistent, so I wanted to give a little background on how we arrived at it. We’ll talk about the different responses to collision, how we use channels to filter collisions, and outline the difference between simple and complex collision geometry.
Blocking, Overlapping and Ignoring
The first thing to know is that when you say something should collide, you have to choose whether you can penetrate it or not. A brick wall will ‘Block’ a player, but a trigger will ‘Overlap’ them, allowing them to pass through. Both generate an event (‘Hit’ or ‘Overlap’ respectively, in UE4 terminology) but it is an important difference*. Other objects may ‘Ignore’ the collision altogether, giving us our three response types.
Trace Channels and Object Channels
The next big question at the heart of collision filtering is “who gets to choose?” Is it the object that decides what types of query to collide with? Or does the query decide what types of object it is looking for? In different scenarios they both make sense, so UE4 supports both!
Imagine we define two ‘Trace Channels’ in our game, one for ‘Visibility’ and one for ‘Weapon’ queries. A brick wall is set up to block both, a shrub blocks visibility but not weapons, and bulletproof glass blocks weapons but not visibility. When you do this kind of query you specify a single Trace Channel.
If I changed this around so the game code had to know what types of object to query for, our game could need a huge number of different types to handle all these situations! It also allows us to make ‘spot fixes’ in content later on, instead of changing the calling code itself and possibly breaking other things.
There are however some situations where you do want to query for objects based purely on their type, and this is where we use ‘Object Channels’. One example is an explosion going off, and you want to quickly find all objects of type ‘Pawn’ or ‘PhysicsBody’ within a certain radius. When you do this kind of query, you can specify multiple Object Channels.
UE4 has a few ‘built in’ Trace Channels (Visibility, Camera) and Object Channels (WorldStatic, WorldDynamic, Pawn, PhysicsBody, Vehicle, Destructible), but you can easily add your own under Edit -> Project Settings -> Collision, though you are limited to 32 in total.
Collisions Between Moving Objects
Things get a bit more complicated when you want to handle collisions between moving objects, because there can be so many combinations. In UE4 each object knows its own Object Channel, plus a list of how it responds to other Object Channels. When two objects intersect, we look at how they respond to each other, and take the least blocking interaction, like so:
So imagine the following scene:
Now the player moves forward. First he will overlap the shrub. The Player is of type Pawn, and the Shrub wants to Overlap that. The Shrub is of type WorldStatic, and the Player wants to Block that. Checking the chart, the final result is Overlap! Similarly the Brick Wall and Player both want to Block each other, so the player walks through the Shrub (generating an Overlap event) and is stopped by the wall (generating a Hit event).
If you had two players and one wanted to become ‘ghostly’, he could change his response to the Pawn channel from Block to Ignore, and then he could walk right through other players.
Even though this system gives a lot of control over what objects in your level will collide with, in practice most objects fall into common configurations. To make things easier, UE4 has a ‘Collision Preset’ system. Each Preset contains an Object Type, and a response to each Trace and Object Channel in your game. When you select an object in the level, you will see a simple dropdown, allowing you to select a Preset:
UE4 has several of these built in (e.g. Invisible Wall, Physics Actor etc), and again you can create your own inside Project Settings. If you don’t want to use a Preset, you can choose ‘Custom…’ and then you can modify every response, liked we talked about above:
Simple and Complex Collision
One final thing to understand is that each object in UE4 can have both a ‘complex’ and ‘simple’ collision representation. Complex Collision refers to using the actual rendering geometry for collision. This is most useful for things like weapon traces, where you want to be able to shoot exactly what you see. You don’t always want to collide with this though, and so each mesh can also have a Simple Collision representation consisting of a collection of spheres, boxes, capsules and convex hulls. When you perform a collision query, you tell UE4 what kind of geometry you want to collide with. Player movement, for example, collides against simple geometry, to avoid getting ‘stuck’ on little details.
In the editor there is a handy View Mode that lets you see the world as a player will collide with it:
So that is the whole, heady world of collision filtering in UE4! I hope it was helpful to understand some of the problems we are trying to solve and how to make use of it for your own game. Any questions or comments? We’re always over in the forums or you can hit me up on Twitter at @EpicJamesG.
*We can actually stop looking for collisions after the first blocking collision, which is a good performance optimization - imagine firing a gun with a range of 1km at a brick wall 1m in front of you, we just skipped 999 meters of work!