Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to create a simple 3D game but things aren't working out as they should. For instance, the mode will not display. I created a class that does the rendering so I think that is where the problem lies.

P.S I am using models from the MSDN website so I know the models are compatible with XNA.

Code:

    class ModelRenderer
    {
        private float aspectratio;
        private Model model;
        private Vector3 camerapos;
        private Vector3 modelpos;
        private Matrix rotationy;
        float radiansy = 0;
        public ModelRenderer(Model m, float AspectRatio, Vector3 initial_pos, Vector3 initialcamerapos)
        {
            model = m;
            if (model.Meshes.Count == 0)
            {
                throw new Exception("Invalid model because it contains zero meshes!");
            }
            modelpos = initial_pos;
            camerapos = initialcamerapos;
            aspectratio = AspectRatio;
            return;
        }
        public Vector3 CameraPosition
        {
            set
            {
                camerapos = value;
            }
            get
            {
                return camerapos;
            }

        }
        public Vector3 ModelPosition
        {
            set
            {
                modelpos = value;
            }
            get
            {
                return modelpos;
            }
        }
        public void RotateY(float radians)
        {
            radiansy += radians;
            rotationy = Matrix.CreateRotationY(radiansy);
        }

        public float AspectRatio
        {
            set
            {
                aspectratio = value;
            }
            get
            {
                return aspectratio;
            }
        }
        public void Draw()
        {


            Matrix world = Matrix.CreateTranslation(modelpos) * rotationy;
            Matrix view = Matrix.CreateLookAt(this.CameraPosition, this.ModelPosition, Vector3.Up);
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), this.AspectRatio, 1.0f, 10000f);
            model.Draw(world, view, projection);
        }
    }

If you need more code just make a comment.

share|improve this question

3 Answers 3

You're combining your model's rotation & translation backwards when setting your world. Your camera is looking at ModelPosition but ModelPosition has been rotated out of view by rotationy.

edit - More accurately explained, the camera is looking at a point in space called 'ModelPosition'. But the model is not located at 'ModelPosition' because it has been rotated away by rotationy. But when you combine those two matrices the other way around, you rotate 'world' first, it will simply rotate in place at the origin, then you combine the translation to send the rotated matrix right to 'ModelPosition' where the camera is looking for it.

Try:

Matrix world = rotationy * Matrix.CreateTranslation(modelpos);
share|improve this answer
    
I tried what you wrote but it still doesn't display the model. I don't think that matters because a * b is the same as b * a. –  Daniel Lopez Nov 26 '11 at 1:27
4  
@Daniel I think you need to read up on linear algebra - matrix multiplication is not commutative, which means A * B != B * A –  Max Nov 26 '11 at 1:34
    
Sorry you are right, I forgot. –  Daniel Lopez Nov 26 '11 at 2:55
up vote 1 down vote accepted

Okay I finally solved it. I noticed that I was drawing the model before calling spriteBatch.End(). So I drew the model after I called spriteBatch.End().

share|improve this answer
    
Do note that after any SpriteBatch has been run, several rendering options will have changed - which can be a problem when drawing 3D models, so you may want to change them back before doing 3D drawing. Here's the list of what changes in XNA 3.1, and here's a cheat sheet to help you find the XNA 4.0 equivalents. –  Jonathan Hobbs Apr 18 '12 at 22:12

Model.Draw is an XNA function so we can rule that out as the problem and more likely it's in one of the matrix arguments you're passing. Try pointing the camera at the origin and centering the model by setting the world matrix to Matrix.Identity. Then you can start from there to see if your model is still rendering or not.

If you still don't see anything, it could be several things, either face culling is in the wrong direction, AspectRatio being zero, or a bad depth setup in your viewport (less likely).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.