2016-11-21 78 views
0
// 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); 
       } 
    } 
} 

} 

不知何故,所有對象Human(h1和h2) setX(h1.getX() + 2);。如何解決它?我只需要其中的一個退居二線

回答

1

也許你可以改變第二回路所以要避免檢測對象與其自身重疊(它會一直做!),也避免檢查每對兩次:

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