2014-02-23 67 views
-1

是找到汽車中心位置的方法嗎?我必須手動將車輪移動到汽車中心,旋轉並翻回來。如何找到網格的中心?

tireMatrix = Matrix.CreateTranslation(2.6f, -4.5f, 1.75f) * 
          Matrix.CreateRotationX(pitch) * 
          Matrix.CreateRotationZ(roll) * 
          Matrix.CreateTranslation(-2.7f, -4.7f, -1.75f); 

手動移動它可以工作,但我無法找到汽車的確切中心,必須有更好的方法來做到這一點。所有的汽車零件都在同一個FBX文件中

+0

我不知道「精確」,但你可以把一個邊界框周圍的車,找到中心矩形 – Mozzie

回答

0

我認爲它可行,但我沒有測試它。

matrix.X = box.Min.X+(box.Max.X-box.Min.X)/2; 
matrix.Y = box.Min.Y+(box.Max.Y-box.Min.Y)/2; 
matrix.Z = box.Min.Z+(box.Max.Z-box.Min.Z)/2; 

要創建BoundingBox box,您可以使用this

0

沒關係了它。我必須使用ModelBone將車輪存儲爲汽車的骨頭,存儲車輪的初始變形,並且我可以在車輪上執行旋轉,而無需將它們翻譯到任何位置。 以備將來參考或者其他人誰可能希望做類似的事情,這裏是代碼

Model carModel; 
     Matrix carWorldMatrix; 

     ModelBone fr_wheel; 
     ModelBone fl_wheel; 
     ModelBone bl_wheel; 
     ModelBone br_wheel; 
     //Matrices for storing the initial transform of each wheel 
     Matrix fr_transform; 
     Matrix fl_transform; 
     Matrix br_transform; 
     Matrix bl_transform; 

     Matrix[] transforms; 

     float wheelRotation; 
     float wheelSteerAngle; 
     float carRotation; 

public void Load(ContentManager content) 
{ 
     carModel = content.Load<Model>("car"); 

     fr_wheel = carModel.Bones["fr_tire"]; 
     fl_wheel = carModel.Bones["fl_tire"]; 
     bl_wheel = carModel.Bones["bl_tire"]; 
     br_wheel = carModel.Bones["br_tire"]; 

     fr_transform = fr_wheel.Transform; 
     fl_transform = fl_wheel.Transform; 
     br_transform = br_wheel.Transform; 
     bl_transform = bl_wheel.Transform; 

     transforms = new Matrix[carModel.Bones.Count]; 
} 

public void Update(GameTime gameTime) 
{ 
    carModel.Root.Transform = carWorldMatrix; 

    Matrix tireRotation = Matrix.CreateRotationX(wheelRotation) * 
          Matrix.CreateRotationY(wheelSteerAngle); 

    fr_wheel.Transform = tireRotation * fr_transform;//this will apply the rotation to the  front right wheel 

} 

public void Draw(ref Camera camera) 
{ 
    carModel.CopyAbsoluteBoneTransformsTo(transforms); 

    foreach (ModelMesh mesh in carModel.Meshes) 
    { 
     foreach (BasicEffect effect in mesh.Effects) 
     { 
      effect.EnableDefaultLighting(); 
      effect.World = transforms[mesh.ParentBone.Index]; 
      effect.View = camera.viewMatrix; 
      effect.Projection = camera.projectionMatrix; 
     } 
     mesh.Draw() 
    } 
}