简体   繁体   English

如何修复我的播放器和其他对象之间的崩溃方法/碰撞检测?

[英]How can I fix my crash method/collision detection between my player and other objects?

Currently, I'm making a 2d java game that includes a tank at the top of the screen shooting the oncoming cars from below, I have made a crash method and collision detection which determines to stop the game when the tank crashes or enters the radius of the cars.目前,我正在制作一个 2d java 游戏,其中包括屏幕顶部的坦克从下方拍摄迎面而来的汽车,我制作了碰撞方法和碰撞检测,确定当坦克碰撞或进入半径时停止游戏的汽车。 However, sometimes it works early, sometimes late and other times it doesn't.但是,有时它会提前工作,有时会延迟,有时则不会。 My question is how can I fix it so that when the tank enters the radius of the car it stops the game with simple code that excludes vectors.我的问题是如何修复它,以便当坦克进入汽车半径时,它会使用不包括向量的简单代码停止游戏。

Below are my classes and code.以下是我的课程和代码。 I'm using Java in Processing.我在处理中使用 Java。

PImage bg;                 //loads bakground
PFont f;                   //loads font

Car[] cars = new Car[3];
//Bullet[] bullets = new Bullet[100];

int x = 0;
int y = 0; 
int game = 0; 
int running = 0;
int over = 1;
int score = 0;
int move = 20;
int cX, cY;
//int carRadius = 20;

Tank tank;

void setup()
{
  size(500,1000);
  textSize(40);
  bg = loadImage("bg.jpeg");             //loads background
  bg.resize(width,height);               //the background will fill the height and width of the screen size
  tank = new Tank(tankX, tankY, 3, 2);      //X pos, Y pos, speedY
  for (int i=0; i<cars.length; i++)
  {
    int cX = (int)random(width-100);      //car xpos
    int cY = (int)random(900);            //car ypos
    int speedY = 3;                       //car speedY
    cars[i] = new Car(cX, cY, speedY);
  }
      
  cars[0] = new Car((int)random(5, width-100), (int)random(5, height), 2); //X pos, Y pos, speedY
  cars[1] = new Car((int)random(5, width-100), (int)random(5, height), 2); //X pos, Y pos, speedY
  cars[2] = new Car((int)random(5, width-100), (int)random(5, height), 2); //X pos, Y pos, speedY
  
  f = createFont("Arial", 36, true);
}

void draw()
{
  if (game == running)
  {
    drawBackground();       //background
    for (Car c : cars) {
      c.draw();
      c.move();
    }
    tank.draw();            //tank
    drawScore();            //draw score

    //if (bullet.crash(cars[0]) == true) {
    //  cars.remove(c);
    //  score++;
    //}
      
    if (game == over)
    {
      tank.speedX = 0;
      tank.speedY = 0;
      move = 0;
      gameOver();
    }
    
    //if tank crashes into cars
    if(tank.crash(cars[0]))
    {
      game = over;
      gameOver();
    }
    if(tank.crash(cars[1]))
    {
      game = over;
      gameOver();
    }
    if(tank.crash(cars[2]))
    {
      game = over;
      gameOver();
    }
    /*

    if(bullet.shoot(cars[0]))
    {
      cars[0].remove(c);
      score++;
    }
    if(bullet.shoot(cars[1]))
    {
      cars[1].remove(c);
      score++;
    }
    if(bullet.shoot(cars[2]))
    {
      cars[2].remove(c);
      score++;
    }
    */
  }
}

void keyReleased() {
  tankXD = 0;
  tankYD = 0;
}

void keyPressed() //controls for the tank using the arrow keys
{
  if(keyCode == LEFT) { 
    tankXD =- 10;
  }
  if(keyCode == RIGHT) {
    tankXD = 10; 
  }
  if(keyCode == DOWN) { 
    tankYD = 10; 
  }
  if(keyCode == UP) { 
    tankYD =- 10; 
  }
  if(keyCode == ' ') {
    bulletSPD = 30;
    //bullets.add(new Bullet(tank.x+30, tank.y+140, 3));
}
}

//void carFill()
// {
//   fill(255,0,0);
// }
 
void drawBackground()
{
  image(bg, y, 0);
}

void drawScore() {
  fill(255);
  textFont(f);
  text("Score: " + String.valueOf(score), 200, 50);
}

void gameOver() {
  clear();
  textFont(f);
  text("Game Over! ", 150, 400);
}
class Car 
{
  //members
   int cX, cY;
   int speedY = 2;
   int speedX = 0;
   int animationCounter = 0;
   int carRadius = 30;
   PImage image1,image2,image3;
   
   //constructor
   Car(int cX, int cY, int speedY)
   {
     this.cX = cX;
     this.cY = cY;
     this.speedY = speedY;
     image1 = loadImage("c1.png");
     image2 = loadImage("c2.png");
     image3 = loadImage("c3.png");
   }
   
   void update() {
     draw();
     move();
   }
   
   void move()
     {
    this.cY = this.cY - speedY; //move upwards
    
    if(this.cY < 0 - image1.height)
    this.cY = height + image1.height;
       
    if(this.cY > height + image1.height +30)
    this.cY = -image1.height;
  }
  
   void draw()
   {
     if (animationCounter >=0  &  animationCounter <=8) 
   { image(image1,this.cX,this.cY); }
   else if (animationCounter >8  &  animationCounter <=16) 
 { image(image2,this.cX,this.cY); }
 else
 { image(image3,this.cX,this.cY); }
 
 animationCounter = animationCounter + 1;
 if(animationCounter>20)
 animationCounter = 0; 
 }

}
int tankX = 215;           //tank xpos
int tankY = 60;            //tank ypos
int tankXD = 0;            //tank x dir
int tankYD =  0;           //tank y dir
int bulletX = tankX;       //bullet xpos
int bulletY = tankY;       //bullet ypos
int bulletW = 8;       //bullet xpos
int bulletH = 20;       //bullet ypos
int bulletSPD = 0;         //bullet speed
int bulletRadius = 4;
float bulletDistance = 5;
PImage image1;

class Tank
{
  //members
  int tankX;
  int tankY;
  int speedX;
  int speedY;
  
  //constructor
  Tank (int tankX, int tankY, int speedX, int speedY)
  {
    this.tankX = tankX;
    this.tankY = tankY;
    this.speedX = speedX;
    this.speedY = speedY;
    image1 = loadImage("tank.png");
  }
  
  void draw() {
    image(image1,tankX, tankY);
    
    tankX+=tankXD;
    tankY+=tankYD;
    bulletX=tankX;
    bulletY+=bulletSPD;
    if(bulletY>800) {
      bulletX=tankX;
      bulletY=tankY;
      bulletSPD=0;
    }
    //draw bullet
    fill(255,0,0);
    stroke(255,0,0);
    rect(bulletX+30, bulletY+140, bulletW, bulletH);

  }

//tank crash method
boolean crash(Car other)
{
  return (abs(this.tankY-other.cY) <20) && abs(this.tankX-other.cX) <10;
}

/*
boolean shoot (Bullet b, Car c) {
  float d = dist(bulletX, bulletY, cX, cY);
  if ((d < 5) == true) {
    // we have a collision
    return true;
  } else {
    return false;
  }
}
*/
/*
boolean shoot(Car c) {
  float d = dist(bulletX, bulletY, cX, cY);
  if (d < 5) {
    // we have a collision
    return true;
  } else {
    return false;
  }
}
*/

}

So I believe you want to fix the collision.所以我相信你想修复碰撞。 If you have two circles and you want to see if they collide, you need to see if the radii collide.如果你有两个圆并且你想看看它们是否碰撞,你需要看看半径是否碰撞。 You can do this by seeing if the distance between the centers is greater than or less than the total of the two radii.您可以通过查看中心之间的距离是大于还是小于两个半径的总和来做到这一点。 Like this:像这样:

//returns whether or not two circles collide
//x1, y1, and r1 are for circle 1
boolean colliding(int x1, int y1, int x2, int y2, int r1, int r2) {
     return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2)); <= r1 + r2
}

If you wish to use rectangular hitboxes instead, check this link .如果您希望改用矩形碰撞框,请查看此链接 I hope this helped, have a good day.我希望这会有所帮助,祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM