碰撞正在跳过某些四边形

时间:2016-08-16 22:24:21

标签: c# opengl game-physics opentk

我已经在OpenTK的修改版本中制定了我自己的碰撞系统。它通过运行foreach循环来检查游戏中的所有四边形(当某些东西移动时运行)并将它们的位置设置回到最后一帧的位置,即它是否与此帧相交。我可能没有解释得那么好,所以这里是SetXVelocity代码,当玩家向右移动时调用。

public void SetXVelocity(float x)
        {
            foreach (Quad quad in quads)
            {
                if (!quad.Equals(this))
                {
                    if (Intersects(quad))
                    {
                        Velocity.x = 0;
                        Position = OldPosition;
                        continue;
                    }
                    if (!Intersects(quad))
                    {
                        OldPosition = Position;
                        Velocity.x = x;
                        continue;
                    }
                }
                else
                {
                    OldPosition = Position;
                    continue;
                }
                OldPosition = Position;
                continue;
            }
        }

以下是Intersects的代码:

public bool Intersects(Quad quad)
        {
            #region
            if (TLCorner.x > quad.TLCorner.x && TLCorner.x < quad.BRCorner.x && TLCorner.y < quad.TLCorner.y && TLCorner.y > quad.BRCorner.y)
            {
                 return true;
            }
            if (BRCorner.x < quad.BRCorner.x && BRCorner.x > quad.TLCorner.x && BRCorner.y > quad.BRCorner.y && BRCorner.y < quad.TLCorner.y)
            {
                return true;
            }
            if (TRCorner.x < quad.TRCorner.x && TRCorner.x > quad.BLCorner.x && TRCorner.y < quad.TRCorner.y && TRCorner.y > quad.BLCorner.y)
            {
                return true;
            }
            if (BLCorner.x > quad.BLCorner.x && BLCorner.x < quad.TRCorner.x && BLCorner.y > quad.BLCorner.y && BLCorner.y < quad.TRCorner.y)
            {
                return true;
            }
            #endregion // Corner Intersection
            if (Math.Round(Left, 2) == Math.Round(quad.Right, 2) && TRCorner.y > quad.TRCorner.y && BRCorner.y < quad.BRCorner.y)
                return true;
            if (Math.Round(Right, 2) == Math.Round(quad.Left, 2) && TRCorner.y > quad.TRCorner.y && BRCorner.y < quad.BRCorner.y)
                return true;
            return false;
        }

顺便说一句,TRCorner是代表右上角等的Vector3。

最后一段代码是Quad类,请记住,实际的类是巨大的,所以我不会尝试包含所有类:

public Vector3 Velocity;
public Vector3 Position;
public int Index;
public VBO<Vector3> VertexData
    {
        get;
        set;
    }
    public VBO<int> VertexCount
    {
        get;
        set;
    }
    public VBO<Vector2> VertexTexture
    {
        get;
        set;
    }

出于某种原因,一些四边形没有碰撞,有些则没有碰撞。有些人甚至发生碰撞,让玩家坚持下去。

1 个答案:

答案 0 :(得分:0)

我认为有些四边形缺失,因为比较中没有涵盖所有可能的条件。

由于您&&满足您的所有条件,因此碰撞需要满足您的所有条件才能被检测到。

if (TLCorner.x > quad.TLCorner.x && TLCorner.x < quad.BRCorner.x && TLCorner.y < quad.TLCorner.y && TLCorner.y > quad.BRCorner.y)
        {
             return true;
        }

if( TLCorner.x == quad.TLCorner.x && TLCorner.y < quad.TLCorner.y && TLCorner.y > quad.BRCorner.y )矩形仍会发生碰撞但未被发现。

其他条件相同。

您可能希望在代码中使用>=<=来覆盖所有条件。

至于为什么它们会相互卡住,你的速度可能会将它们推入一个让它们永远碰撞的状态,因为你在它们相互进入之后而不是在边界重叠时检测到碰撞。

此外,您可能想要反转速度并且&#34;取消卡住&#34;检测到碰撞的方式不会使它们卡在其他附近的矩形中。

  

我强烈建议您使用更好的逻辑进行碰撞检测

链接:https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection

您可以在此处http://jsfiddle.net/knam8/

进行测试