XNA包围球体和物体在同一位置,但看起来不同

本文关键字:位置 看起来 包围 XNA | 更新日期: 2023-09-27 17:51:24

好了,伙计们,我有一个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();
            }
        }

XNA包围球体和物体在同一位置,但看起来不同

不用多想,Matrix worldMatrix = scaleMatrix * translateMatrix;是不是落后了?

不应该是Matrix worldMatrix = translateMatrix * scaleMatrix;

查看此站点可能有用。

http://aarcoraci.wordpress.com/2010/01/11/blender-to-xna-fbx-understanding-the-model/

jv42规则说:SRT:缩放-旋转-平移