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 Algorithms, Competitive Programming, JavaScript, System 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 problems, interview 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!