【问题标题】:The collision work but I don't know how know the collsiion direction碰撞工作,但我不知道如何知道碰撞方向
【发布时间】:2017-03-01 13:06:46
【问题描述】:

我已经与我的地图发生碰撞,碰撞工作,但是当我的玩家发生碰撞时,我停止了玩家的移动并且他不会走路,因为我不知道如何阻止玩家向一个方向移动

这是我在播放器和地图下的代码冲突

public bool IsCollisionTile(Rectangle player)
{
    foreach(Rectangle rect in this._collisionObject)
    {
        if (rect.Intersects(player))
        {
            return true;
        }
    }
    return false;
}

玩家不移动的代码在 Game1 类中

if (mapLoader.IsCollisionTile(player.getDestinationRect()))
{
        player.setCantWalk(true);
}

是Player.cs类中的update和setCantWalk函数

private bool _cantWalk;

    public void Update()
    {
    this._destinationRectangle.X = (int)_position.X;
    this._destinationRectangle.Y = (int)_position.Y;
    this._destinationRectangle.Width = _texture.Width;
    this._destinationRectangle.Height = _texture.Height;
    if(Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                if (_cantWalk == false)
                {
                    _position.Y--;
                }
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                if (_cantWalk == false)
                {
                    _position.Y++;
                }
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                if (_cantWalk == false)
                {
                    _position.X++;
                }
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                if (_cantWalk == false)
                {
                    _position.X--;
                }
            }

        }

        public void setCantWalk(bool walk)
        {
            _cantWalk = walk;
        }

想帮我

【问题讨论】:

  • 您可以尝试在 X 轴上移动播放器,进行碰撞检查,然后在 Y 轴上移动并再次进行检查,然后还原导致检查的任何一个...不看起来很理想,但这是一种选择
  • 您好,我是初学者,不知道怎么做

标签: c# collision-detection monogame tiled


【解决方案1】:

对于更简单的方法,您需要记录对象的移动。因此,在检查重叠时,您知道某个方向上的什么运动导致了重叠。然后,为该对象设置变量 cantWalkX 或 cantWalkY。

更好的解决方案是检查碰撞表面以确定障碍物。这要求您更改相交例程以返回相交对象,并根据它们的相对位置阻止沿该方向移动。例如,如果碰撞对象在您的对象的右侧,则设置 cantWalkXPositive。如果左边还有另一个,将 cantWalkXNegative 设置为 true 等...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多