C ++ / SFML超级马里奥游戏与块的碰撞问题

问题描述 投票:0回答:1

我用C ++ / SFML制作了一个类似于超级马里奥的游戏,当尝试编写一些代码用于Mario碰撞时,我遇到了一些问题:角色,当沿着水平轴移动时与垂直的方块碰撞时,卡在积木的一侧,就像它在看不见的积木上行走一样。我试图修改碰撞函数,例如使mario仅在与块相关的位置包含在块坐标中时才使其与块水平碰撞。

我包括一些用于移动的代码(keyPressed是返回按下的键的函数:]

void Player::movement(Time& gameTime) {
    player.move(v * gameTime.asSeconds());
    view.setCenter(player.getPosition().x + 16, 590);

    if (keyPressed(up) && !jumping) {
        jumping = true;
        v.y = jumpSpeed;
    }
    if (v.y > 200) {
        jumping = true;
    }
    else {
        crouch = false;
    }
    if (keyPressed(left)) {
        if (v.x > -moveSpeed) {
            v.x -= 100;
        }
        else {
            v.x = -moveSpeed;
        }
    }
    else if (keyPressed(right)) {
        noKeyPressed = false;
        if (v.x < moveSpeed) {
            v.x += 100;
        }
        else {
            v.x = moveSpeed;
        }
    }
    else {
        if (v.x < -100) {
            v.x += 100;
        }
        else if (v.x > 100) {
            v.x -= 100;
        }
        else {
            v.x = 0;
        }
    }
    gravity();

    if (big) { //Big is a variable that tells me if mario is big or small
        heightValue = 33; //heightValue is a variable that stores the sprite height in pixels
    }
    else {
        heightValue = 16;
    }
}

void Player::gravity() {
    if (!big) {
        if (player.getPosition().y + 32 < 1100) {
            if (v.y < maxSpeed) {
                v.y += 100;
            }
        }
        if (alive) { //This is useful to check if big mario has fallen
            if (player.getPosition().y + 32 >= 1200) {
                player.setPosition(player.getPosition().x, 1200 - 32);
                jumping = false;
                alive = false;
            }
        }
    }
    else {
        if (player.getPosition().y + 64 < 1100) {
            if (v.y < maxSpeed) {
                v.y += 100;
            }
        }
        if (alive) { //This is useful to check if small mario has fallen
            if (player.getPosition().y + 64 >= 1200) {
                player.setPosition(player.getPosition().x, 1200 - 64);
                jumping = false;
                alive = false;
            }
        }
    }
}

还有碰撞函数,其中的块类在块精灵周围有4个小块,可简化碰撞:

void Player::collisions(Block* block) {
    if (this->player.getGlobalBounds().intersects(block->up.getGlobalBounds())) {
        if (!big) {
            if (this->player.getPosition().y + heightValue <= block->block.getPosition().y) {
                this->player.setPosition(this->player.getPosition().x, block->up.getPosition().y - 32);
                v.y = 0;
                jumping = false;
                score = 100;    
            }
        }
        else {
            if (this->player.getPosition().y + heightValue <= block->block.getPosition().y) {
                this->player.setPosition(this->player.getPosition().x, block->up.getPosition().y - 64);
                v.y = 0;
                jumping = false;
                score = 100;            
            }
        }
    }
    if (this->player.getGlobalBounds().intersects(block->down.getGlobalBounds())) {
        this->player.setPosition(this->player.getPosition().x, block->down.getPosition().y + 1);
        v.y = 0;
    }
    if (this->player.getGlobalBounds().intersects(block->left.getGlobalBounds()) && v.x > 0) {
        this->player.setPosition(block->left.getPosition().x - 32, this->player.getPosition().y);
    }
    else if (this->player.getGlobalBounds().intersects(block->right.getGlobalBounds()) && v.x < 0) {
        this->player.setPosition(block->right.getPosition().x + 1, this->player.getPosition().y);
    }
}

我希望我能正确解释问题。

c++ game-physics sfml collision
1个回答
0
投票

只需一次在一个轴上进行碰撞检测,就可以沿瓷砖平滑移动

伪代码示例:

© www.soinside.com 2019 - 2024. All rights reserved.