Public Group

# Camera Movement Trouble

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

## Recommended Posts

Hi, I've been working on this for weeks now, and everyday I sit down in front of my computer for hours on end I seem to still get nowhere. I've read numerous tutorials on the subject, and tried different things so many times and over again, so I was hoping someone here would be kind enough to help me out. What I want to do is get the camera to follow my object (in this case the airplane, eventually to be a car) in third person. The xna msdn tutorial on the subect seems good, but I just cannot for the life of me get it to work. I am using C# with managed directx 9 (not XNA). At the moment, the airplane seems to rotate okish, but when it moves forward, the camera goes completely haywire in a slanted circle type way. Eventually I want to convert the code to OOP, but for now I'm basing this on the code from the Riemers.com tutorial.

private void SetUpCamera() {

device.Transform.Projection = Matrix.PerspectiveFovLH(1.0f, this.Width / this.Height, 0.3f, 500f);

Vector3 thirdPersonReference = new Vector3(0, 20, -20);

Matrix rotationMatrix = Matrix.RotationY(spacemeshangles.X);

// Create a vector pointing the direction the camera is facing.
Vector3 transformedReference = Vector3.TransformNormal(thirdPersonReference, rotationMatrix);

// Calculate the position the camera is looking from.
Vector3 cameraPosition = transformedReference + spacemeshposition;

// Set up the view matrix and projection matrix.
device.Transform.View = Matrix.LookAtLH(cameraPosition, spacemeshposition, new Vector3(0, 1, 0));

//Viewport viewport = graphics.GraphicsDevice.Viewport;
//float aspectRatio = (float)viewport.Width / (float)viewport.Height;
device.Transform.Projection = Matrix.PerspectiveFovLH(viewAngle, this.Width / this.Height, 0.1f, 500f);

}


That's the code trying to set the camera to be third person, full code is here: http://insoul.net/code.cs Thanks for taking the time to read this. Jess.

##### Share on other sites
Matrix rotationMatrix = Matrix.RotationY(spacemeshangles.X);

You are creating a rotation matrix around the Y-axis, but you're using spacemeshangles.X (not spacemeshangles.Y). Is this intentional?

BTW, you're setting the projection matrix twice (that's not what's causing you're problem but still...).

Also, you might want to take a look at the source code found here. It has code for a camera class that uses vector rotations. It's not third-person (I think), but you can probably change that. Vector rotations can be easier to visualize and work with.

##### Share on other sites
Thanks so much for the reply. Changed the spacemesh angles thing - it was intentional, although I'm not fully sure why I intended it. Thanks for pointing out the two projection calls too.

I took a look at that source code, helped a little in my understanding, but I'm still having problems.

The camera seems a little more stable now in terms of it doesn't go off on a slant anymore.

The plane seems to rotate ok, and go forward/back ok now. The camera also rotates, but from a fixed point; so while the plane flies off into the distance and rotates, the camera merely sits still and rotaes which obviously isn't what I need.

I presume I'm only translating the rotation of the plane, but I don't see how that's happening.

device.Transform.View = Matrix.LookAtLH(normedVector, spacemeshposition, new Vector3(0, 1, 0));

I understand the 3 arguments for this method are cameraPosition, cameraTarget and up. But what Vectors should these be? Everything I try: normalised, unormalised, transformed references from the tutorial etc just don't seem to give the right result. I don't want or like to sound like I haven't researched any of this 3D space theory stuff as I really have, but I feel I'm missing some important aspect.

##### Share on other sites
Maybe try this:

public void SetupCamera(){    Vector3 t = planePos + thirdPersonReference;        Matrix rot = Matrix.RotationY(-spaceMeshAngles.Y);    Matrix trans = Matrix.Translation(-t.X, -t.Y, -t.Z);    device.Transform.View = trans * rot;    // Setup other needed stuff    // ...}

Also try removing the minus sign from -spaceMeshAngles.Y if that code doesn't work.

One other thing - a long time ago, I tried creating a camera class using Matrix.LookAtLH(). It was a major pain and I gave it up and constructed the view matrix the way it's constructed in the link I gave you. Made things much easier. If my code above doesn't work (and it probably won't...), I strongly suggest that you take another look at that link and figure out how the code there works.

1
2

##### Share on other sites
Thanks for the input - tried the code you gave, and although it didn't work, I managed to get the camera working with some more tinkering and when, like you suggested, stopped using Matrix.LookAtLH(). Not sure why that wasn't working, but using the method you suggested certainly helped, so thanks!

I had a good read of those links and I understand what's going on more than I did which is great. Thanks again.

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
13
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013658
×