2011-08-02 2 views
1

나는 확실히 고통스럽게 분명하지만 문제는 여기에 뭔가 내려다 보이는 해요 OK :로 영역 내 프로젝트에서충돌 항상이 충돌하지 않아야 때도 감지

나는 충돌의 두 가지 유형을 사용하고 있습니다를 구 및 상자 상자. 두 가지 모두 동일한 문제가 발생합니다. 그들은 항상 두 대상 간의 충돌을 감지합니다.

 BoundingBox bb; 

나는 또한 모델에 대한 BoundingBox의를 생성하는 방법을 가지고 BB 정의하는 것을 사용 :

 public void Initialize() 
     { 
      bb = CreateBoundingBox(); 
     } 


    protected BoundingBox CalculateBoundingBox() 
    { 

     Vector3 modelMax = new Vector3(float.MinValue, float.MinValue, float.MinValue); 
     Vector3 modelMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); 
     transforms = new Matrix[model.Bones.Count]; 

     foreach (ModelMesh mesh in model.Meshes) 
     { 
      Vector3 meshMax = new Vector3(float.MinValue, float.MinValue, float.MinValue); 
      Vector3 meshMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); 

      foreach (ModelMeshPart part in mesh.MeshParts) 
      { 
       int stride = part.VertexBuffer.VertexDeclaration.VertexStride; 

       byte[] vertexData = new byte[stride * part.NumVertices]; 
       part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, 1); // fixed 13/4/11 

       Vector3 vertPosition = new Vector3(); 
       for (int ndx = 0; ndx < vertexData.Length; ndx += stride) 
       { 
        vertPosition.X = BitConverter.ToSingle(vertexData, ndx); 
        vertPosition.Y = BitConverter.ToSingle(vertexData, ndx + sizeof(float)); 
        vertPosition.Z = BitConverter.ToSingle(vertexData, ndx + sizeof(float) * 2); 

        meshMin = Vector3.Min(meshMin, vertPosition); 
        meshMax = Vector3.Max(meshMax, vertPosition); 
       } 
      } 

      meshMin = Vector3.Transform(meshMin, transforms[mesh.ParentBone.Index]); 
      meshMax = Vector3.Transform(meshMax, transforms[mesh.ParentBone.Index]); 

      modelMin = Vector3.Min(modelMin, meshMin); 
      modelMax = Vector3.Max(modelMax, meshMax); 
     } 

     return new BoundingBox(modelMin, modelMax); 

    } 

그때 만들어 내 baseGameObject 클래스의

나는 경계 상자를 선언 내 충돌을 위해 bb를 사용하는 방법.

public bool BoxCollision(BoundingBox secondBox) 
    { 
     if (bb.Intersects(secondBox)) 
      return true; 
     else    
      return false; 
    } 

마지막으로 충돌 감지를 결정하는 방법을 사용합니다.

public void CollisionCheck() 
    { 
     foreach (NonPlayerChar npc in npcList) 
     { 
      if(player.SphereCollision(npc.model, npc.getWorldRotation())) 
      { npc.position = vector3.Zero; } 

      if (player.BoxCollision(npc.bb)) 
      { npc.position = vector3.Zero; }     
     } 

    } 

위치가 충돌했는지 확인하는 테스트였습니다. 개체 위치를 임의의 위치로 설정할 수 있으며 충돌은 여전히 ​​감지됩니다. 경계 구 충돌에 대해 동일한 문제가 있습니다.

public bool SphereCollision(Model secondModel, Matrix secondWorld) 
    { 
     foreach (ModelMesh modelMeshes in model.Meshes) 
     { 
      foreach (ModelMesh secondModelMesh in secondModel.Meshes) 
      { 
       if(modelMeshes.BoundingSphere.Transform(getWorldRotation()).Intersects(secondModelMesh.BoundingSphere.Transform(secondWorld))) 
        return true; 
      } 
     } 
     return false; 
    } 

내가 뭘 잘못하고 있는지 아는 사람이 있습니까? 할 수있는 가장 쉬운 것들의

+0

'BoxCollision' 함수는 위치를 기반으로하지 않았거나 잘못 읽은 것 같습니다. (이 경우 아마도 (0,0,0)을 중심으로 두 개의 상자가 충돌하는지 ...) –

+0

그리고 'SphereCollision'은 현재 false를 반환 할 수 있습니까? –

+0

우리는 bb.Intersect 메서드에 코드를 볼 수 있습니까? ..? – Olle89

답변

1

하나는 .. 모두 당신의 구체를 잡고 좌표가 비교

예이다

sphere1_x = sphere.x; 
sphere2_x = sphere2.x; 
width of sphere = 2 for example; 

if ((sphere1_x + width/2) > (sphere2_x + width/2)) 
system.out.writeline("collison"); 
else if (sphere1_x (check for other ways of connecting between the coordinate system x>x2, x<x2, y>y2, y<y2 etc) 
else 
system.out.writeline("no collision") 

그렇다면 정말 위의 코드를 리팩토링 할 수 있습니다. 아마도 구체 앞에 먼저 경계 상자를 만드는 것이 더 쉽습니다.

+0

@ olle89- intersect 메서드는 내가 작성하지 않은 프레임 워크의 일부이며 잘못된 것은 없습니다. – TheVeryStupid

+1

아, 꽤 오랫동안 XNA를 사용하지 못했습니다 ... 내 방법은 C++에서 OSG를 사용하는 방법이었습니다 ... – SD1990

+0

@ Spartan- 기본적으로 교차 메서드가 이미 수행 한 작업이 아닙니까? '현재의 boundingShape이 다른 boundingShape와 교차하는지 확인합니다 – TheVeryStupid