0

我正在尝试编写一个函数来检查碰撞对象以避免这种情况

该函数需要检查一个对象是否与其他对象相交:

 static bool CheckCollision(Int32 X, Int32 Y, Int32 Z, Int32 SizeX, Int32 SizeY, Int32 SizeZ)
    {
        bool ret = false;


        for (int i = 0; i < ObjList.Count ; i++ )
        {

            if (Z > ObjList[i].Z + ObjList[i].SizeZ || Z + SizeZ < ObjList[i].Z)
                Console.WriteLine("Z +-");
            else if (Y + SizeY < ObjList[i].Y || Y > ObjList[i].Y + ObjList[i].SizeY)
                Console.WriteLine("Y +-");
            else if (X + SizeX < ObjList[i].X || X > ObjList[i].X + ObjList[i].SizeX)
                Console.WriteLine("X +-");
            else
            {
                Console.WriteLine("||");
                ret = true;
                break;
            }


        }


     //   Console.Write("\n" + ret+"\n");


            return ret;
    }

ObjList - 对象数据列表:

    private struct S_ObjList
{
    public Int32 X;
    public Int32 Y;
    public Int32 Z;
    public Int32 SizeX;
    public Int32 SizeY;
    public Int32 SizeZ;
}

static List<S_ObjList> ObjList = new List<S_ObjList>();

http://pastebin.com/abDZLk9N - 所有代码。

CheckCollision无法正常工作。

例子

这个函数也不起作用(总是返回 true)

        static bool CheckCollision(Int32 X, Int32 Y, Int32 Z, Int32 SizeX, Int32 SizeY, Int32 SizeZ)
    {
        foreach (S_ObjList MyObject in ObjList)
        {

            if ((Z + SizeZ > MyObject.Z || Z < MyObject.Z + MyObject.SizeZ) && (X + SizeX > MyObject.X || X < MyObject.X + MyObject.SizeX) && (Y + SizeY > MyObject.Y || Y < MyObject.Y + MyObject.SizeY))
            {

                return true;

            }

        }
        return false;

    }
4

2 回答 2

1

您可以将X, Y, Z坐标转换为Vector3

[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 12)]
public struct Vector3 : IFormattable
{
    /// <summary>
    /// The X coordinate.
    /// </summary>
    public float X;

    /// <summary>
    /// The Y coordinate.
    /// </summary>
    public float Y;

    /// <summary>
    /// The Z coordinate.
    /// </summary>
    public float Z;

    /// <summary>
    /// Initializes a new <see cref="Vector3"/> instance.
    /// </summary>
    /// <param name="x">The X coordinate.</param>
    /// <param name="y">The Y coordinate.</param>
    /// <param name="z">The Z coordinate.</param>
    public Vector3(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }
}

然后为每个对象创建一个所谓的BoundingBox(或基本上围绕它的圆圈),其中包含对象的中心作为 aVector和半径作为 a float

/// <summary>
/// 
/// </summary>
public class SphericalObstacle
{
    private readonly float _radius;
    private readonly Vector3 _center;
    private readonly ulong _vehicleId;

    /// <summary>
    /// Initializes a new instance of the <see cref="SphericalObstacle"/> class.
    /// </summary>
    /// <param name="center">The center.</param>
    /// <param name="radius">The radius.</param>
    /// <param name="parentVehicleId">The parent vehicle id.</param>
    public SphericalObstacle(Vector3 center, float radius, ulong parentVehicleId)
    {
        _radius = radius;
        _center = center;
        _vehicleId = parentVehicleId;
    }

    /// <summary>Gets the vehicle id.</summary>
    public ulong VehicleId { get { return _vehicleId; } }

    /// <summary>Gets the radius.</summary>
    public float Radius { get { return _radius; } }

    /// <summary>Gets the center.</summary>
    public Vector3 Center { get { return _center; } }

    /// <summary>
    /// Checks for sphere collision.
    /// </summary>
    /// <param name="obstacle">The obstacle.</param>
    /// <param name="tolerance">The tolerance.</param>
    /// <returns>
    ///   <c>true</c> if it collides, <c>false</c> otherwise.
    /// </returns>
    public bool CollidesWith(SphericalObstacle obstacle, double tolerance = 0.0d)
    {
        Vector3 difference = Center - obstacle.Center;
        double distance =
            System.Math.Sqrt(System.Math.Pow(difference.X, 2) + System.Math.Pow(difference.Y, 2) + System.Math.Pow(difference.Z, 2));

        double sumRadius = Radius + obstacle.Radius;
        return distance < (sumRadius + tolerance);
    }

    public override string ToString()
    {
        return string.Format("Radius: {0}, Center: {1}", _radius, _center);
    }
}

如果您对所有要检查碰撞的对象进行此操作,那么您实际上只是在检查围绕它们绘制的圆圈是否相互碰撞。

从您的图片中,我无法完全确定您是否需要球体或边界框,但如果您需要边界框,您几乎可以应用相同的概念,但数学略有不同。顺便说一句,GameDev.net 是这类东西的好来源。

于 2013-07-16T12:43:06.443 回答
0

http://pastebin.com/abDZLk9N - 工作代码:) 只更改了几行代码

于 2013-07-16T12:58:29.120 回答