Rotate/flip a moving image at a certain point
in
Programming Questions
•
28 days ago
Hey,
I have made a sketch of a car moving up and down a road. The car starts at the top and when it reaches the bottom it moves to the other lane and drives upwards, and then down again in the other lane when it reaches the top..
What I'm trying to do now is that when the car, which is an image, reaches the bottom of the road the image should flip/rotate 180 degrees, and when it reaches the top it should then rotate back 180 degrees. I've played around with translate and rotate but have yet not found a way to do this. How can I manage this?
This is my code so far:
- float y;
- float x;
- int speed;
- PImage img;
- void setup(){
- size(800, 800);
- y = -150; // Y start position car. Car starts outside window
- x = 300; // X start pos car.
- speed = 4; // speed
- img = loadImage("cargo.png");
- }
- void draw(){
- println("Speed = "+speed);
- //println("xPos = "+x);
- //println("yPos = "+y);*/
- //road
- fill(78, 75, 75);
- rect(200, 0, 400, 800);
- //roadend
- // yellow line
- fill(254, 247, 0);
- rect(400, 20, 10, 50);
- rect(400, 90, 10, 50);
- rect(400, 160, 10, 50);
- rect(400, 230, 10, 50);
- rect(400, 300, 10, 50);
- rect(400, 370, 10, 50);
- rect(400, 440, 10, 50);
- rect(400, 515, 10, 50);
- rect(400, 585, 10, 50);
- rect(400, 655, 10, 50);
- rect(400, 725, 10, 50);
- //end yellow line
- //Speed y
- y = y + speed;
- /// Change lane and drive upwards
- if (y > height+160) {
- y=800;
- x= 500;
- speed = -speed;
- }
- // back to start pos
- if ((y <= -115) && (x == 500)) {
- y = -150;
- x = 300;
- speed = -speed;
- }
- drawCar();
- }
- void drawCar(){
- image(img, x, y);
- }
- void keyPressed() {
- //ACCELERATE
- if ( key == 'g' || key == 'G' ){
- // up
- if (speed <= -1){
- speed = speed -2;
- }
- //down
- if (speed >= 0) {
- speed = speed +2;
- }
- } //BREAK
- if ( key == 'b' || key == 'B' ) {
- //up
- if (speed <= 0) {
- speed = speed +2;
- }
- //down
- if (speed >= 0) {
- speed = speed -2;
- }
- }
- }
Thx in advance.
1