Sprite collision Rectangles LibGDX

时间:2015-02-23 13:28:39

标签: java libgdx collision-detection collision

我的精灵顶部碰撞存在很大问题。这是我的构造函数

public CoolGuy(){
    full = new Rectangle(0, 0, 50, 98);
    bottom = new Rectangle(0, 0, 50, 15);
    top = new Rectangle(0, 83, 50, 15);
    left = new Rectangle(0, 15, 25, 68);
    right = new Rectangle(25, 15, 25, 68);

    texture = new Texture("data/coolguy1.png");
    sprite = new Sprite(texture);
    sprite.setSize(50, 98);
    this.setPosition(0, 0);
    velocity = 0;
}

这是我的setPosition

public void setPosition(float x, float y){
        full.x = x;
        full.y = y;

        bottom.x = x;
        bottom.y = y;

        left.x = x;
        left.y = y + 15;

        right.x = x + 25;
        right.y = y + 15;

        top.x = x;
        top.y = y + 83;

        sprite.setPosition(x, y);

}

并且此方法返回基于顶部,底部,右侧,左侧碰撞的数字。

public int hits(Rectangle r){

    if(left.overlaps(r)){
        return 2;
    }
    if(right.overlaps(r)){
        return 3;
    }

    if(top.overlaps(r)){
        return 4;
    }
    if(bottom.overlaps(r)){
        return 1;
    }
    return -1;
}

在我的主要课程中,我检查了碰撞

for(GameObject t : list){
            switch(player1.hits(t.getHitBox())){
            case 1:
                player1.action(1, 0, (t.getHitBox().y + t.getHitBox().height));
                break;
            case 2:
                player1.action(2, (t.getHitBox().x + t.getHitBox().width + 1), 0);
                break;
            case 3:
                player1.action(3, (t.getHitBox().x - player1.getHitBox().width - 1), 0);
                break;
            case 4:
                player1.action(4, 0, (t.getHitBox().y - player1.getHitBox().height));
                System.out.println("AAA");
                break;  
            }
}

这是我的精灵应该看的样子(以及我的上,下,右,左矩形)

enter image description here

enter image description here

所以这是我的结果。除了顶部碰撞之外,所有的合作都很好。我无法找到游戏逻辑的错误。 enter image description here

0 个答案:

没有答案