XNA 边界球体和对象位于相同位置但显示不同

发布于 2024-11-02 04:44:42 字数 3967 浏览 1 评论 0原文

好的,大家好,我在边界球定位方面遇到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心清如水 2024-11-09 04:44:42

不用多想,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;?

不必了 2024-11-09 04:44:42

jv42 规则说:SRT:缩放 - 旋转 - 翻译

jv42 the rule say : SRT : scaling - Rotation - Translation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文