Rotate Object when reached Border
in
Programming Questions
•
9 months ago
Hi guys, really new to processing and have a question. I have this sort of pac-man head reversing itself as soon as it reaches the width of the screen but I would also like it to rotate 180 degrees for the mouth to face the correct direction. I've tried the push and popMatrix() but it seems to not rotate it correctly?
- boolean isOpening = true;
boolean isLooping = true;
boolean insideFrame = true;
float mouthAngle = 0;
float mouthChange = 0.025;
int c = 255;
int x = width/2;
int y = 250;
void setup() {
size (500, 500);
}
void mouseClicked() {
if (isLooping) {
noLoop();
isLooping = false;
}
else {
loop();
isLooping = true;
}
}
void draw() {
background(255); //to clean screen
fill (c);
if (isOpening) {
c -= 2;
}
else {
c += 2;
}
if (isOpening) {
mouthAngle += mouthChange;
}
else {
mouthAngle -= mouthChange;
}
if (mouthAngle >= 3.142/4 || mouthAngle <= 0) {
isOpening = !isOpening;
}
//opening & closing limits
//check if shape is within frame, if not then turn back
if (insideFrame) {
x = x+1;
}
else {
x = x-1;
}
pushMatrix();
if (!insideFrame) {
translate (x, y);
rotate (PI);
}
if (x <= 0 || x >= 500) {
insideFrame = !insideFrame;
}
arc(x, y, 100, 100, mouthAngle, TWO_PI-mouthAngle, PIE);
popMatrix();
}
1