zeppeldep
zeppeldep

Reputation: 27

XNA orientation of models

I cannot get the orientation of models correct. In this project the user loads 3 DXF files.

The first two data sets are dealt with correctly in the C#/XNA development. The user can drive along the road using up and down keyboard keys. But I fail at the third process. I cannot get the orientation of oncoming traffic correct. The position is ok, one van at every 100 metres, but the orientation is wrong.

The strangest of all is the normal method to transform the models to the correct place and orientation is causing the models to be on top of the camera! Where did I go wrong? I have made the source code available at the following URL, so you can view the code and see where I went wrong (the zip includes the DXF files used for testing):

http://www.4shared.com/zip/Lk3xmCtO

Thanks in advance.. Kevin.

This is the most incorrect part:

Matrix worldMatrix = Matrix.CreateTranslation(VanPos[vi]);
Matrix[] transforms = new Matrix[Van.Bones.Count];
Van.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh modmesh in Van.Meshes)
{
foreach (BasicEffect be1 in modmesh.Effects)
{
    be1.World = transforms[modmesh.ParentBone.Index] * worldMatrix;

this results in the models being placed at the camera position. using just the CreateTranslation is placing the models at the correct place but the orientation is incorrect.

Upvotes: 2

Views: 380

Answers (1)

Dervall
Dervall

Reputation: 5744

You are never setting an orientation, which means that your stuff is never going to rotate. From some code I have laying around, this is how I draw model meshes.

Key points:

  • You need to include the projection and view matrix and set them to your effects. You might do this, in some following code.
  • An Orientation matrix needs to be created. There are lots of convenience functions to help you do this like Matrix.CreateRotationY
  • Beware of ordering when multiplying matrices. I'm sure you've noticed this but the order really matters. I tend to mess this up and fiddle around with the multiplication order to make things work :)

    public void Draw(Matrix projection, Matrix view)
    {
        // Copy any parent transforms.
        var transforms = new Matrix[model.Bones.Count];
        model.CopyAbsoluteBoneTransformsTo(transforms);
    
        foreach (var mesh in model.Meshes)
        {
            // This is where the mesh orientation is set, as well 
            // as our camera and projection.
            foreach (BasicEffect effect in mesh.Effects)
            {
                Matrix world = Orientation; // <- A Matrix that is the orientation
                world.Translation = Position; // <- A Vector3D representing position
                effect.World = transforms[mesh.ParentBone.Index] *
                    world;
    
                effect.View = view;
                effect.Projection = projection;
            }
            // Draw the mesh, using the effects set above.
            mesh.Draw();
        }
    }
    

Upvotes: 1

Related Questions