tags:

views:

21

answers:

1

I am having trouble with my 2D rotation implementation. I'm trying to create a pan feature for a map (move your mouse up; it should move the map down.) The map is just a 2d background image that is able to be rotated, etc. The problem is when it's in a rotated state moving my mouse up will continue to move the mouse in direction as if it had 0 degrees of rotation. (IE. A rotated map of 90 degrees would switch moving the mouse up with moving the mouse left.) I tried implementing a fix below, but in certain map angles moving up will move the map down & slightly left and moving up will move the map up & slightly right. Which I am looking to remove the "slightlys."

In the OnMouseMove event, I am getting the difference between the previous position and current position to determine how much my mouse moved in the X & Y axis. I then take those X and Y differences and toss it into a 2D rotation formula to calculate the rotated X, Y difference. I take that rotated difference and scale it and add it to my old map point's position to get the new latitude and longitude. This works great for 0 degree coordinates just gets off slightly with something like 93 degrees.

Any help on why it would be getting slightly off? Thanks!

double cosA = Math.Cos(MapAngle.DecimalRadians);
double sinA = Math.Sin(MapAngle.DecimalRadians);

double dx = (_oldMouseX - args.MouseX);
double dy = (_oldMouseY - args.MouseY);

double rdx = (cosA*dx - sinA*dy);
double rdy = (sinA*dx + cosA*dy);

double latitude = oldMapPoint.Latitude.DecimalDegrees + rdy * latitudePerPixelScale;

double longitude = oldMapPoint.Longitude.DecimalDegrees + rdx * longitudePerPixelScale;
A: 

Why are you transforming your mouse coordinates at all? If you don't want your mouse coordinate system to rotate with your image, don't apply the image rotation to the mouse coordinates!

edit: So I re-read the bottom part of your post and realized that you're just trying to correct for the initial rotation.

What you should do is not correct for the mouse being rotated but instead rotate the map around its local coordinates and translate in world coordinates.

This will have the effect of composing both transformations in the manner in which you describe.

Ron Warholic