NKF
April 9, 2020, 6:17am
1
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);
}
glv
April 9, 2020, 6:31am
2
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
:)
NKF
April 9, 2020, 7:00am
3
glv, thanks a lot !! sorry for asking some simple questions cause I’m new to processing…
NKF
April 9, 2020, 7:10am
4
But I find that if I use these codes, I can’t add a background
quark
April 9, 2020, 8:44am
5
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(...)
NKF
April 9, 2020, 9:16am
6
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();
}
quark
April 9, 2020, 9:23am
7
Do you get an error message?
quark
April 9, 2020, 9:29am
8
Do you want the galaxy image to rotate endlessly?
quark
April 9, 2020, 9:33am
9
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);
}
glv
April 9, 2020, 10:23am
10
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:
Loads an image into a variable of type PImage. Four types of images ( .gif, .jpg, .tga, .png) images may be loaded. To load correctly, images must be located in the …
I borrowed the image from the reference, renamed it and dragged and dropped it on sketch and it added it to data folder.
:)
NKF
April 9, 2020, 10:32am
11
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
quark
April 9, 2020, 10:37am
12
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.