4 Replies - 2815 Views - Last Post: 30 December 2012 - 10:13 AM

#1 Grokeezy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 12

XNA 4 - First Person Shooter Camera Rotation Help

Posted 05 December 2012 - 05:03 PM

I am trying to create a VERY simple 3D shooter with XNA 4. I have combined tutorials to get as far as I have, and my main struggle this far has been getting left/right rotation and up/down rotation working together without tilting the model incorrectly.

I have a model I draw in the 3D world which is a gun, and I position my camera next to it so you can see the gun in your field of view.

I use an Xbox controller's right thumbstick to rotate left and right based on the X value, and up and down based on the Y value.

I am attempting to create a limit on the Y value, so that like a real person, you can't flip continuously over your head.

No player model is drawn, only a gun, because you wouldn't be able to see the player and at this point I am trying to make this as simple as possible.

I have tried Riemer's tutorial on the X-Wing that I am sure many of you are familiar with, and the problem I run into is that the multiple axis rotation seems to distort the model and make it face sideways. I was almost sure I had it correctly this time and I have tried multiplying different rotations first, and I still run into problems. All I want at this point is a completely independent model rotation axis, so when I rotate up and down it doesn't matter what rotation my left and right is, the model will still be straight up and down as if it were a gun in real life.

Out of all of this all I want is a model I can rotate left and right all the way around as if a person was holding it, and up and down about 30 degrees each way. I need help rotating the model without it distorting itself and twisting so that it looks like I am holding it sideways in my hands. No matter what I try the model eventually ends up looking sideways relative to something, even when using transforms correctly as I thought.

My gun rotation code:
private void HandleController()
        {

            if (GamePad.GetState(PlayerIndex.One).Triggers.Left > 0)
            {
                cameraPos = new Vector3(0, .1f, 0);
            }
            else
            {
                cameraPos = new Vector3(-.1f, .1f, .3f);
            }
            //////
            modelRotHorizontal = -GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X / 10;
            float maxan = 50;
            if ((dis.Y < maxan) && (dis.Y > -maxan))
            {
                modelRotVertical = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y / 10;

            }
            else
            {
                float variation = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y / 10;

                if ((dis.Y > maxan))
                {
                    if (variation < 0)
                    {
                        modelRotVertical = variation;
                    }
                    else
                    {
                        modelRotVertical = 0;
                    }
                }
                if ((dis.Y < -maxan))
                {
                    if (variation > 0)
                    {
                        modelRotVertical = variation;
                    }
                    else
                    {
                        modelRotVertical = 0;
                    }
                }
            }

            Vector3 camup = new Vector3(1, 0, 0);
            camup = Vector3.Transform(camup, rotation);

            Vector3 camleft = new Vector3(0, 1, 0);
            camleft = Vector3.Transform(camleft, rotation);



            //rotation = new Matrix();
            //rotation *= Matrix.CreateFromAxisAngle(rotation.Up, modelRotHorizontal);
            rotation *= Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), modelRotHorizontal);
            ////again find the for
            rotation *= Matrix.CreateFromAxisAngle(camup, modelRotVertical);
        }

That is to get input from the user, and the actual drawing of the model:
private void DrawGun()
        {
            Matrix[] transforms = new Matrix[player.Bones.Count];
            player.CopyAbsoluteBoneTransformsTo(transforms);

            Vector3 campos = cameraPos;
            campos = Vector3.Transform(campos, rotation);
            campos += playerPos;

            Vector3 newpos = new Vector3(0, 0, 0);


            Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -50), rotation);
            newpos += addVector;

            dis = addVector;


            Vector3 camup = new Vector3(0, 1, 0);
            camup = Vector3.Transform(camup, rotation);

            viewMatrix = Matrix.CreateLookAt(campos, Vector3.Zero + newpos, camup);
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f), aspectratio, .000000001f, 10000f);
            //Draw model
            foreach (ModelMesh mesh in player.Meshes)
            {
                foreach (BasicEffect fx in mesh.Effects)
                {
                    fx.EnableDefaultLighting();
                    fx.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(0.005f, 0.005f, 0.005f) * rotation * Matrix.CreateTranslation(playerPos);
                    fx.View = viewMatrix;
                    fx.Projection = projectionMatrix;

                }
                //Draw mesh using code above
                mesh.Draw();
            }
        }


I thought I had this right until I drew a floor from one of riemer's tutorials, and it creates a problem if I go crazy with my controller and rotate quickly in all different directions. ANY help would be greatly appreciated, this is such a simple task I would think it would be much easier.

Is This A Good Question/Topic? 0
  • +

Replies To: XNA 4 - First Person Shooter Camera Rotation Help

#2 Grokeezy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 12

Re: XNA 4 - First Person Shooter Camera Rotation Help

Posted 05 December 2012 - 05:10 PM

I took two screen captures of my problem, the before picture is the original game when I run it:
Posted Image
And the after picture, after messing with my rotation and looking around crazily, when looking at near the same point again my field seems to be distorted because my camera and model turned somehow a direction I didn't want it to:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#3 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 571
  • View blog
  • Posts: 1,272
  • Joined: 24-April 12

Re: XNA 4 - First Person Shooter Camera Rotation Help

Posted 06 December 2012 - 10:57 AM

I don't fully understand the problem that you are facing there, and I don't really have the time to really dig deep into it today, but - at the risk of tooting my own horn - you might want to check out my "Hollodeck" tutorial.

http://xna-3d-101.co...heHolodeck.html

It's very similar to what you are doing. And it includes complete source code explained in almost painful detail. The big differences are that your example uses a floor AND a skybox, where my tutorial uses the skybox as a floor and walls. It also does not have an attached object like the gun. But it also has object collision. You can either go through every step of the tutorial, or just skip to the end and download all the final source code to experiment with.

The only thing that I see at a glance that could distort a model is the bones transform. Bones are for animation. Some models include separate animating parts, like car wheels, that are attached as bones. The bones transformations have to be done correctly in order to keep all of the parts m their correct relative places.
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 571
  • View blog
  • Posts: 1,272
  • Joined: 24-April 12

Re: XNA 4 - First Person Shooter Camera Rotation Help

Posted 06 December 2012 - 12:30 PM

I also noticed that this code looks wrong, and - now that I've had a chance to examine your pictures a bit more - I think it would create the problem you seem to be experiencing.

camup = Vector3.Transform(camup, rotation);  
viewMatrix = Matrix.CreateLookAt(campos, Vector3.Zero + newpos, camup);  



I "believe" camera up never changes. I "believe" that it's "up" as in "the opposite direction of the direction gravity is pulling in". So unless gravity shifts, it doesn't change. You should probably use a hardcoded Vector3.Up for that value. I think I always do. You can see an example of how I did it in the camera code on the website I posted above.
Was This Post Helpful? 1
  • +
  • -

#5 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 78
  • Joined: 28-June 10

Re: XNA 4 - First Person Shooter Camera Rotation Help

Posted 30 December 2012 - 10:13 AM

View PostBBeck, on 06 December 2012 - 12:30 PM, said:

I "believe" camera up never changes. I "believe" that it's "up" as in "the opposite direction of the direction gravity is pulling in". So unless gravity shifts, it doesn't change. You should probably use a hardcoded Vector3.Up for that value. I think I always do. You can see an example of how I did it in the camera code on the website I posted above.



I just wanted to second this. I believe the up vector should always be what an unrotated camera considers 'up'. I always use Vector3.Up as well. Let us know if that works out for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1