# Follow a path with the camera in XNA

This topic is 3711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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 on other sites
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

• 10
• 9
• 48
• 12
• 10
• ### Forum Statistics

• Total Topics
631383
• Total Posts
2999687
×