[OpenSCAD] rotating an object around its center....

Jordan Brown openscad at jordan.maileater.net
Fri Jul 19 13:21:26 EDT 2019


On 7/19/2019 7:15 AM, Bruno Boettcher wrote:
> hello!
>
> maybe i am too tired to understand it, i have a prism, i rotate into
> place, thus doing it around the 0,0,0 and then i rotate it around its
> axe (by transferring it to the center, and back...)
> this would work, if later on i hadn't the need to translate the whole
> thing....
>
> i made this :
>
> module prism(l, w, h){
>   polyhedron(
>       points=[[0,0,0], [l,0,0], [l,w,0], [0,w,0], [0,w,h], [l,w,h]],
>       faces=[[0,1,2,3],[5,4,3,2],[0,4,5,1],[0,3,4],[5,2,1]]
>       );
> }
> translate([0,0,8]) //translate back
>   {
>     translate([0,0,-8])//translate to center
>       rotate([5,0,0])//give it the recquired angle
>       {
>             rotate([180,-90,0])
>             translate([1.6,-3.15,0])
>             prism(16,3,40);
>       }
>   }
>
> this reads stupid and cumbersome, i am sure there is a much more
> elegant way to make this?

You say you're trying to rotate it around its center, but nothing in
there does that.  The lines marked "translate to center" and "translate
back" cancel each other out.

I'm not sure exactly what you're trying to do, so I can't tell you the
right answer, but I can work through what you *are* doing...

You're making a prism in positive XYZ with one corner at the origin:


You're translating it into +X and -Y so that it's 1.6 +X and 0.15 -Y
from the origin.  (The -Y offset is too small to see on this picture.)

You're rotating it 180 degrees around the X axis (so it is now in +X,
+Y, and -Z) and then -90 degrees around the Y axis, so that it is once
again in +X, +Y, +Z, and now has its long axis along X.  It's offset
+0.15 in Y and +1.6 in Z.



You're rotating it +5 degrees around X, so that the top edge is now
slightly into -Y.



You're translating it -8 Z, taking about half of it into -Z.  (But note
it was floating at +1.6 Z, so it's still a bit above centered.)



and then you're translating it +8 Z, undoing the translation you just did.


Maybe you meant this?

translate([0,0,8])       // translate back to +Z
    rotate([5,0,0])      // give it the required angle
    translate([0,3,-8])  // translate back to +Y, and center in Z
    rotate([180,-90,0])
    prism(16,3,40);




You could simplify that a bit by creating the prism in that basic
orientation in the first place:

module prism(l, w, h){
    polyhedron(
        points=[[0,0,0], [0,0,l], [0,w,l], [0,w,0], [h,0,0], [h,0,l]],
        faces=[[0,1,2,3],[5,4,3,2],[0,4,5,1],[0,3,4],[5,2,1]]
        );
}
translate([0,0,8])       // translate back to +Z
    rotate([5,0,0])      // give it the required angle
    translate([0,0,-8])  // Center in Z
    prism(16,3,40);


Polyhedra hurt my head, so I'd do the prism as an extrusion from a triangle:

module prism(l, w, h){
    points = [[0,0], [h,0], [0,w]];
    linear_extrude(height=l)
        polygon(points);
}
translate([0,0,8])       // translate back to +Z
    rotate([5,0,0])      // give it the required angle
    translate([0,0,-8])  // Center in Z
    prism(16,3,40);

Is that closer to what you were looking for?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20190719/4b7ec947/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hdmhicnadipammii.png
Type: image/png
Size: 13189 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20190719/4b7ec947/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: iegegccflnfogich.png
Type: image/png
Size: 13035 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20190719/4b7ec947/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kiippcfplfibffih.png
Type: image/png
Size: 14477 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20190719/4b7ec947/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: naohcphgfejmhnaj.png
Type: image/png
Size: 14298 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20190719/4b7ec947/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kgoalhblgjdgednb.png
Type: image/png
Size: 13806 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20190719/4b7ec947/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dfkcechpkfhblokf.png
Type: image/png
Size: 13635 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20190719/4b7ec947/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cgpcagjnbcnfjppg.png
Type: image/png
Size: 13665 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20190719/4b7ec947/attachment-0006.png>


More information about the Discuss mailing list