This page has been translated automatically.
Unigine Basics
1. Introduction
2. Managing Virtual Worlds
3. Preparing 3D Models
4. Materials
5. Cameras and Lighting
6. Implementing Application Logic
7. Making Cutscenes and Recording Videos
8. Preparing Your Project for Release
10. Optimization Basics
11. PROJECT2: First-Person Shooter
12. PROJECT3: Third-Person Cross-Country Arcade Racing Game
13. PROJECT4: VR Application With Simple Interaction

Collision Detection and Raycasting

In the real world, bodies usually don't move without constraints and obstacles, and the same goes for virtual worlds. For correct processing of the situation when the body meets an obstacle, collision detection is used.

Regardless of their implementation, collision detection algorithms usually operate with collision shapes that we have been talking about recently.

There are two types of collisions implemented in UNIGINE depending on the types of colliding objects:

  • Shape — Shape collision between two objects with physical properties assigned (having a body and at least one shape, both enabled). In this case, the contact points between the shapes are found.
  • Shape — Surface collision between an object with physical properties assigned and a non-physical object (without physical representation). If the surface has the Collision flag set, it can also passively participate in physical interaction and prevent the physical object from going through. In this case, contact points between the shape and surface polygons are computed.
Notice
Scaling of meshes that participate in collision detection is not supported. So, when you assign a physical body to an object, make sure that its scale is default. Otherwise, the scale will be reset automatically.

So, collision detection is enabled automatically for an object with a body and a collision shape, or when at least one of its surfaces has the Collision flag set.

The algorithm for enabling/disabling collision detection is illustrated below:

Notice
  • Disabling physics simulation globally does not turn off collision detection.
  • If the object has the body and shape assigned and enabled, the collision detection algorithms will use only the shape parameters, while the parameters of its surfaces will be ignored.

Complex scenes may contain a lot of objects for which physics simulation is enabled, but not all of them need to interact with each other.

For optimization purposes, you can use the Collision mask. It allows you to limit the number of objects that a physical object can collide with. Only surfaces and collision shapes with matching masks will collide.

The Exclusion mask can be used to define shapes that should ignore each other.

The whole process is divided into the following stages and phases:

  1. Collision Detection
  2. Collision Response
  3. Callbacks Execution

Phases: (a) — collision detection, (b) — collision response

Collision Detection
#

During this phase, we find all collisions with all contact points and collect all necessary information. First, our pairs of objects that are positioned too far to collide are filtered. Then, all collisions along with contact points for all colliding bodies are found.

So, here we collect all the data required to resolve collisions later — contact points coordinates, normals, depth of shapes penetration, relative velocity (between two bodies), relative friction and restitution.

Collision Response
#

So, we've got all the necessary information about collisions. Now we need to process this information to provide a realistic reaction. Collision response stands for simulation of the changes in the motion of two solid bodies after collision. UNIGINE uses an impulse-based reaction model. During response calculation, two parameters — the restitution and friction coefficients — are taken into account. These coefficients can be set for a shape as well as for a surface.

Collision response

Callbacks Execution
#

At this stage, all user-defined physics callbacks are called. They are executed in certain physics-related events. These callbacks are mainly used for creation, destruction, or modification of other objects, such operations can only be performed in the main thread.

Discrete and Continuous Collision Detection
#

In UNIGINE, physics is simulated at its own frame rate that doesn’t depend on the rendering frame rate. As we discussed earlier, you can specify the number of cycles of physics simulation during one physics tick in the global settings.

Basically, collisions are calculated each physics tick. This approach is called discrete collision detection. Such discretization improves performance and is accurate enough. However, when the frame rate is low, small, fast-moving objects are likely to teleport from one point to another instead of moving smoothly, so collisions are not detected.

In continuous collision detection, the Engine calculates contact points the body will have (in the current physical frame) if it continues its current trajectory. These calculations are based on the velocity of the body and the radius of the collision shape. In other words, moving bodies are extruded along their trajectory, forming a volume used for collision detection at higher speeds.

Thus, contacts are checked not just once per frame but during the entire frame. This approach excludes missed collisions.

Notice
Continuous collision detections are only available for the Capsule and Sphere shapes.

Physics Intersections
#

In some cases, you can use physics intersections for collision detection. Intersection detection lies at the heart of collision detection, but it is a particular case of ray intersection (ray casting).

Intersection calculation is less expensive than collision calculation — a ray is cast from a certain point in a certain direction to find the first intersection with a surface or a collision shape.

For example, calculation of collisions of car wheels with the ground takes time and decreases the frame rate. Instead, you can use the intersection of rays cast from the bottom of the car with the ground.

Ray casting

And if you want to exclude some obstacles, you can use the Physics Intersection mask. Physics intersection is detected only if the Physics Intersection mask of the surface or shape matches the Intersection mask defined for the ray.

Last update: 2024-01-18
Build: ()