views:

556

answers:

2

Hi,

I have ModelVisual3D of a cube. I want to translate and rotate it at the same time. I wish the center of rotation to be in the middle of the cube(the cube rotates around its own axis). But when I try to do this applying both transformations the effect is not what you would expect. Since the object is translating the center of rotation is different thus making it move and rotate in a strange way. How do I get the desired effect?

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  A: 

Apply the rotation first and then the translation. This will ensure that the cube rotates about the correct point - it's centre, and then gets translated.

What I meant was to reverse the order you're applying your transformations. Matrix multiplication is not commutative so you'll get different results depending on the order you apply the transform.

It might also be easier to update the matrices and reapply the complete transformation rather than applying a delta each time.

ChrisF
Do you mean rotate the cube in place and then translate? Beacause this is not what I want, I want it to be translating and rotating just as the earth moves and rotates around its axis...
sklitzz
Thank you for your answer! That is what I was looking for.
sklitzz
A: 

Here's how it can be done without translate transform:

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

Dont know for sun in animation. It works manually after binding it to a slider

rDsouza