XNA Creators Club Online
Page 1 of 1 (13 items)
Sort Posts: Previous Next

Chase Camera Issue

Last post 11/10/2009 4:01 PM by Cokiki. 12 replies.
  • 5/18/2009 3:15 PM

    Chase Camera Issue

    Hey,

    I have the chase camera sample working in my project. The only issue is that the camera seems to initialize pointing down on the model and then moves to back behind the model when the game starts. The weird thing is that I have the camera.reset() and UpdateCameraChaseTarget() in my initialize function. The reset function is working on the camera because if I comment it out, the camera then does move to find the chase target on game start. My issue seems more like a rotation from the top of the model to the back in an arc. 

    I can't figure out how to fix this. I'd greatly appreciate some help. 

    Thanks

    Wes
    Wes McDermott - 3D Ninja For Hire - Homepage
  • 5/18/2009 4:14 PM In reply to

    Re: Chase Camera Issue

    While I don't know that particular sample, it sounds like the Reset() function doesn't calculate the same desired location as the Update() function. You have to put a breakpoint into the code and check what desired/target location is calculated in Reset(), and compare it to Update(). Then make them the same. It may be that there is an ordering problem, too; the model may need to be updated/rendered once for the right position to be calculated relative to the model.
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 5/18/2009 6:54 PM In reply to

    Re: Chase Camera Issue

    jwatte:
    While I don't know that particular sample, it sounds like the Reset() function doesn't calculate the same desired location as the Update() function. You have to put a breakpoint into the code and check what desired/target location is calculated in Reset(), and compare it to Update(). Then make them the same. It may be that there is an ordering problem, too; the model may need to be updated/rendered once for the right position to be calculated relative to the model.


    Hey jwatte,

    Thanks for the suggestion. I can't seem to figure this out. I'm trying to trace down the discrepancy. The DesiredPosition var for the chase Camera seems to start at 0,10,0. However I would think it should be 0,0,0. This should be set by the DesiredPositionOffset variable and is set to 0,0,0. I'm not sure how to fix this.
    Wes McDermott - 3D Ninja For Hire - Homepage
  • 5/23/2009 2:10 AM In reply to

    Re: Chase Camera Issue

    I've seen the same behavior when playing with the ChaseCamera.DesiredOffsetPosition property. Try moving the desired offset of the camera back (Z) and down (Y) and see if that helps.

    Tim
  • 5/26/2009 3:04 PM In reply to

    Re: Chase Camera Issue

    madgamer:
    I've seen the same behavior when playing with the ChaseCamera.DesiredOffsetPosition property. Try moving the desired offset of the camera back (Z) and down (Y) and see if that helps.

    Tim


    Hey Tim,

    Unfortunetly that didn't work. The camera moves, but it still seems like the camera is starting at one position and then being moved to the ChaseCamera.DesiredOffsetPosition.
    Wes McDermott - 3D Ninja For Hire - Homepage
  • 6/2/2009 12:52 AM In reply to

    Re: Chase Camera Issue - perhaps you need Chase direction?

    Have you tried setting a direction for the SHIP object.

    the direction would be 0,0,0 at the start. I am guessing here...


    The camera takes it's direction from the ships direction.

         /// <summary>
            /// Update the values to be chased by the camera
            /// </summary>
            private void UpdateCameraChaseTarget()
            {            
                camera.ChasePosition = ship.Position;
                camera.ChaseDirection = ship.Direction;
                camera.Up = ship.Up;
            }


    If the ship had no direction (0,0,0) you would end up above the ship.
    This I know for a fact: I have this exact problem myself now as I have no ship object, thus no direction, I see my camera directly above the target (no Z just Y)

    Sound about right?
    I am going to derive a direction from something like:

    So In intiatlize perhaps?:

    // Create null or desired rotation matrix from fixed rotation amount
            
    // I'll go for 15 Y 0 X
    Vector3 right = Vector3.Right;
       Matrix rotationMatrix =
                    Matrix.CreateFromAxisAngle(right, 15) *  // right = Vector3.Right;
                    Matrix.CreateRotationY(0);

                // Rotate orientation vectors
                Direction = Vector3.TransformNormal(Direction, rotationMatrix);

    Then set this.ChaseDirection to Direction. I'm guessing...

    Then once your games started hand this over to the update perhaps? I'm not sure what your doing...

    Hope this helps!

    Simon-john Roberts
    MCP, MCDST, ACE, NERD.
    -----------------------------------PROJECTS:0xOrbIt! on Ox for XNA
  • 6/3/2009 2:25 AM In reply to

    Re: Chase Camera Issue - perhaps you need Chase direction?

    Hi Simon,

    Thanks for the help. I understand basically what your saying and it makes sense, but I'm having trouble figuring out how to implement a direction in my code. Can you please take a look at what I have?

     public Game1() 
            { 
                graphics = new GraphicsDeviceManager(this); 
                Content.RootDirectory = "Content"
                graphics.PreferredDepthStencilFormat = SelectStencilMode(); 
     
                //create the chase camera 
                camera = new ChaseCamera(); 
                // Set the camera offsets 
                camera.DesiredPositionOffset = new Vector3(0.0f, 10, 15); 
                camera.LookAtOffset = new Vector3(0.0f, 5, 0); 
                // Set camera perspective 
                camera.NearPlaneDistance = 1; 
                camera.FarPlaneDistance = 600; 
                 
             } 
     
            
            protected override void Initialize() 
            { 
                aspectRatio = (float)GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height; 
                
                //Vector for the Light Direction 
                lightDir = new Vector3(50, 200, -1); 
                //Create the Shadow matrix 
                shadow = Matrix.CreateShadow(lightDir, new Plane(0, 10, 0, -1)); 
                modelVelocity = Vector3.Zero; 
                // Set the position of the model in world space, and set the rotation. 
                modelPosition = Vector3.Zero; 
                modelRotation = 0.0f; 
     
                //Vector3 right = Vector3.Right; 
                //Matrix rotationMatrix = Matrix.CreateFromAxisAngle(right,15)* Matrix.CreateRotationY(0); 
                //modelDirection = Vector3.TransformNormal(modelPosition, rotationMatrix); 
     
                base.Initialize(); 
     
                // Set the camera aspect ratio 
                // This must be done after the class to base.Initalize() which will 
                // initialize the graphics device. 
                camera.AspectRatio = aspectRatio; 
                 
                // Perform an inital reset on the camera so that it starts at the resting 
                // position. If we don't do this, the camera will start at the origin and 
                // race across the world to get behind the chased object. 
                // This is performed here because the aspect ratio is needed by Reset. 
                UpdateCameraChaseTarget(); 
                camera.Reset(); 
               
                 
            } 
     
            
     
            private void UpdateCameraChaseTarget() 
            { 
                camera.ChasePosition = modelPosition; 
                camera.ChaseDirection = modelDirection; 
                camera.Up = Vector3.Up; 
                 
            } 
     
            protected override void Update(GameTime gameTime) 
            { 
                // Allows the game to exit 
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
                    this.Exit(); 
     
                // Get some input. 
                UpdateInput(); 
     
                // Add velocity to the current position. 
                modelPosition += modelVelocity; 
                // Bleed off velocity over time. 
                modelVelocity *= 0.95f; 
                // Create the model direction 
                modelDirection = Vector3.Transform(Vector3.Forward, Matrix.CreateFromAxisAngle(Vector3.Up, modelRotation)); 
                 
                // Update the camera to chase the new target 
                UpdateCameraChaseTarget(); 
                 
                // Update the camera 
                camera.Update(gameTime); 
                
     
                base.Update(gameTime); 
            } 
     
            protected void UpdateInput() 
            { 
                // Get the game pad state. 
                GamePadState currentState = GamePad.GetState(PlayerIndex.One); 
                if (currentState.IsConnected) 
                { 
                     
                    if (currentState.Triggers.Right > 0.0f) 
                    { 
     
                        // Rotate the model using the left thumbstick, and scale it down. 
                        modelRotation -= currentState.ThumbSticks.Left.X * 0.02f; 
     
                        // Find out what direction we should be thrusting, using rotation. 
                        modelVelocityAdd.X = -(float)Math.Sin(modelRotation); 
                        modelVelocityAdd.Z = -(float)Math.Cos(modelRotation); 
     
                        // Now scale our direction by how hard the trigger is down. 
                        modelVelocityAdd *= (currentState.Triggers.Right) / 80; 
     
                        // Finally, add this vector to our velocity. 
                        modelVelocity += modelVelocityAdd; 
                    } 
                     
                    
     
                    // In case you get lost, press A to warp back to the center. 
                    if (currentState.Buttons.A == ButtonState.Pressed) 
                    { 
                        modelPosition = Vector3.Zero; 
                        modelVelocity = Vector3.Zero; 
                        modelRotation = 0.0f; 
                    } 
                } 
            }    

    Wes McDermott - 3D Ninja For Hire - Homepage
  • 6/4/2009 4:38 AM In reply to

    Re: Chase Camera Issue - dynamically positioning the camera behind your thing

    Quite frankly, I now have this working.

    erm I think a bodge job is in order.

    Spagetti Bodge job follows.




    Ok, So its a bodge job.

    Call these in relevant postions.

    Hope this helps.

    The issue is with Vector3.Down as Direction causing unviewable Matrix I think...

    Here follows ridiculously inefficient bodge...


      private void UpdateCameraWithKludgyChecksInMainLoop()
            {

    // I use static instance ... fix accordingly

                Vector3 travel = Vector3.Subtract(ExamplePlayer.Position, ExamplePlayer.OldPosition);
                float testX = Math.Abs(travel.X);
                float testY = Math.Abs(travel.Y);
                float testZ = Math.Abs(travel.Z);

    // check for small dirft fudge
               float travelMargin = .05f;

    // assume drift is tiny
               bool travelIsTiny = true;

    // check if drift is tiny

               if (testX > travelMargin || testY > travelMargin || testZ > travelMargin)
               {
               travelIsTiny = false;
               
               }

    // so fudge the vectors to return valid matrixes

               if (travelIsTiny || tempVector == Vector3.Down)
                {

    // dont allow Vector3.Down as this leads to unveiwable Matirx
                    if (tempVector == Vector3.Down)
                    {

    // ugh!
                        tempVector = new Vector3(0, -.99f, 0f);
                    }

    // just in case

                    else if (tempVector == Vector3.Zero)
                    {

    // utterly abritrary
                        tempVector = Vector3.Forward;
                    }

                    else
                    {

    // do nothing, optionally just populate a variable to observe in testing
                     //   Vector3 testVector = tempVector;
                    }
                }
                else
                {

    // I test for my own direction vector

                    tempVector = ExamplePlayer.GetDirectionVector();

    //repeat hideous kludge

                    if (tempVector == Vector3.Down)
                    {


                        tempVector = new Vector3(0f, -.99f, 0f);
                    }
                }


               //    ChaseCamera.ChasePosition = ExamplePlayer.Position;
               
               // set the chase postion the Player position !!!

               //  Make sure this is Not rubbish before you set it!
               
               UpdateCameraChaseTarget();

                      
            
            }

    //


    //I use a physics engine.... do whatever is appropraite

            public Vector3 GetDirectionVector(Vector3 oldVector)
            {

    // get a normalised Direction  vector
                return Vector3.Normalize(Vector3.Subtract(body.Position, body.OldPosition));


            }





    // HELPER METHOD


      private void SetCameraPosition()
           {
      
    I have to set my main camera to my Static Instance ChaseCamera, you shouldn't have to...

    //        engine.Camera.Position = ChaseCamera.Position;

    //

    Vector3 MyCameraPosition = engine.Camera.Position; // set to your camera

    Vector3 MySubjectsPosition = ExamplePlayer.Position; // set to your subject

    //I've bunged in an offset for height adjustment here - should use lookAtOffset

               Matrix testM = Matrix.Invert(Matrix.CreateLookAt(MyCameraPosition , Vector3.Add(MySubjectsPosition , new Vector3(0, 6f, 0)), Vector3.Up));

    // I have a special static instance so I use

    //  Matrix testM = Matrix.Invert(Matrix.CreateLookAt(engine.Camera.Position, Vector3.Add(ExamplePlayer.Position,ChaseCamera.LookAtOffset, ChaseCamera.Up));

               engine.Camera.SetOrientation(ref testM);
          
         // to fudge a look at.

           }



    -----------------------------------PROJECTS:0xOrbIt! on Ox for XNA
  • 6/17/2009 7:29 PM In reply to

    Re: Chase Camera Issue - perhaps you need Chase direction?

    On second thoughts in your example just test the Matrix.ToString doesnt contain "NaN" or something, and if it does reposition the cameras position slightly -Z or something then recompute before showing the output.

    I did something similar.

    Hope this is Helpful Wes.


    If it solves your problem please indicate this by editing the thread title or similar - to help others in the future.

    Best,

    CyberSi.
    -----------------------------------PROJECTS:0xOrbIt! on Ox for XNA
  • 7/24/2009 6:14 PM In reply to

    Re: Chase Camera Issue - perhaps you need Chase direction?

    simonjohnroberts:
    On second thoughts in your example just test the Matrix.ToString doesnt contain "NaN" or something, and if it does reposition the cameras position slightly -Z or something then recompute before showing the output.

    I did something similar.

    Hope this is Helpful Wes.


    If it solves your problem please indicate this by editing the thread title or similar - to help others in the future.

    Best,

    CyberSi.


    Hi Simon,

    THanks for the help. Sorry for the late reply. I got sidetracked on another project. I will give this a go. It should help me to debug the problem.

    Thanks,
    Wes
    Wes McDermott - 3D Ninja For Hire - Homepage
  • 7/28/2009 1:02 PM In reply to

    Re: Chase Camera Issue - perhaps you need Chase direction?

    Ok, well i am using this in several of the projects i have done now.

    Its a definate worker.

    I also "drop" a player at the start of one of my games... this works fine with NAN --> 0.99 fudge.

    Ugh. sorry its so ugly...

    :)

    Si
    -----------------------------------PROJECTS:0xOrbIt! on Ox for XNA
  • 7/28/2009 1:59 PM In reply to

    Re: Chase Camera Issue - perhaps you need Chase direction?

    simonjohnroberts:


    I also "drop" a player at the start of one of my games... this works fine with NAN --> 0.99 fudge.


    Hey Simon, I'm not sure what you're referencing by "drop". Are you talking about the player's default position in the game? 

    Thanks

    Wes
    Wes McDermott - 3D Ninja For Hire - Homepage
  • 11/10/2009 4:01 PM In reply to

    Re: Chase Camera Issue

    i have same problem too....
    i'm trying to using Sample Chasing Camera with 3D Tutorial Started (i'm new in xna)....

    but, the game also using the first camera not the cashing camera...can some one help me?
    i cant understand the code above...
Page 1 of 1 (13 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG