Rotating an object on command

Hi ToivoUkkone,

In your case, you can simply change the way you draw your rectangle using a boolean variable rotated. You switch it to true when pressing ‘a’ or ‘d’ and to false when pressing ‘w’ or ‘s’.

Then in draw depending on the value of rotated you switch your SIVU and SIVU2 variables.

You can also change the rectMode to CENTER to make it more fluid.

int X;
int Y;
int SIVU;
int SIVU2;
boolean rotated;

void setup ()
{
  fullScreen ();
  colorMode (HSB, 100);
  frameRate (60);
  X = 0;
  Y = 0;
  SIVU = 40;
  SIVU2 = 70;
  rotated = false;
}

void draw ()
{
  rectMode(CORNER);
  background (#1CD332);
  fill (#A8AFA9);
  noStroke ();
  rect (100, 900, 700, 100);
  rect (100, 100, 100, 900);
  fill (#FF05E7);
  stroke (0);
  strokeWeight (1);
  rectMode(CENTER);
  if (rotated) {
    rect (X, Y, SIVU2, SIVU);
  } else {
    rect (X, Y, SIVU, SIVU2);
  }
} 
void keyPressed ()
{ 
  if (key == 'd')
  {  
    X = X + 5;
    rotated = true;
  }  
  if (key == 's')
  { 
    Y = Y + 5;;
    rotated = false;
  }  
  if (key == 'a')
  { 
    X = X - 5;
    rotated = true;
  }  
  if (key == 'w')
  { 
    Y = Y - 5;
    rotated = false;
  }
}
1 Like