【发布时间】:2017-03-26 04:06:09
【问题描述】:
我有一个精灵,Player。我通过以下方式更新Player 的位置:
_position += (_direction * _velocity) * (float)gameTime.ElapsedGameTime.TotalSeconds;
其中_position、_direction 和_velocity 是Vector2s。
我有一个简单的碰撞器 (BoxCollider),它根据碰撞器的位置和尺寸简单地生成一个矩形 (BoundingBox)。
在Initialize() 中,我创建了一个新的List<BoxCollider> 并为其填充关卡的碰撞器。
在Update() 上,我将列表传递给Player 以检查冲突。
碰撞检查方法:
public void CheckPlatformCollision(List<BoxCollider> colliders, GameTime gameTime)
{
var nextPosition = _position + (_direction * _velocity) * (float)gameTime.ElapsedGameTime.TotalSeconds;
Rectangle playerCollider = new Rectangle((int)nextPosition.X, (int)nextPosition.Y, BoundingBox.Width, BoundingBox.Height);
foreach(BoxCollider collider in colliders)
{
if(playerCollider.Intersects(collider.BoundingBox))
{
nextPosition = _position;
}
}
Position = nextPosition;
}
现在,我尝试实现重力的所有方法都失败了。如果Player 从太高掉下来,nextPosition 会变得离Player 太远,并让它卡在半空中。
我也遇到了水平碰撞问题,问题类似:Player 停止得太快,在两者之间留有空间。有时我让Player 贴在对撞机的一侧。
我想知道的是:
如何使用_position、_direction 和_velocity 正确实现重力和跳跃?如何正确处理水平和垂直碰撞?
【问题讨论】:
-
也许这会对你有所帮助:gamedev.stackexchange.com/questions/104954/…
标签: c# xna 2d game-physics monogame