# how to rotate around center of mass

I created a rectangle and rotated it. OF rotates the rectangle around its upper left corner. Is there something that would enable me to rotate the rectangle around its center?

1 Like

ofSetRectMode(int mode);

where mode can be either OF_RECTMODE_CENTER or OF_RECTMODE_CORNER

hope this helps

regards

david

to add on to what david wrote –

note that ofRotate works around the 0,0 position, so the typical operation if you wanted to draw a rectangle at 400,300 for example

``````
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(400,300);
ofRotateZ(ofGetElapsedTimef()*10);
ofRect(0,0,20,20);
ofPopMatrix();

``````

note that the basic idea is:

a) push a copy of the current world (coordinate system)
b) translating to where you want to rotate around
c) rotating
d) drawing (as if you are at 0,0)
e) pop to return back to the copy.

for (d), if you set rectMode to center, then draw with x,y = 0,0. If not, then draw with x,y=(-w/2), (-h/2), ie:

ofRect(-10,-10,20,20);

so that the object is centered at 0,0.

hope that helps!
zach

3 Likes