处理:对象和数组之间的碰撞无法识别?

时间:2017-12-11 04:55:17

标签: processing

我试图找出为什么我的代码没有检测到火箭和障碍物/汽车/卡车之间的碰撞。

我使用相同的代码用于玩家汽车和障碍物并且它起作用,但是火箭不会像他们应该的那样与附加物体碰撞。

这是主要问题

//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;
    }
  }
}

我真的不确定问题是什么,我已经尝试了我能想到的一切

0 个答案:

没有答案