I have two objects at arbitrary locations, and I want to draw a cylinder between one and another. Searching for math solutions tells me I need the dot product and the cross product as angle and axis respectively, but I'm lost on producing a quaternion or transforming my results into the parameters for rotate
.
This is what I have so far:
function dot (v1, v2) = [v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]];
function normalize (v) = v / norm(v);
function distance (v1, v2) = sqrt(
pow(v2[0] - v1[0], 2) +
pow(v2[1] - v1[1], 2) +
pow(v2[2] - v1[2], 2)
);
function lookAt(v1, v2) =
let(n1 = normalize(v1))
let(n2 = normalize(v2))
let(v = dot(n1, n2))
let(angle = [acos(v[0]), acos(v[1]), acos(v[2])])
let(axis = normalize(cross(n1, n2)))
[0, 0, 0]; // I don't know what to return here
module cylTo(v1, v2) {
translate(v1)
rotate(lookAt(v1, v2))
cylinder(distance(v1, v2), 2);
}
v1 = [-33, -20, 22];
v2 = [20, 20, -20];
// Draw sphere 1
translate(v1)
sphere(10);
// Draw sphere 2
translate(v2)
sphere(10);
// Draw cylinder between the two
cylTo(v1, v2);
What is needed for rotate()
so a cylinder()
points at the other object?