UnigineScript
The Language
Core Library
Engine Library
Node-Related Classes
GUI-Related Classes
Plugins Library
High-Level Systems
Samples
C++ API
API Reference
Integration Samples
Usage Examples
C++ Plugins
Content Creation
Materials
Unigine Material Library

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.

Notice
Do not scale any meshes in your world if you want to add physics. Neither objects you want to simulate physically nor other plain meshes should be scaled; otherwise, problems with collision may occur.

Step 1. Assign Physical Properties

  1. Double click on the object that we want to simulate physically. The object should not be scaled.

  2. 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.

  3. 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

  1. 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.
    Notice
    Keep the number of shapes as low as possible. Otherwise, heavy physics calculations can decrease the performance.
    Choose a Box from the list of available shapes:

  2. 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.

  3. 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.

  4. Now we can make it smaller. Decrease the Size along Z axis to 0.2 units.
    Notice
    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.

  5. Press X to activate the movement mode. Drag the shape down.

    Press Z to go back to the normal mode.

    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.
    Notice
    Do not try to create very precise shapes! Shapes should approximate the mesh in a rough way.

  6. Do not forget to enable the shape.

  7. Let's create the second shape — for the board. Click Add button again.

    Here is our second shape:

  8. Decrease its Size along X axis to 0.2 units. Again, do not make it too thin, nor too precise.

  9. Now we need to set the mass of each shape.
    Notice
    Do not try to reproduce real masses. It is the mass ratio between objects in the scene that matters.
    Set the Mass to 15.

    You can see that density is recalculated automatically when changing the mass.
    Notice
    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).
  10. Select the first shape and set its mass to 10.

    Notice
    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).

  1. Increase the Friction up to 3 so that our road sign would stop faster when sliding along the surface.

  2. Decrease the Restitution down to 0.2 so that our road sign will not bounce off too much when hitting some object.

  3. Set the same parameters for the first shape.
  4. 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!

  1. 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.
    Notice
    Keep in mind, that high number of iterations means the higher load and the lower framerate. At the same time they also increase stability.

That is it. Our road sign collides and does not go through non-physical static meshes in the scene. It also collides and interacts with other physical objects. When the character runs into it, the sign falls on the ground.

Last update: 2017-07-03