简体   繁体   English

LibGDX-播放器与平台(或图块)之间的碰撞检测,无需使用TiledMap

[英]LibGDX - Collision detection between Player and a Platform (or tile) WITHOUT using TiledMap

In my game the character is constantly moving from left to right, so there will never be a situation where he hits the right side of a platform. 在我的游戏中,角色不断地从左向右移动,因此永远不会出现他碰到平台右侧的情况。 I had a very brief talk with somebody and he suggested me to model my platform as 4 rectangles so that I would only need 3 cases in my algorithm (ie Player can only collide with the rectangles shaded green in the image below). 我和某人进行了简短的交谈,他建议我将平台建模为4个矩形,这样我的算法中只需要3种情况(即, Player只可以与下图中绿色阴影的矩形碰撞)。 Here's a visual description of what I mean: 这是我的意思的直观描述:

在此处输入图片说明

I gave it a try but the collision detection is still messed up. 我尝试了一下,但是碰撞检测仍然很混乱。 It doesn't go through the side of the platform (this is good news since we want this to happen) but you can't glide on top of it or bounce under it. 它不会通过平台的侧面(这是个好消息,因为我们希望做到这一点),但是您不能在其顶部滑行或在其下方弹跳。 Can anyone see where I may have gone wrong? 谁能看到我可能出了问题的地方?

public void checkCollisions(ArrayList<GameObject> list) {

    for (int i = 0; i < list.size(); i++) {
        for (int j = i + 1; j < list.size(); j++) {
            GameObject go1 = list.get(i);
            GameObject go2 = list.get(j);
            boolean playerPlatformCollision = go1.getType()
                    .equalsIgnoreCase("player") && go2.getType().equalsIgnoreCase("platform");
            if (playerPlatformCollision) {
              Rectangle playerRect = go1.getRect();
              Rectangle platformRect = go2.getRect();
                if (playerRect.overlaps(platformRect)) {

                    float platformRectX, platformRectY, platformRectWidth, platformRectHeight;
                    platformRectX = go2.getRect().getX(); // x position of the platform
                    platformRectY = go2.getRect().getY(); // y position of the platform
                    platformRectWidth = go2.getRect().getWidth(); // width of platform
                    platformRectHeight = go2.getRect().getHeight(); // height of platform

                    Rectangle caseA = new Rectangle(platformRectX,
                            platformRectY, platformRectX + 1,
                            platformRectHeight);
                    Rectangle caseB = new Rectangle(platformRectX + 1,
                            (platformRectY + platformRectHeight) - 1,
                            platformRectWidth - 1, platformRectHeight);
                    Rectangle caseC = new Rectangle(platformRectX + 1,
                            platformRectY, platformRectWidth, 1);

                    if (playerRect.overlaps(caseA)) {
                        go1.setxSpeed(0);
                    } else if (playerRect.overlaps(caseB)) {
                        go1.setySpeed(0);
                    } else if (playerRect.overlaps(caseC)) {
                        go1.setySpeed(-3f);
                    }
                }
            }
        }
    }
} 

Please let me know if you are not clear with any parts of the code. 如果您不清楚代码的任何部分,请告诉我。

I can't be sure that this is the issue, but it seems that this problem is not addressed in your code, and I have had similar problems myself when ignoring this. 我不能确定这是问题所在,但是似乎您的代码中未解决此问题,并且我自己也曾遇到过类似的问题。 When your player collides with the platform, it means that its rectangle is already intersecting the platform, by anywhere between 1 and how fast you are going number of pixels. 当您的播放器与平台碰撞时,这意味着它的矩形已经与平台相交,介于1到像素移动的速度之间。 Just adjusting the x and y speed is not enough. 仅调整x和y速度是不够的。 You must also remove the player rectangle from the platform, or else the player will get stuck. 您还必须从平台上移除播放器矩形,否则播放器将被卡住。 This can be done by using a loop and checking if your player is still colliding. 这可以通过使用循环并检查播放器是否仍在碰撞来完成。 if so, move 1 pixel over in the direction you came from, until no longer colliding. 如果是这样,请沿您的方向移动1个像素,直到不再发生碰撞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM