So how do I setup a camera in 3D?

Started by
1 comment, last by Ron AF Greve 14 years, 11 months ago
Hi, I have a beautiful model rendered on my screen now, but at wrong angle and position. I am creating a 3D real-time strategy game so what I need is a top-down camera with a slight angle.

      foreach (ModelMesh mesh in myModel.Meshes)
      {
        // This is where the mesh orientation is set, as well as our camera and projection.
        foreach (BasicEffect effect in mesh.Effects)
        {
          effect.EnableDefaultLighting();
          effect.AmbientLightColor = new Vector3(0.0f, 0.0f, 0.0f);
          effect.View = Matrix.CreateLookAt(new Vector3(0,0,0), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
          effect.World = Matrix.CreateTranslation(0, 0, 0);
          effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 1680f / 1050f, 0.1f, 100f);
        }
        // Draw the mesh, using the effects set above.
        mesh.Draw();
      }
How would I make my model to appear like in strategy games in that code? I have tried millions of different number combinations and mostly I see nothing rendered or the camera is inside the model or something weird... If I can just get a typical like strategy game view then I can work my way from there.. Basically, a top-down view into my model, but slight angle so that the 3D'ish becomes more seeable. :) Can someone help me out? And ah, is there a link to any tutorials explaining what all those view, project, world are exactly supposed to do? Perhaps a tutorail with pictures?
Advertisement
Oh, I changed some camera *position* values and this is what I saw!

http://img216.imageshack.us/img216/6992/tanku.png

Don't laugh! My tank looks horrible, wtf?
Hi,

It works as follows:

Your world matrix:
Usually you model your tank using some 3D package and it is located in the origin (that makes editing so much eassier). This is called model-space.

Now likely your tank will not merely sit in the origin of the world. It is translated rotated etc. To do that it is multiplied with again a matrix. This is called the world-matrix and yes, that is one matrix per object.

Also your camera is not likely to be located in the center but floating through your world instead. Yet again this camera would be multiplied by a matrix. The view-matrix. However we do not actually multiply the cam by that instead we rotate the whole world with the inverse of the view matrix. Picture it, if you rotate the camera 30 degrees around Y and you look through it. It would seem the world is rotated -30 degrees.

Then the projection matrix. If your screen is not a square drawing a square would result in a rectangle. So the projection matrix adjusts this by squeezing X (or y). That is why you pass the resolution to it. (There is usually also some optimizing in the Z direction but that is not really important). Also it needs to clip stuff i.e. after all the transformations in your case anything with a Z less than or greater than 0.1 or 100 won't be visible.

In the end the X and Y values are divided by Z to get the correct 2D coordinates and the X and Y are a bit translated in that the origin will be in the middle of the 2D screen.

I just looked at you picture and see what the problem is. Your problem seems to be in model space. I guess you modelled the different parts from different views. Either you have to model everything from one point of view (top-down, side etc). Or export the transformations and handle them in your code (you get a hierarchy of matrix multiplications).

So cam and view are ok. You need to build a world matrix for every part of the tank and render them independently.
Ron AF Greve

This topic is closed to new replies.

Advertisement