2011-04-19 56 views
1

好吧,我有一个带边界球定位的XNA问题。我发誓每个人的立场是完全一样的 - 我甚至与调试人员核对,但两人出现在不同的方向。我可以通过乘以因子来进行手动调整以得到正确的结果,但为什么会出现这种效应?下面是代码:XNA边界球体和对象在相同的位置,但以不同的方式出现

边界球:

protected BoundingSphere CalculateBoundingSphere() 
    { 
     BoundingSphere mergedSphere = new BoundingSphere(); 
     BoundingSphere[] boundingSpheres; 
     int index = 0; 
     int meshCount = Model.Meshes.Count; 

     boundingSpheres = new BoundingSphere[meshCount]; 
     foreach (ModelMesh mesh in Model.Meshes) 
     { 
      boundingSpheres[index++] = mesh.BoundingSphere; 
     } 

     mergedSphere = boundingSpheres[0]; 
     if ((Model.Meshes.Count) > 1) 
     { 
      index = 1; 
      do 
      { 
       mergedSphere = BoundingSphere.CreateMerged(mergedSphere, 
        boundingSpheres[index]); 
       index++; 
      } while (index < Model.Meshes.Count); 
     } 

     mergedSphere.Center = Position; 
     return mergedSphere; 
    } 

    internal void DrawBoundingSphere(Matrix view, Matrix projection, 
GameObject boundingSphereModel) 
    { 
     Matrix scaleMatrix = Matrix.CreateScale(BoundingSphere.Radius); 
     Matrix translateMatrix = 
      Matrix.CreateTranslation(BoundingSphere.Center);//BoundingSphere.Center CHANGED THISSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 

     Matrix worldMatrix = scaleMatrix * translateMatrix; 

     foreach (ModelMesh mesh in boundingSphereModel.Model.Meshes) 
     { 
      foreach (BasicEffect effect in mesh.Effects) 
      { 
       effect.World = worldMatrix; 
       effect.View = view; 
       effect.Projection = projection; 
      } 
      mesh.Draw(); 
     } 
    } 
} 

对象抽奖:

class Asteroid : GameObject 
{ 
    public bool Destroyed { get; set; } 
    Vector3 m_Rotations; 
    public int rotDir; 

    public Asteroid(int rot) 
     : base() 
    { 
     Destroyed = false; 
     rotDir = rot; 


    } 

    public void LoadContent(ContentManager content, string modelName) 
    { 
     Model = content.Load<Model>(modelName); 
     Position = Vector3.Down; 
    } 
    public void update(GameTime gameTime)//for making asteroid spin 
    { 
     switch (rotDir) 
     { 
      case 0: m_Rotations.Z += (float)gameTime.ElapsedGameTime.TotalSeconds; 
       break; 
      case 1: m_Rotations.X += (float)gameTime.ElapsedGameTime.TotalSeconds; 
       break; 
      case 2 :m_Rotations.Z += (float)gameTime.ElapsedGameTime.TotalSeconds; 
       break; 


     } 
    } 
    public void Draw(Matrix view, Matrix projection) 
    { 
     BoundingSphere = CalculateBoundingSphere(); 

     //Vector3 pos = BoundingSphere.Center; 

     Matrix[] transforms = new Matrix[Model.Bones.Count]; 
     Model.CopyAbsoluteBoneTransformsTo(transforms); 
     Matrix translateMatrix = Matrix.CreateTranslation(Position);//POSITION 
     Matrix worldMatrix = Matrix.Identity; 
     worldMatrix *= Matrix.CreateRotationX(m_Rotations.X);//making asteroid spin 
     worldMatrix *= Matrix.CreateRotationY(m_Rotations.Y);//making asteroid spin 
     worldMatrix *= Matrix.CreateRotationZ(m_Rotations.Z);//making asteroid spin 
     worldMatrix *= translateMatrix; 

     //Matrix worldMatrix = translateMatrix; 

     if (!Destroyed) 
     { 
      foreach (ModelMesh mesh in Model.Meshes) 
      { 
       foreach (BasicEffect effect in mesh.Effects) 
       { 
        effect.World = 
         worldMatrix* transforms[mesh.ParentBone.Index]; //MAKE THIS ROTATE 
        effect.View = view ; 
        effect.Projection = projection; 


        effect.EnableDefaultLighting(); 
        effect.PreferPerPixelLighting = true; 
       } 
       mesh.Draw(); 
      } 
     } 

回答

0

没有想太多,是不是Matrix worldMatrix = scaleMatrix * translateMatrix;倒退?

是不是应该是Matrix worldMatrix = translateMatrix * scaleMatrix;

0

jv42规则说:SRT:缩放 - 旋转 - 翻译

相关问题