简体   繁体   English

Java LibGDX ArrayList <Rectangle> 无法使碰撞工作

[英]Java LibGDX ArrayList<Rectangle> Can't get Collision working

First of all i want to say that my English isnt that good, and I'm a beginner programmer. 首先,我想说我的英语不好,而且我是一名初学者。 So take it easy with me :L 所以放轻松我:L

So I'm making a 2D game where ground is randomly spawned. 因此,我正在制作一个随机生成地面的2D游戏。 And its made of blocks... How I do this is first i create the blocks and then I add them to Rectangle ArrayList. 它是由块组成的...我首先要做的是创建块,然后将它们添加到Rectangle ArrayList中。 Blocks render correctly. 块正确渲染。 But they won't take any collision when player hits them. 但是当玩家击中它们时,它们不会发生任何碰撞。

At this moment collision doesn't work at all. 目前,碰撞根本不起作用。 When i press D (right) player runs towards right ignoring collision complitely. 当我按D键(右)时,播放器向右跑,忽略了碰撞。 When i press A (left) player don't move at all. 当我按A(左)时,播放器完全不移动。

First I make this ArrayList: 首先,我创建此ArrayList:

static ArrayList<Rectangle> BlockArray = new ArrayList<Rectangle>();

Then I give blocks their X,Y,Width,Height... values in a for loop and after that I add them to the list like this : 然后在for循环中将其X,Y,Width,Height ...值赋给块,然后将它们添加到列表中,如下所示:

BlockArray.add(Block[i]);

Then In player class I run this function every render loop. 然后在播放器类中,每个渲染循环都运行此函数。 It should tell if player can move to right or left or none...: 它应该告诉玩家是否可以向左或向右移动...:

ArrayList<Rectangle> rects = WorldGenerator.BlockArray;
        for(int i = 0; i < rects.size(); i++) {
            // LEFT
            if(player.x >= rects.get(i).getX() + rects.get(i).getWidth() && player.y >= rects.get(i).getY() + rects.get(i).getHeight()){
                canGoLeft = true;
            }
            else{
                canGoLeft = false;
            }

            // RIGHT
            if(player.x <= rects.get(i).getX() && player.y >= rects.get(i).getY() + rects.get(i).getHeight()){
                canGoRight = true;
            }
            else{
                canGoRight = false;
            }
        }

And then finally when user gives input it checks if those booleans are true or not : 最后,当用户输入内容时,它会检查这些布尔值是否正确:

if (Gdx.input.isKeyPressed(Keys.A) && canGoLeft == true){
            player.x -= delta * 350f;
        }
        if (Gdx.input.isKeyPressed(Keys.D) && canGoRight == true){
            player.x += delta * 350f;
        }

So thats my code. 这就是我的代码。 Hopyfully I didn't forget to mention something. 希望我没有忘记提些什么。 And hopefully someone can help me to solve this problem. 希望有人可以帮助我解决这个问题。 Also Like I said before I'm beginner at programming so I might have just a stupid fail in game logic... 也就像我在编程初学者之前说过的那样,所以我可能在游戏逻辑方面只是一个愚蠢的失败...

As far as your collision logic goes, you are changing canGoRight and canGoLeft for each rectangle regardless of previous collision checks. 就您的碰撞逻辑而言,无论先前的碰撞检查如何,您都将为每个矩形更改canGoRightcanGoLeft This means that effectively, the last rectangle in your list is the only one being checked for collisions. 这意味着实际上,列表中的最后一个矩形是唯一一个检查碰撞的矩形。

To resolve that issue, you'll want to change it to be like this (I just added a ! to the conditions, you should rework them rather just inverting the final result): 要解决该问题,您需要将其更改为这样(我刚刚在条件中添加了!,您应该对它们进行重新处理,而不仅仅是反转最终结果):

ArrayList<Rectangle> rects = WorldGenerator.BlockArray;

canGoLeft = true;
canGoRight = true;
for(int i = 0; i < rects.size(); i++) {
    // LEFT
    if(!(player.x >= rects.get(i).getX() + rects.get(i).getWidth() && player.y >= rects.get(i).getY() + rects.get(i).getHeight())) {
        canGoLeft = false;
    }

    // RIGHT
    if(!(player.x <= rects.get(i).getX() && player.y >= rects.get(i).getY() + rects.get(i).getHeight())) {
        canGoRight = false;
    }
}

This way, you assume they can move in a given direction, and any single rectangle that would block that direction will prevent movement that direction. 这样,您假设它们可以沿给定方向移动,并且任何会阻塞该方向的矩形都将阻止该方向的移动。

EDIT: I also looked into your conditions, and it looks like any rectangle to the right of the player will prevent them going to the left, not just a rectangle whose right side is up against the left side of the player. 编辑:我也调查了您的情况,看起来播放器右侧的任何矩形都将阻止它们向左移动,而不仅仅是其右侧向上与播放器左侧相对的矩形。 A much better comparison would be the difference between the player's and rectangle's half-widths and half-heights. 更好的比较是播放器和矩形的半角与半角之间的差异。 This article can explain in detail why. 本文可以详细解释原因。

As far as bigger picture goes, I mentioned in a comment that collision detection has already been written many times, and libgdx includes solid collision detection code that you should use, rather than writing your own collision detection code. 就更大的前景而言,我在一条评论中提到冲突检测已经编写了很多次,并且libgdx包含您应该使用的可靠冲突检测代码,而不是编写自己的冲突检测代码。

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

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