2010-01-12 195 views
1

我有ModelVisual3D的立方体。我想同时翻译和旋转它。我希望旋转中心位于立方体的中间(立方体围绕其自身的轴旋转)。 但是,当我尝试应用这两个转换时,效果不是你所期望的。由于物体正在平移,所以旋转的中心是不同的,从而使它以奇怪的方式移动和旋转。我如何获得理想的效果?3D动画在WPF中同时旋转和翻译

Transform3DGroup transGroup = new Transform3DGroup(); 

DoubleAnimation cardAnimation = new DoubleAnimation(); 
cardAnimation.From = 0; 
cardAnimation.To = 3; 
cardAnimation.Duration = new Duration(TimeSpan.FromSeconds(2)); 

Transform3D transform = new TranslateTransform3D(0,0,0); 
transGroup.Children.Add(transform); 


RotateTransform3D rotateTransform = new RotateTransform3D(); 
AxisAngleRotation3D rotateAxis = 
    new AxisAngleRotation3D(new Vector3D(0, 1, 0), 180); 
Rotation3DAnimation rotateAnimation = 
    new Rotation3DAnimation(rotateAxis, TimeSpan.FromSeconds(2)); 
rotateAnimation.DecelerationRatio = 0.8; 

transGroup.Children.Add(rotateTransform); 


Model.Transform = transGroup; 

transform.BeginAnimation(TranslateTransform3D.OffsetXProperty, 
    cardAnimation); 
rotateTransform.BeginAnimation(RotateTransform3D.RotationProperty, 
    rotateAnimation); 

回答

2

先应用旋转,然后再应用翻译。这将确保立方体围绕正确的点旋转 - 它是中心,然后进行翻译。

我的意思是颠倒你应用转换的顺序。矩阵乘法不可交换,因此根据您应用变换的顺序,您将得到不同的结果。

它也可能更容易更新矩阵和重新彻底改造,而不是每次都将增量。

+0

您的意思是将立方体旋转到位然后翻译?因为这不是我想要的,所以我希望它能够在地球移动并围绕其轴旋转时进行平移和旋转...... – synepis 2010-01-13 08:39:14

+0

谢谢您的回答!这正是我所期待的。 – synepis 2010-01-14 11:51:56

0

下面是它可以在不转换,改造完成:

rotateTransform.CenterX = 0; 
rotateTransform.CenterY = 0; 
rotateTransform.CenterZ = 0; 

不知道对太阳的动画。它在将其绑定到滑块后可以手动工作