Fixed Constraint between 2 Physics Objects
CreatiCode last edited by info-creaticode
When you have enabled the physics engine in a scene, you can add constraints between objects. A constraint is a request that we give to the physics engine, and the physics engine will try its best to satisfy our request.
For example, a fixed constraint is a request that we wish to force the relative position between 2 objects to stay the same no matter how they move. Essentially they will look like 2 parts of one compound object.
Note that the physics engine may not always be able to satisfy our constraint requests, so you need to test our program to make sure your constraint requests are realistic.
To add a fixed constraint, you can use the following block:
The first 2 inputs are the names of 2 objects with physics bodies. Note that if one of them is static (mass = 0), then you should specify that object’s name in the first input.
The third input is an optional name of the constraint. If you may need to remove this constraint later in the program using the “remove constraint” block, then you need to specify a name for it.
After this block runs, the physics engine will try its best to keep these 2 objects at the same relative position as before this block runs, as if they are 2 parts of one object. In other words, these objects can still move, but their relative position will stay the same.
In this program, we add 2 boxes A and B. Box B is at the right of box A, and it is also at a higher position. After we add the fixed constraint, their relative positions do not change, but due to gravity, both objects will start to tilt to the right until box B also lands on the ground.
Here is how it looks:
You can play with the demo project here:
As explained in this wiki page, you can also combine multiple shapes into one compound shape so they will behave like one object. However, there are 2 key differences between these 2 approaches:
- Center of Mass: When we create a compound shape with multiple children shapes, the center of mass is assumed to be at the origin point, which is not accurate. However, when we use the fixed constraint to link 2 or more shapes together, each shape will still keep its own mass. That’s why we see the 2 boxes tilt to the right in the example above.
- Firmness of the Bond: When we create a compound shape, the bonds between the children shapes are very strong, so their relative positions never change. However, when we use a fixed constraint, the bond is weaker, so there may be moments when the relative position between the objects changes. It should be more obvious in the example below.
When we have a car enabled with physics simulation, we can link other objects to its body using the fixed constraint, so that the other objects will move together with the car.
In this program below, we add a box above the car, and add a fixed constraint between the car and the box. After that, as the car moves, the physics engine also moves the box and tries to keep it at the same relative position above the car.
As you can see, sometimes the box will appear to be shaking, which shows that the physics engine is having trouble keeping it at the exact relative position.