2d平台(平滑)碰撞减少了CPU密集度

时间:2014-10-21 02:50:15

标签: java cpu collision

我正在尝试制作2D平台游戏。我有碰撞检测工作,虽然它非常占用CPU。我将所有级别存储在String数组中,并将所有实际游戏对象存储在ArrayList中。我浏览整个ArrayList(每一帧)并相应地进行碰撞检测。这是我现在的碰撞检测代码:

isFalling = true;
    canMoveLeft = true;
    canMoveRight = true;
    colRect.x = this.x - 5;
    colRect.y = this.y - 5;

    for (int i = 0; i < gameObjects.size(); i++) {
        if (gameObjects.get(i).levelNum == levelIndex)
            if (checkCollision(colRect, gameObjects.get(i)))

                if (gameObjects.get(i).type == "brick") {
                if (!pixelFree(centerX(), y + height, gameObjects.get(i)) ||
                    !pixelFree(x + 1, y + height, gameObjects.get(i)) ||
                    !pixelFree(x + width - 2, y + height, gameObjects.get(i))) {
                    isFalling = false;
                    gameObjects.get(i).move();
                }

                if (!pixelFree(x + width, centerY(), gameObjects.get(i)) ||
                    !pixelFree(x + width, y + 1, gameObjects.get(i)) ||
                    !pixelFree(x + width, y + height - 2, gameObjects.get(i))) {
                    canMoveRight = false;
                }

                if (!pixelFree(x - 1, centerY(), gameObjects.get(i)) ||
                    !pixelFree(x - 1, y + 1, gameObjects.get(i)) ||
                    !pixelFree(x - 1, y + height - 2, gameObjects.get(i))) {
                    canMoveLeft = false;
                }

                if (!pixelFree(x + 1, y - 3, gameObjects.get(i)) ||
                    !pixelFree(centerX(), y - 3, gameObjects.get(i)) ||
                    !pixelFree(x + width - 1, y - 3, gameObjects.get(i))) {
                    if (!pixelFree(centerX(), y + height, gameObjects.get(i))) {
                        die();
                    }
                    if (dy < 0) {
                        dy *= -1;
                    }
                }
                if (checkCollision(this, gameObjects.get(i))) {
                    int horizontalDist = (pow(this.centerX() - gameObjects.get(i).centerX(), 2));
                    int verticalDist = (pow(this.centerY() - gameObjects.get(i).centerY(), 2));
                    if (horizontalDist <= verticalDist) {
                        if (this.centerY() <= gameObjects.get(i).centerY()) {



                            y = gameObjects.get(i).y - height - 1;
                        }

                    } else {
                        if (this.centerX() <= gameObjects.get(i).centerX()) {
                            x = gameObjects.get(i).x - width - 1;
                        } else {
                            canMoveLeft = false;
                            x = gameObjects.get(i).x + gameObjects.get(i).width + 1;

                        }

                    }

                }
            } else if (gameObjects.get(i).type == "finisher") {
            if (checkCollision(this, gameObjects.get(i))) {
                if (coinsCaptured == totalCoinsInLevel)
                    finishLevel();
            }
        } else if (gameObjects.get(i).type == "spike") {
            if (checkCollision(this, gameObjects.get(i))) {
                die();
            }

        } else if (gameObjects.get(i).type == "coin") {
            if (checkCollision(this, gameObjects.get(i))) {
                gameObjects.get(i).take();
            }

        }
    }

现在,正如您所看到的,这非常占用CPU。有没有人对CPU更友好的碰撞检测方法有任何想法?请记住,我有移动游戏对象以及静态游戏对象。

0 个答案:

没有答案