Sign in to follow this  
kelendil87

Follow a path with the camera in XNA

Recommended Posts

kelendil87    122
Another day, another problem. I hope that someone more expert can explain how to resolve. I have a Camera (a LookAt camera) and I want to move it around trought a prefixed path, without user input. I move it with input with this code inside the Update method.
                KeyboardState keys = Keyboard.GetState();
                if (keys.IsKeyDown(Keys.Up))
                    cameraRotation.Y -= elapsedTime;
                if (keys.IsKeyDown(Keys.Down))
                    cameraRotation.Y += elapsedTime;
                if (keys.IsKeyDown(Keys.Left))
                    cameraRotation.X -= elapsedTime;
                if (keys.IsKeyDown(Keys.Right))
                    cameraRotation.X += elapsedTime;

                Matrix viewRotationMatrix = Matrix.Transpose(ViewRotation);
                Vector3 cameraMovement = Vector3.Zero;
                if (keys.IsKeyDown(Keys.S))
                    cameraMovement += viewRotationMatrix.Forward;
                if (keys.IsKeyDown(Keys.W))
                    cameraMovement += viewRotationMatrix.Backward;
                if (keys.IsKeyDown(Keys.A))
                    cameraMovement += viewRotationMatrix.Right;
                if (keys.IsKeyDown(Keys.D))
                    cameraMovement += viewRotationMatrix.Left;
                cameraPosition += cameraMovement * 40.0f * elapsedTime;
And this is What the method ViewRotation do:
Matrix ViewRotation
        {
            get
            {
                return Matrix.CreateRotationY(cameraRotation.X) *
                    Matrix.CreateRotationX(cameraRotation.Y);
            }
        }

I need to write a AutoPilot function that bring this camera to another X,Y,Z position with a certain speed. It isn't important if it do a a Ray or a Circle to get to the new position. How can I do? Thanks [Edited by - kelendil87 on April 22, 2008 1:48:49 PM]

Share this post


Link to post
Share on other sites
MJP    19786
It seems to me like you have everything you need, except you need to determine which X and Y angles to use to make the camera point at the destination. Fortunately this is pretty easy:


Vector3 camToDestination = destination - camera.position;
camToDestination.Normalize();

float desiredRotX = Math.Atan2(y, z);
float desiredRotY = Math.Atan2(x, z);



Now with these two angles you could simply set them as "cameraRotation.Y" and "cameraRotation.X" and your camera would instantly point towards the destination point, and you could make it move towards the point by calling viewRotationMatrix.Forward.

Now if you want it to turn smoothly and move in arcs...


cameraRotation.X += (desiredRotX - cameraRotation.X) * rotSpeed * dt;
cameraRotation.Y += (desiredRotY - cameraRotation.Y) * rotSpeed * dt;

Share this post


Link to post
Share on other sites
kelendil87    122
Mmmm... If I have understand properly ( but i don't think so :D ), i need to do something like:



public void GoToPosition(Vector3 destination, GameTime gameTime)
{
Vector3 camToDestination = destination - cameraPosition;
camToDestination.Normalize();

float desiredRotX = (float)Math.Atan2(camToDestination.Y,
camToDestination.Z);
float desiredRotY = (float)Math.Atan2(camToDestination.X,
camToDestination.Z);

cameraRotation.X = desiredRotX;
cameraRotation.Y = desiredRotY;
Matrix viewRotationMatrix = Matrix.Transpose(ViewRotation);
cameraPosition += viewRotationMatrix.Forward *
10.0f *
(float)gameTime.ElapsedGameTime.TotalSeconds;

}



And I recall this in my Update section. But the camera doesn't go to destination and it doens't move smootly. Where I'm wrong?

[Edited by - kelendil87 on April 22, 2008 5:30:28 PM]

Share this post


Link to post
Share on other sites
MJP    19786
Well like I said if you simply assign the new rotation values directly to cameraRotation it will immediately point the camera towards the direction rather than turning smoothly. You have to use that last bit of code I posted (or something similar) if you that's not what you want.

As for it moving in the wrong direction, it looks like I switched your X and Y rotation values (you have your rotation about the X axis in cameraRotation.Y, and vice versa). Sorry about that.

Share this post


Link to post
Share on other sites
kelendil87    122
I try all the different solution. But always there is something wrong.

For example, with this:


public void GoToOrbita(Vector3 destination, GameTime gameTime)
{
Vector3 camToDestination = destination - cameraPosition;
camToDestination.Normalize();

float desiredRotX = (float)Math.Atan2(camToDestination.X, camToDestination.Z);
float desiredRotY = (float)Math.Atan2(camToDestination.Y, camToDestination.Z);
cameraRotation.X = desiredRotX;
cameraRotation.Y = desiredRotY;
Matrix viewRotationMatrix = Matrix.Transpose(ViewRotation);
cameraPosition += viewRotationMatrix.Forward * 2.0f * gameTime.TotalGameTime.Seconds;
}


The function start when I press the T button. But it brings the camera always heigher. It never LookAt my destination and never go on it. Can you be a little more specific on what is wrong?

I'm a little new on game programmig, as you can see :(. Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this