2010-01-12 192 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; 

不知道對太陽的動畫。它在將其綁定到滑塊後可以手動工作