Akshay Vats
Akshay Vats

Reputation: 170

Local Position of Bone wrt Model

I have a requirement to get either position matrix or position vector of a bone (say wheel) with respect to my model (car).

What I have tried -

Vector3.Transform(mesh.BoundingSphere.Center , transforms[mesh.ParentBone.Index]*Matrix.CreateScale(o.Scaling )))

Above doesn't give accurate result.

Upvotes: 0

Views: 315

Answers (1)

Ani
Ani

Reputation: 10896

What you want is to calculate the absolute transforms for each bone. The CopyAbsoluteBoneTransformsTo method can do it for you.

It is equivalent to the following code:

    /// <summary>Calculates the absolute bone transformation matrices in model space</summary>
    private void calculateAbsoluteBoneTransforms() {

      // Obtain the local transform for the bind pose of all bones
      this.model.CopyBoneTransformsTo(this.absoluteBoneTransforms);

      // Convert the relative bone transforms into absolute transforms
      ModelBoneCollection bones = this.model.Bones;
      for (int index = 0; index < bones.Count; ++index) {

        // Take over the bone transform and apply its user-specified transformation
        this.absoluteBoneTransforms[index] =
          this.boneTransforms[index] * bones[index].Transform;

        // Calculate the absolute transform of the bone in model space.
        // Content processors sort bones so that parent bones always appear
        // before their children, thus this works like a matrix stack,
        // resolving the full bone hierarchy in minimal steps.
        ModelBone bone = bones[index];
        if (bone.Parent != null) {
          int parentIndex = bone.Parent.Index;
          this.absoluteBoneTransforms[index] *= this.absoluteBoneTransforms[parentIndex];
        }
      }

    }

Taken from here.

Upvotes: 1

Related Questions