# Rotate an image when it's moving with "if"

**URL:** https://discourse.processing.org/t/rotate-an-image-when-its-moving-with-if/6633
**Category:** Coding Questions
**Created:** [December 15, 2018, 6:01pm UTC](https://discourse.processing.org/t/rotate-an-image-when-its-moving-with-if/6633 "2018-12-15T18:01:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Alexander](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Alexander](https://discourse.processing.org/u/Alexander)
#### Post date: [December 15, 2018, 6:01pm UTC](https://discourse.processing.org/t/rotate-an-image-when-its-moving-with-if/6633/1 "2018-12-15T18:01:34Z")

</div>

Hello! My name is Alex and i want to was a question. i’ ve been doing a race game the previous 2 days and i got to a serious problem. I have the menus, the track and a car, i made the car move.In the first place it’s horisontal with the track and then there is a turn, thant is 315 degrees. But how can i make it rotate to 315 degrees and continue moving, but with different texture of the car, like rotated to 315 degrees when i press “e”? This is my example:

```auto
if(keyPressed && key = 'e'){

               translate(0, 0);
               rotate(radians(315));
               image(car1, cX, cY);  
             
               cX = cX + 4;
               cY = cY - 4;

}

```

Thanks alot if you answer, beacuse it’s my only probleem with this project and i’ve tried to solve it the whole day!

---

<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: [December 15, 2018, 8:25pm UTC](https://discourse.processing.org/t/rotate-an-image-when-its-moving-with-if/6633/2 "2018-12-15T20:25:05Z")

</div>

You need to translate to the center of your object and then rotate it. You can do something like this:

```auto
           translate(cX,cY);
           rotate(radians(315));
           image(car1, 0,0);  
         
           cX = cX + 4;
           cY = cY - 4;

```

You might need to enclose this transformation within a push/pull matrix call to preserve your geometry state.  
If this does not answer your question, consider providing a minimum running snippet showing your current approach.

Kf

---

<div class="post-metadata">

### Author: ![Alexander](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Alexander](https://discourse.processing.org/u/Alexander)
#### Post date: [December 16, 2018, 9:41am UTC](https://discourse.processing.org/t/rotate-an-image-when-its-moving-with-if/6633/3 "2018-12-16T09:41:19Z")

</div>

Thaks alot, but when i press ‘e’ the car rotates, but the first car (the horisontal one) is still there. How can i fix this? Thanks again, you saved my game!

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [December 18, 2018, 5:25pm UTC](https://discourse.processing.org/t/rotate-an-image-when-its-moving-with-if/6633/4 "2018-12-18T17:25:43Z")

</div>

> [@Alexander](#):
>
> Thaks alot, but when i press ‘e’ the car rotates, but the first car (the horisontal one) is still there. How can i fix this? Thanks again, you saved my game!

You haven’t shared that code, or told us how it works. You have only shared a single if statement.

You might want to share your complete sketch here. Use the \</\> button to format the code after pasting it.

My _guess_ is that you don’t have a `background(192)` line at the top of your `draw()`, so all the old cars are still showing on the surface. Use `background` to wipe the sketch each frame, like an animation. Don’t use background to leave everything you draw behind, like drawing with pencil on paper (drawing more cars doesn’t remove the old cars).
