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?
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
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.