Compound Physics Bodies
-
Introduction
On an earlier wiki page, you learned how to add physics bodies using one of these 4 shapes: box, sphere, cylinder and rectangle tube.
However, for objects in other shapes (such as a tube), and for models with more complex shapes (such as a sofa or a table), we need to use one or multiple simple shapes (box/sphere/cylinder) to compose a “compound” shape for them.
To do that, we need to follow 3 steps:
-
Given a target object, add a “compound” physics body to it.
-
Add one or multiple box/sphere/cylinder objects that would represent different parts of the target object, and add physics bodies for each of them.
-
Merge all these simple-shaped objects into the compound object. For this step, you need to use the following block:
- Children Body List: The list of names of objects with simple-shaped physics bodies (added in step 2), separated by a comma.
- Compound Object Name: the name of the target object with the compound physics body.
- Keep Object: whether to discard or keep the simple-shaped objects. For example, suppose in step 2 above, we have created a box with a box-shaped physics body. Then in step 3, if we choose to keep the object, then we will still see the box itself; if we choose not to keep it, then the box will be hidden.
Note that you can run step 3 multiple times. For example, you can add 2 children bodies named “a” and “b” using one block:
You can also use 2 blocks to add “a” first, then add “b”. They will produce the same result, although using one block is slightly faster.
Below we will look at 3 ways to add the physics body to a bowling pin:
Version 1 - Using Simple Shapes (Not Using Compound)
For comparison purposes, let’s try to use a simple shape first. Among the 3 simple shapes (box, sphere and cylinder), clearly the cylinder shape is the closest. The program below will add the cylinder shape:
Here is the result. An obvious problem is that the cylinder is facing sideways, not standing vertically.
Similarly, if we use a box or sphere shape, we would not get a good fit either:
Version 2 - Add a Single Cylinder Body Using a Compound Shape
For the next version, we will use a compound shape, but it will only contain one cylinder shape. The follow program includes all 3 steps: create a compound body, then add a cylinder object with a cylinder-shaped body, and finally add the cylinder body into the compound.
Here is the result in debug mode. Obviously, they do not match very well, but this is good enough for simple projects:
You can play with this project here:
https://play.creaticode.com/projects/7399a1f673e0005aa8baaf3b
Version 3 - Using 4 Parts
To make the physics body match the bowling pin’s shape better, we can add more than one child. In the program below, we use 4 shapes (a sphere, a cylinder, another sphere, then another cylinder) from top to bottom.
Note that these 4 shapes should not overlap with each other; also, their “ma*s” values should reflect the weight of these parts.Here is the code:
Here is what happens when it drops onto the ground:You can play with this project here:
https://play.creaticode.com/projects/dcf74ff44557ff7a864b98c7
Center of Mass (Advanced)
To keep the physics simulation computation simple and fast, the center of mass of an object with a compound shape is assumed to be always at the origin point (x = 0, y = 0, z = 0). In other words, no matter how many parts are there in the compound, the physics engine assumes all of their total mass is concentrated at a single point at the origin point.
This might lead to some unrealistic results. For example, in this program, we have 2 boxes in a compound. If we treat them as one object, the compound should tilt to the right. However, the physics engine assumes the center of mass is at the origin point, which is the blue box, so the object will not tilt.
However, if we shift the blue box to the left, then the center of mass for the entire compound (the parent object) will be between the 2 boxes, so they will tilt to the right:
You can try this project here:https://play.creaticode.com/projects/41f6935e0eb9dffafa428944
-