Monogame碰撞bug

时间:2017-07-27 20:50:50

标签: c# xna game-physics monogame

我最近开始使用Monogame和C#创建一个平台游戏,并且遇到了一个奇怪的地面碰撞错误。我制作了块和玩家,都使用这样的边界碰撞:

public Rectangle Collision
{
    get => new Rectangle((int)_pos.X, (int)_pos.Y, _w, _h);
}
//Only for player entity
public Rectangle GroundCollision
{
    get => new Rectangle((int)_pos.X, (int)_pos.Y+_w, _w, 1);
}

它很好用,但有时空间会出现空间层和地面:

this

块和播放器纹理都是白色的,但是1像素的距离是可见的。经过一些跳跃(有时带动)可以修复它,但是在它再次开始盘旋之后。这是播放器更新代码:

public void Update()
{
    if (_movable)
    {
        Vector2 oldPos = _pos;
        _pos += new Vector2(_vel, -_acc);
        if(_pos.Y >= 96)
        {
            bool c = Collides;
        }
        if (Grounded)
        {
            _pos += new Vector2(0, _acc);
            _acc = 0;
        }
        /*if (Collides)
        {
            _pos = oldPos;
        }*/
    }
}

//Entity Methods
protected void _Move(float dir, float delta)
{
    _vel = (float) _speed * dir * delta;
}

protected void _Gravity(float delta)
{
    float fallspeed = (float) Math.Round(Phys.gravity * delta * -5f);
    if (_acc - fallspeed >= 0)
        _acc = fallspeed;
    else if (_acc >= 0)
        _acc = 0;
}

protected void _Jump(float delta)
{
    _acc += _jumpForce * delta * 10f;
}

那么,有什么方法可以解决这个问题吗?

0 个答案:

没有答案