5

我正在开发一个新游戏,并试图检测玩家(在斜坡上)是否根据他们相对于斜坡坐标的坐标与给定的网格发生碰撞。我正在使用这个功能,它似乎不起作用(斜率似乎太小或什么的)

//Slopes



        float slopeY = max.Y-min.Y;

    float slopeZ = max.Z-min.Z;

    float slopeX = max.X-min.X;

    float angle = (float)Math.Atan(slopeZ/slopeY);

    //Console.WriteLine(OpenTK.Math.Functions.RadiansToDegrees((float)Math.Atan(slopeZ/slopeY)).ToString()+" degrees incline");



    slopeY = slopeY/slopeZ;

    float slopeZX = slopeY/slopeX;

    //End slopes

    float surfaceposX = max.X-coord.X;

    float surfaceposY = max.Y-coord.Y;

    float surfaceposZ = min.Z-coord.Z;



    min-=sval;

    max+=sval;

    //Surface coords



    //End surface coords



    //Y SHOULD = mx+b, where M = slope and X = surfacepos, and B = surfaceposZ



    if(coord.X<max.X& coord.X>min.X&coord.Y>min.Y&coord.Y<max.Y&coord.Z>min.Z&coord.Z<max.Z) {







        if(slopeY !=0) {

        Console.WriteLine("Slope = "+slopeY.ToString()+"SlopeZX="+slopeZX.ToString()+" surfaceposZ="+surfaceposZ.ToString());

            Console.WriteLine(surfaceposY-(surfaceposY*slopeY));



            //System.Threading.Thread.Sleep(40000);



        if(surfaceposY-(surfaceposZ*slopeY)<3 || surfaceposY-(surfaceposX*slopeZX)<3) {

        return true;

        } else {

        return false;

        }

        } else {

        return true;

        }



    } else {





    return false;

    }

有什么建议么?

样本输出:

59.86697
6.225558 2761.331
68.3019 度坡度
59.86698,46.12445
59.86698
6.225558 2761.332
0度坡度


不应该下 斜坡


编辑:部分解决了问题。斜坡检测有效,但现在我可以穿墙了???

//Slopes



        float slopeY = max.Y-min.Y;

    float slopeZ = max.Z-min.Z;

    float slopeX = max.X-min.X;

    float angle = (float)Math.Atan(slopeZ/slopeY);

    //Console.WriteLine(OpenTK.Math.Functions.RadiansToDegrees((float)Math.Atan(slopeZ/slopeY)).ToString()+" degrees incline");



    slopeY = slopeY/slopeZ;

    float slopey = slopeY+1/slopeZ;

    float slopeZX = slopeY/slopeX;

    //End slopes

    float surfaceposX = min.X-coord.X;

    float surfaceposY = max.Y-coord.Y;

    float surfaceposZ = min.Z-coord.Z;



    min-=sval;

    max+=sval;

    //Surface coords



    //End surface coords



    //Y SHOULD = mx+b, where M = slope and X = surfacepos, and B = surfaceposZ



    if(coord.X<max.X& coord.X>min.X&coord.Y>min.Y&coord.Y<max.Y&coord.Z>min.Z&coord.Z<max.Z) {







        if(slopeY !=0) {

        Console.WriteLine("Slope = "+slopeY.ToString()+"SlopeZX="+slopeZX.ToString()+" surfaceposZ="+surfaceposZ.ToString());

            Console.WriteLine(surfaceposY-(surfaceposY*slopeY));



            //System.Threading.Thread.Sleep(40000);

         surfaceposZ = Math.Abs(surfaceposZ);



        if(surfaceposY>(surfaceposZ*slopeY) & surfaceposY-2<(surfaceposZ*slopeY) || surfaceposY>(surfaceposX*slopeZX) & surfaceposY-2<(surfaceposX*slopeZX)) {

        return true;

        } else {

        return false;

        }

        } else {

        return true;

        }



    } else {





    return false;

    }
4

1 回答 1

2

您是否考虑过实现 BSP 树?即使您使用现在使用的代码解决了错误,对于任何大小/复杂度都不错的网格,它也会变得很慢。BSP 或四叉树将大大有助于简化代码和提高性能,而且它们很容易实现。

编辑

这是一个不错的 BSP 教程和概述的链接。

如果您只关心地形(没有垂直墙、门等),四叉树可能更合适:

这是 gamedev.net 上的一个不错的四叉树教程。

这两种算法都旨在将您的几何图形划分为一棵树,以便于搜索。在您的情况下,您正在搜索用于碰撞目的的多边形。构建 BSP 树(非常简单):

为树中的节点定义一个结构:

public class BspNode
{
    public List<Vector3> Vertices { get; set; }

    // plane equation coefficients
    float A, B, C, D;

    BspNode front;
    BspNode back;

    public BspNode(Vector3 v1, Vector3 v2, Vector3 v3)
    {
        Vertices = new List<Vector3>();
        Vertices.AddRange(new[] { v1, v2, v3 });
        GeneratePlaneEquationCoefficients();
    }

    void GeneratePlaneEquationCoefficients()
    {

        // derive the plane equation coefficients A,B,C,D from the input vertex list.
    }

    bool IsInFront(Vector3 point)
    {
        bool pointIsInFront=true;
        // substitute point.x/y/z into the plane equation and compare the result to D
        // to determine if the point is in front of or behind the partition plane.
        if (pointIsInFront && front!=null)
        {
            // POINT is in front of this node's plane, so check it against the front list.
            pointIsInFront = front.IsInFront(point);
        }
        else if (!pointIsInFront && back != null)
        {
            // POINT is behind this plane, so check it against the back list.
            pointIsInFront = back.IsInFront(point);
        }
        /// either POINT is in front and there are no front children,
        /// or POINT is in back and there are no back children. 
        /// Either way, recursion terminates here.
        return pointIsInFront;            
    }

    /// <summary>
    /// determines if the line segment defined by v1 and v2 intersects any geometry in the tree.
    /// </summary>
    /// <param name="v1">vertex that defines the start of the ray</param>
    /// <param name="v2">vertex that defines the end of the ray</param>
    /// <returns>true if the ray collides with the mesh</returns>
    bool SplitsRay(Vector3 v1, Vector3 v2)
    {

        var v1IsInFront = IsInFront(v1);
        var v2IsInFront = IsInFront(v2);
        var result = v1IsInFront!=v2IsInFront;

        if (!result)
        {
            /// both vertices are on the same side of the plane,
            /// so this node doesn't split anything. Check it's children.
            if (v1IsInFront && front != null)
                result =  front.SplitsRay(v1, v2);
            else if (!v1IsInFront && back != null)
                result = back.SplitsRay(v1, v2);
        }
        else
        {
            /// this plane splits the ray, but the intersection point may not be within the face boundaries.
            /// 1. calculate the intersection of the plane and the ray : intersection
            /// 2. create two new line segments: v1->intersection and intersection->v2
            /// 3. Recursively check those two segments against the rest of the tree.
            var intersection = new Vector3();

            /// insert code to magically calculate the intersection here.

            var frontSegmentSplits = false;
            var backSegmentSplits = false;


            if (front!=null)
            {
                if (v1IsInFront) frontSegmentSplits=front.SplitsRay(v1,intersection);
                else if (v2IsInFront) frontSegmentSplits=front.SplitsRay(v2,intersection);
            }
            if (back!=null)
            {
                if (!v1IsInFront) backSegmentSplits=back.SplitsRay(v1,intersection);
                else if (!v2IsInFront) backSegmentSplits=back.SplitsRay(v2,intersection);
            }

            result = frontSegmentSplits || backSegmentSplits;
        }

        return result;
    }
} 
  1. 从网格中选择一个“分区”平面(面),大致将网格的其余部分一分为二。这对于复杂的几何体来说要容易得多,因为完全凸的项目(球体等)往往看起来像列表而不是树。

  2. BspNode从定义分区平面的顶点创建一个新实例。

  3. 将剩余的面分类到两个列表中 - 一个位于分区平面的前面,另一个包含后面的那些面。
  4. 递归到步骤 2,直到列表中不再有节点。

在检查碰撞时,您有两个选择。

  1. 单点:通过调用根节点的方法检查角色或对象移动到树的坐标。.IsInFront(moveDestination)如果该方法返回 false,则目标点在网格“内部”,并且您已经发生碰撞。如果该方法返回 true,则目标点在网格“外部”,并且没有发生碰撞。

  2. 射线相交。这个有点棘手。.SplitsRay()使用对象的当前位置和目标位置调用根节点的方法。如果方法返回true,则在两个位置之间移动将通过网格过渡。这是一种高级(尽管更复杂)的检查,因为它会捕获边缘情况,例如当所需的移动将一个对象在一个步骤中完全通过一个对象时。

我只是快速地将示例代码放在一起;它不完整,甚至可能无法编译,但它应该让你朝着正确的方向前进。

BSP 的另一个优点:使用该.SplitsRay()方法,您可以确定地图上的一个点是否可以从另一个点看到。一些游戏使用它来确定 NPC/AI 是否可以看到彼此或真实玩家。您可以对其稍作修改,以确定他们是否可以听到彼此走路的声音等。

这可能看起来比您原来的方法复杂得多,但它最终更加强大和灵活。值得您花时间进行调查。

于 2011-04-25T18:08:14.433 回答