Content of the material
If you only want to see the demo, you don’t need any of these; all you need is a modern browser.
Mesh Colliders are a particular type of collider which uses an actual mesh for collision detection.
They are quite expensive as they compute collision against every face of the mesh, so depending on the complexity of your meshes they can add up quickly. For this reason, you should avoid using Mesh colliders if possible. If you really need to, you can supply a separate mesh to the component, which has the same shape and structure of the game object’s mesh but with fewer faces.
Normally, collisions between two mesh colliders are ignored. If you want to detect collisions between mesh colliders, you need to set them as Convex in the Inspector. In this case, you’ll need to supply a mesh with less than 255 faces.
All files that are part of this project are covered by the following license, except where explicitly noted.
Collision On a More Complex Mesh
If you have a mesh that can easily fit into a capsule or box, or have a mesh where having precise collision does not matter, using the simple collision we set up in the last section will work fine. However, you may have a mesh that is a more complex shape yet still needs to have precise collision. In this section, we will go over how to set that up.
Back in the Props folder within the Starter Content, browse to the Static Mesh SM_Chair. Double-click on the thumbnail to open it up in the Static Mesh Editor. As you can see, this mesh already has collision on it, and should look something similar to what is shown below:
Let’s assume that for this example, we want the player to be able to sit right on the cushion of the chair. With the collision mesh set up the way it is by default, there is an invisible barrier that would prevent the player from doing so. (Note that when the player is playing the game, they only see the Static Mesh itself, not the collision mesh, so the player would try to sit on the chair and not know why there is something blocking their way.)
Remove the collision on the mesh by selecting Remove Collision from the Collision drop down menu at the top of the editor. We can try to use the primitive shapes that we used in the previous section of this “How-to”, but as you can see in the images below, none of them look quite like what we are looking for.
Sphere Simplified Collision
Capsule Simplified Collision
Box Simplified Collision
It seems none of the primitive shapes quite achieve what we are looking for. However, we can get closer with the other options within the Collision drop down menu. These options are called the K-DOP simple collision generators. K-DOP is a type of bounding volume, which stands for K discrete oriented polytope (where K is the number of axis aligned planes). Basically, it takes K axis-aligned planes and pushes them as close to the mesh as it can. The resulting shape is used as a collision hull. In the Static Mesh Editor K can be:
10 – Box with 4 edges beveled – you can choose X- Y- or Z-aligned edges.
18 – Box with all edges beveled.
26 – Box with all edges and corners beveled.
Here is what our chair mesh looks like with 10-DOP, 18-DOP, and 26-DOP respectively.
These collision meshes are coming closer to what we are trying to achieve, but there is still a gap between the cushions that might prevent the player from sitting down in the chair. Select Remove Collision one more time and once you have done that, go ahead and select Auto Convex Collision from the Collision drop down menu. Once you have done that, you will be presented with the Convex Decomposition panel, which looks much like what is pictured below.
The top option in this window is Accuracy which will generate as few primitives as possible to represent the collision mesh. The bottom option, Max Hull Verts, will increase or decrease the number of vertices your collision mesh has. The higher these values, the more precise your collision will be, but also the more complex your collision mesh will be. Clicking Apply will apply these settings to your mesh.
Max Accuracy and Max Hull Verts
Another simple way to set up complex collision is by using multiple simple shape collision meshes to create the collision for your mesh. If you remember from earlier in this “How-to”, creating another simple collision mesh does not replace the original one, but adds to it. We also went over that each collision mesh has its own transform widget, so that you can translate, rotate, and scale each collision mesh individually. We can use this to create collision for our Static Mesh.
Start by selecting Add Box Simplified Collision from the Collision drop down menu. Left-click on the collision mesh to bring up its transform widget. Translate, rotate, and scale the collision mesh so that it fits along the arm of the chair down to the ground. With the collision mesh still selected, duplicate the collision mesh by either selecting Duplicate Selected Collision from the Collision drop down menu or by pressing Ctrl + W. Move this collision mesh so that it fits along the other arm of the chair. Duplicate the mesh again. With this new mesh selected, move, scale, and rotate this mesh so that is fits along the bottom of chair. Duplicate that collision mesh and move it up so that if fits along the seat of the chair. Duplicate the mesh one more time and rotate it so that it fits along the back of the chair. When you are done, you should have something that looks similar to this:
You could go back and follow the shape of the chair more closely, but for the purposes of this example, just using these five boxes we have set up collision on the entire chair and have the collision set up so that the player can sit on the cushion.
Processing: : A Programming Handbook for Visual Designers and Artists by Casey Reas and Ben Fry