Answered by:
RTS Camera
Question

Hi,
I've went through any XNA game component, tutorial or forum thread out there and still can't find an example for what I need. Maybe someone could enlighten me or point me in the right direction!?
Trying to create a basic RTS style camera. The thing is when doing composite rotations no matter which axis I feed to the Rotate() method, eventually "yaw" and "pitch" rotations composite into a "roll". I want to keep my camera's roll level with the XZ plane. I tried using both "world" and "camera" up and left vectors for this. Didn't work.
What do I need to do?Saturday, November 4, 2006 11:16 PM
Answers

Newborn wrote: Hi,
I tried using both "world" and "camera" up and left vectors for this.
The solution is to use the camera X axis (for pitch) and the world Y axis (for yaw) to do your rotations. To achieve this you just need to make sure you do your matrix multiplies in the correct order.
Here's an example. Let's say you have a camera matrix called "camera" that you want to perform "yaw" and "pitch" rotations on:
//We want to just deal with rotations, so hold onto the current translation and then zero it out in the matrix
Vector3 translation = camera.Translation;
camera.Translation = Vector3.Zero;
//Rotate locally for pitch, rotate globally for yaw
camera = Matrix.CreateRotationX(pitch) * camera * Matrix.CreateRotationY(yaw);
//Restore translations
camera.Translation = translation;
Don't forget to invert the camera matrix before using it as a view matrix:
Matrix view = Matrix.Invert(camera);Sunday, November 5, 2006 8:11 AM
All replies

I recommend you forget about the yaw/pitch/roll thing and model this using Matrix.CreateLookAt(..)Sunday, November 5, 2006 12:37 AM

Newborn wrote: Hi,
I tried using both "world" and "camera" up and left vectors for this.
The solution is to use the camera X axis (for pitch) and the world Y axis (for yaw) to do your rotations. To achieve this you just need to make sure you do your matrix multiplies in the correct order.
Here's an example. Let's say you have a camera matrix called "camera" that you want to perform "yaw" and "pitch" rotations on:
//We want to just deal with rotations, so hold onto the current translation and then zero it out in the matrix
Vector3 translation = camera.Translation;
camera.Translation = Vector3.Zero;
//Rotate locally for pitch, rotate globally for yaw
camera = Matrix.CreateRotationX(pitch) * camera * Matrix.CreateRotationY(yaw);
//Restore translations
camera.Translation = translation;
Don't forget to invert the camera matrix before using it as a view matrix:
Matrix view = Matrix.Invert(camera);Sunday, November 5, 2006 8:11 AM 
a better way to implement any camera is to build it on basis vectors imho. u just have 3 vectors (dir, side, up) and also have position of the camera. in yaw/pitch/roll u change basis, u also can change position in zoom. then just calculate view matrix once u need it.
there was excellent camera tutorial on gametutorials.com but now it's not free of charge
Sunday, November 5, 2006 8:51 AM 
Well, its not necessarily a XNA answer I have for you... However, I'm working on creating some of the samples from the new Charles River Media book: Programming an RTS Game with Direct3D. I'm looking through the book and trying to adapt as many of the code samples to XNA/C# from DirectX 9/C++. There's a very large chapter on Cameras with a focus on RTS. You might want to check out this book, it might give you some ideas you can adapt for XNA. Good luck!Monday, November 6, 2006 7:33 AM

Thanks for the answers, all helpful!
And I will be checking that book out regardless :)
I ended up solving this by not doing accumulative rotations, but rather accumulating the total angle and then recreating the complete rotation each time from scratch. Add quaternion usage to that in order to avoid the 90 degree problems and that's that.
CheersMonday, November 6, 2006 9:03 PM