Need help with some vector math

I have this peace of code:

var physics =;

                // Start from cone location
                var from =;

                // Cast ray in the direction cone points to
                var to = object.transform.look();

                // 1000 units long

                // End position
                var rb = physics.rayCast(from, to);
                if (rb != null) {

Now I got from and got hitpoint. I want to transform and scale cylinder so that it goes from from to hitpoint. Any suggestions?

Thank you

If your cylinder’s origin is at one extreme end of the cylinder, and your cylinder’s size is 1 unit long when it’s scale is 1, then you could try something like this ( I haven’t tested it, but it should give you the idea ):

var dist = Vec4.distance(from, physics.hitPointWorld); // Get the distance to the hit point
object.transform.scale = new Vec4(1, 1, dist); // Set the scale of the cylinder to match the distance to the hit point
object.buildMatrix(); // Update transform matrix with modified scale
object.local.applyQuat(new Quat().fromTo(from, physics.hitPointWorld)); // Rotate object to point at hit point

Did not know of this. Sadly id does nothing for me. :confused:

Ah, I forgot that the inputs to Quat.fromTo needs to be normalized.

Try this code from the LookAt node to do the rotation:

v1 = new Vec4();
v2 = new Vec4();
v1.set(0, 0, 1);

var q = new Quat().fromTo(v1, v2);

Another thing to note is the cylinder can’t be the child of another object or the rotation won’t work because the rotation is set in the local space.

Ok tnx. Scale, rot is in place, now I have to figure out location :wink:

1 Like


this still did nothing, but

laser.transform.rot = q;

Did that work to rotate it?

Kind of, its hard to tell without right location