如果对象与同一对象发生碰撞

时间:2014-11-14 11:03:57

标签: java arraylist libgdx collision

我无法弄清楚如何与同一个ArrayList中的同一个对象进行冲突,但不是lbgdx本身。它甚至可能吗?我该怎么做?

我如何创建对象:

private static List<Tree> objects = new ArrayList<Tree>();


for (int i = 0; i<400 ; i++ ) {
        objects.add(new Tree());
        }

他们在&#34;播放器周围随机产生&#34;有时它们彼此重叠。

树类:

public class Tree {
public TextureRegion sprite;
public Vector2 position;
private int x, y, varx, vary;
public int size;
private Random r;
public boolean injunglex, injungley = false;
public int myLevel;
public Rectangle objrect, objrectUP;
public Rectangle playerrect;
public boolean active = false;
private int random, tolis=700;
public Tree() {
    GameRender.medis++;
    size = GameRender.tilesize*5;
    position = new Vector2(x, y);
    r = new Random();
    random = r.nextInt(2);
    int zenkas = r.nextInt(2);
    if(AssetLoader.load){
    position.y = AssetLoader.getInt("treey"+GameRender.medis);
    position.x = AssetLoader.getInt("treex"+GameRender.medis);  
    }else{
        position.y = getPositionY();
        position.x = getPosition();     
    while(position.y>GameRender.playery-100&&position.y<GameRender.playery+100
            &&position.x>GameRender.playerx-100&&position.x<GameRender.playerx+100){
        position.y = getPositionY2();
        position.x = getPosition2();
       }
    }
}
public int getPosition() {
    int x;
    int zenkasx = r.nextInt(2);
    int renkam = r.nextInt(2);
    int renkam2;
    if(renkam==1){renkam=r.nextInt(50); renkam2=0;} else {renkam=0; renkam2=r.nextInt(50);}
    if(zenkasx==0){
        x = GameRender.playerx+renkam+(r.nextInt(tolis));
    }else x = GameRender.playerx-renkam2-(r.nextInt(tolis));
    return x;
}
public int getPosition2() {
    int x;
    int zenkasx = r.nextInt(2);
    int renkam = r.nextInt(2);
    int renkam2;
    if(renkam==1){renkam=0; renkam2=+r.nextInt(100);} else {renkam=r.nextInt(100); renkam2=0;}
    if(zenkasx==0){
        x = GameRender.playerx+renkam+(r.nextInt(tolis));
    }else x = GameRender.playerx-renkam2-(r.nextInt(tolis));
    return x;
}
public int getPositionY() {
    int x;
    int zenkasx = r.nextInt(2);
    int renkam = r.nextInt(2);
    int renkam2;
    if(renkam==1){renkam=r.nextInt(50); renkam2=0;} else {renkam=0; renkam2=r.nextInt(50);}
    if(zenkasx==0){
        x = GameRender.playery+renkam+(r.nextInt(tolis));
    }else x = GameRender.playery-renkam2-(r.nextInt(tolis));
    return x;
}
public int getPositionY2() {
    int x;
    int zenkasx = r.nextInt(2);
    int renkam = r.nextInt(2);
    int renkam2;
    if(renkam==1){renkam=100; renkam2=+r.nextInt(200);} else {renkam=r.nextInt(200); renkam2=100;}
    if(zenkasx==0){
        x = GameRender.playery+renkam+(r.nextInt(tolis/3));
    }else x = GameRender.playery-renkam2-(r.nextInt(tolis/3));
    return x;
}
private TextureRegion getSprite() {
    int medis = r.nextInt(3)+1;
         if(medis==1) sprite = AssetLoader.tree1;
    else if(medis==2) sprite = AssetLoader.tree2;
    else if(medis==3) sprite = AssetLoader.tree3;
    return sprite;
} 
public void createMe() {
    GameRender.batch.draw(sprite, position.x, position.y, size, size);  
}

}

1 个答案:

答案 0 :(得分:0)

你的问题不清楚

  

我想知道是否有办法在树周围创建一个矩形,如果矩形与另一个树相同的矩形重叠:做一些事情。

但我会尽我所能:

你可以在树上创建两个框。你可以通过检查if (object.box() overlaps otherObject.box())

之类的东西来做到这一点

但是,您可以使用double[][][]然后将其与坐标一起存储,从而获得更详细的碰撞框。

基本上,你会将坐标放在double[][][]中,然后你可以检查另一棵树是否在double[][][]范围内,如果是,那么它们就会发生碰撞。 您可以为坐标设置1,为空气设置0。

例如,if (double[treeX][treeY][treeZ] == 1) do collision stuff here.