4

我正在设计一个 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();
        }
    }

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

4

2 回答 2

2

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

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

XNA 是一个行主要框架,串联是从左到右的。您的代码(从右到左)是您将如何为 OpenGL(列主要框架)执行此操作。

于 2011-04-30T01:08:25.237 回答
1

我的理解是,根据你的代码,如果你的船改变方向,你的炮弹会在发射后改变方向。

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

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

Bullet 1 和 Bullet 2 将各自具有独特的矢量,它们可以在上面移动。

于 2011-04-29T19:56:58.353 回答