I’m trying to create an axle that sits between 2 meshes and stays between them. So far I’ve managed to get the position in the middle using vector3 lerp, but I’m unable to work out how to get it to rotate so the axle ends are pointing at the meshes.
I had a go at using the cross product to find the angle between the 2 vectors but math is not one of my strengths so I have no idea where I’m going wrong (or if I’m even using the right method).
initially force cylinder to lie along x axis, baking transformation and so scaling the x property of the mesh and ensuring axis1 is the first parameter of the RotationFromAxes
axis1 = (sphere1.position).subtract(sphere2.position);
axis3 = BABYLON.Vector3.Cross(camera.position, axis1);
axis2 = BABYLON.Vector3.Cross(axis3, axis1);
mid = ((sphere1.position).add(sphere2.position)).scale(0.5);
mesh.scaling.x = axis1.length(); // reset scale to match distance between spheres
mesh.position = mid; // reposition mesh so that its center is the mid point between spheres
mesh.rotation = BABYLON.Vector3.RotationFromAxis(axis1, axis2, axis3); //match rotation to line joining spheres
The formation of axis3 uses the camera position as an arbitrary vector to calculate an axis perpendicular to axis1
This PG https://www.babylonjs-playground.com/#VYM1E#37 starts with the created cyclinder in its created orientation, vertical. So provided axis1 is assigned to the y axis (rather than the x axis) there is no need to bake the mesh. The orientation of your imported mesh and whether it is already aligned to one of the axes x, y, or z depends on the need for baking and which axis (x, y, or z) that axis1 is assigned to. Sometimes you may need to swap axis2 and axis3 around for the cylinder to be in the proper direction.