简体   繁体   English

Java Brickbreaker桨碰撞检测

[英]Java Brickbreaker paddle collision detection

I am trying to make a brickbreaker game and I have run into some issues when it comes to collision detection. 我正在尝试制作一款打砖块游戏,并且在碰撞检测方面遇到了一些问题。 If you have ever played brick breaker you know that on the paddle, if the ball is moving to the left and you hit the left side of the top, then it continues moving right. 如果您曾经玩过碎砖机,那么您就会知道在桨上,如果球向左移动并且您击中了顶部的左侧,那么球将继续向右移动。 Although, if you hit it on the right side in this case, then the ball changes directions. 但是,如果在这种情况下在右侧击中,则球会改变方向。 And also if you hit it on the sides of the paddle, it bounces off on the Y axis. 而且,如果您在桨叶的侧面击打它,它也会在Y轴上弹起。 Since I have no idea how to do the top part of the paddle, I can't show you the code because I don't have any :) This is the code that I am using for the sides: 由于我不知道如何操作桨的顶部,因此我无法向您显示代码,因为我没有任何:)这是我在两侧使用的代码:

Rectangle rect1 = new Rectangle((int) paddleDir, 570, imsLoader.getImage("paddle1").getWidth(), imsLoader.getImage("paddle1").getHeight());
Rectangle rect2 = new Rectangle((int) ballX, (int) ballY, imsLoader.getImage("ball").getWidth(), imsLoader.getImage("ball").getHeight());
if (rect1.intersects(rect2))
{
    if (rect1.x == rect2.getMaxX() || rect1.getMaxX() == rect2.x)
    {
        ballVX = -ballVX;
        clipsLoader.play("pattleHit", false);
    }
    else
    {
        ballVY = -ballVY;
        ballY += 0.05;
        clipsLoader.play("pattleHit", false);
    }
}

What happens is that the ball when gets hit on the side, just goes right through the paddle going all over the place and when it reaches the other end either goes up or down! 发生的情况是,当球在侧面被击中时,它正好穿过整个位置的球拍,当球到达另一端时,球会上升或下降!

You have to make the ball bounce, right? 你得让球弹跳,对吧?

Let's look at an example. 让我们看一个例子。 Think the top square as the ball (I can't draw that nice). 将顶部正方形视为球(我不能画得很好)。 When the ball collides from left, it should move right and if it's from right, then go left. 当球从左碰撞时,它应向右移动;如果是从右碰撞,则应向左移动。

在此处输入图片说明

You can achieve this with a simple thing. 您可以通过简单的方法实现此目的。 Don't change the horizontal velocity but instead reverse the vertical one. 不要更改水平速度,而应反转垂直速度。

if (ball.getBounds().intersects(paddle.getBounds()))
{
    ball.setVy(-ball.getVy());
}

Easy right! 容易吧!

Now let's figure out how to do bounce effect on bricks. 现在让我们弄清楚如何对砖块反弹。

在此处输入图片说明

This is a scenario when ball hits the brick. 这是当球撞到砖头时的情况。 The red area is the intersection. 红色区域是交叉点。 Now notice it carefully. 现在请仔细注意。

  • If the intersection width is greater than the intersection height, the ball has hit in the bottom or vertical sides of the brick. 如果相交宽度大于相交高度,则球已击中砖的底部或垂直侧。

  • If the intersection height is greater that the intersection width, then it is a horizontal collision. 如果相交高度大于相交宽度,则为水平碰撞。

So we have to first calculate the intersection rectangle. 因此,我们必须首先计算相交矩形。 It's so easy with java. 使用Java很简单。

Rectangle intersection = ball.getBounds().intersection(brick.getBounds());

Now let's implement the bouncing. 现在,让我们实现弹跳。

if (intersection.width >= intersection.height)
{
    ball.setVy(-ball.getVy());
}

if (intersection.height >= intersection.width)
{
    ball.setVx(-ball.getVx());
}

That's it and you should have it fully functional. 就是这样,您应该使其具有完整的功能。

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

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