【发布时间】:2011-10-15 19:40:45
【问题描述】:
问题: Problem http://img684.imageshack.us/img684/5486/prob1b.png Problem http://img810.imageshack.us/img810/3157/prob3.png 绿色方块是玩家,灰色方块是静态砖
当玩家跳到一块瓷砖上时,当玩家站在碰撞瓷砖的边缘附近时,它会被推向左/右。这可能是因为向上的跳跃速度大于 x 运动(甚至可能为 0),因此玩家在 x 轴上被校正...
伪代码如下:
- 1:在玩家周围获得碰撞瓷砖
- 2:foreach 碰撞瓷砖:
{
- 2a:获取交点深度
- 2b:用最小校正轴校正玩家位置 (例如:如果 (Math.Abs(X) > Math.Abs(Y) 则更正 Y 值。否则 正确的 X 值)
// 下面是一些其他不应该成为问题的伪代码:
- 2c:如果碰撞发生在玩家的顶部:取消任何跳跃
- 2d:如果碰撞发生在播放器的底部: IsStandingOnGround = true;
}
一些代码:
foreach (Stone stone in collidingStones)
{
#region Collision Response
if (DrawRect.Intersects(stone.CollisionRect)) // DrawRect is the players collision rectangle
{
//warning bug is in this if-statement. causes the player to be adjusted and go trough neighboar tiles and such
Vector2 correction = Collision.CalculateMinimumTranslationDistance(DrawRect, stone.CollisionRect);
if (Math.Abs(correction.X) > Math.Abs(correction.Y))
{
Location += new Vector2(correction.X, 0);
}
else
{
Location += new Vector2(0, correction.Y);
}
【问题讨论】:
标签: c# xna 2d response collision