Adding Physics Bodies for 3D Objects
CreatiCode last edited by info-creaticode
After enabling physics for a 3D scene, you can enable physics simulation for any object by adding an invisible physics body to it. The physics body and the object will stay synchronized: whenever the physics body moves or turns, the object will move or turn in exactly the same way. On the other hand, when you move or turn the 3D object, its body will follow as well.
You can think of the physics engine as a helper that does a lot of work for you automatically. For example, to show a ball that’s falling down and bouncing up, you no longer need to set the position of the ball frame by frame. Instead, you just need to set up the scene with gravity and link the ball to a physics body, then the engine will move the ball for you automatically.
Because physics bodies will collide with each other, they are also called “colliders”.
The “Add Physics Body” Block
To add the physics body for any object, you can run the following block:
The first input allows you to specify the shape of the physics body to be added. There are 5 choices available: “box”, “sphere”, “cylinder”, “rectangle tube” and “compound”. We’ll focus on the first 4 options on this wiki page. The “compound” option is for more complex objects, which will be explained on this page.
If your object is a simple shape (box, sphere, cylinder or rectangle tube), or if it can be approximated by one of these 4 shapes, you can choose the corresponding option. For example, if you are trying to add physics to a ball, then you should choose “sphere”. Note that “rectangle tube” would only work if the object is a rectangle tube.
On the other hand, to accurately represent the physics of a table, you would need to select the “compound” option.
The mass of a physics body determines how heavy it is. For example, the larger the mass, the more force is required to push the body to move.
Note that the mass can take on a special value of 0. When the mass is 0, this object will not move at all. For example, if you are adding the physics object for the ground plane, then it should have a mass of 0, since the ground will not move, but it might still collide with other physics objects.
In the real world, 2 objects can never overlap with each other. Similarly, when 2 physics bodies collide with each other, the physics engine will make sure they bounce back.
The restitution of a physics body controls how much it rebounds when colliding with another physics body. When one or both of the 2 colliding bodies have a restitution of 0, there will be no bounce at all. If both bodies have a restitution of 100, then the rebound speed will be the same as the speed before the collision. More scientifically speaking, there will be no loss of energy in the collision.
Here is a comparison of 3 spheres with restitutions of 100, 50 and 0. They fall onto a box with a restitution of 100. The yellow ball will always rebound back to its original height; the blue ball will lose half of its power on every bounce; the pink ball will not rebound at all.
The friction of a physics body controls how much it slows down when it glides on another object. When friction is 100 for both bodies, then the friction between them is the strongest. At friction of 0, the body will glide smoothly non-stopping.
Here is a comparison of 3 boxes with the friction of 100, 50 and 0. As they glide down a slope, the orange box stops very quickly due to the large friction. The blue box stops after gliding a little bit. The red box keeps rebounding on its own.
If you set “frozen” to “yes”, then the physics body will stay where it is after it is added to the scene. If “frozen” is “no”, then the body will start to move right away.
If you have a large scene with many objects, you should set “frozen” to “yes” while you set up the scene. When you have finished adding objects, then you can use the following block to unfreeze all physics bodies at the same time:
If you just want to freeze or unfreeze one object, you can use the following block and specify that object’s name:
Debug and Color
By default, we can not see the physics body added to an object. If “debug” is “yes”, the physics body will be drawn as a frame using the color you specify. This will make it easier for us to check if the physics body is matching the shape of the object.
For example, in this program, we have added a box-shaped physics body to a sphere object by mistake:
We can easily see that when we turn on the debugging option:
Use Shapes for Simple Models
When the object is a model (created with the “add model” block), you can still choose one of the 3 shapes (box, sphere or cylinder) to approximate its physics body.
For example, suppose we load a soccer ball model, then we can try to add a sphere-shaped physics body to it:
Since we have set the debug option to “yes”, we can actually see the physics body is not matching with the soccer ball well:
The problem is that the origin points of all models are at their bottom, while for physics bodies, the origin points are at their centers. To fix this issue, we need to add a “z offset” to the model when they are loaded. For this example, the height of the soccer ball is 100, so the z offset should be half of that, which is 50:
Now the physics body is covering the ball precisely:
For another example, suppose we have a sofa, and we would like to use a box to serve as its physics body, we can do it this way:
The box will cover up the entire sofa, and it would not allow anyone to sit in it. As discussed earlier, we can improve it using a compound physics body.
Removing a Physics Body
If you no longer wish to use the physics engine to control an object, you can remove its physics body using this block:
Updating Physics Body Properties
After a physics body has been added to an object, you can change its restitution or friction with the following block:
You need to make sure the object you would like to change is the selected sprite object.
Moving an Object and Its Physics Body
After linking a physics body to an object, they will move in sync. Most of the time, the physics body will move in the scene due to gravity or other forces, which will make the object move with it.
However, you can also move the object itself at any time, and its physics body will be moved as well.
On the other hand, you can first move the object to the desired position, then add the physics body to it:
Rotating an Object with Its Physics Body
Similarly, you can rotate an object before or after adding its physics body, and you will get the same result:
Note that you can use any of these 3 blocks to rotate the object after it has been linked to a physics object: