discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Re: How to use rotate ?

JB
Jordan Brown
Mon, Jul 18, 2022 9:48 PM

Just have to do the trig right, and remember that rotate() does the
rotation around X, then Y, then Z.

First, here's a general rectangular-to-polar conversion function:

function topolar3(p) = [
    norm(p),
    atan2(p.y, p.x),
    atan2(norm([p.x,p.y]), p.z)
];

and now we'll use that to get the angles in a form for rotation:

function getangles(p1, p2) =
    let (polar = topolar3(p2-p1))
    [
        0,
        polar[2],
        polar[1]
    ];

and finally we'll demonstrate:

p1 = [5,8,2];
p2 = [-20,-10,-10];
translate(p1) sphere(d=2);
translate(p2) sphere(d=2);

translate(p1) rotate(getangles(p1, p2)) cylinder(h=100, d=1);

Note: This works if you have a symmetrical object like a cylinder.  If
it's not symmetrical and so you care which way it's rotated along its
new axis, that'll take another rotate, applied inside the rotate shown. 
(It can be combined into that rotation, but it hurts my head to figure
out the resulting 3D rotate when you want to rotate around Z first.)

Just have to do the trig right, and remember that rotate() does the rotation around X, then Y, then Z. First, here's a general rectangular-to-polar conversion function: function topolar3(p) = [ norm(p), atan2(p.y, p.x), atan2(norm([p.x,p.y]), p.z) ]; and now we'll use that to get the angles in a form for rotation: function getangles(p1, p2) = let (polar = topolar3(p2-p1)) [ 0, polar[2], polar[1] ]; and finally we'll demonstrate: p1 = [5,8,2]; p2 = [-20,-10,-10]; translate(p1) sphere(d=2); translate(p2) sphere(d=2); translate(p1) rotate(getangles(p1, p2)) cylinder(h=100, d=1); Note: This works if you have a symmetrical object like a cylinder.  If it's not symmetrical and so you care which way it's rotated along its new axis, that'll take another rotate, applied inside the rotate shown.  (It can be combined into that rotation, but it hurts my head to figure out the resulting 3D rotate when you want to rotate around Z first.)
JB
Jordan Brown
Mon, Jul 18, 2022 10:00 PM

On 7/18/2022 2:48 PM, Jordan Brown wrote:

Just have to do the trig right, and remember that rotate() does the
rotation around X, then Y, then Z.

... and that 3D polar coordinates only need two angles (theta and phi,
azimuth and elevation), not three.

On 7/18/2022 2:48 PM, Jordan Brown wrote: > Just have to do the trig right, and remember that rotate() does the > rotation around X, then Y, then Z. ... and that 3D polar coordinates only need two angles (theta and phi, azimuth and elevation), not three.
F
fxeconomist@yahoo.com
Mon, Jul 18, 2022 10:13 PM

I managed to find a weird piece of code that does the job with a matrix. I don’t understand it - it’s without any trigonometry - but it works.

I managed to find a weird piece of code that does the job with a matrix. I don’t understand it - it’s without any trigonometry - but it works.