简体   繁体   English

与SFML的C ++播放器墙碰撞

[英]c++ player-wall collision with SFML

My friend and I are developing a top down puzzle game. 我和我的朋友正在开发自上而下的益智游戏。
We have already implemented a player class that can walk and collide the walls, but the problem is after the collision itself. 我们已经实现了可以行走并碰撞墙壁的播放器类,但是问题出在碰撞本身之后。

For example: 例如:
When the player hits a wall to its right, we set the velocity on the x axis to be 0 , and the player stops. 当玩家击中右侧的墙时,我们将x轴上的速度设置为0 ,然后玩家停止。 But after that, if the user presses the up key, the player moves one pixel up, and then stops again. 但是之后,如果用户按下向上键,则播放器将向上移动一个像素,然后再次停止。
After that if you keep pressing right and up alternately, the player is walking through the wall pixel after pixel. 此后,如果您一直交替向右和向上按,则播放器将逐个像素地穿墙。

The player moves through the wall 玩家穿过墙壁

And here is the collision detection of the player: 这是播放器的碰撞检测:

for (int i = 0; i < lvl.getSizeInTiles().x; i++) {
        for (int j = 0; j < lvl.getSizeInTiles().y; j++) {
            if (lvl.getTileFromBuffer(i, j).getType() == "wall") {
                if (intersects(lvl.getTileFromBuffer(i, j).getPosition().x, lvl.getTileFromBuffer(i, j).getPosition().y, lvl.getTileFromBuffer(i, j).getSize().x, lvl.getTileFromBuffer(i, j).getSize().y)) {
                    if (faceDirection == "UP" && vel.y < 0) {
                        collidesUp = true;
                    }
                    else {
                        collidesUp = false;
                    }

                    if (faceDirection == "DOWN" && vel.y > 0) {
                        collidesDown = true;
                    }
                    else {
                        collidesDown = false;
                    }

                    if (faceDirection == "LEFT" && vel.x < 0) {
                        collidesLeft = true;
                    }
                    else {
                        collidesLeft = false;
                    }

                    if (faceDirection == "RIGHT" && vel.x > 0) {
                        collidesRight = true;
                    }
                    else {
                        collidesRight = false;
                    }
                }
            }
        }
    }
}
if (vel.x > 0 && !collidesRight) {
    pos.x += vel.x;
}
if (vel.x < 0 && !collidesLeft) {
    pos.x += vel.x;
}
if (vel.y > 0 && !collidesDown) {
    pos.y += vel.y;
}
if (vel.y < 0 && !collidesUp) {
    pos.y += vel.y;
}

Just a quick note: The intersect(x, y, w, h) function is a part of the player class, and tell us if you need to see it. 简要说明一下: intersect(x, y, w, h)函数是播放器类的一部分,请告诉我们是否需要查看它。

We will be happy if you send a reply about our code, or link us to a place we can find a solution. 如果您发送有关我们代码的回复,或将我们链接到我们可以找到解决方案的地方,我们将非常高兴。

So thank you for any help, and please ask questions if you need to, Arad and Ron. 因此,感谢您的任何帮助,如果需要,请问Arad和Ron。

Just a guess with the info provided, but notice that when you go through the wall, you're character isn't actually facing to the right. 只是对提供的信息进行了猜测,但请注意,当您穿墙而出时,您所扮演的角色实际上并没有面朝右边。 It's facing up, so collidesRight will be false. 它面朝上,所以collidesRight将为假。 Instead of inhibiting movement based on which direction your character is facing, inhibit solely based on position ie 与其仅根据角色所面对的方向来禁止移动,不如仅根据位置来进行抑制,即

if (intersecting_right(player, wall) && player.x_vel > 0)
    player.x_vel = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM