Use it when an object should swing, turn, or spin around one anchor point.
Good Fits
HingeJoint2D is useful for:
- doors
- pendulums
- swinging traps
- wheels
Scene Setup
- Add
Rigidbody2Dto the moving body. - Add
HingeJoint2D. - Assign
Connected Bodyif needed. - Place the hinge
Anchor. - Use angle limits if the body should only rotate through part of an arc.
- Use the motor if the body should drive itself continuously.
Use Limits for Controlled Swing
Turn on Use Limits when the hinge should not rotate freely.
Then tune:
Lower AngleUpper Angle
That is usually what you want for doors, trap arms, and controlled pendulums.
Use the Motor for Powered Rotation
Turn on Use Motor when the hinge should actively rotate.
Then tune:
Motor SpeedMax Motor Torque
Use the speed first to get the feel right, then add enough torque to make the movement hold up under the scene's physics.
Practical Example: A Heavy Door
For a simple heavy door:
- Add
Rigidbody2Dto the door. - Add
HingeJoint2D. - Set the anchor at the hinge side of the door.
- Enable limits.
- Start with a modest range like
0to110.
This gives you a scene-authored door instead of a rotation script fighting the physics body.
PHP Example
<?php
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\HingeJoint2D;
class MotorizedWheel extends Behaviour
{
public function awake(): void
{
$joint = $this->gameObject->getComponent(HingeJoint2D::class);
if ($joint === null) {
return;
}
$joint->useMotor = true;
$joint->motorSpeed = 180.0;
$joint->maxMotorTorque = 1200.0;
}
}