简体   繁体   English

如何避免与libgdx多次冲突?

[英]How to avoid multiple collision with libgdx?

I'm coding my first 2D game using java and libgdx : a brick breaker. 我正在使用Java和libgdx编写我的第一个2D游戏:碎砖机。

I have some difficulties handling multiple collision. 我在处理多次碰撞时遇到一些困难。

Let met explain : I created an array of bricks (Brick extends Rectangle). 让我们来解释一下:我创建了一个砖块数组(Brick扩展了Rectangle)。 All those bricks have the same parameters as rectangles but I added 4 booleans : left, right, up, down. 所有这些积木具有与矩形相同的参数,但我添加了4个布尔值:左,右,上,下。 When ball.y > brick.y --> up = true and all the others = false. 当ball.y> brick.y-> up = true时,所有其他= false。 And when up is true, if ball overlaps brick then ball.y *= -1. 当up为true时,如果球与砖重叠,则ball.y * = -1。 (for positions and speed, I'm using Vector2). (关于位置和速度,我使用的是Vector2)。 It's exactly the same for the 3 others boolean (for left and right I use ball.x *=-1 of course). 对于其他3个布尔值,这是完全相同的(对于左右,我当然使用ball.x * =-1)。

if(briques.get(r).up_b == true || briques.get(r).down_b == true) {
            if(ball.overlaps(briques.get(r))) {
                vitb.y *= -1;   
            }
        }    

and

if(briques.get(r).left_b == true || briques.get(r).right_b == true) {
            if(ball.overlaps(briques.get(r))) {
                vitb.x *= -1;   
            }
        }

NB : vitb == ball speed (Vector2). 注意:vitb ==球速度(Vector2)。

This part works perfectly. 这部分工作完美。 But here is the problem : 但这是问题所在:

My ball have a width and height of 10 pixels. 我的球的宽度和高度为10像素。 So, if all the bricks are at least 10 pixels far from each other it's ok. 因此,如果所有积木彼此之间至少相距至少10像素,则可以。 But if it's less, the ball can touch 2 brick at a time. 但是,如果球少一点,则球一次可以碰到2块砖。 And it just freezes then totally bugs... 它只是冻结然后完全是错误...

I've tried so many things, I don't know what to do. 我已经尝试了很多事情,但我不知道该怎么办。 I think it comes from the fact that 2 vectors are sending the ball in 2 different directions so I've tried this : 我认为这是因为两个向量向两个不同的方向发送球,所以我尝试了以下方法:

try {
        if(ball.overlaps(briques.get(r)) && ball.overlaps(briques.get(r+1))) {

            if(((posb.dst(posbr.get(r))) < (posb.dst(posbr.get(r+1)))) || ((posb.dst(posbr.get(r))) == (posb.dst(posbr.get(r+1))))){
                briques.get(r+1).up_b = false;
                briques.get(r+1).down_b = false;
                briques.get(r+1).right_b = false;
                briques.get(r+1).left_b = false;
            }
        }

        if(ball.overlaps(briques.get(r)) && ball.overlaps(briques.get(r + nb_br_par_ligne))) {
            if((posb.dst(posbr.get(r))) < (posb.dst(posbr.get(r+nb_br_par_ligne))) || (posb.dst(posbr.get(r))) == (posb.dst(posbr.get(r+nb_br_par_ligne)))){
                briques.get(r+nb_br_par_ligne).up_b = false;
                briques.get(r+nb_br_par_ligne).down_b = false;
                briques.get(r+nb_br_par_ligne).right_b = false;
                briques.get(r+nb_br_par_ligne).left_b = false;
            }
        }
        } catch(IndexOutOfBoundsException e){
            e.printStackTrace();
        } 

NB : posb == ball position // posbr == brick position // nb_br_par_ligne is the way to find the brick which is up or down "r" (nb_br_par_ligne = 13 here because I have 13 bricks per row). 注意:posb ==球位置// posbr ==砖块位置// nb_br_par_ligne是查找上下“ r”砖块的方法(nb_br_par_ligne = 13,因为我每行有13块砖块)。

In this code I have tried to say "If the ball touches 2 bricks, choose the nearest, if it's the same distance, then choose the one on the left. For the other one, set all booleans to false to avoid collision." 在这段代码中,我试图说:“如果球碰到2块砖,则选择最近的一个,如果距离相同,则选择左侧的一个。对于另一个,将所有布尔值都设置为false以避免碰撞。” I also added something to make bricks disappear only if it overlaps with ball AND if it's the brick that have been chosen, but it doesn't work. 我还添加了一些使砖块仅在与球重叠且已选择砖块的情况下使其消失的方法,但它不起作用。

I've also tried others things and I can't figure out what is wrong. 我也尝试过其他事情,但无法弄清楚出了什么问题。

The two following samples works perfectly but I'm not sure I'm using them correctly with the mutliple collision : I tried to put them in the loop that tests if there is multiple collisions, then I tried to put it out of the loop... 以下两个示例可以完美地工作,但是我不确定在多重碰撞中是否正确使用了它们:我试图将它们放入测试多个冲突的循环中,然后尝试将其排除在循环之外。 ..

if(ball.overlaps(briques.get(r))) {
            briques.get(r).setRes(briques.get(r).getRes()-1);

        }

if(briques.get(i).getRes() == 0) {
            briques.removeIndex(i);

        }

So I'm here to ask if someone can just give me an advice on how to avoid multiple collision if my ball touches 2 bricks. 所以我在这里问,如果我的球碰到2块砖,是否有人可以给我一些建议,如何避免多次碰撞。

Thanks all (and sorry if I made some mistakes with english). 谢谢大家(对不起,如果我在英语方面犯了一些错误)。

You only need to change the velocity once. 您只需要更改一次速度。

Currently you change it two times, if the ball hits two bricks and x * -1 * -1 = x , which means you don't change it at all. 当前,如果球击中了两个积木并且x * -1 * -1 = x ,则将其更改两次,这意味着您根本不会更改它。

Just add a flag that the ball needs to change direction, which can only be set once per frame (or simulation step). 只需添加一个球需要改变方向的标志,该标志每帧只能设置一次(或模拟步骤)。

Then after the collision checks, check this flag (and reset it if necessary) and manipulate the velocity accordingly. 然后,在碰撞检查之后,检查该标志(并在必要时将其重置)并相应地控制速度。

This flag could be an enum: 此标志可能是枚举:

public enum Hit
{
     TOP, BOT, LEFT, RIGHT, NONE;
}

The default would be NONE so that you can check if the ball already hit something. 默认值是NONE这样您就可以检查球是否已经击中了东西。

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

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