Rotating objects attached to other objects

后端 未结 1 711
滥情空心
滥情空心 2021-01-24 16:59

I have a question concerning a single mesh (building) that contains a collection of animated objects (these objects make up the complete building, rising up from the ground when

相关标签:
1条回答
  • 2021-01-24 17:01

    The transformation you're looking for is this:

    Matrix positionRotationMatrix = Matrix.CreateTranslation(-parentPosition) 
                                   * Matrix.CreateFromQuaternion(parentRotation) 
                                   * Matrix.CreateTranslation(parentPosition);
    Vector3 translation = Vector3.Transform(parentPosition + relativePosition,
                                   positionRotationMatrix);
    

    You define your world matrix with the new sub-objects position (translation variable):

    Matrix worldMatrix = Matrix.CreateScale(scale)
                    * Matrix.CreateFromQuaternion(rotation)
                    * Matrix.CreateFromQuaternion(parentRotation)
                    * Matrix.CreateTranslation(translation);
    

    To demonstrate, here's a cube (parent) that is being rotated around it's position, with an arrow whose position and rotation are defined in relation to its parents position and rotation. The arrow follows the cubes rotation, and rotates around it's Z axis independently.

    enter image description here

    Values used for the demonstration:

    parentPosition = Vector3(-1.1f, 0f, -2); //cube
    relativePosition = Vector3(0, 0, -3); //arrow
    parentRotation = Quaternion.CreateFromRotationMatrix(
                           Matrix.CreateRotationZ(angle) 
                         * Matrix.CreateRotationY(angle) 
                         * Matrix.CreateRotationX(0.5f));
    rotation = Quaternion.CreateFromYawPitchRoll(0f, 0f, angle);
    angle += 0.05f;
    
    0 讨论(0)
提交回复
热议问题