Ammo.js 6 Degrees of Freedom Constraint Parameters
This article explains how to configure a 6 Degrees of Freedom (6DoF) constraint in the ammo.js physics engine. It details the required constructor arguments, transform frames, and limit-setting parameters necessary to precisely control the translational and rotational movement between two rigid bodies.
The 6 Degrees of Freedom constraint in ammo.js is represented by the
btGeneric6DofConstraint class. It allows you to restrict or
free movement along three translational axes (X, Y, Z) and three
rotational axes (pitch, yaw, roll).
Constructor Parameters
To instantiate a 6DoF constraint, you use the
Ammo.btGeneric6DofConstraint constructor. The most common
constructor signature requires the following parameters:
rbA(btRigidBody): The first rigid body connected by the constraint.rbB(btRigidBody): The second rigid body connected by the constraint.frameInA(btTransform): The local pivot point and orientation frame relative to rigid body A.frameInB(btTransform): The local pivot point and orientation frame relative to rigid body B.useLinearReferenceFrameA(boolean): Defines which coordinate system is used as the reference frame for limits. Setting this totrueuses body A’s frame;falseuses body B’s frame.
Setting Linear Limits
Linear limits control translation along the local X, Y, and Z axes. If the lower limit is higher than the upper limit, the axis is completely free. If the lower limit is equal to the upper limit, the axis is locked.
setLinearLowerLimit(btVector3): Sets the minimum translation allowed along the X, Y, and Z axes.setLinearUpperLimit(btVector3): Sets the maximum translation allowed along the X, Y, and Z axes.
Setting Angular Limits
Angular limits control rotation around the local X, Y, and Z axes. The rotational axes are indexed as 3, 4, and 5 (representing X, Y, and Z rotation respectively).
setAngularLowerLimit(btVector3): Sets the minimum allowed rotation in radians.setAngularUpperLimit(btVector3): Sets the maximum allowed rotation in radians.
Advanced Parameters: Springs and Motors
To add bounce, suspension, or powered movement to the constraint, you can configure spring and motor parameters for each axis (indices 0 to 5, where 0-2 are linear and 3-5 are angular):
enableSpring(index, boolean): Enables or disables spring behavior on a specific axis.setStiffness(index, value): Controls the stiffness of the spring on the specified axis.setDamping(index, value): Controls the damping (resistance) of the spring to prevent endless oscillation.setEquilibriumPoint(index): Defines the resting position of the spring on the specified axis.getRotationalLimitMotor(index): Returns the motor controller for the specified rotational axis, allowing you to set parameters like target velocity and max motor force.