# Rotation an object using processing

**URL:** https://discourse.processing.org/t/rotation-an-object-using-processing/2089
**Category:** Coding Questions
**Created:** [July 26, 2018, 1:40am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089 "2018-07-26T01:40:10Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![bill](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bill](https://discourse.processing.org/u/bill)
#### Post date: [July 26, 2018, 1:40am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/1 "2018-07-26T01:40:10Z")

</div>

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:

```auto
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;
  }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [July 26, 2018, 1:54am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/2 "2018-07-26T01:54:33Z")

</div>

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

```java
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;
  }
}

```

---

<div class="post-metadata">

### Author: ![bill](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bill](https://discourse.processing.org/u/bill)
#### Post date: [July 26, 2018, 2:02am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/3 "2018-07-26T02:02:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [July 26, 2018, 4:06am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/4 "2018-07-26T04:06:41Z")

</div>

Consider this code:

```auto
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!

---

<div class="post-metadata">

### Author: ![bill](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bill](https://discourse.processing.org/u/bill)
#### Post date: [July 28, 2018, 9:36am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/5 "2018-07-28T09:36:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [July 29, 2018, 4:53am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/6 "2018-07-29T04:53:17Z")

</div>

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](https://processing.org/tutorials/transform2d/) so you can understand what he means.

Kf

---

<div class="post-metadata">

### Author: ![bill](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bill](https://discourse.processing.org/u/bill)
#### Post date: [July 30, 2018, 1:45am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/7 "2018-07-30T01:45:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [July 30, 2018, 1:52am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/8 "2018-07-30T01:52:39Z")

</div>

Did the link help?

Kf

---

<div class="post-metadata">

### Author: ![bill](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bill](https://discourse.processing.org/u/bill)
#### Post date: [July 31, 2018, 1:14am UTC](https://discourse.processing.org/t/rotation-an-object-using-processing/2089/9 "2018-07-31T01:14:52Z")

</div>

yes. I appreciate your cooperative. Thanks Kf
