General math rotation?

Need help with your PC or Modding Projects?
Post Reply
User avatar
Krejlooc
128-bit
Posts: 526
Joined: Mon Feb 27, 2012 4:27 pm

General math rotation?

Post by Krejlooc »

I'm trying to write an algorithm for rotating pixels on a screen at my work office and I don't have any graph paper to check my math. Math has never been my strongest suit in the first place, and I need to make sure my algorithm for rotating around an original set of pixels is sound. Does this look right?

X' = (X*cos(theta)) - (Y*sin(theta))
Y' = (Y*sin(theta)) - (X*cos(theta))

where X' and Y' are the new pixel locations from the original X and Y, and theta is in degrees. Anybody better at math than I want to give this a look? I took it from the following rotation matrix formula:

[X', Y'] = [cos(theta) - sin(theta), sin(theta) - cos(theta)]*[X, Y]
User avatar
MrPopo
Moderator
Posts: 24190
Joined: Tue Aug 26, 2008 1:01 pm
Location: Orange County, CA

Re: General math rotation?

Post by MrPopo »

From wiki:

x' = x cos theta - y sin theta
y' = x sin theta + y cos theta

Also, you need to make sure your coordinants are with respect to the point you are trying to rotate around (i.e. 0,0 is the rotation point) and that theta is correct for the orientation of the axes. The standard axes for pixels measurements are 0,0 in the upper left corner, with Y being positive as you go down. So you'll need to adjust for that.
Blizzard Entertainment Software Developer - All comments and views are my own and not representative of the company.
User avatar
Krejlooc
128-bit
Posts: 526
Joined: Mon Feb 27, 2012 4:27 pm

Re: General math rotation?

Post by Krejlooc »

MrPopo wrote:From wiki:

x' = x cos theta - y sin theta
y' = x sin theta + y cos theta

Also, you need to make sure your coordinants are with respect to the point you are trying to rotate around (i.e. 0,0 is the rotation point) and that theta is correct for the orientation of the axes. The standard axes for pixels measurements are 0,0 in the upper left corner, with Y being positive as you go down. So you'll need to adjust for that.
Ah, I mixed up the x and y in my y' calculation, I always suck at transposition

With respect to rotation point, how would I modify the formula to account for this? I figured 0,0 would be my rotation point in the upper left corner, and that I could manually reposition to the sprite after rotation if I needed to rotate around, say, the center of the sprite, but it'd be much easier if I could select a point of rotation from within the formula. Even if my rotation would typically be around the origin.
User avatar
Krejlooc
128-bit
Posts: 526
Joined: Mon Feb 27, 2012 4:27 pm

Re: General math rotation?

Post by Krejlooc »

Thinking about it, I'm guessing something like this?

X' = ((X-oX)*(cos*theta)) - ((Y-oY)*(sin*theta))
Y' = ((X-oX)*(sin*theta)) + ((Y-oY)*(cos*theta))

where oX and oY are the x and y of the point of rotation. Are there any glaring flaws with this?
User avatar
MrPopo
Moderator
Posts: 24190
Joined: Tue Aug 26, 2008 1:01 pm
Location: Orange County, CA

Re: General math rotation?

Post by MrPopo »

Nope, that looks good. Just remember that since Y increases as you go down, theta increases as you go clockwise.
Blizzard Entertainment Software Developer - All comments and views are my own and not representative of the company.
User avatar
Krejlooc
128-bit
Posts: 526
Joined: Mon Feb 27, 2012 4:27 pm

Re: General math rotation?

Post by Krejlooc »

MrPopo wrote:Nope, that looks good. Just remember that since Y increases as you go down, theta increases as you go clockwise.
That should by accounted for, however, because sin and cos are swapped in the Y' calculations, though, right?
User avatar
MrPopo
Moderator
Posts: 24190
Joined: Tue Aug 26, 2008 1:01 pm
Location: Orange County, CA

Re: General math rotation?

Post by MrPopo »

No, the formula you're working off of is the basic rotation formula based on standard coordinate axes. The cosine terms will be unaffected, but the sine terms will be inverted.

So if you want theta to grow counter-clockwise you would negate the sine terms, giving you:

x' = xcos + ysin
y' = ycos - xsin
Blizzard Entertainment Software Developer - All comments and views are my own and not representative of the company.
User avatar
BoringSupreez
Next-Gen
Posts: 9738
Joined: Wed Feb 11, 2009 10:09 pm
Location: Tokyo

Re: General math rotation?

Post by BoringSupreez »

This thread reminds me of why I don't miss school, and how I'm so not looking forward to college. Seriously, that stuff sucks.
prfsnl_gmr wrote:There is nothing feigned about it. What I wrote is a display of actual moral superiority.
User avatar
Krejlooc
128-bit
Posts: 526
Joined: Mon Feb 27, 2012 4:27 pm

Re: General math rotation?

Post by Krejlooc »

MrPopo wrote:No, the formula you're working off of is the basic rotation formula based on standard coordinate axes. The cosine terms will be unaffected, but the sine terms will be inverted.

So if you want theta to grow counter-clockwise you would negate the sine terms, giving you:

x' = xcos + ysin
y' = ycos - xsin
Much appreciate for the help!
User avatar
Krejlooc
128-bit
Posts: 526
Joined: Mon Feb 27, 2012 4:27 pm

Re: General math rotation?

Post by Krejlooc »

BoringSupreez wrote:This thread reminds me of why I don't miss school, and how I'm so not looking forward to college. Seriously, that stuff sucks.
I miss having time to spend learning new tricks, especially from when I was young and in highschool. Highschool was a breeze for me and I had tons of free time I spent reading books on programming. I learned more about programming in highschool in my free time, than I did in college. And I got my BS in software engineering lol.
Post Reply