0

I am making a breakout game for a school project. The only problem I am running into is the Ball Bouncing when the Ball and Bricks collide. I used ball1.getEllipse().intersects(b.getRectangle()) allowing me to figure out when it is colliding and delete the brick. The bouncing chances depending on the side it collided with. This is the main problem. The .intersect piece does not show me which side the brick gets hit by. I need this to know whether to change the x or y speed. If anyone has any idea on how to figure this out, please leave your input (I have been trying to think of a solution for 5 hours, I gave up)

 public void collision(int i) {

    for (Block b : blocks) {
        if (ball1.getEllipse().intersects(b.getRectangle())) {
            if (!b.isDestroyed()) {
                b.destroy();
                blockCount-=1;
                ball1.brickBounce();`public void collision(int i) {
4

1 回答 1

1

根据我从您的问题中了解到的情况,您想知道球击中矩形的哪一侧。一个简单的方法是获取球的位置和矩形的位置,然后比较两者。如果球的 x 位置小于矩形 x 位置 - 宽度的一半(以获得边界的 x 位置),则球击中左侧。然后你可以对右侧做相反的事情。检查它是否击中顶部或底部与 y 位置和矩形的高度相似。请注意,我假设每个形状的 x 和 y 位置是中心,如果不仅需要进行细微调整以获得与中心相同的结果。

于 2016-10-27T02:59:00.060 回答