当玩家超过1个街区时,碰撞会抽搐

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

我正在使用Monogame编码的基于2D平铺的游戏中编码碰撞。我遇到了一个问题,如果我的玩家一次站在2个牌上,它会开始抽搐,因为碰撞解决了2次,我希望这只能跑一次。我该怎么做?这是我的代码:

    public void HandleCollisions()
    {
        Rectangle plyRect = ply.GetBounds(); //get player rectangle
        Vector2 currentPos = new Vector2(plyRect.X, plyRect.Y); //get player current position
        Vector2 xy = new Vector2(   (float)Math.Floor((ply.pos.X + ply.tex.Width) / world.tileSize), 
                                    (float)Math.Floor((ply.pos.Y + ply.tex.Height) / world.tileSize)); //get tiles position based on player position
        for (int x = (int)xy.X - 4; x <= (int)xy.X + 4; x++) //run through tiles near the player
        {
            for (int y = (int)xy.Y - 4; y <= (int)xy.Y + 4; y++)
            {
                if (x >= 0 && y >= 0 && x < world.GetWorldSize().X && y < world.GetWorldSize().Y) //check if tiles are within map
                {
                    if (world.tiles[x, y] != null) 
                    {
                        if (world.tiles[x, y].collision == Tile.Collision.SOLID) //check if tile is solid
                        {
                            Rectangle tileRect = world.tiles[x, y].GetRect(); //get the tiles rectangle

                            if (plyRect.Intersects(tileRect)) //check if intersecting
                            {
                                Vector2 depth = RectangleExtension.GetIntersectionDepth(plyRect, tileRect); //get intersecting depth

                                if (depth != Vector2.Zero)
                                {
                                    float absDepthX = Math.Abs(depth.X);
                                    float absDepthY = Math.Abs(depth.Y);

                                    if (absDepthY < absDepthX)
                                    {
                                        currentPos = new Vector2(currentPos.X, currentPos.Y + depth.Y); //resolve Y collision first
                                    }
                                    else
                                    {
                                        currentPos = new Vector2(currentPos.X + depth.X, currentPos.Y); //then resolve X collision
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        ply.pos = currentPos; //set player position after the checking is done
    }
c# collision monogame
1个回答
1
投票

一旦检测到一个碰撞,使用休息时突破for循环;声明

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