Rotating an object around it’s center point.
float angle1 = PI/4;
float angle2 = PI/4;
void setup(){
size(200,200);
rectMode(CENTER);
}
void draw(){
pushMatrix();
translate(50,50);
rotate(angle1);
rect(0,0,30,30);
popMatrix();
angle1=angle1+PI/180;
pushMatrix();
translate(100,100);
rotate(angle2);
rect(0,0,30,30);
popMatrix();
angle2=map(second(),0,60,0,PI*2);
}
TIME AND ROTATION: A TRADITIONAL CLOCK
void setup() {
size(100, 100);
stroke(255);
}
void draw() {
background(0);
fill(80);
noStroke();
// Angles for sin() and cos() start at 3 o'clock;
// subtract HALF_PI to make them start at the top
// processing things 0 radians is at the 3pm mark
ellipse(50, 50, 80, 80);
float s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
float m = map(minute(), 0, 60, 0, TWO_PI) - HALF_PI;
float h = map(hour() % 12, 0, 12, 0, TWO_PI) - HALF_PI;
stroke(255);
line(50, 50, cos(s) * 38 + 50, sin(s) * 38 + 50);
line(50, 50, cos(m) * 30 + 50, sin(m) * 30 + 50);
line(50, 50, cos(h) * 25 + 50, sin(h) * 25 + 50);
}
ROTATION EXERCISES
1) Create a sketch with a shape or images spinning on its centre.
2) Using pushMatrix() and popMatrix() create a sketch with three spinning shapes or images in it. Shapes should be spinning from their center. Can you make them all spin at different speeds?
HINT: You will need a different variable for the angle of rotation for each shape and will then need to change these by different amounts.
3) Can you make one rotate once per minute, once per hour and once per day? HINT: You will need to increase the angle of the rotation using map(). angle=map(second(),0,60,0,PI*2);
(One way of doing this is to call pushMatrix to contain the translation, translate the origin to the center the shape, call rotate with a changing angle to create the spin, then return the origin back to it’s usual position after rotate using popMatrix, before doing the same for the next shape.)