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();
}
}
OK guys, I've an XNA problem with bounding sphere positioning. I swear the position of each is exactly the same - I even checked with the debugger, nevertheless the two appear in different directions. I could adjust this manually by multiplying with factors to get this right, but why does this effect appear? Here is the code:
Bounding sphere:
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();
}
}
}
Object 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不用多想,
Matrix worldMatrix = scaleMatrix * translateMatrix;
不是倒着来的吗?不应该是
Matrix worldMatrix = translateMatrix * scaleMatrix;
吗?Without thinking too much, isn't
Matrix worldMatrix = scaleMatrix * translateMatrix;
backwards?Shouldn't it be
Matrix worldMatrix = translateMatrix * scaleMatrix;
?检查这个网站可能会有用。
http://aarcoraci.wordpress。 com/2010/01/11/blender-to-xna-fbx-understanding-the-model/
check this site it may be useful.
http://aarcoraci.wordpress.com/2010/01/11/blender-to-xna-fbx-understanding-the-model/
jv42 规则说:SRT:缩放 - 旋转 - 翻译
jv42 the rule say : SRT : scaling - Rotation - Translation