Rotation an object using processing

Hi there,
I am new to processing. I have an idea to do a sketch of tower crane’s top view. so I have two objects to consider one is the jib rotating and the second is the trolley moving along the jib. I have did the part of rotating jib using arrow keys, however I stuck how to move the trolley along the jib… I would be thankful for your help here is my code below:

float xo;
float yo;
float x;
float y;
float angle=0;
float rh=0;

void setup(){
  size (800,600);
  xo= width/2;
  yo=height/2;
  smooth();
  noStroke();
}
void draw(){
  background(0);
  
 
 translate(xo, yo);
  rotate(angle);
  fill(255,255 ,255);
  rect (5, -20, 30,30);
  
  pushMatrix();
  fill (255,0,0);
  rect (0, 0, 300, 10);
  popMatrix();
}
void keyPressed(){
  if (key==CODED){
    if (keyCode== RIGHT){
      angle+= 1;
    }else if (keyCode== LEFT){
      angle-= 1;
      if (keyCode== UP){
      xo+= 1;
    }else if (keyCode== DOWN){
      yo-= 1;
    }
  
  if (key==' ')
  {
    angle = 0;
    rh = 0 ;
    xo = width/2;
    yo = height/2;
  }
    }
  }
}

You need to format your code. Edit your post, select your code and hit the </> button. In your code, notice the space character is not part of the coded keys, so it should be outside that if statement. Check the code below. Notice I added an else keyword that you missed.

Kf

void keyPressed() {
  if (key==CODED) {

    if (keyCode== RIGHT) {
      angle+= 1;
    } else if (keyCode== LEFT) {
      angle-= 1;
    } else if (keyCode== UP) {
      xo+= 1;
    } else if (keyCode== DOWN) {
      yo-= 1;
    }
  }

  if (key==' ')
  {
    angle = 0;
    rh = 0 ;
    xo = width/2;
    yo = height/2;
  }
}

thank you Kfrajer for your reply, I appreciate that,
I edited the post. what I meant that the jib rotate perfectly there is nothing wrong with it , but my question was regarding the trolley which is represented by white square , I need this trolley(white square) move along the jib when I hit the (up ,down) keys while the the jib (red line)does not move only rotate.
Cheers

Consider this code:

float xo;
float angle=0;

void setup() {
  size(800, 600);
  smooth();
  noStroke();
  reset();
}

void reset() {
  angle = 0;
  xo = 0;
}

void draw() {
  background(0);
  translate(width/2, height/2);
  rotate(angle);
  pushMatrix();
  translate(5, -20);
  fill(255, 255, 255);
  rect(xo, 0, 30, 30);
  popMatrix();
  fill (255, 0, 0);
  rect (0, 0, 300, 10);
}

void keyPressed() {
  if (key==CODED) {
    if (keyCode== RIGHT) {
      angle+= radians(1);
    } else if (keyCode== LEFT) {
      angle-= radians(1);
    } else if (keyCode== UP) {
      xo+= 1;
    } else if (keyCode== DOWN) {
      xo-= 1;
    }
  }
  if (key==' ') {
    reset();
  }
}

Notice the complete lack of a y0 variable!

thank you TfGuy44, I appreciate your cooperative. Indeed yes, I know that yo is variable, but I do not know how to translate it into a code. thanks again.
Cheers
Bilal

I believe Thomas provided you the answer. In his post, he mentioned that you do not need y0 but only x0 is enough. You need to have a look at the transformation tutorial so you can understand what he means.

Kf

1 Like

Thank you Kf… I looked at the link that you have provided.
Cheers

Did the link help?

Kf

yes. I appreciate your cooperative. Thanks Kf