Basic concepts of Unity physics system

Preface: The physics engine is only an approximate simulation of real physics. Both in terms of operational precision and time continuity are not accurate enough. The purpose is simply to make the game have convincing physics, enhance the expressiveness of the game and the user's immersion.


1. Rigidbody

Rigidbodies are the main components that make objects behave physically. Once the Rigidbody component is mounted, it is under the control of the physics engine and can be affected and react to forces.

For components that have been mounted with rigid bodies, it is not recommended to use scripts to directly modify the position of the object or directly change the orientation of the object. If you want to make the object move, you can consider applying force to the rigid body to push the object, and then let the physics engine calculate and produce the desired result; or directly modify the object's velocity (velocity) and angular velocity (angular velocity), which is better than applying force more directly.

In some cases, it is only necessary for the object to have a Rigidbody component, but its movement cannot be controlled by the physics engine. For example, let the character be completely controlled directly by the script, but at the same time not let the character be detected by the trigger, this kind of rigid body motion that is not directly controlled by physics, but performed in other ways is called kinematics (Kinematic). Although the movement of this rigid body is partly out of the control of the physical system and is no longer affected by the force, it will still be processed by the physical system when collision detection is required.

You can turn on or off the Is Kinematic option of the object at any time in the script, but this will bring some performance overhead and should not be used frequently

Two, dormancy

When a rigidbody's movement velocity and rotation velocity have fallen below some pre-defined threshold for a certain amount of time, then the physics engine can assume that it is temporarily stable. In this case, the physics engine no longer needs to repeatedly calculate the motion of the object until it is affected by the force again, which is to say that the object has entered "sleep".

This is a solution to optimize performance.

In most cases, the sleep and wake-up of rigid bodies are automatic, which means we don't need to care about this detail. However, there are always cases where objects do not wake up automatically. For example, an object with a Rigidbody component that is stable on the ground remains suspended in the air after the ground is removed. If you encounter a similar situation, you can actively call the WakeUp method in the script

3. Collider Collider

The physical shape of the object defined by the collider component . The collision body itself is invisible and does not have to be exactly the same as the appearance of the object

In actual production, the approximate shape of the object is used more often than the exact shape, which can improve the running efficiency of the game.

The most resource efficient is a set of basic colliders, including:

  • Box Collider   
  • Sphere Collider //⚠️: It will not become an ellipsoid due to the expansion and contraction of the object
  • Capsule Collider

Multiple collision body components can be mounted on an object at the same time, thus forming a combined collision body

Composite colliders are a Unity term, not a component. It means that an object mounts multiple collider components or the object has multiple collider sub-objects. The collider component mounted by the child object will also become part of the physical shape of the parent object.

When simulating objects with complex shapes, it is recommended to add several sub-objects to represent the physical shape, because it is convenient to control offset and rotation separately with sub-objects. But it should be noted that only one rigid body component is mounted on the parent object, and the rigid body component is not mounted on the child object.

Fourth, the physical material Physics Materials

The characteristics of the surface material of the collision body must be simulated so that the actual physical effect can be correctly simulated when the collision body interacts

Surface properties such as friction coefficient and elasticity can be set

5. Trigger Trigger

Colliders block rigidbody motion by default. If you want to detect whether two objects are in contact without causing an actual physical collision, you need to check the Is Trigger property of the collider component to make it a trigger

When a collider enters the range of the trigger, the script's OnTriggerEnter() method is called. But be careful, at least one of the two objects has a rigid body component (it can be a dynamic rigid body), otherwise the script cannot be triggered.

6. Classification of collision bodies

For an object that contains a collision body component, whether there is a rigid body on the object or not, and the dynamic settings on the rigid body component will change the physical collision characteristics of the object. Based on these properties, colliders are classified.

1. Static collision body Static Trigger

A collider with no rigidbody attached. Static colliders are usually used to make fixed parts of the level, such as terrain, obstacles, etc.

2. Rigidbody Collider

A collider that mounts a normal Rigidbody component. The physics engine will always simulate and calculate the physical state of the rigid body collider, so the rigid body collider will react to the collision and the force applied by the script

3. Kinematic Rigidbody Collider

A collider with a Rigidbody component mounted and set to Dynamic Rigidbody. The position of a Dynamic Rigidbody Collider can be modified directly in script to move it without reacting to collisions, force and velocity changes.

The classification of triggers is similar to that of colliders, so no more description

7. Collision event table

                                                                                                         Collision event table

 

static collider

Rigid Body Collider

Dynamic Rigidbody Collider

static collider

 

✔️

 

Rigid Body Collider

✔️

✔️

✔️

Dynamic Rigidbody Collider

 

✔️

 

                                                                                                        trigger event table

 

static collider

Rigid Body Collider

Dynamic Rigidbody Collider

static trigger

rigid body trigger

Dynamic Rigid Body Trigger

static collider

 

 

 

 

✔️

✔️

Rigid Body Collider

 

 

 

✔️

✔️

✔️

Dynamic Rigidbody Collider

 

 

 

✔️

✔️

✔️

static trigger

 

✔️

✔️

 

✔️

✔️

rigid body trigger

✔️

✔️

✔️

✔️

✔️

✔️

Dynamic Rigid Body Trigger

✔️

✔️

✔️

✔️

✔️

✔️

summary:

  • Only when there is a rigid body collider will it collide
  • Only if both are static will it not trigger.

Eight, layer Layer

Similar to tags, each object can also belong to a "layer". Arranging different objects on different layers, and specifying which layers cannot collide with each other, can accomplish the purpose ingeniously.

The layer collision matrix can be found in Edit->Project Settings->Physice in the Unity main menu. The left and upper sides of the layer collision matrix are the names of all layers. Checking it means that the corresponding two layers will collide.

9. Physical joints

Joints refer to a physical connection relationship. It always restricts the degrees of freedom of one type of motion and allows the degrees of freedom of another type of motion.

Unity provides many different types of joints. The following table lists the joints in some 3D scenes

physical joints

components

Introduction

fixed joint

Fixed Joint

Used to securely connect two objects without sliding or rotating. For example, it can show sticking and grasping objects

hinge joint

Hinge Joint

The two connected objects can rotate around the joint, such as a house door

spring joint

Spring Joint

Two objects are connected by a spring, and if they are too close or too far away, they will receive the restoring force of the spring. The elasticity of the spring can be adjusted with spring and damper

Character joints

Character Joint

Joints specially designed for making wayward characters. It is generally used to express the effect of inanimate humanoid characters moving with external forces in games. Due to the complex configuration, it is necessary to use the Ragdoll tool to assist in the production

10. Radiation detection

"Ray detection" is an indispensable technology in game development practice. In simple terms, it is to shoot a virtual "ray" in the game world, and observe whether the ray hits an object, and where exactly it hits the object. Although the name is "Ray", the position, direction and length of the actual emission can be set according to actual needs. There are not only straight line rays, but also spherical rays, box rays, etc. Therefore, the "ray" in ray detection can be regarded as a generalized "ray" with a certain range and oxide.

11. Character controller and physics system

There are broadly two ways to control characters:

  • Directly control the position and orientation of objects with scripts
  • Control the character's behavior by applying forces and changing velocities through rigid bodies

The advantage of using rigid bodies is that the workload is less, and most of the processing can be done by relying on the physical system. But the disadvantage is that the character may be stuck by the corner, or be bounced due to obstacles, and so on. Bugs are unavoidable.

If you don't use rigid bodies, you can write your own scripts, which is a little bit flexible and controllable, but the disadvantage is that the workload and difficulty are relatively large.

 

 

Guess you like

Origin blog.csdn.net/m0_63024355/article/details/130544744