Rotate while key is pressed and stop in the last rotated position.

So i have this code that's is kinda working. My problem is when I release the key is that the square rotates a little backwards, is it a processing glitch or something about the resolution?

float theta = 0.01;
float rotated;
float ini = 0;

void setup() {
  size(500,500);
}

void draw() {
  background(255);
  fill(0);
  rectMode(CENTER);
  translate(width/2, height/2);
  if(keyPressed) {
    if(keyCode == RIGHT) {
      rotate(ini + theta);
      theta = theta + 0.05;
      rotated = theta;
    }
  }
  if(theta >= 2*PI) theta = 0;
  rotate(rotated);
  rect(0, 0, 50, 50);
  println(theta);
}

void keyReleased() {
  ini = theta;
  theta = 0;
}

Comments

  • here

    float theta = 0.01;
    
    void setup() {
      size(500, 500);
      fill(0);
      rectMode(CENTER);
    }
    
    void draw() {
      background(255);
    
      translate(width/2, height/2);
      rotate(theta);
      rect(0, 0, 50, 50);
      println(theta);
    }
    
    void keyPressed() {
      if (keyCode == RIGHT) {
        theta = theta + 0.05;
      }
      if (theta >= 2*PI) theta = 0;
    }
    
    void keyReleased() {
      //
    }
    
Sign In or Register to comment.