# Need help with rotating a shape

**URL:** <https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548>\
**Category:** Coding Questions\
**Created:** [October 16, 2018, 9:35pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548 "2018-10-16T21:35:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rebelcoder](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@rebelcoder](https://discourse.processing.org/u/rebelcoder)\
**Post date:** [October 16, 2018, 9:35pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/1 "2018-10-16T21:35:40Z")

</div>

Hi guys I am new to processing. I am trying to make a spaceship in an asteroid game. I have to make the spaceship using the 4 points using quad shape. The spaceship moves across the screen which is pretty simple by changing the y- coordinate. I need the ship to turn when a specific key is pressed to right and left. Can this be done with trigonometry? any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Eeyorelife](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Eeyorelife](https://discourse.processing.org/u/Eeyorelife)\
**Post date:** [October 16, 2018, 9:51pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/2 "2018-10-16T21:51:14Z")

</div>

If you write a minimal working sketch that serves as a starting point it is easier to give the help that you need. Because the code shows a little where you are and where you are headed.

But essentially you want to use the rotate() function which you can find on [processing.org/reference](http://processing.org/reference)

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 16, 2018, 9:55pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/3 "2018-10-16T21:55:09Z")

</div>

See here: [https://discourse.processing.org/t/how-do-i-draw-an-angular-space-with-a-quad](https://discourse.processing.org/t/how-do-i-draw-an-angular-space-with-a-quad)

---

<div class="post-metadata">

**Author:** ![Eeyorelife](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Eeyorelife](https://discourse.processing.org/u/Eeyorelife)\
**Post date:** [October 17, 2018, 10:30am UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/5 "2018-10-17T10:30:30Z")

</div>

Please format you code by highlighting it and pressing the `</>` button.

What you can do is you can use a boolean to mark if a button has been pressed. And then if another button is pressed, the boolean is set to false. Then you use the boolean to decide whether you should keep rotating or not. Here is a seudocode:

```auto
void keyPressed(){
  if(key is d or D){
    rotateToTheLeft is true;
  } else rotateToTheLeft is false;
if(key is a or A){
    rotateToTheRight is true;
  } else rotateToTheRight is false;
}

void draw(){
  if(rotateToTheLeft) rotate to the left;
  if(rotateToTheRight) rotate to the right;
}

```

---

<div class="post-metadata">

**Author:** ![Eeyorelife](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Eeyorelife](https://discourse.processing.org/u/Eeyorelife)\
**Post date:** [October 17, 2018, 10:38am UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/6 "2018-10-17T10:38:20Z")

</div>

I see I I might have misunderstood what you wanted. If you want the spacecraft to continue to rotate until the button is released then you can just set the boolean to false in `keyReleased()`. `keyPressed()` detects when a key is being pressed down and `keyReleased()` detects when a key is released. But if you use keyPressed() in draw like this: `if(keyPressed) {}` the function will be called every time draw is called and a button is being pressed. So you could do that as well.

---

<div class="post-metadata">

**Author:** ![rebelcoder](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@rebelcoder](https://discourse.processing.org/u/rebelcoder)\
**Post date:** [October 17, 2018, 4:02pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/7 "2018-10-17T16:02:55Z")

</div>

but how would I write the boolean in the code? how do I check this true. how do I put the is true and is false in my code

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 17, 2018, 5:27pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/8 "2018-10-17T17:27:19Z")

</div>

A boolean is just a variable that has only two values, `true` or `false`.

Here is a boolean:

```auto
boolean my_first_bool = true; // It's a true one!

```

You can use the value of a boolean directly in a conditional statement, because it already has a value of true or false:

```java
if( my_first_bool ) { // No need to add '== true'!
  println( "It's true!" );
}

```

---

<div class="post-metadata">

**Author:** ![rebelcoder](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@rebelcoder](https://discourse.processing.org/u/rebelcoder)\
**Post date:** [October 17, 2018, 6:20pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/9 "2018-10-17T18:20:45Z")

</div>

Can we rotate the shape without using the rotate function? because rotate changes the whole coordinate system. Later in the program I have to detect collisions. How do we use trig to rotate something

" When the user presses the D or L key on the keyboard, the ship should turn slowly to the  
right. When the user presses the A or J key on the keyboard, the ship should turn slowly  
to the left. The spaceship will always fly in the direction that it is pointing. When the user  
presses the S or K key on the keyboard, the ship should stop turning, and fly in a straight  
line. The user does not have to hold the key down. So after pressing D or L the spaceship  
should slowly fly in a perfect clockwise circle, until some other key is pressed. "

---

<div class="post-metadata">

**Author:** ![Eeyorelife](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Eeyorelife](https://discourse.processing.org/u/Eeyorelife)\
**Post date:** [October 17, 2018, 6:43pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/10 "2018-10-17T18:43:38Z")

</div>

You should use `pushMatrix()` and `popMatrix()`. Check them out in the reference.

---

<div class="post-metadata">

**Author:** ![rebelcoder](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@rebelcoder](https://discourse.processing.org/u/rebelcoder)\
**Post date:** [October 18, 2018, 9:35pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/11 "2018-10-18T21:35:27Z")

</div>

![Untitled](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/decf9e7e67763d475a7705dcd9f5dc0de6fafea4.jpeg)

Can anyone tell me how to make the ship face turned when angle is changed?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 18, 2018, 9:51pm UTC](https://discourse.processing.org/t/need-help-with-rotating-a-shape/4548/12 "2018-10-18T21:51:27Z")

</div>

Yeah, that should be easy enough to add to the code I already wrote.

```auto
void setup(){size(600,400);oooOO();stroke(255);ooo=oo();oooo=oo();O=O();OO=O()/2;OOO=
map(O(),100,200,PI/2,PI);}void draw(){OoOoO();background(o());line(o,oo,ooo,ooO);line(
ooo,ooO,Ooo,OoO);line(oOo,oOO,Ooo,OoO);line(oOo,oOO,o,oo);}int o(){OO();OOO();
return(0);}float o,oo,OoOoO,ooO,Ooo,OoO,oOo,oOO,O,OO,oooo,ooo,OOO;
float O(){return(random(100,200));}float oo(){return(random(
TWO_PI));}void oooOO(){o=width/2;oo=height/2;}void OoOoO(){OoOoO = map(mouseX,0,width,0,TWO_PI);}
void OOO(){Ooo=o+O*cos(oooo+OoOoO);OoO=oo+O*sin(oooo+OoOoO);ooO=oo+OO*sin(oooo+OOO+OoOoO);}
void OO(){ooo=o+OO*cos(oooo+OOO+OoOoO);oOo=o+OO*cos(oooo-OOO+OoOoO);oOO=oo+OO*sin(oooo-OOO+OoOoO);}

// Code by TfGuy44. DO NOT STEAL.

```

Simple.

… Or you could learn about `rotate()`. That’s much easier.
