Rotating Squares on the grid

Hey guys, I have created a 2d grid of squares 5x5

I am trying to rotate the squares individually, the code i have so far is
ofSeedRandom(0);

ofSetRectMode(OF_RECTMODE_CENTER);

for(int i = 0; i < 5; i++){

for(int j=0; j<5; j++){

float r =(i+j+ofGetElapsedTimef())*60;

    glPushMatrix();
    glTranslatef(ofGetWindowWidth()/3,ofGetWindowHeight()/3,0);
    glRotatef(r/10,1,1,1);
    ofSetColor(colors[i].r, colors[i].g, colors[i].b);
    ofRect(i*80, j*80, 60,60);
    glPopMatrix();

this rotates the whole grid not the squares individually, any help would be apperciated.

thanks!

heyho,
you translate your scene always to the same point, hence the anchor for all rotate operations is the same.

void ofApp::draw()
{
    drawSquare(ofGetWidth() / 2 - 100, ofGetHeight() / 2, 50, 0, ofColor::pink);
    drawSquare(ofGetWidth() / 2, ofGetHeight() / 2, 50, 27.5, ofColor::pink);
    drawSquare(ofGetWidth() / 2 + 100, ofGetHeight() / 2, 50, 45, ofColor::pink);
}
void ofApp::drawSquare(int x, int y, int size, float rotation, ofColor color)
{
    ofSetRectMode(OF_RECTMODE_CENTER); // smarter to do that once in setup
    ofPushMatrix();
    ofTranslate(x, y);
    ofRotateZ(rotation);
    ofSetColor(color);
    ofDrawRectangle(0, 0, size, size);
    ofPopMatrix();
}

hope that helps
thomas

1 Like

Thank you this was really helpful! I have one more question if i want to make a for loop would i do that in draw or the drawsquare function.

your loop should be in the draw function. inside the loop you would call drawSquare to draw a single square.