Hinge Constraint between 2 Physics Objects
CreatiCode last edited by info-creaticode
You can use a hinge constraint to connect 2 physics objects through a rotation axis.
This constraint is most commonly used to simulate the connection between a wall and a door, or between a car body and a wheel. Typically, one of the 2 objects is static or has a much larger mass, so the other object will be rotated around the hinge axis. There will be no other relative movement or rotation between the 2 objects.
Add the Hinge Constraint
You can use the following block to add a hinge constraint.
It accepts these parameters:
First Object’s Name and Sprite: You can specify the first object using its own name and the sprite where this object is created. Normally the first object is the one that does not move.
Hinge’s Relative Position from First Object in X/Y/Z: This is the relative position of the hinge from the first object. Usually, the hinge itself is outside the object, so the hinge’s center and the first object’s center are not in the same position. For example, if the first object is at x of 100, and the hinge’s relative x position is 60, then the hinge will be at an x position of 160.
Hinge’s Relative Direction from First Object: These parameters control the direction of the rotation axis. If x is 0, y is 0 and z is 1, then the hinge axis’s direction will be the same as the Z-axis’s direction.
Second Object’s Name and Sprite: You can specify the second object using its own name and the sprite where this object is created.
Hinge’s Relative Position from Second Object in X/Y/Z: This is the relative position of the hinge from the second object. For example, assuming the first object is static, so it will determine the position of the hinge, and the hinge’s position will determine the second object’s position. if the hinge is at the x position of 160, and its relative x position to the second object is -60, then the second object will be at x of 220.
Hinge’s Relative Direction from Second Object: These parameters control which axis is the hinge aligned with relative to the second object. If this direction is different from the relative direction of the first object, then the second object will be rotated to align its axis to the first object.
Name: The name of this hinge constraint. You can refer to this constraint later using its name.
Set Rotation Speed for the Hinge Constraint
By default, the relative angle between the 2 objects will not change, unless some force is applied to one or both objects.
However, you can add a “motor” to the constraint, so that there is a constant relative rotation between the 2 objects. This can be used to simulate the rotation of a wheel or a robot arm.
You can use the following block to set the rotation speed:
- Speed: The speed is how many degrees the hinge will rotate per second.
- Name: The name of the hinge constraint you are trying to change.
Set Limits for the Hinge Constraint
You can set some limits on the hinge constraint using the following block:
Name: the name of the constraint that you are trying to change
Min and Max Angles: the lower and upper bounds of the relative angle between the 2 objects.
Softness: This parameter is not active at the moment. It’s reserved for future updates.
Bias: This parameter controls whether the objects will revert back to the center position when it hits the limit. It accepts any number between 0 and 1. At 0, the objects will keep their relative angle when the angle hits the upper or lower bounds. At 1, the objects will revert back strongly when their relative angle hits the bounds.
Relaxation: When the objects revert their relative angles (when bias is not 0), how fast the reversion happens (with how much force).
Example 1 - Wall and Door
The following program adds a first box named “wall”, which is static. Then it adds a second box named “door”, which is not static. These 2 objects are placed left and right, and a hinge constraint is added between them.
We apply a limit to the hinge constraint, so that it will only rotate up to 45 degrees. Lastly, when we apply an impulse on the door, it will rotate up to the limit. Since we have set the “bias” to 0, the door will stay at that angle, instead of reverting back.
Here is the result:
Here is the shared project:
Example 2 - Jumping Dog
For this example, we create a dog with a box as its body, a sphere as its head, and 4 cylinders as its legs. The head is attached to the body using a fixed constraint. The 4 legs are attached to the body using hinge constraints, so we can make the legs swing by setting the motor speed of the 4 hinge constraints. We are also setting some limits on the hinge constraints so that the legs do not swing to the wrong position.
This is the result you will get:
Here is the shared project: