We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to write a piece of code that rotates a group of 25 stars, and scales them randomly too.
Found good code for rotating PShapes, but scale doesn't seem to work for me. I always end up with scale somehow in the draw loop, which to my understanding means it is going to be run over and over (See results). I know this is a very basic question, but I'm still learning.
Thanks,
// A list of objects ArrayList polygons;
void setup() { size(640, 360, P3D);
// Make a PShape PShape star = createShape(); star.beginShape(); star.noStroke(); star.fill(0, 127); star.vertex(0, -50); star.vertex(14, -20); star.vertex(47, -15); star.vertex(23, 7); star.vertex(29, 40); star.vertex(0, 25); star.vertex(-29, 40); star.vertex(-23, 7); star.vertex(-47, -15); star.vertex(-14, -20); star.endShape(CLOSE);
// Make an ArrayList polygons = new ArrayList();
// Add a bunch of objects to the ArrayList // Pass in reference to the PShape // We coud make polygons with different PShapes for (int i = 0; i < 25; i++) { polygons.add(new Polygon(star)); } }
void draw() { background(255);
// Display and move them all for (Polygon poly : polygons) { scale(0.9); poly.display(); poly.move(); } } // A class to describe a Polygon (with a PShape)
class Polygon { // The PShape object PShape s; // The location where we will draw the shape float x, y; // Variable for simple motion float speed; float theta1 = 0;
Polygon(PShape s_) { x = random(width); // y = random(-500, -100); y = random(height); s = s_; // speed = random(2, 6); speed = 0; }
// Simple motion void move() { y += speed; if (y > height+100) { y = -100; } } //
// Draw the object void display() { pushMatrix(); translate(x, y); rotateZ(theta1); // scale(0.5); shape(s); theta1+=0.02; popMatrix(); } }
Answers
@tillemetry --
To isolate
scale()
and reset it after use, wrap it and whatever it affects with calls topushMatrix()
/popMatrix()
.For discussion of rotation after translation, see this recent thread:
P.S. Please edit your post with the gear icon and format your code with highlight , CTRL+o
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text