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 › I need help rotating triangle
Page Index Toggle Pages: 1
I need help rotating triangle (Read 1640 times)
I need help rotating triangle
Oct 12th, 2009, 6:03pm
 
I'm trying to make a triangle rotate left or right depending on the arrow keys pressed. I am having trouble making the triangle stay in that position ones it rotates. When I hit the arrow key again the triangle has to start rotating from its new position.

This is what I have so far:

 boolean up, down, left, right;
 Triangle t1;
 float rotAmt;
 
 void setup()
 {
   t1 = new Triangle();
   size(800,600);

 }
 
 void draw()
 {

   stroke(255);
   fill(173,255,47);
   
   t1.turn();
   t1.drawTriangle();
   
 
   
   

   
 }
   
   class Triangle{
    int x1, y1, x2, y2, x3, y3;
   
   Triangle(){
    x1 = 355;
    y1 = 300;
    x2 = 383;
    y2 = 245;
    x3 = 411;
    y3 = 300;
   }
 
     void drawTriangle()
     {
       triangle(x1,y1,x2,y2,x3,y3);

 
     }
   
   
   void turn(){
      if (left)
     {
     translate (383,272);
     rotate(-rotAmt);
     translate (-383,-272);      
     }
   
    if(right)
   {
     translate (388,272);
     rotate(rotAmt);
     translate (-388,-272);
   }
   }
 
     }
   void keyPressed(){
      switch(keyCode){
      case(LEFT):
      left = true;
      rotAmt += .02;
      break;
      case(RIGHT):
      right = true;
      rotAmt += .02;
      break;
  }
 }

    void keyReleased(){
      switch(keyCode){
      case(LEFT):
      left = false;
      rotAmt=0;
      break;
      case(RIGHT):
      right = false;
      rotAmt = 0;
      break;
}
}
 
Re: I need help rotating triangle
Reply #1 - Oct 12th, 2009, 7:36pm
 
Some hints:
  • The rotation applied by calling rotate() is reset for every frame
  • rotAmt=0; in keyReleased() is changing the rotation angle back to zero, so when you release a (left/right) key the triangle will be back in it's starting position
  • "right" and "left" (or clockwise and anticlockwise) can be thought of as "positive" and "negative", or rather increasing and decreasing the rotation angle (which is allowed to go negative). You do not need to remember "left" or "right" as well as the rotation angle, just the rotation angle is enough (that is, don't try to rotate by rotAmt or -rotAmt depending on the "direction")
  • Unfortunately, keyPressed() is probably happening more times than you expect: if you hold a key down, it will (probably) fire a succession of keyPressed() events, not just one for the entire time you hold the key, and this will be dependent on things such as your key repeat speed (as set by/for your Operating System)
  • Bonus hint: if there are many keyPressed() events from holding down a key, you will not need a keyReleased() method, and the keyPressed() method is the only place that needs to know about "left" or "right"; all you need to do is think how rotAmt needs to be adjusted when the left or right arrow is pressed (and/or repeated by holding down the key)
Re: I need help rotating triangle
Reply #2 - Oct 13th, 2009, 1:22am
 
The best approach to dealing with keypresses is to use a boolean and switch it on keypress/keyrelease.  This avoids any issues with repeating keys, is more responsive and makes it easy to deal with multiple key presses.  For an example see this post.
Re: I need help rotating triangle
Reply #3 - Nov 11th, 2009, 10:20am
 
When the key repeat happens, it triggers keyReleased() as well.

If you find you still need a keyrelease, just put keyReleased() before keyPressed() in the script. I found i needed one because i couldn't just reset the variable without knowing which key's variable to reset. (my project uses most of the keys).
Page Index Toggle Pages: 1