Rotation and Translation not performing as intended

Hi, I have a 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:

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.

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():

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);
image(arrow_texture_R, arrow_pos_x - 150, arrow_pos_y - 31, 150, 60);
popMatrix(); // Restore the saved origin and coordinate system.
1 Like

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: :stuck_out_tongue_winking_eye:

with pushMatrix():
    translate(arrow_pos_x, arrow_pos_y)
    ellipse(0, 0, arrow_texture_R.width, arrow_texture_R.width)
    image(arrow_texture_R, -arrow_texture_R.width >> 1, -arrow_texture_R.height >> 1)