0

スタンフォード大学の Java の CS オンライン コースの課題をこなしながら、初めてのブレイクアウト ゲームをやりました。

しかし、プレイテスト中に、ボールが斜めに移動しているときに時々ブロックにぶつかると、不自然な方法で連続して複数のブロックにぶつかることに気付きました。

衝突検出コードを少し改善する必要があると思いますが、すでにいくつかのことを試してみましたが、役に立ちませんでした。

このプログラムには ACM ライブラリを使用しています。架空の Rectangle がボールを囲み、その四隅を使用して衝突を検出します。

ゲーム中にいくつかのアドオン (ドロップしてボーナスを与えるアイテム) を追加したため、ゲーム内で多くの速度が変化します - vx 変数が大きく変化します。

これは私の問題に関連していると思います。これは、ボールがこのいくつかのレンガが連続して破壊されるよりも速い速度で移動するときであることに気付いたからです。

関連するコードをここに追加します。ただし、ここですべてのコードを確認できます: https://gist.github.com/frodosda/5604272

// 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;
    }
}

よろしくお願いします。

4

1 に答える 1

0

タイムステップを確認する必要があります。タイム ステップが大きい速度では、オブジェクトが次のタイム ステップで複数のアイテムに衝突することがよくあります。そのため、タイム ステップを小さくして、衝突検出がより頻繁に発生するようにします。

また、よりスマートな分析を行い、ヒットする最初のレンガ (x または y の最大値/最小値を持つもの) を確認してから、そこで停止する必要があります。次に、この衝突後のボールの最終的な位置を計算して、時間ステップが大きい場合でもボールが本来のように動作するようにする必要があります。

于 2013-05-18T13:23:16.980 に答える