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 › Help with Rotating Zoog
Page Index Toggle Pages: 1
Help with Rotating Zoog (Read 2377 times)
Help with Rotating Zoog
Sep 25th, 2009, 8:41pm
 
Hello,

   I am new to processing and have a school project that I need some help on.  The part of the assignment I need help on is that we are supposed to rotate the alien, Zoog, presented in the textbook "Learning Processing."  I have used the rotate() function before but I cannot use that here.  There are a number of buttons at the bottom of the screen and if I use the rotate() function those too will be rotated.  I have tried using an equation to rotate Zoog that I will post below but that rotates him about the origin and seems to move Zoog in an upward spiral.  The ultimate goal is to have Zoog rotating in one spot both clockwise and counter-clockwise when certain events happen.  Any help will be appreciated.  Below is a sample of my program:

float xpos = 250;
float ypos = 250; //X and Y positions for Zoog
float theta = .05; //Angle of Zoog's rotation

void setup()
{
 size(500,500);
 frameRate(30);
 smooth();
}

void draw()
{
 background(255);
 rectMode(CENTER);
 ellipseMode(CENTER);
 
 //Draw "Left" button
 fill(0);
 rect(60,450,50,50);
 stroke(255);
 line(35,450,60,425);
 line(35,450,60,475);
 line(35,450,85,450);
 
 //Draw "Right" button
 rect(125,450,50,50);
 line(150,450,125,425);
 line(150,450,125,475);
 line(100,450,150,450);
 
 //Draw "Up" button
 rect(190,450,50,50);
 line(190,425,215,450);
 line(190,425,165,450);
 line(190,425,190,475);
 
 //Draw "Down" button
 rect(255,450,50,50);
 line(255,475,280,450);
 line(255,475,230,450);
 line(255,475,255,425);
 
 //Draw "Rotate" button
 rect(320,450,50,50);
 ellipse(320,450,30,30);
 line(305,450,290,438);
 line(305,450,315,445);
 
 //Zoog's Body
 stroke(0);
 fill(175);
 rect(xpos,ypos,20,100);
 
 //Zoog's head
 fill(ypos,0,xpos);
 ellipse(xpos,ypos-30,60,60);
 
 //Zoog's eyes
 fill(xpos,0,ypos);
 ellipse(xpos-19,ypos-30,16,32);
 ellipse(xpos+19,ypos-30,16,32);
 
 //Zoog's legs
 line(xpos-10,ypos+50,xpos-20,ypos+60);
 line(xpos+10,ypos+50,xpos+20,ypos+60);
 
 if(((keyPressed) && (key == 'a') || (key == 'A')) || (mousePressed && mouseX >= 35 && mouseX <= 85 && mouseY <= 475 && mouseY >= 425))
 {
   xpos--;
 }
 
 if(((keyPressed) && (key == 's') || (key == 'S')) || (mousePressed && mouseX >= 100 && mouseX <= 150 && mouseY <= 475 && mouseY >= 425))
 {
   xpos++;
 }
 if(((keyPressed) && (key == 'd') || (key == 'D')) || (mousePressed && mouseX >= 165 && mouseX <= 215 && mouseY <= 475 && mouseY >=425))
 {
   ypos--;
 }
 if(((keyPressed) && (key == 'f') || (key == 'F')) || mousePressed && mouseX >= 230 && mouseX <= 280 && mouseY <= 475 && mouseY >= 425)
 {
   ypos++;
 }
 if(((keyPressed) && (key == 'r') || (key == 'R')) || mousePressed && mouseX >= 295 && mouseX <= 340 && mouseY <= 475 && mouseY >= 425)
 {
   xpos = xpos*cos(theta) - ypos*sin(theta);
   ypos = xpos*sin(theta) + ypos*cos(theta);
 }
}
Re: Help with Rotating Zoog
Reply #1 - Sep 26th, 2009, 3:26am
 
i would stay with your first try and count up   theta++; when pressing R or the rotate button. but what you have think of is where to put your  rotate(radians(theta)); and anther thing to remember is that processing always rotates arround the origin which is (0,0) left upper corner. So what you have to do is a combination of translating rotating and translating your object again and it will work.

Give it a try, if if doesnt work, ask again Smiley

Re: Help with Rotating Zoog
Reply #2 - Sep 26th, 2009, 12:51pm
 
Thanks for the help.  I put in the rotate function again but this time I placed it before drawing Zoog. I think what was happening is that the rotation was being executed at the end of the draw loop and when it started again everything was drawn the way it had originally been.  I am still having one last issue, however.  When the rotate button is clicked or the 'r' key is pressed Zoog rotates but only so long as the button is being pressed.  As soon as the key is released Zoog goes back to his original orientation.  Any idea how to keep Zoog at the angle he ends at when the rotate button is not being pressed?  For example, if I rotate Zoog enough so he is at a 45 degree angle and then let go, how can I keep him at that angle and continue to translate his position around the window?  Again, thanks for helping me resolve my previous issue.  Below is my new code:

float xpos = 250;
float ypos = 250; //X and Y positions for Zoog
float theta = .05; //Angle of Zoog's rotation

void setup()
{
 size(500,500);
 frameRate(30);
 smooth();
}

void draw()
{
 background(255);
 rectMode(CENTER);
 ellipseMode(CENTER);
 
 //Draw "Left" button
 fill(0);
 rect(60,450,50,50);
 stroke(255);
 line(35,450,60,425);
 line(35,450,60,475);
 line(35,450,85,450);
 
 //Draw "Right" button
 rect(125,450,50,50);
 line(150,450,125,425);
 line(150,450,125,475);
 line(100,450,150,450);
 
 //Draw "Up" button
 rect(190,450,50,50);
 line(190,425,215,450);
 line(190,425,165,450);
 line(190,425,190,475);
 
 //Draw "Down" button
 rect(255,450,50,50);
 line(255,475,280,450);
 line(255,475,230,450);
 line(255,475,255,425);
 
 //Draw "Rotate" button
 rect(320,450,50,50);
 ellipse(320,450,30,30);
 line(305,450,290,438);
 line(305,450,315,445);
 
 translate(xpos,ypos);
 if(((keyPressed) && (key == 'r') || (key == 'R')) || mousePressed && mouseX >= 295 && mouseX <= 340 && mouseY <= 475 && mouseY >= 425)
 {
   rotate(theta);
   theta+=.02;
   //xpos = xpos*cos(theta) - ypos*sin(theta);
   //ypos = xpos*sin(theta) + ypos*cos(theta);
 }
 //Zoog's Body
 stroke(0);
 fill(175);
 rect(0,0,20,100);
 
 //Zoog's head
 fill(ypos,0,xpos);
 ellipse(0,0-30,60,60);
 
 //Zoog's eyes
 fill(xpos,0,ypos);
 ellipse(0-19,0-30,16,32);
 ellipse(0+19,0-30,16,32);
 
 //Zoog's legs
 line(0-10,0+50,0-20,0+60);
 line(0+10,0+50,0+20,0+60);
 
 if(((keyPressed) && (key == 'a') || (key == 'A')) || (mousePressed && mouseX >= 35 && mouseX <= 85 && mouseY <= 475 && mouseY >= 425))
 {
   xpos--;
 }
 
 if(((keyPressed) && (key == 's') || (key == 'S')) || (mousePressed && mouseX >= 100 && mouseX <= 150 && mouseY <= 475 && mouseY >= 425))
 {
   xpos++;
 }
 if(((keyPressed) && (key == 'd') || (key == 'D')) || (mousePressed && mouseX >= 165 && mouseX <= 215 && mouseY <= 475 && mouseY >=425))
 {
   ypos--;
 }
 if(((keyPressed) && (key == 'f') || (key == 'F')) || mousePressed && mouseX >= 230 && mouseX <= 280 && mouseY <= 475 && mouseY >= 425)
 {
   ypos++;
 }
}
Re: Help with Rotating Zoog
Reply #3 - Sep 26th, 2009, 1:09pm
 
Just put rotate(theta); out of the if ().
Re: Help with Rotating Zoog
Reply #4 - Sep 26th, 2009, 1:26pm
 
Haha! Pure genius! Thank you so much! I've come to realize from programming over the past couple of years that the most bothersome issue is usually the easiest to correct.  Thank you guys so much for helping me out. There is one last part to this assignment that I'm going to try, and if I need help I'll post the problem.  

The last part I have to do is to shrink and grow Zoog.  I've started playing with this already and I use a variable for the length and width of the rectangles and ellipses, then on a conditional I decrement the values by 1 and Zoog shrinks but to me it just looks funny.  I haven't quite finished the code so I won't post yet but if anyone has any ideas I'd love to read them.  Smiley
Re: Help with Rotating Zoog
Reply #5 - Sep 26th, 2009, 1:30pm
 
try http://processing.org/reference/scale_.html
Re: Help with Rotating Zoog
Reply #6 - Sep 28th, 2009, 10:34am
 
Thanks guys.  I really appreciate all the help.  Technically I've done everything required but I'd like to try to add a little more flare to my program.  I was thinking of either changing Zoog's eye color, head color, body color, or any combination of the three based on either his position or some other factor.  Basically I want the effect of a gradual change in color.  I've been playing with the random() function but it produces some wild results that slow down my program.  I'll post my "final" program below if anyone wants to take a look and give some input on how to have some of Zoog's parts change color.

float xpos = 275;
float ypos = 275; //X and Y positions for Zoog
float theta = .0; //Angle of Zoog's rotation
float s = 1; //Variable for the scale

void setup()
{
 size(550,550);
 frameRate(30);
 smooth();
}

void draw()
{
 background(255);
 rectMode(CENTER);
 ellipseMode(CENTER);
 
 //Draw "Left" button
 fill(0);
 rect(40,500,50,50);
 stroke(255);
 line(15,500,40,475);
 line(15,500,40,525);
 line(15,500,65,500);
 
 //Draw "Right" button
 rect(105,500,50,50);
 line(130,500,105,475);
 line(130,500,105,525);
 line(80,500,130,500);
 
 //Draw "Up" button
 rect(170,500,50,50);
 line(145,500,170,475);
 line(170,475,195,500);
 line(170,475,170,525);
 
 //Draw "Down" button
 rect(235,500,50,50);
 line(235,525,260,500);
 line(235,525,210,500);
 line(235,525,235,475);
 
 //Draw "Rotate CCW" button
 rect(300,500,50,50);
 ellipse(300,500,30,30);
 line(285,500,270,488);
 line(285,500,295,495);
 
 //Draw "Rotate CW" button
 rect(365,500,50,50);
 ellipse(365,500,30,30);
 line(340,500,325,512);
 line(340,512,350,500);
 line(350,500,365,510);
 
 //Draw "Shrink" button
 rect(430,500,50,50);
 rect(430,500,30,30);
 line(445,485,455,475);
 line(445,485,445,480);
 line(445,485,450,485);
 
 line(415,485,405,475);
 line(415,485,415,480);
 line(415,485,410,485);
 
 line(415,515,405,525);
 line(415,515,410,515);
 line(415,515,415,520);
 
 line(445,515,455,525);
 line(445,515,450,515);
 line(445,515,445,520);
 
 //Draw "Grow" button
 rect(495,500,50,50);
 rect(495,500,30,30);
 line(510,485,520,475);
 line(520,475,515,485);
 line(520,475,510,480);
 
 line(510,515,520,525);
 line(520,525,515,515);
 line(520,525,510,520);
 
 line(480,485,470,475);
 line(470,475,475,485);
 line(470,475,480,480);
 
 line(480,515,470,525);
 line(470,525,475,515);
 line(470,525,480,520);
 
 //Move origin to (xpos,ypos)
 translate(xpos,ypos);
 rotate(theta);
 scale(s);
 
 //Zoog's Body
 stroke(0);
 fill(0,0,255);
 rect(0,0,20,100);
 
 //Zoog's head
 fill(0,255,0);
 ellipse(0,0-30,60,60);
 
 //Zoog's eyes
 fill(255,0,0);
 ellipse(0-19,0-30,16,32);
 ellipse(0+19,0-30,16,32);
 
 //Zoog's legs
 line(0-10,0+50,0-20,0+60);
 line(0+10,0+50,0+20,0+60);
 
 //Move Zoog left if 'a' key is pressed or the "Left" button is clicked
 if(((keyPressed) && (key == 'a') || (key == 'A')) || (mousePressed && mouseX >= 15 && mouseX <= 65 && mouseY <= 525 && mouseY >= 475))
 {
   xpos--;
 }

 //Move Zoog right if 's' key is pressed or the "Right" button is clicked  
 if(((keyPressed) && (key == 's') || (key == 'S')) || (mousePressed && mouseX >= 80 && mouseX <= 130 && mouseY <= 525 && mouseY >= 475))
 {
   xpos++;
 }
 
 //Move Zoog up if 'd' key is pressed or the "Up" button is clicked
 if(((keyPressed) && (key == 'd') || (key == 'D')) || (mousePressed && mouseX >= 145 && mouseX <= 195 && mouseY <= 525 && mouseY >= 475))
 {
   ypos--;
 }
 
 //Move Zoog down if 'f' key is pressed or the "Down" button is clicked
 if(((keyPressed) && (key == 'f') || (key == 'F')) || mousePressed && mouseX >= 210 && mouseX <= 260 && mouseY <= 525 && mouseY >= 475)
 {
   ypos++;
 }
 
 //Rotate Zoog counter-clockwise if 'r' key is pressed or the "Rotate CCW" button is clicked
 if(((keyPressed) && (key == 'r') || (key == 'R')) || mousePressed && mouseX >= 275 && mouseX <= 325 && mouseY <= 525 && mouseY >= 475)
 {
   theta-=.02;
 }
 
 //Rotate Zoog clockwise if 'e' key is pressed or the "Rotate CW" button is clicked
 if(((keyPressed) && (key == 'e') || (key == 'E')) || mousePressed && mouseX >= 340 && mouseX <= 390 && mouseY <= 525 && mouseY >= 475)
 {
   theta += .02;
 }
 
 //Shrink Zoog if the 'q' key is pressed or the "Shrink" button is clicked
 if(((keyPressed) && (key == 'q') || (key == 'Q')) || mousePressed && mouseX >= 405 && mouseX <= 455 && mouseY <= 525 && mouseY >= 475)
 {
   s -= .01;
 }
 
 //Expand Zoog if the 'w' key is pressed or the "Grow" button is clicked
 if(((keyPressed) && (key == 'w') || (key == 'W')) || mousePressed && mouseX >= 470 && mouseX <=520 && mouseY <= 525 && mouseY >= 475)
 {
   s += .01;
 }
}
Re: Help with Rotating Zoog
Reply #7 - Sep 28th, 2009, 1:56pm
 
where you have fill(0, 0, 255); etc fix one of the values and change the others to xpos,  ypos or even mouseX and mouseY (although you'll need to clamp these to the range 0-255 ideally, see constrain() in the reference)
Re: Help with Rotating Zoog
Reply #8 - Sep 28th, 2009, 3:44pm
 
or use map() to convert it to another range, in this case 0-255
http://processing.org/reference/map_.html

and try to combine it with lerpColor() to not only count R G or B up, but to change between 2 colors. in this case map your values to something between 0-1
http://processing.org/reference/lerpColor_.html

Re: Help with Rotating Zoog
Reply #9 - Sep 29th, 2009, 2:14am
 
and think about using HSV colour rather than RGB - it'll let you change through the entire spectrum rather than just changing one channel.
Re: Help with Rotating Zoog
Reply #10 - Sep 29th, 2009, 3:05am
 
He means HSB, just a typo i guess. check out :
http://processing.org/reference/colorMode_.html

Re: Help with Rotating Zoog
Reply #11 - Sep 29th, 2009, 3:17am
 
HSV exists - it is Hue Saturation Value, where Value is used as an alternative name of Brightness.
But you are right to point to right term (in Processing...), of course! :-D
Page Index Toggle Pages: 1