与LibGdx发生先发制人的冲突

时间:2013-03-16 02:57:10

标签: java collision-detection libgdx collision quadtree

我当前的实体引擎使用四叉树循环遍历所有实体(一个数组),然后使用每个实体拥有的libgdx Rectangle实例检查冲突。

My QuadTree(它很长,所以我认为id只是链接到它)

在Entity类中,我有一个placeFree方法:

public boolean placeFree(float px, float py){

    //the pre-emptive collision Rectangle
    preCollBox.set(px,py,hitBox.getWidth(),hitBox.getHeight());

    return screen.placeFree(preCollBox);

}

screen.placeFree方法:

Qtree = new Quadtree(0 , new Rectangle(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()));

public boolean placeFree(Rectangle rect){

    boolean result = false;

    Qtree.clear();

    for (int i = 0; i < collideEnt.size; i++) {
          Qtree.insert(collideEnt.get(i));
    }

    for(int i  = 0; i < collideEnt.size;i++){
        returnEntity.clear();
        Qtree.retrieve(returnEntity, rect);
        for (int x = 0; x < returnEntity.size; x++) {
            Entity ec = returnEntity.get(x);
                result = !(ec.hitBox.overlaps(rect));
         }
    }

    return result;

}

要检查先发制人的碰撞,请执行以下操作:

if(Gdx.input.isKeyPressed(Keys.D) && placeFree(x+10,y)){
        x+=10;
    }

基本上,placeFree()实际上将当前hitbox的副本放在新位置,并检查是否发生了任何碰撞。我想要做的是在向左移动之前检查我的播放器左侧区域是否没有碰撞。

我目前的问题是,如果我添加3个“Solid”实例(实体的子类),它只适用于添加的第一个实体。为什么会发生这种情况,我该如何解决这个问题呢?

这就是我的意思(红色框是普通的hitBox,绿色是先发制人的命中框)。

在这张照片中你可以看到先发制人的hitBox刚刚通过: pic

但是在这个中它不能,因为我首先添加了该块(顺便说一句,我使用了placeFree(x,y-100)来更清楚地显示绿色框):

pic2

0 个答案:

没有答案
相关问题