Rotating object in a moving object

Basically I want the rotatingObject() to be with the movingObject() as it moves. Also I want that when you press the 's' key, the movingObject() "resets", it will appear at the top and move down again.

float xspeed = 5;
float yspeed= 3;
float x = 480; 
float y = -100;
float theta;
void setup () {
  background(255);
  size(960, 640);
}

void draw () {
  background(255);
  rotatingObject();
  movingObject();
}

void movingObject() {
  noFill();
  ellipse(x, y, 25, 25);
  ellipse(x, y, 100, 300);
  y=y+yspeed;
  if (y >= height/2) {
    yspeed =0;
    x = x + xspeed;
    if ((x > width - 105) || (x < 105)) {
      xspeed = xspeed * -1;
    }
  }
}
void rotatingObject() {
  noFill();
  translate(width/2, height/2);
  pushMatrix();
  rotate(theta);
  ellipse(0, 0, 25, 25);
  rectMode(CENTER);
  rect(0, 0, 100, 100);
  theta += 0.02;
  popMatrix();
  strokeWeight(1);
  translate(-480, -320);
}

Answers

  • edited April 2018
    void keyPressed () {
    
        if(key==‘s‘) 
              y=12;
    
    }
    
  • float xspeed = 5;
    float yspeed= 3;
    float x = 480; 
    float y = -100;
    float theta;
    
    void setup () {
      background(255);
      size(960, 640);
    }
    
    void draw () {
      background(255);
    
      rotatingObject();
      movingObject();
    }
    
    void movingObject() {
      noFill();
      ellipse(x, y, 25, 25);
      ellipse(x, y, 100, 300);
      y=y+yspeed;
      if (y >= height/2) {
        yspeed  =  0;
        x = x + xspeed;
        if (x > width - 105 || x < 105) {
          xspeed = xspeed * -1;
        }
      }
    }
    void rotatingObject() {
    
      pushMatrix();
    
      noFill();
    
      if (y<height/2) 
        translate(width/2, height/2);
      else
        translate(x, y);
      rotate(theta);
      ellipse(0, 0, 25, 25);
      rectMode(CENTER);
      rect(0, 0, 100, 100);
      theta += 0.02;
      strokeWeight(1);
    
      popMatrix();
    }
    
    void keyPressed () {
      if (key=='s') {
        //start all over
        x = 480; 
        y = 12;
        xspeed = 5;
        yspeed = 3;
      }
    }
    //
    
Sign In or Register to comment.