Mark Harrison
Mark Harrison

Reputation: 304544

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]);

Upvotes: 10

Views: 9956

Answers (3)

jarppa
jarppa

Reputation: 25

module rotate_and_place(x,y,z,pt,pos){
        rotate([x, 0, 0])
        translate(pos)
        translate(-s/2)
        translate(pt)
        rotate([0, y, z])
        translate(-pt)
        children();   
    }

s=[10,10,1];
rotate_and_place(0,0,45,s/2,s/2) //original no center
//rotate_and_place(0,0,45,s/2,[0,0,0]) //like center=true
//rotate_and_place(0,0,45,s/2,[12,-3,0.5]) //now center is here
cube(s);

Upvotes: 0

Mark Harrison
Mark Harrison

Reputation: 304544

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();   
}

Upvotes: 36

Ryan Jarvis
Ryan Jarvis

Reputation: 61

If you are willing to 'center' the shape it is much easier:

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

Upvotes: 2

Related Questions