Need help with rotating a shape

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.

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

See here:

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:

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;

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.

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

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

Here is a boolean:

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:

if( my_first_bool ) { // No need to add '== true'!
  println( "It's true!" );
1 Like

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. "

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


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

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

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.


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