Quantcast

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

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

bac1024
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);
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

MichaelAtOz
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!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

bac1024
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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

bac1024
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

MichaelAtOz
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!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

jdawgaz
you can also think of it this way.

the rightmost transformation gets done first, then the one to the left of that, and so on.



--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Openscad developer

The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov


On Tue, Feb 14, 2017 at 10:14 PM, MichaelAtOz <[hidden email]> wrote:
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");
}

<http://forum.openscad.org/file/n20447/trans-rota.jpg>

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!
--
View this message in context: http://forum.openscad.org/I-can-t-figure-out-what-s-going-on-with-rotate-and-translate-tp20443p20447.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

bac1024
In reply to this post by MichaelAtOz
heh, I actually did a test very similar to your graphic to figure out how all of it worked. :)   Thank you for your help.....
Loading...