# I can't figure out what's going on with rotate and translate...

7 messages
Open this post in threaded view
|

## I can't figure out what's going on with rotate and translate...

 I'm sure I've done things like this before, but this one is giving me issues, I'm sure it's related to the rotate, but my objects are not lining up right with my translate commands...  Can someone tell me what's going on here, and how to fix it? (The first hinge works ok, but the second one, the pieces don't all move the same....) module Hinge(x,y,z,d){         \$fn=25;     Dir= d==0?0:7;     Rot= d==0?0:180;         echo (Dir);     rotate([0,0,0]) translate([x,y,z])   cube([29,16,2],center=false);     rotate([0,90,0]) translate([x,y+20,z+(7-Dir)]) sphere(d=2,center=false);     difference(){         union(){             rotate([0,0,0]) translate([x,y+16,z])  cube([7,4,2],center=false);             rotate([0,90,0]) translate([x,y+20,z])  cylinder(h=7,d=4,center=false);         }         rotate([0,90,0]) translate([x,y+20,z+Dir])  sphere(d=2,center=false);     }       rotate([0,90,0]) translate([x,y+20,z+(22-Dir)]) sphere(d=2,center=false);     difference(){         union(){             rotate([0,0,0]) translate([x+15,y+16,z]) cube([7,4,2],center=false);             rotate([0,90,0]) translate([x,y+20,z+15]) cylinder(h=7,d=4,center=false);         }         rotate([0,90,0]) translate([x,y+20,z+15+Dir])  sphere(d=2,center=false);     } } Hinge(0,0,0,1); Hinge(40,0,0,1);
Open this post in threaded view
|

## Re: I can't figure out what's going on with rotate and translate...

 Administrator As a generalisation, you want to do rotates before translates, particularly when tilting cylinders. module Hinge(x,y,z,d){         \$fn=25;     Dir= d==0?0:7;     Rot= d==0?0:180;         echo (Dir);     rotate([0,0,0]) translate([x,y,z])   cube([29,16,2],center=false);      translate([x,y+20,z+(7-Dir)]) rotate([0,90,0]) sphere(d=2,center=false);     difference(){         union(){             rotate([0,0,0]) translate([x,y+16,z]) cube([7,4,2],center=false);              translate([x,y+20,z])rotate([0,90,0]) cylinder(h=7,d=4,center=false);         }         translate([x+7,y+20,z+(7-Dir)])rotate([0,90,0]) sphere(d=2,center=false);     }        translate([x+15,y+20,z+(7-Dir)])rotate([0,90,0]) sphere(d=2,center=false);     difference(){         union(){             rotate([0,0,0]) translate([x+15,y+16,z]) cube([7,4,2],center=false);             translate([x+15,y+20,z]) rotate([0,90,0]) cylinder(h=7,d=4,center=false);         }         translate([x+15+7,y+20,z+(7-Dir)]) rotate([0,90,0]) sphere(d=2,center=false);     } } Hinge(0,0,0,1); Hinge(40,0,0,1); You may need to fix the Dir bits... Admin - PM me if you need anything, or if I've done something stupid... Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Open this post in threaded view
|

## Re: I can't figure out what's going on with rotate and translate...

 ok, I'm confused now (ok, a little more?) You say to do the rotates before the translates, but then you moved my rotates to after the translates?
Open this post in threaded view
|

## Re: I can't figure out what's going on with rotate and translate...

 Ok, nevermind.  I got it straightened out....  I think you meant to do the rotates AFTER the translates, because otherwise the XYZ order is changed.
Open this post in threaded view
|

## Re: I can't figure out what's going on with rotate and translate...

 Administrator In reply to this post by bac1024 Sorry, I should have been clearer. Operations apply to the child, and rotate() rotates around an axis. translate() sphere() translates the sphere, rotate() sphere() rotates the sphere, in it's place, so you don't see any effect rotate() translate() sphere(), moves the sphere away from the axis, then rotates it on an axis So what I meant by do rotates before, was to apply the rotate to the object, rotate() object() then translate it translate()     rotate()         object(); Try thinking like the {} brackets are in place translate() {     rotate() {        object();     } } Or try this code: module cubeOrigin(col) {   color(col)     cube([10,15,10]);   color("red")     cylinder(d=1,h=10); } cubeOrigin("lightgreen"); rotate([0,0,90])   cubeOrigin("green"); translate([0,20,0])   cubeOrigin("lightblue"); rotate([0,0,90])   translate([0,20,0])     cubeOrigin("blue"); translate([0,40,0])   cubeOrigin("plum"); rotate([0,0,90])   translate([0,40,0])     cubeOrigin("purple"); translate([0,0,30]) { // move up a layer & reverse order of translate/rotate translate([0,20,0])   cubeOrigin("lightblue"); translate([0,20,0])   rotate([0,0,90])     cubeOrigin("blue"); translate([0,40,0])   cubeOrigin("plum"); translate([0,40,0])   rotate([0,0,90])     cubeOrigin("purple"); } Note the red origin (at [0,0,0] before any transformations). Admin - PM me if you need anything, or if I've done something stupid... Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!