是否可以找到汽车中心的位置?我必须手动将车轮移动到汽车的中心,旋转并将其平移。
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 :(得分: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
答案 1 :(得分: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()
}
}