Rotate a 3D object around its center and own axes in WPF 3D

In WPF 3D, various transformations could be applied to an object. One particular problem that I occasionally run into is when I want to apply a rotation transformation to an object and rotate it around its own center and axes.

The way I do it is as follows. First I apply 3 identity RotateTransform3D‘s to the object with AxisAngleRotation3D‘s objects underneath: one for the X axis, one for the Y axis and one for the Z axis. Then whenever I want to rotate the object around a certain axis, I obtain the corresponding RotateTransform3D object, set its center according to the (possibly) translated center of the object, and apply the rotation angle to the underlying AxisAngleRotation3D object. Some code will make this more clear.

First apply the 3 identity transforms to the object:

Let’s say the function SetRotation is to be used for setting the rotation of the object. It will work as follows:

3 comments

    • Eric on April 6, 2017 at 3:26 AM
    • Reply

    Hello,

    thanks a lot for the info!

    when I am rotating my cube (with 6 parallel sides of Course but height != depth !=width) the Rotation around Y (Rotation lying on ground) works perfect, but when I rotate around Z (Up vector is 0,1,0), the Rotation Looks strange, works but after it the height is still the base height before, the width is still the base width as before and not switched those two…

    Any idea what’s wrong?

    Thanks a lot!
    Eric

    1. What do you mean “strange”? I’m not sure I understand what you mean by “after it the height is still the base height before, the width is still the base width as before and not switched those two…”. Could you post some images?

    • Robert Morgan on August 28, 2018 at 7:57 PM
    • Reply

    I tried the example and noticed that my Z doesn’t transform like the others. X and Y seem to interact with the other perfectly but Z gives me a problem. No matter which way I move X or Y, Z is using always using my world coordinates which is point up.

Leave a Reply

Your email address will not be published.