xna 3d 子弹浴和绘制方法出了问题

发布于 2024-11-04 07:38:39 字数 2144 浏览 0 评论 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();
        }
    }

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

I'm designing a 3D game, in which I have a camera behind a spaceship to move around. I have added some asteroids in the game as well, and I want to add bullets. I used quaternions to control the path of the spaceship and the camera behind it. Nevertheless, when I use the same quaternions (the rotation in specific so that i can calculate the path of the bullet) on the bullet, I get a really funny effect and the paths are totally wrong. Any ideas as to why this happens?

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();
        }
    }

Have in mind that my spaceship is a field of the bullet object and that is how I get the spaceship. Rotation quaternion. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

眼眸里的快感 2024-11-11 07:38:39

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

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

XNA是一个行主框架,串联是从左到右的。您的代码(从右到左)是针对 OpenGL(一个列主要框架)执行此操作的方式。

You should concatenate the effect.World matrix opposite of the order you are currently doing it. This shouldn't affect the shape of the object but will solve other problems. It should be like this:

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

XNA is a row major framework, the concatenation is left to right. Your code (right to left) is how you would do it for OpenGL, a column major framework.

静待花开 2024-11-11 07:38:39

据我了解,根据您的代码,如果您的船改变方向,您的射弹在发射后会改变方向。

您需要创建一个射弹对象并存储每个射弹对象中每个射弹的方向。当射弹被实例化时,您将其方向设置为与船舶相同(如上面所做的那样)。然而,在该射弹的整个剩余生命周期中,您不需要改变其方向。

示例:子弹 1 从 A 点向 B 点发射,然后飞船转向并从 C 点向 D 点发射子弹 2。

子弹 1 和 2 将各自具有唯一的行进矢量。

It is my understanding that, based upon your code, your projectiles will change direction after being fired if your ship changes direction.

You need to create a projectile object and store the direction of each individual projectile each projectile's object. When the projectile is instantiated, you will set its direction to the same as the ship (as you have done above). However, for the entire rest of the life of that projectile, you need to not change its direction.

Example: Bullet 1 is fired from POINT A towards POINT B, then the ship turns and fires Bullet 2 from POINT C to POINT D.

Bullet 1 and 2 will each have unique vectors upon which they travel.

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