0

I have a multitouch WPF application and I'd like to do a rotation of some stuff in it. The problem with ManipulationDelta, that ManipulationOrigin always is a center between 2 fingers, but I'd like to rotate with moving finger around fixed one.

Example: Finger1 fixed on screen and Finger2 goes around it, rotation with center in Finger1 point needed. Then Finger2 fixed and Finger1 goes around - now rotation with center in Finger2 point. All that stuff is done between one session of ManipulationStarted and ManipulationCompleted, because fingers is always on screen.

Now I'm trying to calculate vector between old position and new position, and rotate around finger, which position changed for minimum. It works, but not very well.

Any ideas how to get right rotation point at every moment in more nice way?

UPD: Problem of MSDN example (Clemens's code) - rectangle under Finger1 change it's position, while Finger1 was fixed.

Finger1 and Finger2 on fixed on screen. Two finger on screen

Finger2 rotated around Finger1 Finger2 rotated around FInger1

UPD2: If I do rotation around e.Menipulators.First().GetPosition(RotatingControl) everything is OK. Problem, that finger can be changed and I need to know which finger is moving now. It's not a hard problem. But sometimes both fingers rotates - in such case rotation should occurs around ManipulationOrigin

igofed
  • 1,402
  • 1
  • 9
  • 15
  • How should this work, for example, if the user starts with Finger1 fixed and does a part rotation moving Finger2, then fixes Finger2 and moves Finger1? Should the rotation point be fixed once per manipulation or should it be dynamic? – J... Oct 16 '12 at 16:47
  • It should be dynamically changed depending on current rotating finger. – igofed Oct 16 '12 at 17:53

1 Answers1

1

When transforming an object by the values provided by ManipulationDeltaEventArgs, you would usually update a transform matrix. This could be the Matrix property of a MatrixTransform used as RenderTransform as shown below.

<Rectangle ...>
    <Rectangle.RenderTransform>
        <MatrixTransform x:Name="transform"/>
    </Rectangle.RenderTransform>
</Rectangle>

This matrix would be updated according to translation, rotation and scaling delta values in a ManipulationDelta event handler like this:

ManipulationDelta d = e.DeltaManipulation;
Matrix m = transform.Matrix;
m.Translate(d.Translation.X, d.Translation.Y);
m.RotateAt(d.Rotation, e.ManipulationOrigin.X, e.ManipulationOrigin.Y);
m.ScaleAt(d.Scale.X, d.Scale.Y, e.ManipulationOrigin.X, e.ManipulationOrigin.Y);
transform.Matrix = m;

This transformation will work perfectly with your fixed and moving fingers, except that it not only rotates, but also moves and scales the object. You could omit the scaling if you drop the m.ScaleAt(...) call and get a transform that is still near to what you want, as long as the distance between the fingers does not vary too much.

However, there is still the movement, but that is something you can't get rid of. Imagine you first have finger 1 fixed, then rotate by 180°, then fix finger 2 and rotate back again by 180°. Obviously the total rotation is zero, but the object has moved.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I read this example at MSDN. Problem here is that rectangle rotates around ManipulationOrigin, which is not an real rotation center. Manipulation origin is a center between 2 fingers, so when you fix Finger1 and rotate Finger2 rectangle change position not only under Finger2, but under Finger1 too. I updated question with picure examples. – igofed Oct 17 '12 at 08:17
  • I have understood your problem. What i wanted to say is that you can only achieve the desired behaviour if you not only rotate, but *also translate*. – Clemens Oct 17 '12 at 08:34
  • I updated question. I can rotate right without translation, but on every DeltaManipulation I should calculate right finger and there are some problems with that. – igofed Oct 17 '12 at 08:45
  • Can't you see that you simply *have to* take translation into account. Otherwise it will never work well, at least not with WPF manipulation events. You could of course build you own touch processing from scratch. – Clemens Oct 17 '12 at 09:59
  • 1
    See [Input Overview, Touch and Manipulation](http://msdn.microsoft.com/en-us/library/ms754010.aspx#touch_and_manipulation) for the details of input and manipulation event flow. – Clemens Oct 17 '12 at 10:03
  • Thank you, now I can see such simple thing as translation while rotation. I fixed all my stuff. – igofed Oct 17 '12 at 16:28