Rotate multiple elements on it's center - celullar automata

Hi! i have something similar to an celullar automata, multiple shapes with separation on x and y coordinate. The generation of the shape of each object is random (3 possible shapes) and i want them to be random rotated (this will be static, will not change).

This is the draw method where I create the shapes, if I translate and rotate the elements I get some weird results and also I lost the x and y position given in the constructor, wich i use on a distributor class to get access to each object by a mouse click.

void draw() {

int w = 30;
pushMatrix();

if (emerger) {
  fill(col);
} else {

  fill(color);
}
rectMode(CENTER);

translate(x, y);
rotate(radians(90));
if (type== 1) { 
  rect(x, y, w*escala, w*scale);
} else if (type== 2) {
  rect(x, y, w+10*scale, w*scale);
} else if (type== 3) {
  rect(x, y, w+10*scale, w*scale);
  rect(x, y, w-20*scale, w-20*scale);
}
popMatrix();

}

How can i create a rotated(0,90,180,270) shape without changing its position?

Pics:

Original not rotated http://prntscr.com/6zbe43 Rotated but losing coordinates http://prntscr.com/6zbez1

Thanks!

Tagged:

Answers

  • When you apply translate and rotate, you usually draw at 0, 0. Drawing at x, y is duplicate of translate(x, y).

    To rotate on a point (usually the center), you translate this point to 0, 0 (ie. translate(-hw, -hh); half-width, half-height for the center), do the rotation, then translate the coordinates to x, y.

  • Hi PhiLho i don't get it yet, what the half-w and half-h in this case? i have to do something like this? This obviously doesnt work

    //w and h are the particle size

    pushMatrix(); translate(x-w, y-h); rotate(radians(90)); translate(x,y) if (type== 1) { rect(x, y, wescala, wscale); } else if (type== 2) { rect(x, y, w+10scale, wscale); } else if (type== 3) { rect(x, y, w+10scale, wscale); rect(x, y, w-20scale, w-20scale); } popMatrix();

  • Answer ✓

    Instead of drawing a rectangle with rect(x,y,w,h), do:

    pushMatrix();
    translate(x+(w/2),y+(h/2));
    // rotate() here if you want it to rotate by some amount.
    rect(-w/2,-h/2,w,h);
    popMatrix();
    
  • TfGuy44 thanks for your answer, it worked! PhiLho thank you too for your help. Have a good day!!

Sign In or Register to comment.