简体   繁体   English

简单的碰撞检测项目

[英]Simple Collision Detection Project

I am a newbie with coding and doing a collision detection project for my assignment.我是一个编码新手,并为我的任务做一个碰撞检测项目。 I think I got the Algorithm right but my problem is how will I access the object from my ArrayList I created and remove the object once collision is detected.我想我的算法是正确的,但我的问题是如何从我创建的 ArrayList 访问对象,并在检测到碰撞后删除该对象。 Please see my code and help me to improve it to add the remove() from it.请查看我的代码并帮助我改进它以从中添加 remove()。 I added a boolean variable to check if true or false then do the remove function.我添加了一个布尔变量来检查是真还是假,然后执行删除功能。

My end goal is to remove the ghost when it collides with the green ball我的最终目标是当它与绿球相撞时消除鬼魂


    ArrayList<Greenball> array1 = new ArrayList<Greenball>();
    ArrayList<Ghost> array2 = new ArrayList<Ghost>();

    void setup(){
      size(1000,500);
      
      
    }
    
    void draw(){
      background(255);
      
      
      for(int i = 0; i < array1.size() ; i++){
       array1.get(i).move();
       array1.get(i).display();
      }
      
      for (int i = 0; i < array2.size() ; i++){
       array2.get(i).move();
       array2.get(i).display();
      }
    }
    
    void mousePressed(){
      if (array1.size() != 5) array1.add(new Greenball(int(mouseX),int(mouseY), 40, 40)); //max at 5
    }
    
    void keyPressed(){
      if (array2.size() != 5)array2.add(new Ghost(int(random(40,960)), int(random(40,460)), 40, 40)); //max at 5

class Ghost{
  
  PImage ghost;
  int x,y,w,h,xSpeed,ySpeed;
  int pixelSize = 40;
  Ghost(int x_, int y_, int w_, int h_){
  x = x_;
  y = y_;
  w = w_;
  h = h_;  
  xSpeed = int(random(-10,10));
  ySpeed = int(random(1,10));  
  }
  
  void move(){
   x = x + xSpeed;
   y = y + ySpeed;
   
   if(x > width-pixelSize || x < 0){
    xSpeed = xSpeed * -1; 
   }
   if(y > height-pixelSize || y < 0){
    ySpeed = ySpeed * -1; 
   }
    
  }
  
  void display(){
   ghost = loadImage("greenghost.png");
   image(ghost,x,y,w,h);
}

class Greenball{
  
 PImage g;
 int x,y,w,h,xSpeed,ySpeed;
 int pixelSize = 40;
 boolean alive=true;
 Greenball(int x_, int y_, int w_, int h_){
   x = x_;
   y = y_;
   w = w_;
   h = h_;
   xSpeed = int(random(-10,10));
   ySpeed = int(random(1,10));
 }
 
 void move(){
   x = x + xSpeed;
   y = y + ySpeed;
   
   if(x > width-pixelSize || x < 0){
    xSpeed = xSpeed * -1; 
   }
   if(y > height-pixelSize || y < 0){
    ySpeed = ySpeed * -1; 
   }
 }
 
 void display(){
   g = loadImage("green.png");
   image(g,x,y,w,h);
 }
 
 
 void checkForCollision(Ghost ghost){
   float d = dist(x,y,ghost.x,ghost.y);
   float r1 = (x+y)/2;
   float r2 = (ghost.x + ghost.y)/2;
   
   if (d < r1 + r2) alive = false;
   else alive = true;
   }

}

在此处输入图片说明

在此处输入图片说明

Add the Collision Detection Function inside draw() and run two nested for loops so that you check if any of the Ghosts is colliding with any of the Greenballs You can replace your draw function with the following code:draw()添加碰撞检测函数并运行两个嵌套的for循环,以便您检查是否有任何Ghosts与任何Greenballs发生碰撞您可以使用以下代码替换您的 draw 函数:

void draw() {
  background(255);

   // loop through all the Ghosts
   for(int i = 0; i < array2.size(); i++) {

     //move and display the Ghosts
     array2.get(i).move();
     array2.get(i).display();

     /*
      * For every Ghost, check if that Ghost is colliding with any Greenball.
      * Here you are checking collision with each Ghost for each Greenball.
      */
     for (int j = 0; j < array2.size(); j++) { // for each Greenball,
       if (array1.get(i).checkForCollision(array2.get(j)) { // if any Ghost is Colliding with it,
         array1.get(j).alive = false; // then set "alive = false" for it     
       }     
     }
   }

  // loop through all the Greenballs in reverse order, so that each and every Greenball is checked
  for(int i = array1.size() - 1; i >= 0; i--) {
    if (array1.get(i).alive = false) { // if any Greenball is dead,
      array1.remove(i); // then remove it
    } else { // else, move and display it.
      array1.get(i).move();
      array1.get(i).display();
    }
  }
 
}

Suggestion: You don't need to loadImage() everytime you display the Ghost or Greenball as it is very taxing on your system.建议:您不需要在每次显示GhostGreenball时都loadImage()因为这对您的系统来说非常繁重。 Just use ghost = loadImage("ghost.png") once in the Constructor and you're done.只需在构造函数中使用ghost = loadImage("ghost.png")一次即可。

It depends on where do you call the checkForCollision method from.这取决于您从何处调用checkForCollision方法。 I think the best place for it is the draw method.我认为最好的地方是draw方法。 Try replace your second loop with the following code:尝试用以下代码替换第二个循环:

Iterator<Ghost> ghostIterator = array2.iterator();  // get iterator object
while (ghostIterator.hasNext()) {
    Ghost ghost = ghostIterator.next();             // get current ghost object
    ghost.move();                                   // recalculate position
    boolean collided = false;
    for (int i = 0, len = array1.size(); i < len; i++) {
        if (array1.get(i).checkForCollision(ghost)) {   // check for collision
            ghostIterator.remove(ghost);               // remove current ghost if it collided
            collided = true;
            break;               // stop inside loop due to collision detected
        }
        if (!collided) {
            ghost.display();  // if ghost isn`t collided display it
        }
    }
}

Please note you should change return type of the checkForCollision method from void to boolean and returns calculated alive value.请注意,您应该将checkForCollision方法的返回类型从void更改为boolean并返回计算出的alive值。

void draw(){
  background(0);
  
  for(int i = 0; i < gball1.size() ; i++){
    Greenball gball = gball1.get(i);
    gball.move();
    gball.display(); 
   }
  

  for(int i = 0; i < ghost2.size() ; i++){
    
    Ghost ghost = ghost2.get(i);
    ghost.move();
    ghost.display();
    
    for(int e = 0; e < gball1.size(); e++){
      Greenball b = gball1.get(e);
      if(ghost.ifCollided(b)){
        ghost2.remove(i);
      }
    }
   }

}

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

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