about         games         blog         contact


Farseer Physics (Box2D) and Unity (Part #3)

Part #1 | Part #2 | Part #3 | Part #4 | .unitypackage | GitHub
Now that we have the basics, concave objects and joints, what about collision filtering? Eventually you will reach a development stage in your game that "certain objects should not collide with each other", or "Group A" should ignore collisions from "Group B". That's when Collision Filtering kicks in.

I like the way that Farseer Physics Engine extends this feature from Box2D. Instead of setting hard-coded bit flags on each fixture, you just need to set collision groups. A fixture can belong to zero or more groups and it can also collide with any group(s) you want to. When I was porting this to Unity, I avoided using Unity layers and tags because both properties are used a lot on other things, but it also couldn't be a DIY hard-coded solution.

Now, there's a new configuration window. Right now there's only the option to name the collision categories, but I plan to port all settings defined in Farseer Physics Settings.cs file (so you don't have to change them in code).

Image 1 - New configuration window

I also decided not to use PlayerPrefs (and EditorPrefs) for the configuration files, since the PlayerPrefs can't be easily shared between developers, and any end user can edit them, so it's unsafe. The configuration is compiled with all the other scripts.

Image 2 - Configuring collision categories

To begin with this tutorial, download the latest package then open the scene indicated on the image below (Image 3).

Image 3 - Base scene

For convenience's sake, the collision groups are already setup. They are located at Assets/Tests/CollisionGroups. To create a new group, right-click at a folder then go to Create/FarseerUnity Collision Group (Image 4).

Image 4 - New collision group asset

The Collision Group asset is pretty simple. It's just a bunch of checkboxes that you can determine the category (or categories) this group belongs and what collides with it (Image 5). This process saves you from doing this setup on each shape.

Image 5 - A Collision Group asset

Now, inside the "STATIC" GameObject, there are 3 GameObjects that are the ground and walls of the scene, go to the "filter Collision" property of the FSShapeComponent and change it to "Preset File" (Image 6).

Image 6 - Preset file popup field

Drag the "Ground" Collision Group asset to the object field (Image 7).

Image 7 - A wild object field appears

Now, inside the "complexBody" GameObject there's a FSConcaveShapeComponent that also needs to be setup (Image 8). Drag the "RegularObjects" collision group to it.

Image 8 - Setting up another collision group

Don't forget to click on the "Generate convex shapes" button (since you altered its properties).

Image 9 - To update the convex shapes

Setup the "Sphere00" to use the "Oranges" group, and the "Cube" to use the "Rectangles" group. Then duplicate all the dynamic objects several times to test the collision filtering, then press play!

Image 10 - Testing the scene

That's it for part 3. The next part of the tutorial will cover collision events.