Help appending array and detecting collision?

edited December 2017 in Questions about Code

Hello, my current code is a racing game, the problem is detecting the collision with rockets. Currently there are problems

When an object is deleted, the main car also gets deleted

When an object and rocket collide, nothing happens

The rocket array doesn't append

The code doesn't run when I have a number in the rocket array

Can anyone please help, I've been trying to figure this out for a while now with no avail.

  Rocket(float tempx, float tempy, float tempspeed, float tempsizex, float tempsizey, float tempcolor) {
    x = tempx;
    y = tempy;
    sizeX = tempsizex;
    sizeY = tempsizey;
    speed = tempspeed;
    rocketColor = tempcolor;
  }
  //If launched is false, then the rocket will continue moving down the screen, however if it is true
  //then it will not
  void update() {
    if (!launched) {
    y += speed;
    }
  }
  //If powerup is false, and launched is true, then the rocket will fire by holding the mouse key
  void launchspeed() {
    if (!powerup) {
      if(launched) {
   y -= speed*2; 
      }
    }
  }

  void display() {
   fill(255);
    rectMode(CENTER);
    rect(x, y, sizeX, sizeY); 
    if (y >= height+50) {
         // y = -50;

    }
  }
  //If your car comes into contact with the rocket, it will pick it up
  //the boolean powerup becomes true, activating the new location for the rocket, it now follows the car
  void collected(Car car) {
    boolean leftP = (x + sizeX/2 > car.x - car.sizeX/2);
    boolean rightP = (x - sizeX/2 < car.x + car.sizeX/2);
    boolean topP = (y + sizeY/2 > car.y - car.sizeY/2);
    boolean bottomP = (y - sizeY/2 < car.y + car.sizeY/2);

    if (leftP && rightP &&topP && bottomP) {
     powerup = true;
    }
  }
  //Follows the car, however, like mentioned in the Car class, it follows the car but not its true Y location to avoid moving the car when shooting the rocket
  void follow(Car car) {
   if (powerup) {
      x = car.p;
      y = car.c;
   }
  }
  //When the boolean is true and the mouse is held, then powerup becomes false adn launched becomes true.
  //When powerup becomes false, the rocket shoots upwards and hits an obstacle
  //When launched becomes true, the original update loop doesnt run, but the launchspeed loop does, causing the rocket to fly upwards
  void shooting() {
   if (mousePressed && powerup) {
     powerup = false;
     launched = true;
   }
  }


  void hit(Obstacle obstacle) {

    boolean leftH = (x + sizeX/2 > obstacle.x - obstacle.sizeX/2);
    boolean rightH = (x - sizeX/2 < obstacle.x + obstacle.sizeX/2);
    boolean topH = (y + sizeY/2 > obstacle.y - obstacle.sizeY/2);
    boolean bottomH = (y - sizeY/2 < obstacle.y + obstacle.sizeY/2);

    if (leftH && rightH && topH && bottomH) {
      obstacle.sizeX = 0;
      obstacle.sizeY = 0;

    } 
  }

    void timerRocket() {
    //Converts milliseconds to actual seconds
    //int converts millis to integers, minus temp time 
    tC = intervalC+int(millis()/1000)-tempTimeC;

    ////nf formats the numbers into strings, so time = 00, it'll show the string time, and adds 2 zeros
    timeC = nf(tC, 2);
    ////if the seconds equal 6 + car add, then the array appends and another car appears onscreen
    //carAdd starts at 0, and when the first timer reaches 6, it adds another six, so when the timer reaches 12, it adds a car, and the variable carAdd goes to 18
    if (tC == 6 + rocketAdd) {
      //The new object being added to the array, spawns on a random lane
      Rocket o = new Rocket(50 + b*floor(random(0, 5)), -80, speed, 10, 20, color(255, 0, 0));
      rocket = (Rocket[]) append(rocket, o);
      //Timer that adds the cars every six seconds
      rocketAdd +=5;
    }
    }


}
Tagged:

Answers

  • Can you please edit your post to fix your formatting? While you're at it, can you narrow your problem down to a smaller example program instead of posting your full program?

  • Sorry I thought I fixed it, I edited some code out, but this is only two of the 7 classes I have.

  • That is a ton of code to ask us to debug for you. Please narrow the problem down to a smaller example.

  • No, what you've done is delete code that we needed to run it, that's more work for us.

  • post the longer version below please and tell us which line numbers we have to look at (this means, post the code, and post your comment, then write down the line numbers, then edit the post and write your line numbers and tell us where to look (or make a 2nd post with the line numbers from the first))

    thank you!

    Chrisir ;-)

  •   //If your car comes into contact with the rocket, it will pick it up
      //the boolean powerup becomes true, activating the new location for the rocket, it now follows the car
      void collected(Car car) {
        boolean leftP = (x + sizeX/2 > car.x - car.sizeX/2);
        boolean rightP = (x - sizeX/2 < car.x + car.sizeX/2);
        boolean topP = (y + sizeY/2 > car.y - car.sizeY/2);
        boolean bottomP = (y - sizeY/2 < car.y + car.sizeY/2);
    
        if (leftP && rightP &&topP && bottomP) {
         powerup = true;
        }
      }
      //Follows the car, however, like mentioned in the Car class, it follows the car but not its true Y location to avoid moving the car when shooting the rocket
      void follow(Car car) {
       if (powerup) {
          x = car.p;
          y = car.c;
       }
      }
      //When the boolean is true and the mouse is held, then powerup becomes false adn launched becomes true.
      //When powerup becomes false, the rocket shoots upwards and hits an obstacle
      //When launched becomes true, the original update loop doesnt run, but the launchspeed loop does, causing the rocket to fly upwards
      void shooting() {
       if (mousePressed && powerup) {
         powerup = false;
         launched = true;
       }
      }
    
    
      void hit(Obstacle obstacle) {
    
        boolean leftH = (x + sizeX/2 > obstacle.x - obstacle.sizeX/2);
        boolean rightH = (x - sizeX/2 < obstacle.x + obstacle.sizeX/2);
        boolean topH = (y + sizeY/2 > obstacle.y - obstacle.sizeY/2);
        boolean bottomH = (y - sizeY/2 < obstacle.y + obstacle.sizeY/2);
    
        if (leftH && rightH && topH && bottomH) {
         obstacle.alive = false;
          obstacle.sizeX = 0;
          obstacle.sizeY = 0;
             sizeX = 0;
             sizeY = 0;
             println("hit");
        } 
      }
      void hitTruck(Obstacle truck) {
    
        boolean leftH = (x + sizeX/2 > truck.x - truck.sizeX/2);
        boolean rightH = (x - sizeX/2 < truck.x + truck.sizeX/2);
        boolean topH = (y + sizeY/2 > truck.y - truck.sizeY/2);
        boolean bottomH = (y - sizeY/2 < truck.y + truck.sizeY/2);
    
        if (leftH && rightH && topH && bottomH) {
          truck.sizeX = 0;
          truck.sizeY = 0;
             sizeX = 0;
             sizeY = 0;
             println("hit");
        } 
      }
    

    The main part is void hit() On some objects it works and some it doesn't, despite being the same object.

  • this line:

      if (leftH && rightH && topH && bottomH) {
    

    the && means AND; but they can't all be true; what you want is || which means OR

  • Makes no difference in this case, and I tried || in other instances and had weird results. The obstacles which are being appended dont register as being hit, that is the problem

     void timer() {
        //Converts milliseconds to actual seconds
        //int converts millis to integers, minus temp time 
        t = interval+int(millis()/1000)-tempTime;
        //nf formats the numbers into strings, so time = 00, it'll show the string time, and adds 2 zeros
        time = nf(t, 2);
        //if the seconds equal 6 + car add, then the array appends and another car appears onscreen
        //carAdd starts at 0, and when the first timer reaches 6, it adds another six, so when the timer reaches 12, it adds a car, and the variable carAdd goes to 18
        if (t == 6 + carAdd) {
          //The new object being added to the array, spawns on a random lane
          Obstacle j = new Obstacle(cars, 50 + x*floor(random(0, 5)), -80, speed, sizeX, sizeY);
          obstacle = (Obstacle[]) append(obstacle, j);
          //Timer that adds the cars every six seconds
          carAdd +=6;
        }
    

    This is where I append the obstacles

  • edited December 2017

    Did you check whether the if clause ever gets true?

    to test this insert println("append"); after if (t == 6 + carAdd) {

    I'd use something with >= like

    if ( interval + millis() - tempTime >= 6000 + carAdd )  {
    

    (without understanding what you mean here)

  • Yes it appends, I had println the location too on my updated code. I use the same code for the car, which it works without problem.

     void accident(Obstacle obstacle) {
        boolean left = (x + sizeX/2 > obstacle.x - obstacle.sizeX/2);
        boolean right = (x - sizeX/2 < obstacle.x + obstacle.sizeX/2);
        boolean top = (y + sizeY/2 > obstacle.y - obstacle.sizeY/2);
        boolean bottom = (y - sizeY/2 < obstacle.y + obstacle.sizeY/2);
    //When the booleans are true, then the car disappears
        if (left && right &&top && bottom && obstacle.sizeX > 0) {
           sizeX = 0;
           sizeY = 0;
        }
     }
    

    This is in another class.

    What I found out though, is that when I call the function into another loop, an error occurs

          for (int i = 0; i < rocket.length; i++) {
            rocket[i].update();
            rocket[i].display(); 
            rocket[i].collected(car);
            rocket[i].shooting();
            rocket[i].launchspeed();
            rocket[i].follow(car);
            rocket[i].hit(obstacle[i]);
            rocket[i].hitTruck(truck[i]);
            //rocket[i].timerRocket();
          }
    

    rocket[i].hit(obstacle[i]); works fine here, but if I add it in

    my obstacle code, I get an error "arrayindexoutofboundsexception 1"

    which I find odd considering I have a similar instance in another piece of code.

    for (int i = 0; i < obstacle.length; i++) {
            obstacle[i].display();
            car.accident(obstacle[i]);
            obstacle[i].timer();
            obstacle[i].addToScreen();
            obstacle[i].update();
            obstacle[i].swerve(car);
            obstacle[i].difficulty();
    
    
          }
    
  • in the above code, error "arrayindexoutofboundsexception 1" would occur

    because of this line when there are more rockets than obstacles :

    rocket[i].hit(obstacle[i]);

    because you use i for both, and i being i = 0; i < rocket.length;

  • Answer ✓

    this is still valid:

    the && means AND; but they can't all be true; what you want is || which means OR

    Remark

    to check rockets against cars you need a nested for loop to check every rocket against every car:

    Or every obstacle against every car:

    for (int i = 0; i < obstacle.length; i++) {
        for (int i2 = 0; i2 < cars.length; i2++) {
    

    Remark

    To get real help either post your entire code OR an MCVE: https://stackoverflow.com/help/mcve

    I don't have time, I am sorry.

    Bye.

  • Here's the working code, problem still persists. Warning, its long

    //X location
    float x = 100;
    //Lanes size
    float sizex = 8;
    float sizey = 150;
    
    //Obstacles location
    float b =100;
    float y;
    float sizeX = 40;
    float sizeY = 80;
    
    float carX = 157;
    float carY = 450;
    float speed = 5;
    String time = ":00";
    int t;
    int interval = 00;
    //CarAdd adds time for when the next car is supposed to appear
    int carAdd = 0;
    int tempTime = 0;
    //Timer for the trucks
    String timeTruck = "00";
    int tTruck;
    int intervalTruck = 00;
    int truckAdd = 0;
    int tempTimeTruck = 0;
    
    String timeC = ":00";
    int tC;
    int intervalC = 00;
    int tempTimeC = 0;
    
    //Array of lanes, 16 are appearing on the screen
    
    //Obstacle Class
    Obstacle[] obstacle = new Obstacle[1];
    Obstacle[] truck = new Obstacle[1];
    Obstacle[] oil = new Obstacle[2];
    
    //Rocket Class
    Rocket[] rocket = new Rocket[1];
    
    //Car class
    Car car;
    
    
    void setup() {
      size(700, 800); 
    
      //All obstacles spawn in a ranom lane
      for (int i = 0; i < obstacle.length; i++) {
        obstacle[i] = new Obstacle(50 + x*floor(random(0, 5)), -80, speed, sizeX, sizeY);
      }
      for (int i = 0; i < truck.length; i++) {
        truck[i] = new Obstacle(50 + b*floor(random(0, 5)), -150, speed, 40, 120);
      }
      for (int i = 0; i < oil.length; i++) {
        oil[i] = new Obstacle(50 + b*floor(random(0, 5)), -50, speed, 30, 30);
      }
      for (int i = 0; i < rocket.length; i++) {
        rocket[i] = new Rocket(50 + x*floor(random(0, 5)), -50, speed, 10, 20, color(0, 0, 255));
      }
    
      //Cars starting location, starts in the second lane
      car = new Car(carX, carY, 40, 80);
    }
    
    
    void draw() {
      background(100); 
    
      for (int i = 0; i < obstacle.length; i++) {
        obstacle[i].display();
        car.accident(obstacle[i]);
        obstacle[i].timer();
        obstacle[i].addToScreen();
        obstacle[i].update();
        obstacle[i].difficulty();
      }
    
      for (int i = 0; i < truck.length; i++) {
        truck[i].display();
        truck[i].update();
        car.accident(truck[i]);
        truck[i].timerTruck();
        truck[i].addToScreen();
        truck[i].difficulty();
      }
      for (int i = 0; i < oil.length; i++) {
        oil[i].display();
        oil[i].update();
        oil[i].addToScreen();
        oil[i].timer();
        oil[i].difficulty();
        car.oilspill(oil[i]);
        car.oilreset(oil[i]);
      }
      for (int i = 0; i < rocket.length; i++) {
        rocket[i].update();
        rocket[i].display(); 
        rocket[i].collected(car);
        rocket[i].shooting();
        rocket[i].launchspeed();
        rocket[i].follow(car);
        //rocket[i].hit(obstacle[i], car);
        rocket[i].hitTruck(truck[i]);
        rocket[i].checkcollision(obstacle[i]);
      }
      car.display();
    
    }
    
    void keyPressed() {
      if (keyCode == LEFT) {
        car.switchLanesLeft();
      }
      if (keyCode == RIGHT) {
        car.switchLanesRight();
      }
    }
    
    
    class Car {
      //X and Y location, as well as sizes
      //The red car is your car
      //Slipped checks to see if you landed in an oil spill
      float x = 157;
      float y = 600;
      float sizeX = 40;
      float sizeY = 80;
      float c;
      float p;
      float grip = 2;
      boolean alive = true;
      boolean slipped = false;
    
      Car(float tempX, float tempY, float tempSizeX, float tempSizeY) {
        x = tempX;
        y = tempY;
        sizeX = tempSizeX;
        sizeY = tempSizeY;
      }
      void switchLanesLeft() {
        if (!slipped&& keyCode == LEFT ) {
          x-=100;
        }
        if (slipped) {
          x +=0;
        }
      }
      void switchLanesRight() {
        if  (!slipped && keyCode == RIGHT) {
    
          x+=100;
        } 
        if (slipped) {
          x +=0;
        }
      }
    
      void accident(Obstacle obstacle) {
        boolean left = (x + sizeX/2 > obstacle.x - obstacle.sizeX/2);
        boolean right = (x - sizeX/2 < obstacle.x + obstacle.sizeX/2);
        boolean top = (y + sizeY/2 > obstacle.y - obstacle.sizeY/2);
        boolean bottom = (y - sizeY/2 < obstacle.y + obstacle.sizeY/2);
        //When the booleans are true, then the car disappears
        if (left && right &&top && bottom && obstacle.sizeX > 0) {
          sizeX = 0;
          sizeY = 0;
        }
      }
      void accidentTruck (Obstacle truck) {
        boolean leftT = (x + sizeX/2 > truck.x - truck.sizeX/2);
        boolean rightT = (x - sizeX/2 < truck.x + truck.sizeX/2);
        boolean topT = (y + sizeY/2 > truck.y - truck.sizeY/2);
        boolean bottomT = (y - sizeY/2 < truck.y + truck.sizeY/2);
    
        if (leftT && rightT &&topT && bottomT && truck.sizeX > 0) {
          sizeX = 0;
          sizeY = 0;
        }
      }
      //Checks the same as above, if the car comes into contact with oil, then the boolean will turn true
      //What this obstacle does is stops you from changing lanes for a short time
      void oilspill(Obstacle oil) {
        boolean left = (x + sizeX/2 > oil.x - oil.sizeX/2);
        boolean right = (x - sizeX/2 < oil.x + oil.sizeX/2);
        boolean top = (y + sizeY/2 > oil.y - oil.sizeY/2);
        boolean bottom = (y - sizeY/2 < oil.y + oil.sizeY/2);
    
        if (left && right &&top && bottom) {
          slipped = true;
        }
      }
      //Once the oilspill is ofscreen, then slipped goes back to false, so that you can change lanes again
      void oilreset(Obstacle oil) {
        if (slipped && oil.y > height+100) {
          slipped = false;
        }
      }
      void display() {
        //Constrains the car the the screen so it doesnt go off when switching lanes
        x= constrain(x, 50, 450);
        //c = y is made so you can "trick" the rocket into thinking that it is following the car
        //When the rocket is used, it uses the X and Y location to launch from, however, when setting the speed of the rocket to launch, the car would
        //Also launch. By putting c = y, the car stays in place but the rocket still launches
        c = y;
        p = x;
    
        noStroke();
        fill(255, 0, 0);
        rectMode(CENTER);
        rect(x, y, sizeX, sizeY);
      }
    }
    
    
    class Rocket {
    
      float x = 100;
      float y;
      float sizeX =10;
      float sizeY = 20;
      float speed = 10;
      float rocketColor;
      float moment;
    
      float rocketAdd = 0;
    
      boolean powerup = false;
      boolean launched = false;
    
    
      Rocket(float tempx, float tempy, float tempspeed, float tempsizex, float tempsizey, float tempcolor) {
        x = tempx;
        y = tempy;
        sizeX = tempsizex;
        sizeY = tempsizey;
        speed = tempspeed;
        rocketColor = tempcolor;
      }
      //If launched is false, then the rocket will continue moving down the screen, however if it is true
      //then it will not
      void update() {
        if (!launched) {
        y += speed;
        }
      }
    
      void display() {
       fill(255);
        rectMode(CENTER);
        rect(x, y, sizeX, sizeY); 
        if (y >= height+50) {
             // y = -50;
    
        }
      }
      //If your car comes into contact with the rocket, it will pick it up
      //the boolean powerup becomes true, activating the new location for the rocket, it now follows the car
      void collected(Car car) {
        boolean leftP = (x + sizeX/2 > car.x - car.sizeX/2);
        boolean rightP = (x - sizeX/2 < car.x + car.sizeX/2);
        boolean topP = (y + sizeY/2 > car.y - car.sizeY/2);
        boolean bottomP = (y - sizeY/2 < car.y + car.sizeY/2);
    
        if (leftP && rightP &&topP && bottomP) {
         powerup = true;
        }
      }
    
        //If powerup is false, and launched is true, then the rocket will fire by holding the mouse key
      void launchspeed() {
        if (!powerup) {
          if(launched) {
       y -= speed*2; 
          }
        }
      }
    
      //Follows the car, however, like mentioned in the Car class, it follows the car but not its true Y location to avoid moving the car when shooting the rocket
      void follow(Car car) {
       if (powerup) {
          x = car.p;
          y = car.c;
       }
      }
      //When the boolean is true and the mouse is held, then powerup becomes false adn launched becomes true.
      //When powerup becomes false, the rocket shoots upwards and hits an obstacle
      //When launched becomes true, the original update loop doesnt run, but the launchspeed loop does, causing the rocket to fly upwards
      void shooting() {
       if (mousePressed && powerup) {
         powerup = false;
         launched = true;
       }
      }
    
    
      void hit(Obstacle obstacle) {
    
        boolean leftH = (x + sizeX/2 > obstacle.x - obstacle.sizeX/2);
        boolean rightH = (x - sizeX/2 < obstacle.x + obstacle.sizeX/2);
        boolean topH = (y + sizeY/2 > obstacle.y - obstacle.sizeY/2);
        boolean bottomH = (y - sizeY/2 < obstacle.y + obstacle.sizeY/2);
    
        if (leftH && rightH && topH && bottomH && sizeX > 0 && sizeY > 0) {
    
         obstacle.alive = false;
          obstacle.sizeX = 0;
          obstacle.sizeY = 0;
             sizeX = 0;
             sizeY = 0;
             println("hit");
        } 
      }
    
      void checkcollision(Obstacle obstacle) {
    
          for (int j = 0; j < rocket.length; j++) {
          if (this != rocket[j] && collision(obstacle)) {
            obstacle.alive = false;
          }
        }
      }
        boolean collision(Obstacle obstacle) {
        if (rocket == null) {
          return false;
        }
        //Checks if the obstacles have collided.
        boolean left = (x + sizeX/2 > obstacle.x - obstacle.sizeX/2);
        boolean right = (x - sizeX/2 < obstacle.x + obstacle.sizeX/2);
        boolean top = (y + sizeY/2 > obstacle.y - obstacle.sizeY/2);
        boolean bottom = (y - sizeY/2 < obstacle.y + obstacle.sizeY/2);
    
        return (left && right && top && bottom);
      }
    
    
      void hitTruck(Obstacle truck) {
    
        boolean leftH = (x + sizeX/2 > truck.x - truck.sizeX/2);
        boolean rightH = (x - sizeX/2 < truck.x + truck.sizeX/2);
        boolean topH = (y + sizeY/2 > truck.y - truck.sizeY/2);
        boolean bottomH = (y - sizeY/2 < truck.y + truck.sizeY/2);
    
        if (leftH && rightH && topH && bottomH && sizeX > 0 && sizeY > 0) {
          truck.sizeX = 0;
          truck.sizeY = 0;
             sizeX = 0;
             sizeY = 0;
             println("hit");
        } 
      }
    
        void timerRocket() {
        //Converts milliseconds to actual seconds
        //int converts millis to integers, minus temp time 
        tC = intervalC+int(millis()/1000)-tempTimeC;
        ////nf formats the numbers into strings, so time = 00, it'll show the string time, and adds 2 zeros
        timeC = nf(tC, 2);
        ////if the seconds equal 6 + car add, then the array appends and another car appears onscreen
        //carAdd starts at 0, and when the first timer reaches 6, it adds another six, so when the timer reaches 12, it adds a car, and the variable carAdd goes to 18
        if (tC == 6 + rocketAdd) {
          //The new object being added to the array, spawns on a random lane
          Rocket o = new Rocket(50 + x*floor(random(0, 5)), -80, speed, 10, 20, color(255, 0, 0));
          rocket = (Rocket[]) append(rocket, o);
          //Timer that adds the cars every six seconds
          rocketAdd +=5;
        }
        }
    
    
    }
    
    
    class Obstacle {
      //checked if the car are placed, starts as false
      boolean placed = false;
      boolean alive = true;
      //Location of the cars, when they go into the lanes
      float x=100;
      float y;
      //Car speeds
      float speed = 5; 
      float sizeX = 40;
      float sizeY = 80;
      //the moment the car is off the screen
      float moment;
      color carColor;
      float difficultAdd = 0;
    
    
      Obstacle(float tempX, float tempY, float tempS, float tempSX, float tempSY) {
        x = tempX;
        y = tempY;
        speed = tempS;
        sizeX = tempSX;
        sizeY = tempSY;
        moment = 0;
        placed = false;
        alive = true;
        addToScreen();
      }
    
      void update() {
        //If the moment the car is off the screen is less than the milliseconds, and if 
        //The car is not placed, then we add it to the screen
        //If not we update the speed
        if (moment < millis()) {
          if (!placed && alive) {
            addToScreen();
          } else {
            y += speed;
          }
        }
      }
    
      void timer() {
        //Converts milliseconds to actual seconds
        //int converts millis to integers, minus temp time 
        t = interval+int(millis()/1000)-tempTime;
        //nf formats the numbers into strings, so time = 00, it'll show the string time, and adds 2 zeros
        time = nf(t, 2);
        //if the seconds equal 6 + car add, then the array appends and another car appears onscreen
        //carAdd starts at 0, and when the first timer reaches 6, it adds another six, so when the timer reaches 12, it adds a car, and the variable carAdd goes to 18
        if (t == 6 + carAdd) {
          //The new object being added to the array, spawns on a random lane
          Obstacle j = new Obstacle( 50 + x*floor(random(0, 5)), -80, speed, sizeX, sizeY);
          obstacle = (Obstacle[]) append(obstacle, j);
          //Timer that adds the cars every six seconds
          carAdd +=6;
          println("added", x, y);
          println("append");
        }
    
        //Displays the timer
        text(time, 600, 125);
        //Timer font
      }
    
     void timerTruck() {
        //Timer for the truck spawning, since they are bigger, they spawn every 12 seconds
        tTruck = intervalTruck+int(millis()/1000)-tempTimeTruck;
        //nf formats the numbers into strings, so time = 00, it'll show the string time, and adds 2 zeros
        timeTruck = nf(tTruck, 2);
        //Same as the car timer, when the timer reaches 11, the array appends and adds 11 to the timer, so at 22 seconds another truck will appear, and again at 33 seconds
        if (tTruck == 11 + truckAdd) {
          //Appends the truck, like above
          Obstacle d = new Obstacle( 50 + b*floor(random(0, 5)), -150, speed, 40, 150);
          truck = (Obstacle[]) append(truck, d);
          truckAdd += 11;
        }
      }
    
     void display() {
        rectMode(CENTER);
        rect( x, y, sizeX, sizeY);
        //If the car or truck goes completely offscreen, then it resets 
        //placed turns to false
        if (y >= height+150) {
          placed = false;
          alive = true;
          addToScreen();
        } 
      }
    
    void addToScreen() {
        if (placed && !alive) {
          return;
        } 
    
        //moment equals a random time between 0 and 5,
        //Checks when the obstacles will respawn 
        //Respawn before the screen, so it will appear as they are flowing smoothly
        //Spawn between the five lanes
        moment = millis() + floor(random(0, 5000));
        y = -150;
        x = 50 + b*floor(random(0, 5));
        placed = true;
        alive = false;
        //checks if each new appended obstacle spawns into eachother
        //If the obstacles spawn overlapping eachother, then they will despawn
        //
        for (int j = 0; j < truck.length; j++) {
          if (this != truck[j] && collision(truck[j])) {
            placed = false;
          }
        }
        //Checks for each obstacle, if they have collieded with eachother or one another
        for (int j = 0; j < oil.length; j++) {
          if (this != oil[j] && collision(oil[j])) {
            placed = false;
          }
        }
        for (int j = 0; j < obstacle.length; j++) {
          if (this != obstacle[j] && collision(obstacle[j])) {
            placed = false;
          }
        }
    
      }
    
      //If the obstacle spawns in the same locaction as another obstacle, then it returns false and deletes
      boolean collision(Obstacle obstacle) {
        if (obstacle == null) {
          return false;
        }
        //Checks if the obstacles have collided.
        boolean left = (x + sizeX/2 > obstacle.x - obstacle.sizeX/2);
        boolean right = (x - sizeX/2 < obstacle.x + obstacle.sizeX/2);
        boolean top = (y + sizeY/2 > obstacle.y - obstacle.sizeY/2);
        boolean bottom = (y - sizeY/2 < obstacle.y + obstacle.sizeY/2);
    
        return (left && right && top && bottom);
      }
    
     void difficulty() {
        //Converts milliseconds to actual seconds
        //int converts millis to integers, minus temp time 
        t = interval+int(millis()/1000)-tempTime;
        //nf formats the numbers into strings, so time = 00, it'll show the string time, and adds 2 zeros
        time = nf(t, 2);
        //if the seconds equal 6 + car add, then the array appends and another car appears onscreen
        //carAdd starts at 0, and when the first timer reaches 6, it adds another six, so when the timer reaches 12, it adds a car, and the variable carAdd goes to 18
        if (t == 20+ difficultAdd) {
          speed +=2;
          difficultAdd += 20;
        }
      }
    }
    
Sign In or Register to comment.