Table of contents
1.
Introduction 
2.
Collision Physics in Video Games
3.
Compound colliders
4.
Mesh colliders 
5.
Static colliders 
6.
Physics materials
7.
3D collision Detection
7.1.
Axis-aligned bounding boxes
7.2.
Bounding spheres
7.3.
Using a physics engine
8.
Frequently Asked Questions
8.1.
What is Unity 3D?
8.2.
Explain what is the use of AssetBundle in Unity3D?
8.3.
Explain what is Fixed Timestep in Unity3D? Why does Fixed Timestep setting affect game speed?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Custom Collision Boundaries

Author Shivani Singh
0 upvote

Introduction 

Colliders, which connect to GameObjects and identify the shape of a GameObject for the use of physical collisions, can be used by Unity to handle collisions between GameObjects. let us see first what this collider is. A collider is invisible and does not have to be the same shape as the mesh of the GameObject. In most cases, a rough approximation of the mesh is more effective and unrecognizable in gameplay.

Primitive colliders are the simplest (and least processor-intensive) collider types. These are the Box Collider, the Sphere Collider, and the Capsule Collider in 3D. We can also use the Box Collider 2D and Circle Collider 2D in 2D. To make compound colliders, combine any number of these into a single GameObject.

To more realistically predict the behavior of solid objects, we must first determine whether they collide with each other when they move, and if they do, we must take action, such as implementing forces that adjust their velocities, causing them to move in the opposite direction. Understanding collision physics is especially important for game developers in this situation. 

Collision Physics in Video Games

A collision takes place within the context of rigid body simulations when the shapes of two rigid bodies intersect or when the spacing between such shapes falls below a small tolerance.

If we have n bodies in our computation, the computational complexity of sensing collisions with pairs tests is O(n2), which computer scientists despise. The number of pairwise tests grows in quadratic function with the number of bodies, and deciding whether two shapes collide in arbitrary positions and orientations is already expensive. To optimize the collision detection method, we generally divide it into two phases: broad and narrow.

The broad phase is in charge of identifying pairs of solid bodies that are possibly colliding and excluding pairs that are undoubtedly not colliding from the entire set of bodies in the computation. To stay well under the O(n2) time complexity, this step must be able to scale very well with the number of rigid bodies. To accomplish this, this phase typically employs space partitioning in conjunction with bounding volumes that demonstrate an upper bound for collision.

Image source: broad phase

The narrow phase works on pairs of rigid bodies discovered by the broad phase that may collide. It is a refinement step in which we determine whether or not collisions are occurring and compute the contact for each collision that is discovered.

 Image source: narrow phase

Compound colliders

Compound colliders estimate the shape of a GameObject while consuming minimal processor resources. You can add additional colliders to child GameObjects to increase flexibility. For example, you can spin boxes comparative to the parent GameObject's local axes. When creating a compound collider like this, only one Rigidbody component should be used, which should be positioned on the root GameObject in the ranking.

Shear transforms do not work properly with primitive colliders. If you use a mixture of rounds and non-uniform scales in the Transform hierarchy to create a shape that is no longer a primitive shape, the primitive collider cannot correctly represent it.

Mesh colliders 

However, in some cases, even compound colliders are insufficiently accurate. In 3-Dimension, you can use Mesh Colliders to precisely match the shape of the GameObject's mesh. The Polygon Collider 2D does not perfectly match the shape of the sprite graphic in 2D, but you can modify the shape to any level of detail you want.

These colliders require significantly more processing power than primitive types, so use them judiciously to maintain good performance. A mesh collider can also not collide with some other mesh collider (i.e., nothing happens when they make contact). In some cases, you can get around this by identifying the mesh collider as Convex in the Auditor.

The collider shape is generated as a "convex hull," which is equivalent to the previous mesh and with any undercuts filled in.

Because a convex mesh collider can collide with other mesh colliders, you could use this function when you have a moving character with a suitable shape. However, a good rule of thumb is to use mesh colliders for scene geometry and compound primitive colliders to estimate the structure of moving GameObjects.

Static colliders 

To create floors, walls, and other motionless elements of a Scene, you can add colliders to a GameObject without even a Rigidbody component. These are known as static colliders. Colliders on a GameObject with a Rigidbody, on the other hand, are referred to as dynamic colliders. Static colliders can communicate with dynamic colliders, but they do not move in response to collisions because they lack a Rigidbody.

Physics materials

When colliders collide, their surfaces must mimic the material properties they are meant to represent. A sheet of ice, for example, will be slippery, whereas a rubber ball will provide a lot of tension and be very fluffy. Although colliders' shapes are not distorted during collisions, their friction and bounce can be adjusted using Physics Materials. Getting the criteria just right may require some trial and error. A slippery material, such as ice, has zero (or very low) friction. Rubber is a grippy material with high friction and a close bounce.

3D collision Detection

In this section, we will see different bounding volume techniques used to process collision detection in 3D environments.

Axis-aligned bounding boxes

Axis-aligned bounding boxes (AABB), like 2D collision detection, are the swiftest algorithm to determine whether two-game entities are coinciding or not. This entails covering game entities in a non-rotated (thus axis-aligned) box and verifying their positions in 3D coordinate space to see if they overlap.

Image source: AABB

The axis-aligned constraint exists for performance. The overlapping area between the two non-rotated boxes can be identified using only logical comparisons, whereas rotated boxes require extra trigonometric operations, which are more time-consuming to calculate. If you have entities that will rotate, you can either change the dimensions of the bounding box so that it still wraps the object, or use a different bounding geometry type, such as spheres (which are more flexible).

Bounding spheres

Utilizing bounding spheres to identify collisions is more complex than AABB, but it is still relatively simple to test. The main advantage of spheres is, that they are rotationally invariant, so even if the wrapped entity rotates, the bounding sphere remains the same. And the main disadvantage is that, unless the entity being wrapped is truly spherical, the wrapping is rarely the best fit (i.e. wrapping a person with a bounding sphere will cause a lot of false positives, whereas an AABB would be a better match).

Using a physics engine

Collision detection algorithms are provided by 3D physics engines, with the majority of them also based on bounding volumes. A physics engine works by generating a physical body, which is usually coupled with a graphic display of it. This body has material characteristics such as velocity, position, rotation, torque, and so on. This shape is taken into account in collision detection calculations.

Frequently Asked Questions

What is Unity 3D?

Unity 3D is a potent cross-platform and completely integrated development engine that allows users to create gameplay and other interactive 3D content right out of the box.

Explain what is the use of AssetBundle in Unity3D?

AssetBundles are documents that can be outsourced from Unity and contain any asset. AssetBundles are designed to make it easy to download content into your application.

Explain what is Fixed Timestep in Unity3D? Why does Fixed Timestep setting affect game speed?

The Fixed Timestep feature allows you to schedule system updates at regular intervals. All authentic events that accumulate between time epochs will be managed by a queue-like mechanism. If the frame rate falls below a certain threshold limit set for a fixed timestep, the game speed may suffer.

Conclusion

To summarise, we talked about collision in this blog. We also talked about collision physics in video games, compound colliders, mesh colliders, static colliders, and physics materials. Then we saw various 3D collision detection techniques such as axis-aligned bounding boxes, bounding spheres, and physics engines.

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must have a look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass