在2D平台游戏(冰冷塔)中实现碰撞

时间:2017-11-08 20:13:17

标签: c++ 2d collision allegro

我和这个人打了几天,我不知道怎么做,所以我想请你帮忙。我不知道碰撞在这里应该是什么样子,所以玩家可以跳过街区的“向下区域”,并保持在街区。

block.cpp

bool block::CollidingWithPlayer(character& player) { 
    for (int i = 1; i < MAX_BLOCKS; i++) {
        if (player.x + player.width >= coordinateX[i] && player.x <= coordinateX[i] + width[i] && player.y + player.height >= coordinateY[i] && player.y <= coordinateY[i] + block_height) {
            player.onGround = true;
            return true;
        }
    }
}

character.cpp

void character::startJump(map& Map, character& player) {
    if (onGround)
    {
        vel[1] = -11.0;
        onGround = false;
    }
}
void character::updateJump(block& Block, character& player) {
    if (!onGround) {
        Block.CollidingWithPlayer(player);
        vel[1] += 0.5;
        y += vel[1];
        x += vel[0];
    }
    if (y > 460){
        y = 460;
        vel[1] = 0.0;
        onGround = true;
        vel[0] = 0.0;
    }

    if ((x + width >= START_OF_RIGHT_WALL && x <= WALL_WIDTH + START_OF_RIGHT_WALL) || (x + width >= START_OF_LEFT_WALL &&x <= START_OF_LEFT_WALL + WALL_WIDTH)){
    vel[0] *= -1;
    bound = true;
    if (direction == 1)
        direction = 2;
    else if (direction == 2)
        direction = 1;
    }
}

0 个答案:

没有答案