Sign in to follow this  
watermelonChris

Some questions about positioning/matricis/rotations

Recommended Posts

Hey guys...

Can someone please explain to me how the 'world matrix' relates to objects, I understand that a matrix is a equation/formula for translating/rotating/scaling operations. However, a model does not have a 'rotation' vector as far as I can see, so what is the matrix acting/translating on? Wouldeach object/model have it's own world matrix?

When you have a model you would like to rotate, should one keep a 'rotationX/Y/Z' variable in radion/degrees? How is the rotation 'state' stored?

Second question, I've encountred times when positioning my camera that if I have say the camera position vector3 set as (0, 5, 0), It won't display anytihng on screen. However if I add just a tiny increment to the x/z component the models will show on screen? Why does this happen?

Thank you!

Share this post


Link to post
Share on other sites
a "world matrix" moves an objects vertices from their local/object space (the space you "modeled" the program in, using a 3d modeling program for example) to "world space". All objects have their own world matrix, which translates, rotates, and scales the object relative to all the other objects in the scene. Don't forget that the order in which you multiply your transformation matrices together DOES matter, since each transformation matrix does the transformation around the point (0,0,0). for example, if you do this:

objectWorldMatrix = Rotation * Translation;

The the object will rotate (spin) around the point (0,0,0) (assuming you modeled the object around the point 0,0,0 in the modeling program), then it will move the object to another location in the world. but if you did:

objectWorldMatrix = Translation * Rotation;

Then the object will be moved to another location in the world, and then the rotation is applied and the object is again rotated around (0,0,0), which will now create an "orbiting" effect, instead of just spinning on it's own axis.

The way you store rotations depends on how you do them really. There are a couple different functions to get a rotation matrix, something like rotate only around the x axis, rotate only around the y axis, or rotate around all axes. usually they take the rotation in degrees radians. So if you are going to rotate your object every frame, you will want to keep track of the degrees that it has rotated, and add to the degrees every frame. You'll want to check to make sure if the degrees are greater than 3.14 and change it back to 0, and if they are smaller than 0, change them to 3.14. You will be creating a fresh rotation matrix each frame, so its the degrees that you want to keep track of. There are other ways to rotate objects too like using quaternions for example.

About your second question. that could be so many things, but my first guess is that you are not doing your multiplications correctly or you are skipping a step. It sounds like you have your models positioned in projection space and not world space (projection space has a 0 to 1 z axis, so if its greater than 1 or less than 0, it will not be displayed on the screen). If this happens to be your problem, make sure you are sending the World * View * Projection matrix to the shaders (at least pixel shader). But maybe thats not the problem, just a suggestion. Another thing it could be is you are moving your camera WAY too fast. for example, if your model is 5 units in front of the camera like you say, but you move your camera 1 unit each frame, if you are getting 30 frames per second, that's the camera moving 30 units every second. most likely though you are probably getting hundreds to thousands of frames per second if you have a very simple application. try slowing down the movement of your camera if you are even moving your camera. Another thing it could be is you have your far plane and near plane too close to each other when you set up your projection matrix.

Share this post


Link to post
Share on other sites
Thank you for your reply.

"All objects have their own world matrix, which translates, rotates, and scales the object relative to all the other objects in the scene."
So I need to create a matrix for each model?

Where is the monkey's matrix in this example? Is it creating a matrix automatically?
In which case if I were to add a second object/model, how would I modify the below?
[code]Matrix[] transforms = new Matrix[monkey.Bones.Count];
float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
monkey.CopyAbsoluteBoneTransformsTo(transforms);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
Matrix view = Matrix.CreateLookAt(cameraPosition, cameraView, Vector3.Up);
foreach (ModelMesh mesh in monkey.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.View = view;
effect.Projection = projection;
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(monkeyPosition);
}
mesh.Draw();
}[/code]

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