Lock joint turning mesh, does it work the way its supposed to

I am using cannon physics lock joint, due to cannot create new mesh due to collision detection not working and the child/parent are not working the way I hoped so I tried using the lock joint

var lockJoint = new BABYLON.PhysicsJoint(BABYLON.PhysicsJoint.LockJoint, {});
cylinderHandle.physicsImpostor.addJoint(tenisNet.physicsImpostor, lockJoint);

it is turning one mesh to its side from its original orientation, i also tried messing with these settings, these numbers are exaggerated due to testing it, but they done seem to have made any effect on how the two meshes/impostors are connected together

collision: true, connectedPivot: new BABYLON.Vector3(50,50,50), connectedAxis: new BABYLON.Vector3(120,50,0), mainAxis: new BABYLON.Vector3(0,40,0.3)

Am I doing it correctly, or do these properties not effect it.

thanks to anyone if they can help

Pinging @RaananW


Would you be able to share a reproduction on the playground? this way we will know we are working on the same codebase :slight_smile:

Hi, thanks again for the help here is the link to the play ground

1 Like

The lock constraint takes no pivot/axis information. it simply “locks” two impostors.

You had 2 issues i could see in the playground. The first was that you used cylinder mesh on both impostors. The handle might be a cylinder (well, sorry - it IS a cylinder :-)), but the net is not. The net can be seen as a box with no corners, so i would either use a box impostor, or a mesh impostor (while being extra careful about performance).

Once you set the impostors correctly, you can use a compound to connect the two:

lockjoint trial | Babylon.js Playground

Notice that I also created a box-ground (and not a flat one). When using such low values the physics engine might not calculate collisions correctly. So it is better to define a larger box so that the collision with the ground will be registered for sure.

I hope this helps!

The main reason I went away from parent and child and tried lock joints was due to, using vr equipment to try and pick it up by the handle, when I try to pick up the child which I want as cylinder handle, it pulls it away from the parent causing problems, so i want to lock them together in both ways, were as by using child and parent, only the child follows the parent

the code i am using for the xr and vr equipment is:

 xr.input.onControllerAddedObservable.add((controller) => {
                    controller.onMotionControllerInitObservable.add((motionController) => {            
                        const squeeze = motionController.getComponentOfType('squeeze');
                        if (squeeze) {
                            // check its state and handle state changes
                            squeeze.onButtonStateChangedObservable.add(() => {
                                // pressed was changed
                                if (squeeze.changes.pressed) {
                                    // is it pressed?
                                    if (squeeze.pressed) {
                                        // animate position
                                        controller.getWorldPointerRayToRef(tmpRay, true);
                                        const position = controller.grip ? controller.grip.position : controller.pointer.position;
                                        let mesh = scene.meshUnderPointer;
                                        if (xr.pointerSelection.getMeshUnderPointer) {
                                            mesh = xr.pointerSelection.getMeshUnderPointer(controller.uniqueId);
                                        if (mesh && mesh !== ground && (mesh.physicsImpostor || mesh.name == "cylinder")) {   //mesh name changed   // this is the animation of 2 meshes coliding 
                                            const animatable = BABYLON.Animation.CreateAndStartAnimation('meshmove',
                                                mesh, 'position', 30, 15, mesh.position.clone(),
                                                new BABYLON.BezierCurveEase(0.3, -0.75, 0.7, 1.6), () => {
                                                    if (!mesh) return;
                                                    meshesUnderPointer[controller.uniqueId] = mesh;
                                                    observers[controller.uniqueId] = xr.baseExperience.sessionManager.onXRFrameObservable.add(() => {
                                                        const delta = (xr.baseExperience.sessionManager.currentTimestamp - lastTimestamp);
                                                        lastTimestamp = xr.baseExperience.sessionManager.currentTimestamp;
                                                        controller.getWorldPointerRayToRef(tmpRay, true);
                                                        tmpRay.direction.scaleInPlace(0.1);  // how far away the object is
                                                        // const rotation = controller.grip ? controller.grip.rotation : controller.pointer.rotation; //
                                                        // // tmpRot.copyFrom(rotation);
                                                        // meshesUnderPointer[controller.uniqueId].rotation.copyFrom(rotation); //
                                                        const position = controller.grip ? controller.grip.position : controller.pointer.position;
                                                        tmpVec.copyFrom(position);              //this is the momentum i think
                                                        tmpVec.subtractToRef(oldPos, tmpVec);
                                                        tmpVec.scaleInPlace(1000 / delta);
                                                        if(mesh.physicsImpostor){ // //problem is here
                                    } else {
                                        // throw the object
                                        if (observers[controller.uniqueId] && meshesUnderPointer[controller.uniqueId]/* && meshesUnderPointer[controller.uniqueId].physicsImpostor*/) { //physics impostor change
                                            observers[controller.uniqueId] = null;
                                            if(meshesUnderPointer[controller.uniqueId].physicsImpostor){ //

thanks for any further help

I know this code. It’s my code :slight_smile: From my tennis example.
Why don’t you try creating a playground with the objects and this code, and show exactly what doesn’t work?

Thanks for all the help, you are giving me.

from looking at this, rather than making a whole set of meshes as the racket, you instead changed you controller into the handle, and then connected the “tennis net” to the handle, which in turn stops the problems with clashing physics impostor.

Am I getting this correct, if so this might be a better idea for me to go along this route, due to the problem of using multiple physics impostors with parent and children, also with the physics engine, which is the best one to use with babylon

This is the way i like doing things, it does not mean that it is the right way.

You can do it in any way you want, i just like keeping things as simple as possible, otherwise you have unexpected issues. Wherever possible, keep it simple.

Thanks for all the help, this has helped me get past the what i have been trying to get work for the past couple of days, now i can focus on the main part of what im trying to make, the bat works the way i wanted, thanks for all the help.

heres the playground of your example for if anyone looks in the future https://playground.babylonjs.com/#CHF3KW#74

1 Like