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.
IndexProgramming Questions & HelpPrograms › Rotation and Translation
Page Index Toggle Pages: 1
Rotation and Translation (Read 1322 times)
Rotation and Translation
Aug 12th, 2005, 3:44am
 
Hi just came across this program and have been having a lot of fun with it.  I'm at a loss to why this doesn't work.  I want the left and right arrow keys to rotate the graphic, while the up and down keys to move it forward or backward in it's local space.  Would anyone mind taking a look at my code here?  Thanks.

bot b1 = new bot(0,0);

void setup(){
 size(400,400);
 background(0);
 fill(200);
 noStroke();
 rectMode(CENTER);
 smooth();
 framerate(60);
}
void draw(){
 b1.update();
}
void keyPressed(){
   if (keyCode == LEFT){b1.rot--;}
   if (keyCode == RIGHT){b1.rot++;}
   if (keyCode == UP){b1.x++;}
   if (keyCode == DOWN){b1.x--;}
}

class bot{
 int x, y;
 int s = 10;
 int rot = 0;
 bot (int cx, int cy){
   x=cx;
   y=cy;
 }
 void update(){
   background(0);
   rotate(radians(rot));
   translate (x, y);    
   rect(0, 0, s, s);
 }
}
Re: Rotation and Translation
Reply #1 - Aug 12th, 2005, 9:47am
 

Try initializing the x and y coordinates to somewhere visible. There's nothing wrong with the program.

bot b1 = new bot(width/2, height/2);
Re: Rotation and Translation
Reply #2 - Aug 12th, 2005, 10:33am
 
oh that's not the problem, i've just been starting him in the top left to simplify what i was doing.  if i rotate then translate (as in this code), he translates along his local axis correctly but rotate around 0,0.  if i translate then rotate, he rotates in place correctly but translates in world space.
Re: Rotation and Translation
Reply #3 - Aug 12th, 2005, 11:40am
 

Do you mean this?

Code:

void update(){
background(0);
translate (x,y);
rotate(radians(rot));
translate (0, 0);
rect(0, 0, s, s);
}

Re: Rotation and Translation
Reply #4 - Aug 13th, 2005, 3:00am
 
i don't see what that would do.  i believe the translation matrix is additive so adding 0, 0 wouldn't do anything.  i think what i wrote makes sense logically but there's something about the way processing is working with it that i'm not understanding.  it behaves like the matrix gets reset to 0, 0 each time.
Re: Rotation and Translation
Reply #5 - Aug 13th, 2005, 7:35am
 
ok, i think i'm starting to understand what the problem is here.  it seems that resetMatrix() is being called behind the scenes at every draw() loop.  the following code executes as i would expect it:
Code:

rectMode(CENTER);
background(0);
framerate(1);

rect(0, 0, 10, 10);

translate(20, 20);
rect(0, 0, 10, 10);

translate(20,20);
rotate(radians(45));
rect(0, 0, 10, 10);

translate(20, 20);
rect(0, 0, 10, 10);


then i took the same code and broke it up into sections rendering each square in a seperate call to draw().  this is producing what looks like the same problem as my original post.
Code:

int f=-1;

void setup(){
rectMode(CENTER);
background(0);
framerate(1);
}

void draw(){
f++;
switch (f){
case 1:
rect(0, 0, 10, 10);
break;

case 2:
translate(20, 20);
rect(0, 0, 10, 10);
break;

case 3:
translate(20,20);
rotate(radians(45));
rect(0, 0, 10, 10);
break;

case 4:
translate(20, 20);
rect(0, 0, 10, 10);
break;

default:
f = 0;
}
}

so it's resetting each time and not remembering which way it was oriented.  maybe i'm answering my own question here.  looks like i need to store the matrix and recall it each loop - if that's even possible.  anyone have some insight on what i'm trying to do here?

Re: Rotation and Translation
Reply #6 - Aug 13th, 2005, 12:15pm
 
pushMatrix, popMatrix is what you are looking for..

Code:

int f=-1;

void setup(){
rectMode(CENTER);
background(0);
framerate(1);
}

void draw(){
f++;
pushMatrix();
switch (f){
case 1:
rect(0, 0, 10, 10);
break;

case 2:
translate(20, 20);
rect(0, 0, 10, 10);
break;

case 3:
translate(20,20);
rotate(radians(45));
rect(0, 0, 10, 10);
break;

case 4:
translate(20, 20);
rect(0, 0, 10, 10);
break;

default:
f = 0;
}
popMatrix();
}



that should keep moving it from where it is positioned..

-seltar
Re: Rotation and Translation
Reply #7 - Aug 13th, 2005, 8:39pm
 
hmmm, not so.  did you test it yourself?  i've been playing with this and i think i've come up with a solution - it's actually the other way around:
Code:

int f=-1;

void setup(){
rectMode(CENTER);
background(0);
framerate(1);
}

void draw(){
f++;
if (f > 0){popMatrix();}
switch (f){
case 1:
rect(0, 0, 10, 10);
break;

case 2:
translate(20, 20);
rect(0, 0, 10, 10);
break;

case 3:
translate(20,20);
rotate(radians(45));
rect(0, 0, 10, 10);
break;

case 4:
translate(20, 20);
rect(0, 0, 10, 10);
break;

default:
f = 0;
}
pushMatrix();

}

because it needs to store the matrix at the end (push to the next), and then if it's legal, pop off to restore at the beginning.  feels like a hack, but it looks like it will work.
Page Index Toggle Pages: 1