简单碰撞检测项目

huangapple go评论84阅读模式
英文:

Simple Collision Detection Project

问题

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

void keyPressed(){
  if (array2.size() != 5) array2.add(new Ghost(int(random(40, 960)), int(random(40, 460)), 40, 40));
}

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

注:以上是您提供的代码的翻译,我已经去除了注释并进行了格式调整。如果您有关于代码的具体问题或修改需求,请随时提问。

英文:

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. Please see my code and help me to improve it to add the remove() from it. 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&lt;Greenball&gt; array1 = new ArrayList&lt;Greenball&gt;();
    ArrayList&lt;Ghost&gt; array2 = new ArrayList&lt;Ghost&gt;();

    void setup(){
      size(1000,500);
      
      
    }
    
    void draw(){
      background(255);
      
      
      for(int i = 0; i &lt; array1.size() ; i++){
       array1.get(i).move();
       array1.get(i).display();
      }
      
      for (int i = 0; i &lt; 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 &gt; width-pixelSize || x &lt; 0){
xSpeed = xSpeed * -1; 
}
if(y &gt; height-pixelSize || y &lt; 0){
ySpeed = ySpeed * -1; 
}
}
void display(){
ghost = loadImage(&quot;greenghost.png&quot;);
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 &gt; width-pixelSize || x &lt; 0){
xSpeed = xSpeed * -1; 
}
if(y &gt; height-pixelSize || y &lt; 0){
ySpeed = ySpeed * -1; 
}
}
void display(){
g = loadImage(&quot;green.png&quot;);
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 &lt; r1 + r2) alive = false;
else alive = true;
}
}

简单碰撞检测项目

简单碰撞检测项目

答案1

得分: 1

draw()函数内添加_Collision Detection Function_,并运行两个嵌套的for循环,以便检查是否有任何Ghosts与任何Greenballs发生碰撞。

您可以使用以下代码替换您的draw()函数:

void draw() {
  background(255);

   // 遍历所有Ghosts
   for(int i = 0; i < array2.size(); i++) {

     // 移动并显示Ghosts
     array2.get(i).move();
     array2.get(i).display();

     /*
      * 对于每个Ghost,检查该Ghost是否与任何Greenball发生碰撞。
      * 在这里,您正在检查每个Ghost是否与每个Greenball发生碰撞。
      */
     for (int j = 0; j < array2.size(); j++) { // 对于每个Greenball,
       if (array1.get(i).checkForCollision(array2.get(j))) { // 如果有任何Ghost与之碰撞,
         array1.get(j).alive = false; // 那么将其设置为“alive = false”     
       }     
     }
   }

  // 反向遍历所有Greenballs,以便检查每一个Greenball
  for(int i = array1.size() - 1; i >= 0; i--) {
    if (array1.get(i).alive == false) { // 如果有任何Greenball死亡,
      array1.remove(i); // 那么将其移除
    } else { // 否则,移动并显示它。
      array1.get(i).move();
      array1.get(i).display();
    }
  }
 
}

建议:您不需要每次显示GhostGreenball时都调用loadImage(),因为这会对您的系统造成很大负担。只需在构造函数中使用ghost = loadImage("ghost.png")一次即可。

英文:

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:

void draw() {
background(255);
// loop through all the Ghosts
for(int i = 0; i &lt; 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 &lt; 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 &quot;alive = false&quot; 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 &gt;= 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. Just use ghost = loadImage(&quot;ghost.png&quot;) once in the Constructor and you're done.

答案2

得分: 0

依赖于您从何处调用`checkForCollision`方法我认为最好的地方是`draw`方法尝试用以下代码替换您的第二个循环

    Iterator<Ghost> ghostIterator = array2.iterator();  // 获取迭代器对象
    while (ghostIterator.hasNext()) {
        Ghost ghost = ghostIterator.next();             // 获取当前ghost对象
        ghost.move();                                   // 重新计算位置
        boolean collided = false;
        for (int i = 0, len = array1.size(); i < len; i++) {
            if (array1.get(i).checkForCollision(ghost)) {   // 检查碰撞
                ghostIterator.remove(ghost);               // 如果发生碰撞,移除当前ghost
                collided = true;
                break;               // 由于检测到碰撞,停止内部循环
            }
            if (!collided) {
                ghost.display();  // 如果ghost没有碰撞,显示它
            }
        }
    }
    
请注意您应该将`checkForCollision`方法的返回类型从`void`更改为`boolean`,并返回计算出的`alive`
英文:

It depends on where do you call the checkForCollision method from. I think the best place for it is the draw method. Try replace your second loop with the following code:

Iterator&lt;Ghost&gt; 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 &lt; 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.

答案3

得分: 0

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);
      }
    }
  }
}
英文:
void draw(){
background(0);
for(int i = 0; i &lt; gball1.size() ; i++){
Greenball gball = gball1.get(i);
gball.move();
gball.display(); 
}
for(int i = 0; i &lt; ghost2.size() ; i++){
Ghost ghost = ghost2.get(i);
ghost.move();
ghost.display();
for(int e = 0; e &lt; gball1.size(); e++){
Greenball b = gball1.get(e);
if(ghost.ifCollided(b)){
ghost2.remove(i);
}
}
}
}

huangapple
  • 本文由 发表于 2020年10月17日 01:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64394117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定