2013-08-01 1 views
0

어떻게하면 큐브와 비행기 사이의 충돌을 할 수 있습니까? 비행기에서 구를 할 수는 있지만 비행기에서 큐브를 계산할 수는 없지만 x, y, z의 입방체와 비행기에서 탐지를 할 수 있지만 그것을 파악할 수 없습니다.비행기 엔티티에서 큐브 엔티티 충돌

여기 내 충돌 테스터 코드입니다. 여기

public static bool sphereAndSphere(Sphere a, Sphere b, ref Contact contact) 
    { 
     // Get the vector from the centre of particle B to the centre of particle A 
     Vector3 separationVector = b.Position - a.Position; 
     float sumOfRadii = a.Radius + b.Radius; 
     float distance = separationVector.Length(); 

     if (distance < sumOfRadii) 
     { 
      contact.contactPoint = a.Position + separationVector/2f; 
      separationVector.Normalize(); 
      contact.contactNormal = separationVector; 
      contact.penetrationDepth = sumOfRadii - distance; 

      return true; 
     } 

     return false; 
    } 

    // This assumes that the Origin is in the centre of world 
    // and that the planes are the boundaries of the world and that all rigid-bodies are within the boundaries 
    public static bool sphereAndPlane(Sphere a, PlaneEntity b, ref Contact contact) 
    { 
     // Depth of sphere into plane (if negative, no collision) 
     float depth = Vector3.Dot(a.Position, b.DirectionFromOrigin) + a.Radius + b.OffsetFromOrigin; 

     if (depth > 0) 
     { 
      contact.contactPoint = a.Position + (a.Radius - depth) * b.DirectionFromOrigin; 
      contact.contactNormal = -b.DirectionFromOrigin; 
      contact.penetrationDepth = depth; 

      return true; 
     } 

     return false; 
    } 

상자

public static bool sphereAndBox(Sphere a, Cube b, ref Contact contact) 
    { 
     Vector3 relativePoint = Vector3.Transform(a.Position, Matrix.Invert(b.WorldTransform)); 

     // Early out check, based on separation axis theorem 
     if (Math.Abs(relativePoint.X) - a.Radius > b.halfSize.X 
      || Math.Abs(relativePoint.Y) - a.Radius > b.halfSize.Y 
      || Math.Abs(relativePoint.Z) - a.Radius > b.halfSize.Z) 
      return false; 

     Vector3 closestPoint = Vector3.Zero; 

     closestPoint.X = MathHelper.Clamp(relativePoint.X, -b.halfSize.X, b.halfSize.X); 
     closestPoint.Y = MathHelper.Clamp(relativePoint.Y, -b.halfSize.Y, b.halfSize.Z); 
     closestPoint.Z = MathHelper.Clamp(relativePoint.Z, -b.halfSize.Z, b.halfSize.Z); 

     float distance = (closestPoint - relativePoint).LengthSquared(); 

     if (distance < a.Radius * a.Radius) 
     { 
      contact.contactPoint = Vector3.Transform(closestPoint, b.WorldTransform); 
      contact.contactNormal = a.Position - contact.contactPoint; 
      contact.contactNormal.Normalize(); 
      contact.penetrationDepth = a.Radius - (float)Math.Sqrt(distance); 
      return true; 
     } 

     return false; 
} 
+1

모든 꼭지점이 평면의 같은면에 있으면 충돌이 없음 – Blau

+0

ok..i 내 평면이 vector3 (0, -2,0)이고 내 큐브가 vector3 (0 , 5,0), 그 공중에 그리고 드롭 다운 .. 어떻게 비행기 충돌 테스트를 큐브 안타 때? – user2640299

답변

0

에 큐브에 대한 테스트 비행기 또는 큐브 중 하나가 축 (폭, 길이, 높이)와 일치하지 않는 큐브 - 또는 정렬되지 않은 경우, 당신은 발견 할 것이다 큐브의 각 모서리 위치를 큐브 전체가 아닌 평면에 대해 개별적으로 테스트하는 것이 가장 쉽습니다. 그런 다음 첫 번째 모서리가 비행기를 가로 지르면 충돌을 등록하십시오.

평면과 축이 모두 축으로 정렬 된 경우 큐브가 대칭 (WLH) 인 경우 큐브를 반경이 큐브 너비의 절반 인 구로 생각하십시오.

관련 문제