tags:

views:

540

answers:

1

I have a simple 3D cube that I can rotate using the following code:

void mui3D_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{

            RotateTransform3D rotation = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0), mui.Model.Bounds.Location);

            DoubleAnimation rotateAnim = new DoubleAnimation(0, 130d TimeSpan.FromMilliseconds(3000));

            rotateAnim.Completed += new EventHandler(rotateAnim_Completed); 

            mui.Transform = rotation;

            rotation.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, rotateAnim);
    }

Each time it executes, this code rotates the cube using an animation around the Y axis from an angle of 0 to 130 degrees.

However I would like to apply the rotation "cumulatively" so that the any previous rotation is taken into account and the cube commences each rotation from the angle it finished the previous rotation.

For example: the animation constructor, instead of requiring a "from" and "to" value for the angle, simply rotates the cube an an additional 130 degrees based on whatever the current rotation angle is.

I could easily use a member variable that contains the current angle, pass it to the animation and then update it when the animation has completed. But I'm wondering if there is a standard approach using WPF to acheive this.

Any thoughts?

+1  A: 

I'm sure there's a method for retrieving the current Euler rotation angle in degrees from the object's transformation matrix. You could then use that as the "from" value and animate to the "to" value.

Failing that, simply create a variable somewhere in your application that remembers the number of degrees the cube as been rotated. Each time the function is run just add the number of degrees you'd like it to rotate and then store the result back in your variable.

some pseudo-code:

angle = 0
function onClick:
    new_angle = angle + 30
    Animate(angle, new_angle)
    angle = new_angle
Soviut
Do you know the method for retrieving the Euler rotaion from the objects transofrm matrix? I'm a bit rusty on 3D math but it sounds what I'm looking for. (Otherwise looks like I've got even more reading to do). I've already tried your second option as I said in the question. Thanks anyway.
Ash
Chances are good that the matrix object has functions for extracting the rotation, scale, translation, etc. I don't know off hand since I haven't used that particular 3D library.
Soviut