0

所以我让我的碰撞检测工作,但由于某种原因,玩家似乎进入地面 1 帧,然后在下一帧跳出它,然后按预期跌落。

我用于碰撞检测的代码

        void DoCollisions()
    {
        onGround = false;
        Position.Y += Velocity.Y;
        Vector2 tileCollision = GetTileCollision();
        if (tileCollision.X != -1 || tileCollision.Y != -1)
        {
            onGround = true;
            Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
                new Rectangle(
                    tileCollision.X * World.tileEngine.TileWidth,
                    tileCollision.Y * World.tileEngine.TileHeight,
                    World.tileEngine.TileWidth,
                    World.tileEngine.TileHeight
                )
            );
            Position.Y -= Velocity.Y;
            Velocity.Y = 0;
            Position.Y += collisionDepth.Y;

        }

        Position.X += Velocity.X;
        tileCollision = GetTileCollision();
        if (tileCollision.X != -1 || tileCollision.Y != -1)
        {
            Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
                new Rectangle(
                    tileCollision.X * World.tileEngine.TileWidth,
                    tileCollision.Y * World.tileEngine.TileHeight,
                    World.tileEngine.TileWidth,
                    World.tileEngine.TileHeight
                )
            );
            Position.X -= Velocity.X;
            Velocity.X = 0;
            Position.X += collisionDepth.X;

        }
    }

    Vector2 GetTileCollision()
    {
        int topLeftTileX = (int)(CollisionRectangle.TopLeft.X / World.tileEngine.TileWidth);
        int topLeftTileY = (int)(CollisionRectangle.TopLeft.Y / World.tileEngine.TileHeight);
        int BottomRightTileX = (int)(CollisionRectangle.DownRight.X / World.tileEngine.TileWidth);
        int BottomRightTileY = (int)(CollisionRectangle.DownRight.Y / World.tileEngine.TileHeight);

        for (int i = topLeftTileX; i <= BottomRightTileX; i++)
        {
            for (int j = topLeftTileY; j <= BottomRightTileY; j++)
            {
                if (World.tileEngine.TileIsSolid(i, j))
                {
                    return new Vector2(i,j);
                }
            }
        }

        return new Vector2(-1,-1);
    }
4

1 回答 1

2

仅以Y方向为例,乍一看似乎您正在调整Y两次,一次是在碰撞检查之前,然后当您遇到碰撞时,您会恢复Y并将对象推出collisionDepth

您可能不应该恢复Y,如果 collisionDepth.Y 表明您正在与地面相交,您应该将您的对象向后推深度量,这会使您与地面齐平。

        if (tileCollision.X != -1 || tileCollision.Y != -1)
        {
            onGround = true;
            Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
                new Rectangle(
                    tileCollision.X * World.tileEngine.TileWidth,
                    tileCollision.Y * World.tileEngine.TileHeight,
                    World.tileEngine.TileWidth,
                    World.tileEngine.TileHeight
                )
            );
            Velocity.Y = 0;
            // Depending on your coordinate system this should
            // be either + or - collisionDepth.Y
            Position.Y += collisionDepth.Y;
        }
于 2011-01-14T18:28:19.367 回答