2012-12-07 3 views
1

3D 세계에서 카메라를 움직이는 데 어려움을 겪고 있습니다. 두 가지보기 모드를 만들 것입니다.동일한 각도의 모델 뒤로 카메라를 이동하는 방법은 무엇입니까?

1 : fps (첫 사람) 용. 두 번째 : 문자 뒤의 외부보기 (두 번째 사람).

그물을 몇 가지 예제로 검색했지만 프로젝트에서 작동하지 않습니다. 여기

내 코드는 내가 당신이라면 F2가

//Camera 
     double X1 = this.camera.PositionX; 
     double X2 = this.player.Position.X; 
     double Z1 = this.camera.PositionZ; 
     double Z2 = this.player.Position.Z; 


     //Verify that the user must not let the press F2 
     if (!this.camera.IsF2TurnedInBoucle) 
     { 
      // If the view mode is the second person 
      if (this.camera.ViewCamera_type == CameraSimples.ChangeView.SecondPerson) 
      { 
       this.camera.ViewCamera_type = CameraSimples.ChangeView.firstPerson; 

       //Calcul position - ?? Here my problem 
       double direction = Math.Atan2(X2 - X1, Z2 - Z1) * 180.0/3.14159265; 
       //Calcul angle - ?? Here my problem 

       this.camera.position = .. 
       this.camera.rotation = .. 

       this.camera.MouseRadian_LeftrightRot = (float)direction; 
      } 
      //IF mode view is first person 
      else 
      { 
        //.... 

답변

1

Xna에서 매우 기본적인 3 인칭 카메라 (2 인칭의 의미)가 있습니다. 그것은 당신이 플레이어의 월드 행렬이 저장되어 있고 액세스 할 수 있습니다 가정

Vector3 _3rdPersonCamPosition = playerWorldMatrix.Translation + (playerWorldMatrix.Backward * trailingDistance) + (playerWorldMatrix.Up * heightOffset);// add a right or left offset if desired too 
Vector3 _3rdPersonCamTarget = playerWorldMatrix.Translation;//you can offset this similarly too if desired 
view = Matrix.CreateLookAt(_3rdPersonCamPosition, _3rdPersonCamTarget , Vector3.Up); 

당신의 FPS 캠이 제대로 작동하고 기본적으로 플레이어와 동일한 위치와 방향이라고 가정하는 경우, 당신이 그것을 대체 할 수있는 장소에보기 매트릭스의 이 같은 위의 playerWorldMatrix의 :

Matrix FPSCamWorld = Matrix.Invert(yourWorkingFPSviewMatrixHere); 

내가 대신 FPSCamWorld을 사용할 수 있습니다 playerWorldMatrix을 쓴 지금 어디든지.

+0

감사합니다. –

1

을 누르면, 내가이 제대로 이동 있으리라 믿고있어 (당신의 지금 작업 FPS 카메라를 취할 것,보기를 변경하는 데 사용되는 위치 매트릭스를 가지고있다 , 등?), 다른 번역 변환을 추가하여 플레이어 뒤쪽으로 "이동"합니다.

넣어 또 다른 방법 :

경우는 FPS 뷰에 대한 "번역/뷰 매트릭스는"뭔가 같은 :

(죄송합니다, 한 동안 XNA 연주하지 않은, 그래서 기억이 안나요 적절한 클래스 이름) 다음

var camTranslateMatrix = [matrix representing player position]; 
var camDirectionMatrix = [matrix representing player direction, etc]; 
var camViewMatrix = camTranslateMatrix * camDirectionMatrix; 

당신과 같이 변경 수 :

var camTranslateMatrix = [matrix representing player position]; 
var camDirectionMatrix = [matrix representing player direction, etc]; 

// If not in 3rd person, this will be identity == no effect 
var camThirdPersonMatrix = 
     IsInThirdPersonMode ? 
      new TranslateMatrix(back a bit and up a bit) : 
      IdentityMatrix(); 

var camViewMatrix = 
     camTranslateMatrix * 
     camDirectionMatrix * 
     camThirdPersonMatrix; 

센 확인 그렇지? 그런 식으로, 당신이 그렇게 할 때마다 매끄럽지 못한 톤없이 두보기 사이를 전환하는 것이 쉽지 않을 것입니다.

관련 문제