Constraints
AdvancedProgrammer Constraints restrict rigidbodies to certain movement patterns. For example, a realistic knee joint can only move along one axis and can’t bend forwards. Constraints can either link two rigidbodies together, or link a single rigidbody to a point in the world. They allow for interaction and dependency among rigidbodies. types of constraints:
- PhysicsSample sample project.
Create a constraint
Note
Currently, you can only use constraints from scripts. Simulation static method CreateConstraint:
RigidBodyA to the world at its current location.The boolean useReferenceFrameA specifies which coordinate system the limit is applied to (either RigidBodyA or the world).CreateConstraint(ConstraintTypes type, RigidbodyComponent rigidBodyA, Matrix frameA, bool useReferenceFrameA);
Note
- ConstraintTypes.Point2Point, the frame represents a pivot in A. Only the translation vector is considered. useReferenceFrameA is ignored.
- ConstraintTypes.Hinge, the frame represents a pivot in A and Axis in A. This is because the hinge allows only a limited angle of rotation between the rigidbody and the world.
- ConstraintTypes.ConeTwist, useReferenceFrameA is ignored.
- ConstraintTypes.Gear needs two rigidbodies to be created. This function will throw an exception.
RigidBodyA to RigidBodyB.CreateConstraint(ConstraintTypes type, RigidbodyComponent rigidBodyA, RigidbodyComponent rigidBodyB, Matrix frameA, Matrix frameB, bool useReferenceFrameA)
Note
- ConstraintTypes.Point2Point, the frame represents a pivot in A or B. Only the translation vector is considered. useReferenceFrameA is ignored.
- ConstraintTypes.Hinge the frame represents pivot in A/B and Axis in A/B. This is because the hinge allows only a limited angle of rotation between the rigidbody and the world in this case.
- ConstraintTypes.ConeTwist, useReferenceFrameA is ignored.
- ConstraintTypes.Gear, useReferenceFrameA is ignored. The frame just represents the axis either in A or B; only the translation vector (which should contain the axis) is used.
useReferenceFrameA determines which coordinate system (RigidBodyA or RigidBodyB) the limits are applied to.
Add constraints to the simulation
After you create a constraint, add it to the simulation from a script by calling:
or:this.GetSimulation().AddConstraint(constraint);
disableCollisionsBetweenLinkedBodies stops linked bodies colliding with each other. Likewise, to remove a constraint from the simulation, use:var disableCollisionsBetweenLinkedBodies = true;this.GetSimulation().AddConstraint(constraint, disableCollisionsBetweenLinkedBodies);
this.GetSimulation().RemoveConstraint(constraint);
See also
- Colliders
