【问题标题】:Is there a nicer way to pass perform collision detection on an array of objects? [closed]有没有更好的方法来通过对对象数组执行碰撞检测? [关闭]
【发布时间】:2019-02-04 19:53:02
【问题描述】:

所以我正在使用 Java(处理)并且我有一个对象数组,我需要它们能够与自己发生碰撞,但我不太确定如何有效地做到这一点。我有一个基本的方法,但它写得非常糟糕,并且不允许动态数组。

我目前有一个静态数组,但我希望将来有一个动态数组。我目前运行通过检查每个可能的组合来检测碰撞的方法。总会有 3 个对象,而且永远不可能少于 3 个。

这是我的代码:

Circle[] arrayOfCircles = new Circle[3]; //declare array

void setup() {

  size(1000, 1000);

  for (int i = 0; i < arrayOfCircles.length; i++) {
    createPuck(i);
  } // creates objects

}

void createPuck(int i) {

  float speedY;
  float speedX;

  if (tempSpeedX == 0 && tempSpeedY == 0) {
    speedY = createSpeedY();
    speedY = speedY * multiplier;
    speedX = 3 * multiplier;
  } else {
    speedX = tempSpeedX;
    speedY = tempSpeedY;
    tempSpeedX = 0;
    tempSpeedY = 0;
  }
  arrayOfPucks[i] = new Puck(width, int(random(0, height)), speedX, speedY);
}

int createSpeedY() {

  int tempSpeed = int(random(-3, 3));

  do {
    if (tempSpeed == 0) {
      tempSpeed = int(random(-3, 3));
    }
  } while (tempSpeed == 0);

  return tempSpeed;
}

void draw() {
  detectCollisions();
}

void detectCollisions() {
  for (int i = 0; i < arrayOfCircles.length; i++) {

    boolean collision = false;

    if (i==0) {

      collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i+1]);

      if (collision == true) {
        arrayOfCircles[i].bounce(arrayOfCircles[i+1]);
      }

      collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i+2]);

      if (collision == true) {
        arrayOfCircles[i].bounce(arrayOfCircles[i+2]);
      }
    } else if (i==1) {

      collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i-1]);

      if (collision == true) {
        arrayOfCircles[i].bounce(arrayOfCircles[i-1]);
      }

      collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i+1]);

      if (collision == true) {
        arrayOfCircles[i].bounce(arrayOfCircles[i+1]);
      }
    } else if (i==2) {

      collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i-1]);

      if (collision == true) {
        arrayOfCircles[i].bounce(arrayOfCircles[i-1]);
      }

      collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i-2]);

      if (collision == true) {
        arrayOfCircles[i].bounce(arrayOfCircles[i-2]);
      }
    } // end of if
  } // end of for loop
} // end of function

class Circle {

  float x, y;
  float speedX, speedY;
  final int radius = 10;

  Circle(float x, float y, float speedX, float speedY) {
    this.x = x;
    this.y = y;

    this.speedX = speedX;
    this.speedY = speedY;
  }
  // constructor

  void move() {
    this.x = this.x-speedX; //move left
    this.y = this.y-speedY;
  }

  //methods
  void render() {
    fill(0);
    ellipse(this.x, this.y, this.radius, this.radius);
  }

  void bounce() {
    this.speedX = this.speedX * -1;
    this.speedY = this.speedY * -1;
  }

  boolean detectSelfCollision(Puck other) {
    for (int i = 0; i == 2; i++) {
    }
    float distX = this.x - other.x;
    float distY = this.y - other.y;
    float distance = sqrt( (distX*distX) + (distY*distY) );

    if (distance <= radius*2) {
      return true;
    } else {
      return false;
    }
  }
}

代码确实起作用,因为它可以满足我的要求,但它不是很整洁,有很多重复并且不是很可扩展。

任何建议或帮助将不胜感激,我什至可以根据伪代码重新创建任何建议。我只是在这里碰壁。

【问题讨论】:

  • 你能提供你的Circle类吗?
  • 如果代码有效,并且您正在寻找改进它的方法,那么Code Review SE 将是这个问题的更好场所。如果您决定将问题移到那里,请务必遵守他们的问题标准。
  • @oleg.cherednik 更新了代码部分。
  • @JohnBollinger 谢谢,我会看看,问题是我不确定我在找什么。
  • 嗯,就是这样,@Jonathan。如果你不知道你在找什么,那我们怎么知道?代码审查是关于一般的“我如何使它变得更好?”问题。 SO 是关于更具体的问题。

标签: java arrays class oop object


【解决方案1】:

您的detectCollisions() 方法可以重写:

public void detectCollisions() {
    for (int i = 0; i < circles.length; i++)
        for (int j = 0; j < circles.length; j++)
            if (i != j && circles[i].detectCollision(circles[j]))
                circles[i].bounce(circles[j]);
}

【讨论】:

  • 这正是我想要的,非常感谢!
  • @Johnathan 请注意,虽然这不能很好地扩展,因为它是 n^2 时间。对于少量的圈子来说会很好,但随着你添加更多的圈子会很快变坏。如果您打算有很多圈子,那么请考虑查找更有效的碰撞检测算法,请参阅something like this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
相关资源
最近更新 更多