如何解决简单的碰撞检测项目
我是一名新手,负责编码并为我的任务做一个碰撞检测项目。我想我算法正确,但是我的问题是,一旦检测到冲突,如何从我创建的ArrayList中访问对象并删除该对象。请查看我的代码并帮助我对其进行改进,以从中添加remove()。我添加了一个布尔变量来检查是对还是错,然后执行删除功能。
我的最终目标是当幽灵与绿球碰撞时将其移除
$ npm install jsdom
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)); //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,h);
}
解决方法
在List<Animal>
内添加冲突检测功能并运行两个嵌套的List<Dog>
循环,以便检查Dog
中的任何一个是否与任何private static void ShowList<T>(List<T> MyAnimals)
{
Console.WriteLine("List of " + typeof(T).Name);
foreach (var item in MyAnimals)
{
Console.WriteLine(item);
}
}
您可以使用以下代码替换draw函数:
draw()
建议:您不需要每次显示for
或Ghosts
时都Greenballs
,因为这对您的系统非常费力。只需在构造函数中使用一次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();
}
}
}
就可以了。
这取决于您从哪里调用checkForCollision
方法。我认为最好的方法是draw
方法。尝试用以下代码替换第二个循环:
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
}
}
}
请注意,您应该将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);
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。