shown in other instead.
Adding Physics for Object
Learn the basics of real-time physics in Unigine applications. Make your object collide with static objects in the scene, move according to physical laws and interact with other physical objects.
Step 1. Assign Physical Properties
- Double click on the object that we want to simulate physically. The object should not be scaled.
- Press N to go to the Node panel -> Body tab.
Right now there is no body assigned. It means that the object do not have any physical properties. It is just a passive collider: it can stop an active collider from going through, but it cannot be moved by the character. All objects are passive colliders by default.
- Choose a Rigid type from the list. By this we will tell the engine that our object is a simple non-deformable rigid body.
Step 2. Create Collision Shape
- Go to the Shapes tab. Here, we are going to create two collision shapes: one for legs and one for the board. They will approximate the volume that our rigid body occupies.
Choose a Box from the list of available shapes:Keep the number of shapes as low as possible. Otherwise, heavy physics calculations can decrease the performance.
- Click on the Add button to create the chosen collision shape.
You can see the shape appeared. Now our road sign can actually collide and be moved by other physical objects.
- Let's make this shape approximate legs of the road sign. Before adjusting its size, we may need to disable the shape. Otherwise, the object may fall through the ground before we get it right.
- Now we can make it smaller. Decrease the Size along Z axis to 0.2 units.
Do not create too thin shapes. If there is no CCD by the contact and velocities are too high, such shapes can be not detected. As a result, they inter-penetrate into each other.
- Press X to activate the movement mode. Drag the shape down.
If you want to position the shape more precisely, set its Position along Z axis manually:
As you can see, our shape is bigger then the real mesh and not exact. Do not worry, that is how things should be. Such approximation seems crude, but in the game it will be enough to create convincing physical interaction.Do not try to create very precise shapes! Shapes should approximate the mesh in a rough way.
- Do not forget to enable the shape.
- Let's create the second shape — for the board. Click Add button again.
Here is our second shape:
- Decrease its Size along X axis to 0.2 units. Again, do not make it too thin, nor too precise.
- Now we need to set the mass of each shape.
Set the Mass to 15.Do not try to reproduce real masses. It is the mass ratio between objects in the scene that matters.
You can see that density is recalculated automatically when changing the mass.Shape mass parameters are used to calculate the total mass of the body (i.e. your physical object) only if Body->Shape-based mass parameters box is checked (by default).
- Select the first shape and set its mass to 10.
You can uncheck Body->Shape-based mass parameters box and manually set Body->Mass for the whole body at once. This parameter will override individual shape masses.
Step 3. Adjust Physical Reaction
We can tweak the way te body moves back when it hits another physical body or passive collider surfaces (these are all objects in the scene by default).
- Increase the Friction up to 3 so that our road sign would stop faster when sliding along the surface.
- Decrease the Restitution down to 0.2 so that our road sign will not bounce off too much when hitting some object.
- Set the same parameters for the first shape.
- Right now the global Physics parameters (such as a damping of linear and angular velocities of all physical bodies in this scene) are in effect. But we want the body to stop even faster, when it is given an impulse by some other body.
Go to the Body tab and increase LDamping (i.e. damping of body's linear velocity) up to 1.2.
Step 4. Troubleshoot Inter-Penetration
For example, our road sign falls down extremely fast due to high gravity.
We have approximated our shape with a box. Boxes do not have continuous collision detection (unless they collide with a sphere or a capsule shape). As a result, it can inter-penetrate with other objects. But we don't want that!
- Press P to open global Physics settings and increase the number of iterations to 2. It means all physics (collisions, joints etc) will be calculated twice before objects will be rendered onto the screen.
Keep in mind, that high number of iterations means the higher load and the lower framerate. At the same time they also increase stability.