2010-07-11 5 views
0

저는 xna 개발을 처음 접했고 위치 벡터를 생성자에 전달하여 Primitives3D sample에서 큐브를 배치하려고합니다. unfortunatly 작동하지 않습니다 .. 대신 단지 회전하는 arround입니다.xna에 3d 오브젝트 위치 지정

public class CubePrimitive : GeometricPrimitive 
{ 
    public Vector3 position; 

    /// <summary> 
    /// Constructs a new cube primitive, using default settings. 
    /// </summary> 
    public CubePrimitive(GraphicsDevice graphicsDevice) 
     : this(graphicsDevice, 1, new Vector3(-3,0,0)) 
    { 
    } 

    /// <summary> 
    /// Constructs a new cube primitive, with the specified size. 
    /// </summary> 
    public CubePrimitive(GraphicsDevice graphicsDevice, float size, Vector3 posi) 
    { 
     this.position = posi; 

     // A cube has six faces, each one pointing in a different direction. 
     Vector3[] normals = 
     { 
      new Vector3(0, 0, 1), 
      new Vector3(0, 0, -1), 
      new Vector3(1, 0, 0), 
      new Vector3(-1, 0, 0), 
      new Vector3(0, 1, 0), 
      new Vector3(0, -1, 0), 
     }; 

     // Create each face in turn. 
     foreach (Vector3 normal in normals) 
     { 
      // Get two vectors perpendicular to the face normal and to each other. 
      Vector3 side1 = new Vector3(normal.Y, normal.Z, normal.X); 
      Vector3 side2 = Vector3.Cross(normal, side1); 

      // Six indices (two triangles) per face. 
      AddIndex(CurrentVertex + 0); 
      AddIndex(CurrentVertex + 1); 
      AddIndex(CurrentVertex + 2); 

      AddIndex(CurrentVertex + 0); 
      AddIndex(CurrentVertex + 2); 
      AddIndex(CurrentVertex + 3); 

      // Four vertices per face. 
      AddVertex(posi+(normal - side1 - side2) * size/2, normal); 
      AddVertex(posi + (normal - side1 + side2) * size/2, normal); 
      AddVertex(posi + (normal + side1 + side2) * size/2, normal); 
      AddVertex(posi + (normal + side1 - side2) * size/2, normal); 
     } 

     InitializePrimitive(graphicsDevice); 
    } 
} 

가 어떻게 올바르게 수정하는 나에게 보여 주시겠습니까 :

그게 내가는 cubeprimitive 클래스의 코드를 수정하는 방법? .. 미리 감사드립니다.

답변

1

전통적으로 모델을 움직이려 할 때, 모델의 각 정점을 수정하는 대신 World 행렬을 변경합니다.

Primitives3DGame.cs에서이 줄을 찾기 :

Matrix world = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll); 

을 그리고이 같은으로 변경 :

Matrix world = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll) 
       * Matrix.CreateTranslation(-3, 0, 0); 

(그리고, 물론, 당신이 만든 변경 사항을 제거에 게시 된 귀하의 질문.)

지금까지 귀하의 코드가 실제로 작동하고 있다고 말할 수 있습니다. 그러나 아마도 큐브가 회전하고 있지만 같은 위치에 머물러 있기를 기대하고있었습니다 (위 코드가하는 것과 같습니다). 당신이하고있는 것은 효과적으로과 동일합니다 :

Matrix world = Matrix.CreateTranslation(-3, 0, 0) 
       * Matrix.CreateFromYawPitchRoll(yaw, pitch, roll); 

참고 역순 - 당신은 원점 중심으로 회전 첫 번째 모델과 다음의 번역을하고 있습니다.