Wednesday, January 13, 2010

Rotation... Again

Down to finally matching doors, but it took a helluva effort.
I figured out how to rotate an array completely, but given a certain index in the array? This was just as difficult as figuring out how to rotate an array in the first place.

The problem was, I was looking for an easy formula I could plug-in and just go. It seems like, given an index, it should be easy-peasy to find the rotated variety. What I was forgetting, however, was that it's never that easy.

Short of rereading the door positions every time I rotate, I figured I could look up rotating a point around another point:

x' = Cos(Theta) * x - Sin(Theta) * y
y' = Sin(Theta) * x + Cos(Theta) * y

Where Theta is radians from degrees of desired rotation. This showed me, though, my biggest mistake -- it only rotates around the origin. X=0, Y=0. I had to specify the origin as the center of the array. After I came to that realization, it all fell into place:

private void rotate(Vector2 point, Vector2 origin, int degrees)
{
float radians = MathHelper.ToRadians(degrees);

Vector2 v = point - origin;

v = new Vector2((int)Math.Round((v.X * Math.Cos(radians) - v.Y * Math.Sin(radians))),
(int)Math.Round((v.X * Math.Sin(radians) + v.Y * Math.Cos(radians))));

point = v + origin;
}

That big chunk in the middle is the standard rotation formula.

Now, my doors fit as long as the orientation and positions match! Although the positions when rotated are no longer exclusively the upper-left... so I have to work in the door width for the final fitting calculations.

Victory!

No comments:

Post a Comment