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 & HelpSyntax Questions › weird rotate() problems - snippet inside
Page Index Toggle Pages: 1
weird rotate() problems - snippet inside (Read 1192 times)
weird rotate() problems - snippet inside
Sep 25th, 2005, 1:17am
 
I'm having some trouble rotating shapes (rects).

I can't seem to find the origin of rotation. When I rotate a shape a few different times (putting the rotation back each time), like 0, 15, 30, 45 -- the rectangles "slide" to the left (i.e, negative x) and the bottom (i.e., positive y).

Do I need to reposition my object at (0,0), rotate it there, and reposition it?

Or is there something else I should try?

<blockquote>
void bigRect(int x, int y, int lineSize, color col, float rot) {
 // x, y, of center. lineSize is 1, 2, 3. col is color. rot is rotation - degrees.
 int sizer = lineSize*lineSize*25;
 fill(col);
 if (rot == 0) //rotation is 0
   {
   rect(x, y, 300, 5);
 }
 else {
   rotate(radians(rot));
fill(black);
   rect(x, y, 300, 2);
   rotate(-radians(rot)); //reverse the rotation so that we can go back to 0,0 for remaining shapes
   fill(dkblue);
   rect(x, y, 300, 3);
</blockquote>
Re: weird rotate() problems - snippet inside
Reply #1 - Sep 25th, 2005, 2:32am
 
guess you're looking for something like this:

Code:

void bigRect(int x, int y, int lineSize, color col, float rot) {
// x, y, of center. lineSize is 1, 2, 3. col is color. rot is rotation - degrees.
int sizer = lineSize*lineSize*25;
fill(col);
if (rot == 0) //rotation is 0
{
rect(x, y, 300, 5);
}
else {
pushMatrix();
translate(x,y);
rotate(radians(rot));
translate(-x,-y);
fill(black);
rect(x, y, 300, 2);
rotate(-radians(rot)); //reverse the rotation so that we can go back to 0,0 for remaining shapes
fill(dkblue);
rect(x, y, 300, 3);
popMatrix();
}
}


-seltar
Re: weird rotate() problems - Solved! Thanks.
Reply #2 - Sep 25th, 2005, 6:31pm
 
Seltar's solution works like a charm. I knew there was somthing that I wasn't doing, but I couldn't figure out how to use pushMatrix( ) and popMatrix( ).

Thanks.
Re: weird rotate() problems - snippet inside
Reply #3 - Jul 1st, 2007, 12:05am
 
Hi,

I guess I'm having a similar problem with the rotate() function. I have this:


void setup()
{
 frameRate(30);
 size(1024, 400,OPENGL);
 smooth();
}
void draw()
{
 int num = 4;
 background(0);
 
 for(int i = 0; i < num; i++)
 {  
 noStroke();

 //171 and 163 are just random numbers
 curve(171, 163 ,0, width/2, height/2, 0, random(0,width/4), random(0,height/4), 0, 171, 163,0);

 }
}


I want to be able to rotate this, taking the centre point (width/2,height/2) as the origin. I read somewhere that I need to mark the first point of the curve to 0,0 (rather then (width/2,height/2) and then translate the whole thing to the centre point because the default origin for rotation in processing is 0,0. But i couldn't make it work and I can not understand what pushMatrix() and popMatrix() are meant to do.

any help would be great,

thanks,

e.

Re: weird rotate() problems - snippet inside
Reply #4 - Jul 4th, 2007, 6:33pm
 
pushMatrix() saves the current matrix to the stack
popMatrix() restores last stack entry

why use it? so we dont have accumulative transformations when we dont need them.. (they are also useful, yes)


//currently we are at the origin (0,0,0)
pushMatrix();
translate( 10, 10, 0 );
drawObject1();  // object is rendered at (10,10,0)
translate( 10, 0, 0 );
drawObject2();  // object is rendered at (20,10,0)
popMatrix();

drawObject3();  // object is rendered at (0,0,0)


in case you didnt saved the matrix before the translations and restored it later you would be rendering object 3 at position of object 2, (20,10,0)

i hope this helps you out.
Re: weird rotate() problems - snippet inside
Reply #5 - Jul 24th, 2007, 11:47am
 
Hell yeah!!

Thanks for that short but clear explanation!!!!

Cheers
Page Index Toggle Pages: 1