Trouble with Rotate

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.

    Screen Shot 2018-02-01 at 10.25.44 PM Screen Shot 2018-02-01 at 10.25.31 PM

  • edited February 2018
    • 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.

  • edited February 2018

    I thought this would have an effect on solving this issue:

    • theta belongs into the class

    It means each rectangle can store its own angle.

  • Answer ✓
    int x1, y1, x2, y2;
    
    Square leftSquare, rightSquare;
    
    
    int switcher = 0;
    
    void setup() {
    
      size(1800, 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 ( switcher%2 == 0) {
        rightSquare.display2();
        pushStyle();
        stroke(255, 69, 0);
        strokeWeight(10);
        leftSquare.display();
        popStyle();
      } else if ( switcher%2 == 1) {
        leftSquare.display();
        pushStyle();
        stroke(255, 69, 0);
        strokeWeight(10);
        rightSquare.display2();
        popStyle();
      } 
    
      rotateKey() ;
    }
    
    void keyPressed() {
      if (key == 'r') {
        //rotated = true;
      } else if (key == TAB) {
        //rotated = false;
        switcher++;
        switcher=switcher%2;
      }
    }
    
    void rotateKey() {
      if (key == 'a') {
        if (switcher%2 == 0) {
          leftSquare.theta +=  0.05;
        } else  if (switcher%2 == 1) {
          rightSquare.theta +=  0.05;
        }
      } 
      // ----------------------------
      else if (key == 'd') {
        if (switcher%2 == 0) {
          leftSquare.theta -=  0.05;
        } else  if (switcher%2 == 1) {
          rightSquare.theta -=  0.05;
        }
      }
    }
    
    // =========================================
    
    class Square {
    
      float x;
      float y;
    
      float w;
      float h;
    
      color c;
      color d;
    
      float theta = 0.01;
    
      // constructor
      Square(float xIn, float yIn) {
        x = xIn;
        y = yIn;
        w = 100;
        h = 100;
        c = color(255, 255, 255);
        d = color(69, 69, 69);
      } // constructor
    
      void display() {
        pushMatrix();
        fill(c);
        translate (x, y);
        rotate(theta);
        rect(-w/2, -h/2, w, h);
        popMatrix();
      }
    
      void display2() {
        pushMatrix();
        fill(d);
        translate (x, y);
        rotate(theta);
        rect(-w/2, -h/2, w, h);
        popMatrix();
      }
    }
    //
    
  • Wow that works perfectly! Thanks a bunch Chrisir

  • it's still far from perfect (syntactical suggestions)

  • edited February 2018

    The fix works perfectly. I’m not concerned with the syntax suggestions as it’s still a rough version.

Sign In or Register to comment.