We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
translate(); bug? (Read 580 times)
translate(); bug?
Feb 29th, 2008, 4:45pm
 
I've been learning processing bit by bit the last couple of weeks, and I've finally ran into a problem I can't seem to solve. The code is supposed to draw the cube by lines, and when it's done, just replace it with a box to save time and use fills. The drawing part works, but when it is replaced with a box, it places it on a different place then the original box. How come? The problem doesn't seem to be with the replaced boxes, since those are 'translated' as they should..



code:

import processing.opengl.*;

float eyeZ = 80;
float eyeY;
float eyeX;

int cubesize;
float increase;
int cubetotal;
int boxdone;

float zdepth1 = 10;
float xwidth1 = 10;
float ywidth1 = 10;

float zdepth2 = 10;
float xwidth2 = 10;
float ywidth2 = 10;

float zdepth3 = 10;
float xwidth3 = 10;
float ywidth3 = 10;

float zdepth4 = 10;
float xwidth4 = 10;
float ywidth4 = 10;

void setup(){
size(640, 480, OPENGL);
smooth();
noFill();
background(255);
}

void draw(){
 
  background(255);
  camera(eyeX, eyeY, eyeZ,
        0.0, 0.0, 0.0,
        0.0, 1.0, 0.0);

// box(10);
eyeY = eyeY-0.1;
eyeX = eyeX-0.1;

drawCube();
translate(10, 0, 0);
drawCube();

increase = increase+0.001;


}

void drawCube(){
strokeWeight(2);
if(boxdone == 0){
if(xwidth1 > 0){
xwidth1=xwidth1-increase;
}else if(xwidth2 > 0){
 xwidth2=xwidth2-increase;
 zdepth4=zdepth4-increase;
}



if(ywidth1 > 0){
ywidth1=ywidth1-increase;
}else if(ywidth2 > 0){
 ywidth2=ywidth2-increase;
 zdepth3=zdepth3-increase;
}else if(zdepth2 > 0){
zdepth2=zdepth2-increase;

}

if(zdepth1 > 0){
zdepth1=zdepth1-increase;
}else if(xwidth3 > 0){
 ywidth3=ywidth3-increase;
 xwidth3=xwidth3-increase;  
}else if(xwidth4 > 0){
 ywidth4=ywidth4-increase;
 xwidth4=xwidth4-increase;  
}else{
 boxdone=1;
}




 //drawphase
line(xwidth4,0,10,0);  
line(0,ywidth4,0,10);
line(10,10,xwidth3,10);
line(10,ywidth3,10,10);

translate(0,0,10);
line(xwidth2,0,10,0);
line(0,ywidth2,0,10);
line(10,10,xwidth1,10);
line(10,ywidth1,10,10);
translate(0,0,-10);
 
 
rotateZ(radians(45));
line(0,0,zdepth2,0,0,10);
rotateZ(radians(45));
line(10,-10,zdepth1,10,-10,10);
rotateZ(radians(180));
translate(-10,0,0);
rotateZ(radians(45));
line(0,0,zdepth3,0,0,10);
rotateZ(radians(45));
line(10,-10,zdepth4,10,-10,10);
//rotateZ(radians(-360));

}else if(boxdone == 1){
 translate(5,5,5);
 strokeWeight(1.71);

box(10);
boxdone=2;
cubetotal++;
 zdepth1 = 10;
xwidth1 = 10;
ywidth1 = 10;

zdepth2 = 10;
xwidth2 = 10;
ywidth2 = 10;

zdepth3 = 10;
xwidth3 = 10;
ywidth3 = 10;

zdepth4 = 10;
xwidth4 = 10;
ywidth4 = 10;
translate(-5,-5,-5);

}else if(boxdone == 2){
  //stroke(255,0,0);
 translate(5,5,5);

 strokeWeight(1.71);
  box(10);
 translate(-5,-5,-5);
   
}
}


Re: translate(); bug?
Reply #1 - Feb 29th, 2008, 6:28pm
 
It is undoubtly a matrix stack problem. See
http://processing.org/reference/pushMatrix_.html
and
http://processing.org/reference/popMatrix_.html

I can't explain in details, since I haven't read the whole code, but I think using push/popMatrix would make it work :

Code:
pushMatrix();
drawCube();
popMatrix();
translate(10, 0, 0);
pushMatrix();
drawCube();
popMatrix();
Re: translate(); bug?
Reply #2 - Feb 29th, 2008, 9:53pm
 
antiplastik wrote on Feb 29th, 2008, 6:28pm:
It is undoubtly a matrix stack problem. See
http://processing.org/reference/pushMatrix_.html
and
http://processing.org/reference/popMatrix_.html

I can't explain in details, since I haven't read the whole code, but I think using push/popMatrix would make it work :

Code:
pushMatrix();
drawCube();
popMatrix();
translate(10, 0, 0);
pushMatrix();
drawCube();
popMatrix();

Thanks! It does work, though I still do not understand matrix stacking.. Could you link me to some more info?
Re: translate(); bug?
Reply #3 - Mar 1st, 2008, 9:40am
 
Basically, pushMatrix() is like saving the location where you will draw, set with translate, and the rotation/scale.

popMatrix() will then take you back to that position, and will apply the rotation and scale you had there.
This is the example from the manual. It draws a rect at 0,0 and saves that position. Then moves 30 to the right and 20 down, draws another rect, jumps back to the position saved with pushMatrix(), and draws a rect at 15,10 (so a bit to the right of the saved location).

Code:
rect(0, 0, 50, 50);   //White rectangle 
pushMatrix();
translate(30, 20);
fill(0);
rect(0, 0, 50, 50); //Black rectangle
popMatrix();
fill(102);
rect(15, 10, 50, 50); //Gray rectangle


But like I said, you can also use this to jump back to a certain rotation or scale (or all together)
Page Index Toggle Pages: 1