[FIXED]Placing 3D models on the map

Hello everyone,

I am trying to place the 3D model we created on the map. I do draw it but it currently draws over me. And me being the camera.

I want to be able to draw the model on the map on a position I give it and then later add a form of AI to it.
The IMP is the model we are using. Any help is greatly appreciated.
This is the code currently being used.

            Matrix[] transorms = new Matrix[IMP.Bones.Count];

        foreach (ModelMesh mesh in IMP.Meshes)
            foreach(BasicEffect effect in mesh.Effects)
                effect.World = transorms[mesh.ParentBone.Index] * Matrix.CreateTranslation(IMPPos);
                //effect.World = Matrix.CreateTranslation(IMPPos);
                effect.View = Matrix.CreateLookAt(IMPPos, Vector3.Zero, Vector3.Up);
                effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), GraphicsDevice.Viewport.AspectRatio, 0.5f, 1000f);
        // TODO: Add your drawing code here
        IMP.Draw(effect.World, effect.View, effect.Projection);`

Greetings jeromer


is the position of the object as a Vector3

The problem is that your camera position is also IMPPos

You can set your camera position here:
effect.View = Matrix.CreateLookAt(IMPPos, Vector3.Zero, Vector3.Up);
The first value is the current position of the camera, the second value is the position we are looking at.

So you could for example set the first value to 0,10,10 and the second one to IMPPos and you will look at the model

What you really want to do is

  • Have a camera object, with the current camera position and direction
  • Have several objects for your game entities, with a position, rotation, and model

In the render you would then

  • Create a view matrix -
    Matrix view = Matrix.CreateLookAt(Camera.Position, Camera.Position + Camera.Direction, Vector3.Up);
    Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), GraphicsDevice.Viewport.AspectRatio, 1,500);

And then use these two matrices for each model draw

effect.World = Scale * Rotation * transform[parent etc. ] * Translation

mode.Draw(effect.World, view, projection);`

Thanks for the response.
Yes that is right IMPPos is the model vector.

I might have phrased my question wrong. What I want is to move the camera around through our map.
The imp is one of the enemies somewhere in the map. I also have a seperate camera class to move the camera.
But I can’t seem to draw it at the position i want it to be. I hope that makes it a bit clearer.

I’ve added some stuff to the first response.

in effect.View = Matrix.CreateLookAt(IMPPos, Vector3.Zero, Vector3.Up);
You have IMPPos and V3.Zero switched.

Once again thanks for the response and once again I am sorry, I must be doing something wrong. IT still draws on my camera. And not as I want it.I want to draw the model on my “floor” and be able to move the camera to and from it. As in any standard fps where there are models on the map. My current code for drawing the model is this:

        Matrix[] transorms = new Matrix[IMP.Bones.Count];
        Matrix view= Matrix.CreateLookAt(camera.Position, camera.Position + camera.Rotation, Vector3.Up);
        Matrix projection= Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), GraphicsDevice.Viewport.AspectRatio, 0.5f, 1000f);
        foreach (ModelMesh mesh in IMP.Meshes)
            foreach(BasicEffect effect in mesh.Effects)
                effect.World = transorms[mesh.ParentBone.Index] * Matrix.CreateTranslation(IMPPos)*Matrix.CreateRotationY(5f);
                //effect.World = Matrix.CreateTranslation(IMPPos);
                //effect.View = 
                //effect.Projection = 
        // TODO: Add your drawing code here
        IMP.Draw(effect.World, view, projection);

Thanks in advance,


that looks correct.

If it still draws on your camera then your camera.Position and IMPPos must still be related.

It’s also possible that the model is so large that it appears it’s still on the camera, when in fact, the camera has moved a bit (or the model has moved a bit, same thing).

Btw camera.Postion+ camera.Rotation makes little sense if you are actually calculating the rotation.

In the same vein for correct rotation you want to multiply your rotation matrix * translation matrix instead of the other way round for the model

We have had some private conversation and I looked at the code, but I will post here so in future maybe someone finds a solution if they have a similar problem

The way this program was set up was like this

Draw the environment / map
Draw the sprites / text
Draw the model (which has the problem)

Now, it turns out the model was, as i expected, waaaaaaaaaaaaaaay too large. So large in fact, that I had to set the scale on it to 0.00005 or something like that. You can do that in the Content.mgfx already, so you don’t have to create the matrix in game, but either way is fine.

So it was a giant model alright.

But the real problem was the fact that it was drawn after the spritebatch.

The spritebatch sets the depths buffer to DepthBuffer.None

So … our giant model was way behind our environment/map, but it was drawn on top since there was no depth comparison.
This gave the illusion that it “sits” on top of the camera and moves with the camera, when in fact it was a perspective illusion