我试图将边界球体添加到模型中以进行碰撞检测。问题是模型底部始终与球体的中心处于同一水平。有没有办法找到模型的中间(基于Y轴),以便我可以将其作为球体的中心?认为这将解决我的问题。提前谢谢。
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);
}
return mergedSphere;
}
答案 0 :(得分:0)
通常当人们对boundingSphere位置有问题时,他们不会考虑骨骼转换。
由于你要合并球体,它意味着你有多个ModelMesh对象......每个对象都有自己的ModelBone对象,它有一个变换矩阵。
当内容处理器从网格的顶点创建边界球时,它不会考虑可能影响网格最终位置的任何骨骼变换。因此,通过骨骼变换的Translation属性手动平移网格的BoundingSphere.Center以设置Boundingsphere的正确位置非常重要。
例如:
Matrix[] meshTransforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(meshTransforms);
//then in the foreach loop
int tempIndex = Index++;
boundingSpheres[tempIndex] = mesh.BoundingSphere;
boundingSpheres[tempIndex].Center += meshTransforms[mesh.ParentBone.Index].Translation;