We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! I am making a project that involves a lots of shapes and then animating some of them. Awhile i was trying to make this one square with a circle inside rotate, I ran into multiple problems. This are the objects that I am trying to rotate (that are, of course, in void draw() ):
fill(237, 26, 55);
rect(336, 605, 112, 121);
fill(255);
ellipse(392, 662, 63, 63);
So, firstly I tried using only rotate(PI/2) before that, but it just disappeared. Then I tried translate, in case that was what was missing, but once again nothing. I thought that maybe rectMode(CENTER) would finally make everything work, but it actually made everything move out of place! I tried putting everything between push and pop matrix but it didn't work. I don't understand what is happening or what I am doing wrong. And the thing is that I moved these objects to the end of the code so it wouldn't interfere with the rest. I searched everywhere but I cannot find anything to help me. Thank you for your time.
Answers
I'm not really sure what you're asking. Can you please post a MCVE instead of a disconnected snippet of code?
Hi! So this is the original (with bits of it deleted so there is not too much code on the screen):
then what i did was move what i wanted to the end and added rotate:
which made the rect disappear.
Then I tried to use translate before that..
And then I tried to use rectMode(CENTER) and push/popMatrix but the rect mode only made things worse and the push/pop didn't do anything
what I am trying to do is make it spin around it's center, but I can't find anything to help me with my problems.
Thank you, I hope this helps you understand my problem better!
Have a look Examples from Transformation.
And my sample code:
float x, y, z; PShape yellowBox, redBox, greenBox, whiteBox; void setup() { size(800, 600, P3D); coloredboxes(); } void draw() { background(0); lights(); camera(width/2, height/2, (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 0); float ZZ = PI/100*z++; translate(width/2, height/2, 0); // rotateX(PI/8+ZZ/10); // rotateY(PI/3+ZZ/25); // rotateZ(PI/3+ZZ/25); rotateX(radians(90-35.26)); // rotateY(-radians(30)); rotateZ(-radians(45)); // rotateY(-PI/2); drawAxes(); ellipseMode(RADIUS); // Set ellipseMode to RADIUS stroke(255, 0, 0); noFill(); ellipse(0, 0, sqrt(pow(150, 2)+pow(150, 2)), sqrt(pow(150, 2)+pow(150, 2))); // Draw white ellipse using RADIUS mode //shape(yellowBox); pushMatrix(); rotateZ(-ZZ/2); pushMatrix(); translate(150, 150, 0); // rotateX(-PI/8); // rotateY(PI/3); rotateZ(PI/4); rotateY(-ZZ*2); //drawAxes(); shape(redBox); popMatrix(); popMatrix(); } void coloredboxes() { yellowBox = createShape(BOX, 95, 95, 95); yellowBox.setStroke(color(255, 255, 0)); yellowBox.setFill(color(255, 255, 0, 64)); redBox = createShape(BOX, 50, 50, 50); redBox.setStroke(color(255, 0, 0)); redBox.setFill(color(255, 0, 0, 64)); greenBox = createShape(BOX, 105, 105, 105); greenBox.setStroke(color(0, 255, 0)); greenBox.setFill(color(0, 255, 0, 32)); whiteBox = createShape(BOX, 105, 105, 105); whiteBox.setStroke(color(255, 255, 255)); whiteBox.setFill(color(0, 0, 0, 0)); } void drawAxes() { stroke(255, 0, 0); fill(255, 0, 0); box(400, 2, 2); stroke(0, 255, 0); box(2, 400, 2); stroke(0, 0, 255); fill(0, 0, 255); box(2, 2, 400); fill(255, 255, 255); text("x", 210, 0, 0); text("-x", -210, 0, 0); text("-y", 0, -210, 0); text("+y", 0, 210, 0); text("-z", 0, 0, -210); text("+z", 0, 0, 210); }Thank you for answering. I tried to see if it could help, but I couldn't find a way to. I just wanted a simple (it doesn't seem that simple anymore :S) rotation motion. Is there any way to isolate rectMode(CENTER); to a single part of the code?
You wrote:
The answer is the usage of pushMatrix and popMatrix as demonstrated below.
Rotating the Correct Way
The correct way to rotate the square is to:
rectMode(CENTER);).see
https://www.processing.org/tutorials/transform2d/
Example
Here is an example.
Key elements:
usage of
rectMode(CENTER);translate() command before rotate() command
call
rect()with 0,0 as position (which is the origin of the coordinate system / Matrix)pushMatrix and popMatrix isolate their section against the rest of the code: Note that the blue box is not rotating
Here is the code:
Hello! Yes, it was exactly that that I wanted to do! I ended up not using rectMode and just adjusting the rect coordinates to the translate, but the rest helped me so much! Thank you very much!