处理 - 如何在碰撞后删除数组中的对象

时间:2015-11-23 04:46:39

标签: arrays class processing collision

以下是我到目前为止的代码。当我将鼠标与一个圆圈碰撞时,我怎么能这样做呢?我确定我可以使用thing.remove(0);但我不知道该把它放在哪里。

任何帮助都会很棒,谢谢!

ArrayList<Thing> things = new ArrayList();
Player person;

boolean testMode = true;

void setup() {
size(600, 600);

things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());

person = new Player();
}

void draw() {
background(255);
noStroke();
noCursor();

person.display();

 for (Thing t : things) {    
t.display();
if (collision(person, t) == true) {
text("OUCH!", person.x, person.y-30);
  things.remove(0);
}
}
}

/////////////////////////////////////

boolean collision (Player p, Thing t) {
float d = dist(p.x, p.y, t.xPos, t.yPos); 
if (p.radius + t.radius > d) {
// we have a collision
return true;
} else {
return false;
}
}

////////////////////////

class Thing {
//fields
float xPos;
float yPos;
float radius = 30;

//constructor
Thing() {
xPos = random(500); 
yPos = random(500);
}

//methods
void display() {

float objDist = dist(xPos, yPos, mouseX, mouseY);
if ( objDist < radius ) {
  fill(0, 0, 255, 128);
} else {   
  fill(0, 255, 0, 70);
}

ellipse(xPos, yPos, radius, radius);
}
}

//////////////////////////////////

class Player {
float x;
float y;
float radius = 30;

void display() {
fill(255,0,0,70);
ellipse(mouseX,mouseY,radius, radius);
}
}

1 个答案:

答案 0 :(得分:1)

你的代码几乎就在那里,但有一些问题似乎已经过去了:

  1. 您正在以鼠标坐标(things.remove(0);)绘制播放器,但您不会更新播放器在碰撞检测中使用的x,y坐标。这使您的距离计算保持不变,因此碰撞代码返回false
  2. 如果碰撞返回true,则现有代码正在删除第一个元素(ArrayList<Thing> things = new ArrayList(); Player person; boolean testMode = true; void setup() { size(600, 600); //for loops can help you avoid repeating yourself in code for(int i = 0 ; i < 8; i++){ things.add(new Thing()); } // things.add(new Thing()); // things.add(new Thing()); // things.add(new Thing()); // things.add(new Thing()); // things.add(new Thing()); // things.add(new Thing()); // things.add(new Thing()); person = new Player(); } void draw() { background(255); noStroke(); noCursor(); person.x = mouseX; person.y = mouseY; person.display(); for (int i = 0 ; i < things.size(); i++) {//regular for loop to avoid ConcurrentModificationException (which is through usin using for : ) Thing t = things.get(i); t.display(); if (collision(person, t) == true) { //text("OUCH!", person.x, person.y-30); //this will probably appear for a slit second println("OUCH!"); //things.remove(0);//don't remove the first, remove the thing that collided things.remove(t); } } } ///////////////////////////////////// boolean collision (Player p, Thing t) { float d = dist(p.x, p.y, t.xPos, t.yPos); if ((p.radius + t.radius) * .5 > d) {//.5 is the same as /2 -> although you named the properties of player and thing radius, you are drawing the diameter (twice the size) // we have a collision return true; } else { return false; } } //////////////////////// class Thing { //fields float xPos; float yPos; float radius = 30; //constructor Thing() { xPos = random(500); yPos = random(500); } //methods void display() { float objDist = dist(xPos, yPos, mouseX, mouseY); if ( objDist < radius ) { fill(0, 0, 255, 128); } else { fill(0, 255, 0, 70); } ellipse(xPos, yPos, radius, radius); } } ////////////////////////////////// class Player { float x; float y; float radius = 30; void display() { fill(255, 0, 0, 70); //ellipse(mouseX, mouseY, radius, radius);//you're drawing the player at it's updated positions but the x,y properties used in collision detection aren't updated ellipse(x, y, radius, radius); } } ),但它应该删除当前事物碰撞
  3. 碰撞测试是使用两个对象的总和&#39;半径,但是使用半径作为直径绘制对象:要么检查与半径的一半的碰撞,要么使用半径绘制椭圆* 2
  4. 这就是我在代码中的意思:

    data: {
       'insertPost':1,
       'title':"'+title+'",
       'content':"'+content+'"
    }