openscad

OpenSCAD: Rotating around a particular point?


The following code rotates the second cube around the origin. How can I rotate the second cube around its center point ([5,5,0]) instead?

cube([10,10,1]);
rotate([0,0,45]) cube([10,10,1]);


Solution

  • This module will perform the desired rotation.

    // rotate as per a, v, but around point pt
    module rotate_about_pt(a, v, pt) {
        translate(pt)
            rotate(a,v)
                translate(-pt)
                    children();   
    }
    
    cube([10,10,1]);
    rotate_about_pt(45,0,[5,5,0]) cube([10,10,1]);
    

    In newer versions (tested with the January 2019 preview) the above code generates a warning. To fix that, update the parameters to rotate:

    module rotate_about_pt(z, y, pt) {
        translate(pt)
            rotate([0, y, z]) // CHANGE HERE
                translate(-pt)
                    children();   
    }