# Need help with some vector math

I have this peace of code:

``````var physics = PhysicsWorld.active;

// Start from cone location
var from = object.transform.world.getLoc();

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

// 1000 units long
to.mult(1000);

// End position
to.add(from);

var rb = physics.rayCast(from, to);
if (rb != null) {
trace(physics.hitPointWorld);
}
``````

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.

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);
v2.setFrom(physics.hitPointWorld).sub(from).normalize();

var q = new Quat().fromTo(v1, v2);
object.local.applyQuat(q);
``````

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

1 Like

laser.transform.local.applyQuat(q);

this still did nothing, but

laser.transform.rot = q;

Did that work to rotate it?

Kind of, its hard to tell without right location