# Rotation and Translation not performing as intended

**URL:** <https://discourse.processing.org/t/rotation-and-translation-not-performing-as-intended/1401>\
**Category:** Processing.py\
**Created:** [July 1, 2018, 7:24am UTC](https://discourse.processing.org/t/rotation-and-translation-not-performing-as-intended/1401 "2018-07-01T07:24:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ScottR](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@ScottR](https://discourse.processing.org/u/ScottR)\
**Post date:** [July 1, 2018, 7:24am UTC](https://discourse.processing.org/t/rotation-and-translation-not-performing-as-intended/1401/1 "2018-07-01T07:24:36Z")

</div>

Hi, I have a processing.py script that includes the simulation of some projectiles. I am trying to make an arrow rotate to follow its path as it flies, but I cannot seem to get the rotate and translate functions to only effect the rendering of the arrow. Here is the section of my code that _attempts_ to do this along with the behavior I intended:

```auto
ellipse(arrow_pos_x, arrow_pos_y, 10, 10)#Draws small circle at the tip of the arrow.
translate(arrow_pos_x, arrow_pos_y)#Sets origin to arrow tip.
angle = atan(arrow_vel_y/arrow_vel_x)#Calculates the angle that the arrow should be rotated. (vel = velocity)
rotate(angle)#Rotates the canvas to draw the arrow.
image(arrow_texture_R, arrow_pos_x - 150, arrow_pos_y - 31, 150, 60)#Draws the arrow (arrow is 200 pixels long and 60 pixels thick.
rotate(-1 * angle)#Rotates the canvas back to normal (further rending occurs after this process)
translate(-1 * arrow_pos_x, -1 * arrow_pos_x)#The origin is returned to the top left of the screen.

```

This code results in all of the other rendered objects to be rotated and moved to follow the path of the arrow.  
E.g. The ground becomes tilted and flies across the screen.  
Any help with my problem would be greatly appreciated, I feel as though I misunderstand how translate() and rotate() work, but the reference didn’t really clear it up for me.

---

<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 1, 2018, 8:50am UTC](https://discourse.processing.org/t/rotation-and-translation-not-performing-as-intended/1401/2 "2018-07-01T08:50:05Z")

</div>

I would not use rotate or translate to restore the coordinate system to what it was previously. Instead, I suggest you use `pushMatrix()` and `popMatrix()`:

```auto
ellipse(arrow_pos_x, arrow_pos_y, 10, 10);
pushMatrix(); // Save where the origin and coordinate system is now.
translate(arrow_pos_x, arrow_pos_y);
angle = atan(arrow_vel_y/arrow_vel_x);
rotate(angle);
image(arrow_texture_R, arrow_pos_x - 150, arrow_pos_y - 31, 150, 60);
popMatrix(); // Restore the saved origin and coordinate system.

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 1, 2018, 9:51am UTC](https://discourse.processing.org/t/rotation-and-translation-not-performing-as-intended/1401/3 "2018-07-01T09:51:09Z")

</div>

Adding to @TfGuy44, we can use `with` block statements w/ Python Mode for functions like **pushMatrix()**, **pushStyle()**, **beginShape()**, etc; so we don’t need their **popXXX()** or **endXXX()** counterparts: 😜

```auto
with pushMatrix():
    translate(arrow_pos_x, arrow_pos_y)
    ellipse(0, 0, arrow_texture_R.width, arrow_texture_R.width)
    rotate(arrow_vel_y/arrow_vel_x)
    image(arrow_texture_R, -arrow_texture_R.width >> 1, -arrow_texture_R.height >> 1)

```

> <https://github.com/jdf/processing.py/blob/master/mode/examples/Python%20Mode%20Differences/WithStatements/WithStatements.pyde>
