Search code examples
c#xna

Calculating local positions when I have global positions, global rotations, and local rotaitions


I have a set of data where I have the local rotations, global rotations and global positions for a model. I need to calculate the local positions for each bone.

I first tried doing bone.globalpos - bone_parent.globalpos and then realized that these are not dependent on rotations so this is the difference between the parent and the child bone. I need some advice on how to factor in the rotations as well for the bones. Both the local and global rotations are stored as quaternions.

I store the data a follows per bone

bone.globalpos (Vector3)
bone.globalrot (Quaternion)
bone.localpos (Unknown, but should be a Vector3)
bone.localrot (Quaternion)

Since the local pose should be also based on rotation and not just the distance from the parent, I am lost at how to calculate this.

Example data:

bone_parent.globalpos = (-1.252995  8.46457E-05  37.22802)
bone_parent.globalrot = (-0.5405273 -0.4560547 -0.4560547 0.5405273)
bone_parent.localpos = (0 0 0) (I provided)
bone_parent.localrot = (1.0 0.0 0.0 0.0)
bone_child.globalpos = (-1.252995  5.001462  37.22802)
bone_child.globalrot = (0.4948359 0.505096 -0.505096 0.4948359)
bone_child.localpos = (0  0  5.001377) This is what I need to calculate, but here is the answer to what I'm trying to get
bone_child.localrot = (0.07421875 0.9970703 0 0)

Solution

  • As far as I see, you want to express the direction vector in the parent's coordinate system. This can be done by transforming the direction vector with the parent's inverse global rotation.

    child.localPos = transform(conjugate(parent.globalRot), child.globalPos - parent.globalPos)