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

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:

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!

1 Like

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

           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

1 Like

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).