cannon.js icon indicating copy to clipboard operation
cannon.js copied to clipboard

help with cylinder transform to keep two 3D points connected

Open labriata opened this issue 7 years ago • 2 comments

Hello,

I'm using cylinders to visually connect pairs of spheres. Spheres are atoms and cylinders bonds between atoms.

So I want the cylinder to move in space together with the spheres, to keep each end of the cylinder visually connected to each sphere.

The spheres are moved by cannon. I can retrieve their positions as vectors with:

new THREE.Vector3( world.bodies[i].position ); new THREE.Vector3( world.bodies[i+1].position );

But then, how do I translate and rotate the cylinder so that it will still be connecting the two spheres? I tried quaternions and such but so far nothing worked. Maybe you can think of a clever solution?

Also, the distance between pairs of spheres is always constant, but IF they changed, how could I make the cylinders longer or shorter (at constant radius, so not just scaling) ?

Thanks community!

labriata avatar Aug 25 '18 11:08 labriata

Update -I'm trying to .lookAt(sphere2.position) but it doesn't work as intended. But since I'm doing this inside a ARmarker, I wonder if there is some conversion between marker and global coordinates to be done?

labriata avatar Aug 25 '18 16:08 labriata

By default the lookAt tries to point the objects local z axis at the position indicated. So it all depends on the way that the geometry is created. Plus it only works with either no parent or the parent has not been translated or rotated. You would simply scale in the z - since that is the axis around which the cylinder is formed. Not sure if that helps.

ghost avatar Sep 19 '18 11:09 ghost