We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys, I'm working on having a couple features for two squares. The first is being able to switch between them using tab (which works) and being able to rotate the selected one. When rotating then stopping it with any key, the program should leave it at its current rotation and select the other square and be able to rotate that one. As of now the original one that was rotated is just switching its rotation property to the newly selected square. I really am at a loss after spending lots of time searching for answers and debugging, so any help would be greatly appreciated. Below is my program. Cheers!
int x1, y1, x2, y2;
Square leftSquare, rightSquare, holdSquare;
boolean rotated = false;
float theta = 0.01;
int switcher = 0;
void setup() {
size(800, 500); //original 1200, 800
x1 = 325;
y1 = 350;
x2 = 775;
y2 = 350;
leftSquare = new Square(x1, y1);
rightSquare = new Square(x2, y2);
}
void draw() {
background(0);
if (rotated == true && switcher%2 == 0) {
rightSquare.display2();
pushStyle();
stroke(255, 69, 0);
strokeWeight(10);
leftSquare.testRotateLeft();
leftSquare.display();
popStyle();
} else if (rotated == true && switcher%2 == 1) {
leftSquare.display();
pushStyle();
stroke(255, 69, 0);
strokeWeight(10);
rightSquare.testRotateRight();
rightSquare.display2();
popStyle();
} else if (switcher%2 == 0) {
pushStyle();
stroke(255, 69, 0);
strokeWeight(10);
leftSquare.display();
popStyle();
rightSquare.display2();
} else if (switcher%2 == 1) {
pushStyle();
stroke(255, 69, 0);
strokeWeight(10);
rightSquare.display2();
popStyle();
leftSquare.display();
}
}
void keyPressed() {
if (key == 'r') {
rotated = true;
}
if (key == TAB) {
//rotated = false;
switcher++;
leftSquare.x = x1;
leftSquare.y = y1;
rightSquare.x = x2;
rightSquare.y = y2;
}
}
void rotate() {
if (key == 'a') {
theta = theta + 0.05;
} else if (key == 'd') {
theta = theta - 0.05;
}
}
class Square {
float x;
float y;
float w;
float h;
color c;
color d;
void testRotateLeft() {
rotate();
translate(x1, y1);
rotate(theta);
x = 0;
y = 0;
}
void testRotateRight() {
rotate();
translate(x2, y2);
rotate(theta);
x = 0;
y = 0;
}
Square(float xIn, float yIn) {
x = xIn;
y = yIn;
w = 100;
h = 100;
c = color(255, 255, 255);
d = color(69, 69, 69);
}
void display() {
fill(c);
rect(x, y, w, h);
}
void display2() {
fill(d);
rect(x, y, w, h);
}
void setColor(float r, float g, float b) {
int red = (int)r;
int green = (int)g;
int blue = (int)b;
c = color(red, green, blue);
d = color(69, 69, 69);
}
}
Answers
Here's what it looks like when I rotate one of them then hit tab to switch to the other square.
line 1 belongs into the setup because it's only used there
theta belongs into the class
if you'd held your squares in an array, draw could be much shorter (use switcher as index for the array)
instead of having two display functions bring the color in the class like you did with x,y
Do you have any ideas for my issue, other than syntax stuff? I put the squares in an array but that doesn't fix it. The other syntactical suggestions don't really have an effect on solving this issue.
I thought this would have an effect on solving this issue:
It means each rectangle can store its own angle.
Wow that works perfectly! Thanks a bunch Chrisir
it's still far from perfect (syntactical suggestions)
The fix works perfectly. I’m not concerned with the syntax suggestions as it’s still a rough version.
;-)