How to rotate an image continuously?

I know how to rotate an image but I don’t know how to make it rotates continuously by itself
Could someone teach me pls

void setup()
{
  size(500,500, P3D);
  background(255);
}

void draw()
{
translate(width/2, height/2);
rotateZ(PI/3.0);
rect(-26, -26, 52, 52);
}

Hello,

One method:

float angle; //Angle in radians

void setup()
  {
  size(500,500, P3D);
  background(255);
  }

void draw()
  {
  translate(width/2, height/2);
  angle = angle + TAU/500;      //TAU rad = TWO_PI rad = 360 deg
  rotateZ(angle);
  rect(-26, -26, 52, 52);
  }

Resources:
https://processing.org/
https://processing.org/reference/TAU.html

:)

glv, thanks a lot !! sorry for asking some simple questions cause I’m new to processing…

But I find that if I use these codes, I can’t add a background

void draw()  {
  translate(width/2, height/2);
  pushMatrix();
  angle = angle + TAU/500;      //TAU rad = TWO_PI rad = 360 deg
  rotateZ(angle);
  rect(-26, -26, 52, 52);
  popMatrix();
}

You can use pushMatrix() and popMatrix() to isolate transformations. Your background() will be added before pushMatrix() either before or after the translate depending on your imageMode(...)

quark, sorry, I try it, put image() before and after translate but still can’t generate the background. What is my problem?

float angle; 
PImage bg;
void setup()
  {
  size(500,500,P3D);
  bg = loadImage("galaxy.png");
  }

void draw()
  {
  image(bg,0,0,width,height);
  translate(width/2, height/2);
 
  pushMatrix();
  angle = angle + PI/100;      
  rotateZ(angle);
  rect(-26, -26, 52, 52);
  popMatrix();
  }

Do you get an error message?

Do you want the galaxy image to rotate endlessly?

Try this

float angle; 
PImage bg;
void setup()
{
  size(500, 500, P3D);
  bg = loadImage("galaxy.png");
  imageMode(CENTER);
}

void draw()
{
  translate(width/2, height/2);

  pushMatrix();
  angle = angle + PI/100;      
  rotateZ(angle);
  image(bg, 0, 0, 1.42 * width, 1.42 * height);
  popMatrix();
  rect(-26, -26, 52, 52);
}

Hello,

Your posted code works:

float angle; 
PImage bg;
void setup()
  {
  size(500,500,P3D);
  bg = loadImage("galaxy.png");
  }

void draw()
  {
  image(bg,0,0,width,height);
  translate(width/2, height/2);
 
  pushMatrix();
  angle = angle + PI/100;      
  rotateZ(angle);
  rect(-26, -26, 52, 52);
  popMatrix();
  }

Please read the reference very carefully and it will become clear:

I borrowed the image from the reference, renamed it and dragged and dropped it on sketch and it added it to data folder.

:)

Oh I solve it now! It is my minor mistake, I write the wrong image type so it can’t show the image.
Thank you very much quark and glv

1 Like

Glad you got it to work.

I did ask if there was an error message and if you had told us that Processing couldn’t find the image file it would have saved us all a lot of effort. In future make sure you report any error messages. :smile:

okay, thanks a lot :grinning: :grinning: