2013-08-14 3 views
0

나는 게임에서 벽에 충돌하고 있으며 현재 내가 벽에 걸린 채 붙어 있습니다. 나는 벽에 내 캐릭터 슬라이드를 만들려고하지만 여전히 충돌합니다.3D 벽 슬라이딩 충돌

내 캐릭터가 자신이 직면 한 각도로 작성한 벡터에서 벗어납니다. 이것은 내 업데이트입니다

private static bool CheckForCollisions(ref Crate c1, ref Player c2,bool direction) 
    { 
     for (int i = 0; i < c1.model.Meshes.Count; i++) 
     { 
      // Check whether the bounding boxes of the two cubes intersect. 
      BoundingSphere c1BoundingSphere = c1.model.Meshes[i].BoundingSphere; 
      c1BoundingSphere.Center += c1.position + new Vector3(2, 0, 2); 
      c1BoundingSphere.Radius = c1BoundingSphere.Radius/1.5f; 

      for (int j = 0; j < c2.model.Meshes.Count; j++) 
      { 
       BoundingSphere c2BoundingSphere = c2.model.Meshes[j].BoundingSphere; 
       if (direction) 
        c2BoundingSphere.Center += c2.position + new Vector3(c2.getPlannedDirection().X, 0, 0); 
       else if (!direction) 
        c2BoundingSphere.Center += c2.position + new Vector3(0, 0, c2.getPlannedDirection().Y); 
       //c2BoundingSphere.Center += c2.position; 

       if (c1BoundingSphere.Intersects(c2BoundingSphere)) 
       { 
        return true; 
       } 
      } 
     } 
     return false; 
    } 

:

내 충돌 기능입니다

for (int x = 0; x <= 29; x++) 
     { 
      for (int y = 0; y <= 29; y++) 
      { 
       if (crate[x, y].getType() == 11 && collisionEnabled) 
       { 
        if (CheckForCollisions(ref crate[x, y], ref player,true)) 
        { 
         player.clearPlannedDirectionX(); 
         //Console.Write(player.getPosition().X + "," + player.getPosition().Y + "," + player.getPosition().Z); 
         //movePlayer = false; 
        } 
        if (CheckForCollisions(ref crate[x, y], ref player,false)) 
        { 
         player.clearPlannedDirectionZ(); 
         //Console.Write(player.getPosition().X + "," + player.getPosition().Y + "," + player.getPosition().Z); 
         //movePlayer = false; 
        } 
       } 
      } 
     } 
+0

getPlannedDirection()을 사용하고있다. .X 및 getPlannedDirection(). 첫 번째에서 Y 코드를 사용하고 있지만 clearPlannedDirectionX() 및 clearPlannedDirectionZ()를 두 번째로 사용하고 있습니다. –

+0

예, z 축에 충돌이 발생하면 멈추게됩니다. x 축과 동일합니다. 이것은 플레이어가 위치를 업데이트하기 전에 모두 실행됩니다. 그것이 "PlannedDirection"이라고 불리는 이유입니다. 미안하지만 내 질문에 그 점을 명확히해야했습니다. – bbdude95

답변

2

가 gamedev 스택 교환해야하고, 내가 선 검사로 전환하는 것이 좋습니다 아닌 것 같은이 보인다 구체. 어쨌든

, 당신이 당신의 영역 기반 시스템에 결혼하는 경우, 대신 벽 밖으로 그럴 수 "푸시"선수 :

private static bool CorrectCollisions(ref Crate c1, ref Player c2) 
{ 
    for (int i = 0; i < c1.model.Meshes.Count; i++) 
    { 
     // Check whether the bounding boxes of the two cubes intersect. 
     BoundingSphere c1BoundingSphere = c1.model.Meshes[i].BoundingSphere; 

     c1BoundingSphere.Center += c1.position + new Vector3(2, 0, 2); 
     c1BoundingSphere.Radius = c1BoundingSphere.Radius/1.5f; 

     for (int j = 0; j < c2.model.Meshes.Count; j++) 
     { 
      BoundingSphere c2BoundingSphere = c2.model.Meshes[j].BoundingSphere; 
      c2BoundingSphere.Center += c2.position; 

      Vector3 dir=c2BoundingSphere.Center - c1BoundingSphere.Center; 
      float center_dist_sq=dir.dot(dir); 
      float min_dist=c2BoundingSphere.Radius+c1BoundingSphere.Radius; 
      if (center_dist_sq < min_dist*min_dist) { 
        dir.normalize(); 
        c2.position += dir*(min_dist-sqrt(center_dist_sq)); 
      } 
     } 
    } 
    return false; 
} 

그런 다음 업데이트를 볼 수 더 같은 :

for (int x = 0; x <= 29; x++) { 
    for (int y = 0; y <= 29; y++) { 
     if (crate[x, y].getType() == 11 && collisionEnabled) { 
      CorrectCollisions(ref crate[x, y], ref player); 
     } 
    } 
} 

(2,0,2) 벡터 또는 2/3의 목적이 무엇인지 모르겠다. 개념을 표현하기 위해 무시했다.

+0

c2.position + = (c2BoundingSphere.Center - c1BoundingSphere.Center) - (c2BoundingSphere.Radius - c1BoundingSphere.Radius); "Vector3를 플로트로 변환 할 수 없습니다 .2 번째 빼기가 쉼표로되어 있습니까? – bbdude95

+0

미안 해요. 내가 1D에서 아이디어를 디자인했기 때문입니다. 방향 벡터를 추출하는 방법을 보여주기 위해 예제를 편집했습니다. 3D로 겹쳐서 구형 교차점을 결합 했으므로 수학 중복이 있으므로 두 번 사용하지 마십시오 – MickLH

+0

이 행에서 : float center_dist_sq = dir.dot (dir); DOT 제품에 두 개의 Vector3s가 필요하지 않습니까? ? – bbdude95