首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BreakOut游戏中的砖块碰撞检测问题

BreakOut游戏中的砖块碰撞检测问题
EN

Stack Overflow用户
提问于 2013-05-18 21:19:41
回答 1查看 2.1K关注 0票数 0

我在完成斯坦福大学java CS在线课程的作业时,完成了我的第一个BreakOut游戏。

然而,我在游戏测试中注意到,当球有时在对角移动时撞到砖头时,它会以一种不自然的方式连续击中几块砖头。

我猜我必须改进一下我的碰撞检测代码,我已经尝试了几种方法,但都没有效果。

我在这个程序中使用ACM库。一个虚构的矩形包围着我的球,我用这个矩形的4个角来检测碰撞。

在游戏中,由于我添加了几个插件(掉落并给你加成的物品),所以游戏中有很多速度变化-- vx变量变化很大。

我认为这与我的问题有关,因为我注意到球的移动速度比连续损坏的几块砖的速度更快。

我会在这里添加相关的代码。不过,您可以在此处查看所有代码:https://gist.github.com/frodosda/5604272

代码语言:javascript
复制
// Defines the initial Direction of the Ball - inside the "MoveBall" Method
    vx = rGen.nextDouble(1.0, 3.0); // Random Horizontal Direction
    if (rGen.nextBoolean(0.5)) vx = -vx;
    vy = 2.0; // Vertical Direction

/** Checks if the ball collided or not with an object */

private void verificarColisaoBola () {

    GObject objColisao = getObjColisao(); // The object with wich the ball colided

    if (objColisao == raquete) { // If colidded with paddle
        vy = -vy; // Change vertical tranjectory of ball
        // prevents that the ball get "glued" to the paddle
        bola.setLocation(bola.getX(),bola.getY() - PADDLE_HEIGHT / 2);

        // Changes the direction of the ball when it hits the borders of the paddle - provides the player more control
        if ((bola.getX() < raquete.getX() + 0.20 * raquete.getWidth() && vx > 0)
            || (bola.getX() > raquete.getX() + 0.80 * raquete.getWidth() && vx < 0)) {
            vx = -vx;
        }

    } else if (objColisao != null) { // Colision with a brick
        remove (objColisao); // remove the brick
        nTijolos--; // counts one less brick
        vy = -vy; // Changes vertical direction
}

/** Finds if the limits of the ball - 4 corners - hits an object  
 * @return The object that collided with the ball - or null */

private GObject getObjColisao () {
    if (getElementAt (bola.getX(), bola.getY()) != null) { // Top left corner
        return getElementAt (bola.getX(), bola.getY());
    } else if (getElementAt (bola.getX() + bola.getWidth(), bola.getY()) != null) { // Top Right corner
        return getElementAt (bola.getX() + bola.getWidth(), bola.getY());
    } else if (getElementAt (bola.getX(), bola.getY() + bola.getWidth()) != null) { // Bottom Left corner
        return getElementAt (bola.getX(), bola.getY() + bola.getWidth());
    } else if (getElementAt (bola.getX() + bola.getWidth(), bola.getY() + bola.getWidth()) != null) { // Bottom Right corner
        return getElementAt (bola.getX() + bola.getWidth(), bola.getY() + bola.getWidth());
    } else {
        return null;
    }
}

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2013-05-18 21:23:17

你需要检查你的时间步长。具有大时间步长的速度通常会导致对象在下一个时间步长中命中多个项目。因此,使您的时间步长较小,以使碰撞检测更频繁地发生。

您还应该做一些更聪明的分析,并检查您可能命中的第一块砖(具有最大/最小x或y值的砖),然后停下来。然后,您将需要计算球在此碰撞后的最终位置,以便即使在大的时间步长下,球的行为也应该是正确的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16624712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档