开发者

control a spaceship in 3 axes in XNA

when i control the spaceship in 1 axis all is fine, that is, the depth(z) and the rotation on the z plane 360 degrees, so thats 2 axis. I also have a camera right behind it which i have to maintain its position. When the 3rd comes in place all go bad. let me show you some code: Here is the part that fails:

draw method of spaceship :

public void Draw(Matrix view, Matrix projection)
        {
public float ForwardDirection { get; set; }
        public 开发者_如何学JAVAfloat VerticalDirection { get; set; }

            Matrix[] transforms = new Matrix[Model.Bones.Count];
            Model.CopyAbsoluteBoneTransformsTo(transforms);

            Matrix worldMatrix = Matrix.Identity;
            Matrix worldMatrix2 = Matrix.Identity;

            Matrix rotationYMatrix = Matrix.CreateRotationY(ForwardDirection);
            Matrix rotationXMatrix = Matrix.CreateRotationX(VerticalDirection); // me

            Matrix translateMatrix = Matrix.CreateTranslation(Position);

            worldMatrix = rotationYMatrix * translateMatrix;
            worldMatrix2 = rotationXMatrix * translateMatrix;
            //worldMatrix*= rotationXMatrix;

            foreach (ModelMesh mesh in Model.Meshes) //NEED TO FIX THIS
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World =
                        worldMatrix * transforms[mesh.ParentBone.Index]; ; //position
                    //effect.World =
                        //worldMatrix2 * transforms[mesh.ParentBone.Index]; ; //position
                    effect.View = view; //camera
                    effect.Projection = projection; //2d to 3d

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

For the extra axis the worldMatrix2 is implemented which i DONT know how to combine with the other axis. Do i multiply it? Also:

The camera update method has a similar problem:

public void Update(float avatarYaw,float avatarXaw, Vector3 position, float aspectRatio)
        {
            //Matrix rotationMatrix = Matrix.CreateRotationY(avatarYaw);
            Matrix rotationMatrix2 = Matrix.CreateRotationX(avatarXaw);

            //Vector3 transformedheadOffset =
                //Vector3.Transform(AvatarHeadOffset, rotationMatrix);

            Vector3 transformedheadOffset2 = Vector3.Transform(AvatarHeadOffset, rotationMatrix2);
            //Vector3 transformedheadOffset2 = Vector3.Transform(transformedheadOffset, rotationMatrix2);


            //Vector3 transformedReference =
                //Vector3.Transform(TargetOffset, rotationMatrix);


            Vector3 transformedReference2 = Vector3.Transform(TargetOffset, rotationMatrix2);
            //Vector3 transformedReference2 = Vector3.Transform(transformedReference, rotationMatrix2);


            Vector3 cameraPosition = position + transformedheadOffset2;  /** + transformedheadOffset;  */
            Vector3 cameraTarget = position + transformedReference2;  /** +  transformedReference;  */

            //Calculate the camera's view and projection 
            //matrices based on current values.
            ViewMatrix =
                Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
            ProjectionMatrix =
                Matrix.CreatePerspectiveFieldOfView(
                    MathHelper.ToRadians(GameConstants.ViewAngle), aspectRatio,
                    GameConstants.NearClip, GameConstants.FarClip);
        }
    }

Lastly here is the method of the Game class Update:

spaceship.Update(currentGamePadState,
    currentKeyboardState); // this will be cahnged when asteroids are placed in the game, by adding a new parameter with asteroids.
            float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
            gameCamera.Update(spaceship.ForwardDirection,spaceship.VerticalDirection,
                spaceship.Position, aspectRatio);


Somewhere in your code, you probably have methodology that manipulates and sets the variables 'ForwardDirection' & 'VerticalDirection' which seem to represent angular values around the Y & X axis respectively. Presumably you may intend to have a variable that represents an angular value around the Z axis as well. I'm also assuming (and your code implies) that these variables are ultimately how you store your spaceship's orientation from frame to frame.

As long as you continue to try to represent an orientation using these angles, you will find it difficult to achieve the control of your spaceship.

There are several types of orientation representations available. The 3 angles approach has inherent weaknesses when it comes to combining rotations in 3 dimensions.

My recommendation is that you shift paradigms and consider storing and manipulating your orientations in a Matrix or a Quaternion form. Once you learn to manipulate a matrix or quaternion, what you are trying to do becomes almost unbelievably easy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜