我是libgdx和游戏开发人员的新手。我的任务是给我一个非常简单的2D游戏,该游戏已经实现了矩形碰撞检测。

游戏只是由一个可以由玩家控制的正方形组成,该正方形位于墙壁内部,墙壁内还有其他分散的正方形。现在,我需要在玩家广场和分散的广场/墙之间实现像素完美碰撞。

我最初尝试使用Pixmap,并尝试检查当前坐标的像素对于播放器矩形以及与之碰撞的任何矩形是否透明,但是在正确实现代码方面遇到了麻烦。

我了解我必须只检查两个对象在特定坐标处的像素颜色,然后查看它们是否都不透明,以免发生碰撞,但是我遇到了麻烦。我花了很长时间研究和寻找在线信息,因此,对于如何执行此操作的任何帮助或建议将不胜感激!

public boolean entityCollision(Entity e1, Direction direction, float newX, float newY) {
    boolean collision = false;

    for(int i = 0; i < entities.size(); i++) {
        Entity e2 = entities.get(i);

        if (e1 != e2) {

            if (newX < e2.x + e2.width && e2.x < newX + e1.width &&
                newY < e2.y + e2.height && e2.y < newY + e1.height) {
                collision = true;

                int colorOne = player.getPixel((int)newX,(int)newY);
                int colorTwo = enemy.getPixel((int)e2.x,(int)e2.y);

                if (colorOne != 0 && colorTwo != 0) {
                  collision = true;
                  e1.entityCollision(e2, newX, newY, direction);
                }
            }
        }
    }

    return collision;
}

最佳答案

看一下libgdx的the Rectangle implementation。它允许您执行以下操作:

Rectangle e1Rec = new Rectangle(e1.x, e1.y, e1.width, e1.height);
Rectangle e2Rec = new Rectangle(e2.x, e2.y, e2.width, e2.height);

if (e1Rec.contains(e2Rec)) {
   // Collision!
}

关于java - libgdx像素碰撞非常简单的2D游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46433118/

10-12 00:13
查看更多