Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating a full 360 degrees in WPF 3D

I have a ModelVisual3D of a cube and I want to animate it to rotate around its axis for 360 degrees. I make a RoationTransform3D which I tell to rotate 360 but it doesn't rotate at all, also if you say 270 degrees it rotates only 90 degrees but in the opposite direction. I guess he computer calculates the "shortest path" of the rotation. The best solution I have come up with is to make one animation turn 180 and after it finishes call another 180 to complete the full rotation. Is there a way to do it in one animation?

RotateTransform3D rotateTransform = new RotateTransform3D();
myCube.Model.Transform = rotateTransform;

AxisAngleRotation3D rotateAxis =
      new AxisAngleRotation3D(new Vector3D(0, 1, 0), 180/*or 360*/);
Rotation3DAnimation rotateAnimation =
      new Rotation3DAnimation(rotateAxis, TimeSpan.FromSeconds(2));

rotateTransform.BeginAnimation(RotateTransform3D.RotationProperty,
      rotateAnimation);
like image 424
synepis Avatar asked Jan 14 '10 12:01

synepis


2 Answers

My understanding is that the Rotation3DAnimation uses a Spherical Linear interpolation, so it will always find the shortest path.

One workaround is to use Rotation3DAnimationUsingKeyFrames: setup a key frame at 120, 240, and 360 and you should be good.

Sorry no code right now, I don't have WPF on this machine...

-Jason

like image 64
Jason Hernandez Avatar answered Oct 08 '22 15:10

Jason Hernandez


I know this was already answered but in my search for a better way to do this I found this alternative:

You can set the rotation value to 180 (or half of what you want to rotate) and then set the repeat behavior to repeat twice and "IsCummulative" to true.

like image 26
Mark Avatar answered Oct 08 '22 16:10

Mark