简体   繁体   中英

C# XNA Camera position follow concrete mesh/bone

How to make the camera position was dependent on the rotation and position of a particular element (mesh / bone) in the model.

 #region CameraModeEye
 if (mode == CameraMode.Eye) // do poprawienia
 {
     Matrix[] transforms = new Matrix[objectModel.Model.Bones.Count];
     objectModel.Model.CopyBoneTransformsTo(transforms);

     // test
     Matrix World = (transforms[objectModel.Model.Meshes["Eye"].ParentBone.Index] * objectModel.Transform) * transforms[3];

     new_position = transforms[3].Forward;
     new_lookAt = new_position + Vector3.Forward;
     camVector = Vector3.Up;
 }
 #endregion

 ...

 view = Matrix.CreateLookAt(new_position, new_lookAt, camVector);

I have a model of a robot ( in the shape of a scorpion ) that has such elements as: Body, Head, Eye.

"Head" is rotated with the axis X and is dependent on the "Body" and "Eye" rotates with the Y axis and is dependent on the "Head".

I wanted the camera position to mount slightly from the "Eye". I have tried many ways but every time I get something like this:

World.Forward {X: -2,301742E-24 Y: 3,456487E-14 Z -1}

where Actually it should be something like this:

              {X: -10.0         Y: 500.0        Z: 0}

I can not understand. I get tired of it for two days and I can not find a solution. Please help

if (mode == eye)//pseudoed
{
  Matrix[] transforms = new Matrix[objectModel.Model.Bones.Count];
     objectModel.Model.CopyAbsoluteBoneTransformsTo(transforms);

  Vector3 cameraPosition = (transforms["eye"] * objectModel.Transform).Translation;//I'm assuming objectModel.Transform is the world matrix for the model 
  Vector3 cameraLookAt = cameraPosition + transforms[eye].Forward;//camera looking in the same direction as the eye

  view = Matrix.CreateLookAt(cameraPosition, cameraLookAt, Vector3.Up);
}

This code should convey the way to do what I think you are trying to do. Notice I use CopyAbsoluteBoneTransformsTo rather than just CopyBoneTransformsTo . The Absolute version of transforms[eye] is actually the result of multiplying the eye to the head to the body which gives the worldspace location and orientation of the eye. Whereas the transforms[eye] without using the Absolute version is simply the rotational and translational difference between the eye and the head (which is what your code was using).

Your World.Forward should not give -10,500,0, but possibly (World.Translation + World.Forward) might.

OOO, exactly what I meant.

Later I understood that I should use 'CopyAbsoluteBoneTransformsTo' and not 'CopyBoneTransformsTo'. Finally, I wrote this code but that he was my 11 lines. : D

Now, the operation on Matrix's takes only 3 lines:

 Matrix[] transforms = new Matrix[objectModel.Model.Bones.Count];
 objectModel.Model.CopyAbsoluteBoneTransformsTo(transforms);

 Matrix offset = Matrix.CreateTranslation(Vector3.Down * 70 + Vector3.Forward * 10);
 new_position = (offset * transforms[3] * objectModel.Transform).Translation;
 new_lookAt = new_position + transforms[3].Down; 

I had to add one additional matrix (it is stored globally in the classroom), which is responsible for moving position of the element model of "Eye". Otherwise, the camera position is inside the "Eye" and the view is hide behind wall model.


It was just another little problem. Namely, there is no rotation axis Z. I do not know how he would explain it.

Suppose that:

  • Human neck is my Bone "Head" and you can only rotate it forward and backward.

  • Human head is my Bone "Eye" and you can only rotate it to the left and right.

When rotate to the neck is back, and the head rotate to the left or to the right we see the image at an angle.

My robot is looking in front all the time as if they just missed the Z-axis rotation

Did I should use something like that at the very end?

 view * = Matrix.CreateRotationZ (MathHelper.ToRadians (Rotz));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM