代码之家  ›  专栏  ›  技术社区  ›  synepis

在WPF中同时旋转和平移三维动画

  •  1
  • synepis  · 技术社区  · 15 年前

    我有一个立方体的ModelVisual3D。我想同时翻译和旋转它。我希望旋转中心在立方体的中间(立方体绕自己的轴旋转)。 但是当我尝试应用这两种转换时,效果并不是您所期望的。因为物体在移动,所以旋转中心是不同的,所以它以一种奇怪的方式移动和旋转。我怎样才能达到预期的效果?

    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 回复  |  直到 12 年前
        1
  •  2
  •   ChrisF    15 年前

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

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

    更新矩阵和重新应用完整的转换也可能更容易,而不是每次都应用一个增量。

        2
  •  0
  •   Carsten    12 年前

    以下是不进行翻译和转换的方法:

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

    不知道动画中的太阳。它在绑定到滑块后手动工作