TWild

Hi all. I'm having trouble with my camera class. All is well (walking, elevating, strafing) until I pitch up a bit and start yawing. What happens is, as I'm yawing, the camera seems to be rolling in the direction of the yaw on its own. This only happens when I have my camera type set to FPS, where I'm trying to limit to yawing on the world Y axis. The FREE type works as intended and yaws on it's own local Y (Up) axis. Any ideas

Before:

http://img435.imageshack.us/img435/2787/ss1ja1.jpg

After pitching about 35 degrees up and then yawing to the left several times around:

http://img282.imageshack.us/img282/4946/ss2aq7.jpg

I pasted my Yaw, Pitch, and BuildView methods below.

Yaw

public void Yaw(float angle, float dt)

{

Matrix m = new Matrix();

// If camera is FPS style, create rotation around world Y

if (m_Type == CamType.FPS)

{

m = Matrix.CreateRotationY(angle * dt);

}

// If camera is FREE style, create rotation around local Up axis

else if (m_Type == CamType.FREE)

{

m = Matrix.CreateFromAxisAngle(m_Up, angle * dt);

}

// Transform Right and Look vectors by rotation matrix

m_Right = Vector3.Transform(m_Right, m);

m_LookAt = Vector3.Transform(m_LookAt, m);

}

PITCH

public void Pitch(float angle, float dt)

{

Matrix m = new Matrix();

// Create rotation around local Right axis

m = Matrix.CreateFromAxisAngle(m_Right, angle * dt);

// Transform Look and Up vectors by rotation matrix

m_LookAt = Vector3.Transform(m_LookAt, m);

m_Up = Vector3.Transform(m_Up, m);

}

BuildView

public void BuildView()

{

// Building the matrix (right-handed)

// rx, ux, fx, 0

// ry, uy, fy, 0

// rz, uz, fz, 0

// -r.p, -u.p, f.p, 1

m_LookAt = Vector3.Normalize(m_LookAt);

m_Right = Vector3.Cross(m_Up, m_LookAt);

m_Right = Vector3.Normalize(m_Right);

m_Up = Vector3.Cross(m_LookAt, m_Right);

m_Up = Vector3.Normalize(m_Up);

// Build view matrix

float x = -Vector3.Dot(m_Right, m_Pos);

float y = -Vector3.Dot(m_Up, m_Pos);

float z = -Vector3.Dot(m_LookAt, m_Pos);

m_View.M11 = m_Right.X;

m_View.M12 = m_Up.X;

m_View.M13 = m_LookAt.X;

m_View.M14 = 0.0f;

m_View.M21 = m_Right.Y;

m_View.M22 = m_Up.Y;

m_View.M23 = m_LookAt.Y;

m_View.M24 = 0.0f;

m_View.M31 = m_Right.Z;

m_View.M32 = m_Up.Z;

m_View.M33 = m_LookAt.Z;

m_View.M34 = 0.0f;

m_View.M41 = x;

m_View.M42 = y;

m_View.M43 = z;

m_View.M44 = 1.0f;

}

### Re: XNA Game Studio Express Camera woes

Kaisor

If your trying to use a right handed coordinate system, you're doing a few things backwards...

m_Right = Vector3.Cross(m_Up, m_LookAt); In a right handed syste, up X at will give you the left vector.

Switch this to:

m_Right = Vector3.Cross(m_LookAt, m_Up);

m_Up = Vector3.Cross(m_LookAt, m_Right); this is giving you the Down vector

Switch this to:

m_Up = Vector3.Cross(m_Right, m_LookAt);

the Matrix class has setters for Right, Up, and Forward. You can use these

m_view.Right = m_Right;

m_View.Up = m_Up;

m_View.Forward = m_LookAt;

Hope this helps

### Re: XNA Game Studio Express Camera woes

TWild

Thanks for the reply. I changed those two lines and now it seems like everything is upside down - instead of hills, I have valley's and all of that is above the camera, which is upside down too.

BTW, I'm pretty much using the camera class from Frank Luna's "Introduction to 3D Game Programming with DirectX 9.0." I was able to get the camera to work flawlessly in DX9, but I think switching over to RH coordinates is what is confusing me.

### Re: XNA Game Studio Express Camera woes

dafin0

very nice looking map man, i see u know what your doing. i would just like to know if u need any help with the art/texture side of things with this game. i have done a post here ill just copy and past so u can see what im all about. once again nice work so far.

"Texture artist looking for work

if you need someone to do textures for your levels i would be more than happy to help you out there, i have been doing computer art for 5 years and working on game textures for 2 years. i can do photorealistic textures at high and low resolution for 3d and 2d game levels, i can do particle effects textures at low and high resolution. I¡¯m not very skilled in starting skins for 3d objects but if given a low quality out line for the skin i can do it up to look much better.

If you would like to see some of my work all you have to do is ask and ill send a email with it, and if you would like me to do the textures for you i would just like to know what type of game your thinking of making or making now.

Thanks for your Time, David fink (dafin0)"

### Re: XNA Game Studio Express Camera woes

TWild

dafin0 wrote:
 very nice looking map man, i see u know what your doing. i would just like to know if u need any help with the art/texture side of things with this game. i have done a post here ill just copy and past so u can see what im all about. once again nice work so far. "Texture artist looking for work if you need someone to do textures for your levels i would be more than happy to help you out there, i have been doing computer art for 5 years and working on game textures for 2 years. i can do photorealistic textures at high and low resolution for 3d and 2d game levels, i can do particle effects textures at low and high resolution. I¡¯m not very skilled in starting skins for 3d objects but if given a low quality out line for the skin i can do it up to look much better. If you would like to see some of my work all you have to do is ask and ill send a email with it, and if you would like me to do the textures for you i would just like to know what type of game your thinking of making or making now. Thanks for your Time, David fink (dafin0)"

Thanks, when I start actually building a game from the engine I'll look you up.

### Re: XNA Game Studio Express Camera woes

dafin0

thanks man, im really looking for a start in the game texture deign. If you would like my email its dafin02@hotmail.com and some art i have done at www.freewebs.com/dafin0art (nothing really there to show textures i can do that would go into a game... looking at a picture of grass or wood is not that incredible)

TWild

ok, thanks

### Re: XNA Game Studio Express Camera woes

Kaisor

I think the problem is when you build your matrix. Instead of setting each component try this.

m_view.Right = m_right;

m_view.Up = m_Up;

m_view.Forward = m_LookAt;

### Re: XNA Game Studio Express Camera woes

Shazen

Check out the example in the xna help that ships with xna. It has an example of how to set up a FPS camera that has a lot less code than what your using. I adapted it to my scene & it works great.

I think I found it in the reference section under Matrix. At the bottom of the page was a link to it.

### Re: XNA Game Studio Express Camera woes

TWild

Kaisor wrote:
 I think the problem is when you build your matrix. Instead of setting each component try this. m_view.Right = m_right; m_view.Up = m_Up; m_view.Forward = m_LookAt;

Ok, I'll try that.

Do you know what row/col the members represent Below is how the MSDN shows the view matrix is calculated (DX Right Hand - Matrix.LookAtRH -which I'm sure XNA uses the Right-Handed system) http://msdn.microsoft.com/archive/default.asp url=/archive/en-us/directx9_m_Summer_04/directx/ref/ns/microsoft.directx/s/matrix/m/lookatrh.asp

```zaxis = normal(cameraPosition - cameraTarget)
xaxis = normal(cross(cameraUpVector, zaxis))
yaxis = cross(zaxis, xaxis)

xaxis.x      yaxis.x      zaxis.x     0
xaxis.y      yaxis.y      zaxis.y     0
xaxis.z      yaxis.z      zaxis.z     0
-dot(xaxis, cameraPosition) -dot(yaxis, cameraPosition) -dot(zaxis, cameraPosition) 1```

Do the members Matrix.Right, Matrix.Up, and Matrix.Forward represent {M11, M21, M31}, {M12, M22, M32}, and {M13, M23, M33}, respectively (Looking like a column vector matrix)

which seems that Matrix.Right would be {M11, M12, M13}. Also, what about the members Matrix.Backward and Matrix.Down

This is all pretty confusing to me and is something I need to get straight. I believe that once I have this figured out I can solve my cam issue easily.

### Re: XNA Game Studio Express Camera woes

TWild

Shazen wrote:
 Check out the example in the xna help that ships with xna. It has an example of how to set up a FPS camera that has a lot less code than what your using. I adapted it to my scene & it works great.I think I found it in the reference section under Matrix. At the bottom of the page was a link to it.

I'm trying to port my DX9 code over to XNA and a lot of it was already complete. My cam was working fine over there and I'd like to get the equivilant to work over here. If I just can't get it, I'll give that a try. Thanks

### Re: XNA Game Studio Express Camera woes

Mike36

I have a similar problem with a quaternion-based camera and I'm perplexed about why it happens. I posted the source for my demo (the camera is in the Camera.cs classfile) that demonstrates the problem at: http://www.spellflight.com/GameModels/SkysphereDemo.zip It is best seen using the mouse, holding down the right-mouse button and rolling it in a circle.

### Re: XNA Game Studio Express Camera woes

TWild

Mike36 wrote:
 I have a similar problem with a quaternion-based camera and I'm perplexed about why it happens. I posted the source for my demo (the camera is in the Camera.cs classfile) that demonstrates the problem at: http://www.spellflight.com/GameModels/SkysphereDemo.zip It is best seen using the mouse, holding down the right-mouse button and rolling it in a circle.

It almost seems to be a UP problem. Like the Up vector is being transformed along with the Look or something, when it should stay at {0.0f, 1.0f, 0.0f}.

### Re: XNA Game Studio Express Camera woes

Mike36

My camera is based upon the one created by Michael Schuld at www.thehazymind.com and I'm very much a newbie when it comes to understanding the matrix math behind the camera, so to speak. I asked him about the z-rotation phenomenon. He pointed out that in my case, the camera rotation is relative to the camera's position and not the world position. So if I understand correctly, after you, say, rotate X, the camera is in a new position. Then rotating on Y axis is relative to the camera's new position. After some experimentation with his suggested solution, I finally got my camera to pivot in the way I wanted. I just re-uploaded my demo source with the changes, so maybe that'll help you.

### Re: XNA Game Studio Express Camera woes

Kaisor

Here's the deal with the matrix

Default:

right.x    right.y    right.z      0

up.x       up.y        up.z        0

back.x   back.y   back.z   0

pos.x     pos.y     pos.z      1

So XNA coordinate system looks like this:

+X (right)  -- To your right

+Y (up) -- up

+Z (backward) -- behind you

-Z (Forward) -- Into the screen;

I havn't seen all of your code, so I don't know how you're handling this, but the camera's transformation matrix and view matrix are not the same.

Create the transformation matrxi by setting:

m_transformation.Right = m_Right;

m_transformation.Up = m_Up;

m_transformation.Forward = m_LookAt

m_transformation.Translation = m_Position;

then create the veiw matrix like so:

m_view = Matrix.CreateLookAt(m_Position, m_Position + m_LookAt, m_Up);

Let me know if this works.