【问题标题】:How to make the processing of collisions for objects of the same class?如何对同一类的对象进行碰撞处理?
【发布时间】:2016-11-21 13:28:34
【问题描述】:
// array containing the active humans.
public final Array<Human> activeHumans = new Array<Human>();
// object pool.
public final Pool<Human> humanPool = new Pool<Human>() {
    @Override
    protected Human newObject() {
        return new Human(100, 500);
    }
};

.................................................. ....................

@Override
public void update(float dt) {
checkCollisions();
}

public void checkCollisions() {

// human-human collision
    for (int i=0; i<activeHumans.size(); i++) {
    Human h1 = activeHumans.get(i);

    for (int j=0; j<activeHumans.size(); j++) {
        Human h2 = activeHumans.get(j);

        if (h1.getRectangle().overlaps(h2.getRectangle())) {
                h1.setX(h1.getX() + 2);
              }
    }
}

}

不知何故,人类(h1 和 h2)的所有对象都是 setX(h1.getX() + 2);。如何解决?我只需要他们中的一个让开

【问题讨论】:

    标签: libgdx collision-detection collision


    【解决方案1】:

    也许您可以更改第二个循环以避免检查对象与自身重叠(它总是会这样做!)并且还避免检查每对两次:

    for (int j=i+1; j<activeHumans.size(); j++) ...
    

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 1970-01-01
      • 2017-07-10
      • 2013-04-22
      • 1970-01-01
      • 2021-05-07
      • 2016-10-02
      • 2015-12-04
      • 2015-07-26
      相关资源
      最近更新 更多