# Rotating an object on command

**URL:** <https://discourse.processing.org/t/rotating-an-object-on-command/3896>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [September 27, 2018, 11:22am UTC](https://discourse.processing.org/t/rotating-an-object-on-command/3896 "2018-09-27T11:22:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ToivoUkkonen](https://avatars.discourse-cdn.com/v4/letter/t/8edcca/32.png) [@ToivoUkkonen](https://discourse.processing.org/u/ToivoUkkonen)\
**Post date:** [September 27, 2018, 11:22am UTC](https://discourse.processing.org/t/rotating-an-object-on-command/3896/1 "2018-09-27T11:22:13Z")

</div>

Hello, in school we are currently learning coding and for a while i’ve been trying to get an object to rotate, when I press certain keys. Here is the code I am working with as of right now:

```auto
int X;
int Y;
int SIVU;
int SIVU2;

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

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

```

So what i want is the object to rotate 90 degrees, to the left when I press A, to the right, when I press D and go back to the start position with W and S. Please help me i’ve tried everything I myself could.  
Thank you.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 27, 2018, 6:51pm UTC](https://discourse.processing.org/t/rotating-an-object-on-command/3896/2 "2018-09-27T18:51:27Z")

</div>

I don’t see you using the `rotate()` command anywhere in your code – just moving things left, right, up and down.

Have you looked at the rotate command?

- [http://processing.org/reference/rotate\_.html](http://processing.org/reference/rotate_.html)

Have you read the tutorial on 2D Transformations, which covers how to rotate things?

- [https://processing.org/tutorials/transform2d/](https://processing.org/tutorials/transform2d/)

**Edit** : …ah, I see, by “rotated” you mean swapping the width and height variables to create a 90-degree effect, rather than actually rotating.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 27, 2018, 7:25pm UTC](https://discourse.processing.org/t/rotating-an-object-on-command/3896/3 "2018-09-27T19:25:23Z")

</div>

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.

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![ToivoUkkonen](https://avatars.discourse-cdn.com/v4/letter/t/8edcca/32.png) [@ToivoUkkonen](https://discourse.processing.org/u/ToivoUkkonen)\
**Post date:** [October 3, 2018, 3:24pm UTC](https://discourse.processing.org/t/rotating-an-object-on-command/3896/4 "2018-10-03T15:24:38Z")

</div>

Thank you so much for looking into my code and helping, I got it to work now!
