2013-07-16 56 views
0

我試圖寫一個函數來檢查碰撞對象,以避免這種檢查的對象C#碰撞:3D(XYZ)

此功能需要檢查相交對象是否其他對象:

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無法正常工作。

EXAMPLE

此功能也不起作用(總是返回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; 

    } 
+0

你好傾倒你的代碼的幾行就更難讓我們來幫助你。請解釋如何使用完整的代碼片段來展示問題。 – Romoku

+0

我添加了一張圖片。 –

回答

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(或基本上圍繞我一個圓圈T),爲每個對象,其中包含對象的中心作爲Vector和半徑爲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 is a good source for stuff like this, by the way.