How to use the LockConstraint

Hey there!

I am trying to use the LockConstraint to lock to “glue” 2 objects together. In all the demos I saw, the pivot point was chosen arbitrarily, which caused the 2 objects to be moved in order to satisfy the constraint, hence not acting like a glue but more like a magnet between 2 points.

Instead, I would like to create a LockConstraint that is satisfied given the initial relative positions of my 2 objects, gluing them in place relative to each other.

I defined the pivot point as being the average position of the 2 objects, and then transform this position in the local space of the 2 objects to create the constraint but I can’t make it work unfortunately:

Does anyone know how these constraint works :joy: ?

constraint pivot and axis are in object local space. no need to transform them using object inverse world matrix.

note, initial position is important. as bodies will try to resolve the constraint, it can introduce forces to that extend.

Thanks that’s better! But I am not sure how I can account for object rotation if I don’t use an inverse world matrix somewhere :thinking:

If I understand correctly, the pivotA is the position of bodyA in local space of bodyB and and pivotB is the position of bodyB in local space of bodyA ? x)

correct! if you want to set world space anchor position, then yes, you’ll need to use parent inv matrix. I’m a bit biased as I always set values from local.

Here is a version using world space pivot :

Ah I see the issue in my first playground was not calling computeWorldMatrix(true) before computing the inverse!

I tried using the same method for the constraint axis but I did not get the expected result:

I think the problem is that I do not really understand what the axis should be for a LockConstraint. For a hinge it is quite intuitive that the axis is the axis of the hinge itself in the local space of both objects, but here I really have a hard time understanding that :confused:

For a lock constraint, the axis importance is pretty low :slight_smile: I would try any unit axis.

That’s also what I expected, but here is what I have when I disable the constraint:

And then when I enable the constraint I expect to get the same result, but instead depending on the axis I choose I get this (both axis are transformed with the inverse world matrix):

or that (both axis are Vector3.Up()):