c# - xna 3d bullet bath 和 draw 方法出了问题

标签 c# visual-studio xna xna-4.0

我正在设计一个 3D 游戏,在这个游戏中,我在宇宙飞船后面安装了一个摄像头,可以四处移动。我在游戏中也添加了一些小行星,我想添加子弹。我用四元数来控制宇宙飞船的路径和它后面的相机。然而,当我在子弹上使用相同的四元数(特定的旋转以便我可以计算子弹的路径)时,我得到了一个非常有趣的效果并且路径完全错误。关于为什么会发生这种情况的任何想法?

public void Update(GameTime gameTime)

    {
        lazerRotation = spaceship.spaceShipRotation;

        Vector3 rotVector = Vector3.Transform(new Vector3(0, 0, -1), lazerRotation); 


        Position +=  rotVector* 10 * (float)gameTime.ElapsedGameTime.TotalSeconds;
        //

    }
    //spaceShipRotation = Quaternion.Identity;
    //Quaternion additionalRot = Quaternion.CreateFromAxisAngle(new Vector3(0, -1, 0), leftRightRot) * Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), upDownRot);
    //spaceShipRotation *= additionalRot;
    //Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -1), spaceShipRotation);
    //    futurePosition += addVector * movement * velocity;


    public void Draw(Matrix view, Matrix projection)
    {


       // //Quaternion xaxis = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, -1), spaceship.leftrightrot);
       // //Quaternion yaxis = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), spaceship.updownrot);
       // //Quaternion rot = xaxis * yaxis;
       //Matrix x =  Matrix.CreateRotationX(spaceship.leftrightrot);
       //Matrix y = Matrix.CreateRotationY(spaceship.updownrot);
       //Matrix rot = x * y;

        Matrix[] transforms = new Matrix[Model.Bones.Count];
        Model.CopyAbsoluteBoneTransformsTo(transforms);
        Matrix worldMatrix = Matrix.CreateFromQuaternion(lazerRotation) * Matrix.CreateTranslation(Position);



        foreach (ModelMesh mesh in Model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {


                effect.World = worldMatrix * transforms[mesh.ParentBone.Index];  //position
                effect.View = view; //camera
                effect.Projection = projection; //2d to 3d

                effect.EnableDefaultLighting();
                effect.PreferPerPixelLighting = true;
            }
            mesh.Draw();
        }
    }

请记住,我的宇宙飞船是子弹对象的一个​​场,这就是我获得宇宙飞船的方式。旋转四元数。提前致谢。

最佳答案

您应该连接 effect.World 矩阵,与您当前执行的顺序相反。这不应该影响对象的形状,但会解决其他问题。应该是这样的:

effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;

XNA 是一个行主要框架,串联是从左到右。您的代码(从右到左)是您为 OpenGL 这一列主要框架编写的代码。

关于c# - xna 3d bullet bath 和 draw 方法出了问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836111/

相关文章:

c++ - 有没有整理C++代码的工具?

c# - 如何(反)序列化需要自定义 JsonConverter 的类型列表?

c# - 两个圆的切线

c# - 使用c#将具有大量列标题的Excel文件导入mysql

c# - Entity Framework 中的关系问题

c# - 如何将一个对象统一放置在另一个对象之上?

c# - 从内存中删除项目/页面/用户控件

regex - 使用 Visual Studio 进行条件替换

c# - 画一个平滑扩大的圆圈

c# - 捕获最小化远程桌面的屏幕截图