Public Group

# 2D rotation

This topic is 621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I would do this with OpenGL's rotation functions but I can't on the thing I'm doing this so I need to do it "manually"

I'm trying to rotate an object following this https://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm

But I'm not really sure what "r" or "w" stands for in that, and what's the difference between "initial angle" and "angle of rotation"?

Normally I'd just call my sister and ask about maths who knows all math stuff but I can't atm

##### Share on other sites
But I'm not really sure what "r" or "w" stands for in that, and what's the difference between "initial angle" and "angle of rotation"?

r = distance from origin (radius).

w = angle of rotation. By how much are we rotating the object. This is also listed as f.

initial angle = the current angle of rotation. How much is the object currently rotated. (This is listed as q and see later in the formulas.)

The formulas are a bit confusing, because they swap between using q (which is the same as q) and w (which is the same as f).

Edit: Ninja-ed Alberth!

Edited by Lactose!

##### Share on other sites

Why not use glm? It operates like OpenGL does, but it's a template library, so it's purely C++ code.

'r' looks like a radius, the distance from origin (ie sqrt(x*x + y*y) ). 'w' looks like a typo to me, I think it should be 'f'. At least 'r sin ( q + f )' makes a lot more sense to me.

The page seems to assume you already rotated 'q', so that's your initial angle (and basically that's how you ended up being in your current location (x, y)). You want to rotate an additional 'f' now.

It's not very clearly explained to say the least. In the final set of equations (for x' and y') there is no q, only f, x, and y.

I hope this helps enough.

Edit: Ninja-ed by Lactose!

Edited by Alberth

##### Share on other sites

No wonder it was confusing for me too since that "w" probably is a typo. But the fact is, I've always been bad at maths..

Thanks alot for the explanations, I understand it now!

EDIT: I've used GLM once before. I'll check it out again in the future if I run to similiar "issues"

Edited by yps_sps

##### Share on other sites
Once you understand complex numbers, you almost don't need to remember any formulas. Rotation around the origin is multiplication by (cos(theta) + i * sin(theta)).

So x + i * y maps to

(x + i * y) * (cos(theta) + i * sin(theta)) = x * cos(theta) + i * y * cos(theta) + x * i * sin(theta) + i^2 * y * sin(theta)
= (x * cos(theta) - y * sin(theta)) + i * (x * sin(theta) + y * cos(theta))


But you can actually use a complex number class (like std::complex<float> in C++) and you can make (cos(theta) + i * sin(theta)) be the representation you use for rotations. Then applying the rotation is just multiplication.

##### Share on other sites

This is the rotation formula (Capital X,Y is the starting point and lower case is the result - This is XNA notation but the math is the same no matter what you use.):

x = X * (float)Math.Cos(TurnAngle) - Y * (float)Math.Sin(TurnAngle);

y = X * (float)Math.Sin(TurnAngle) + Y * (float)Math.Cos(TurnAngle);

##### Share on other sites

that "w" probably is a typo.

It's not exactly a typo, the fact is that the 'w' is the closest representation of ? (Omega, the greek character, used for angles) in qwerty keyboard.

##### Share on other sites

Okay, so I'm going to break this down for you. In order to demystify the math, first understand what the math is really doing.

Do you understand vectors?
A vector is a point on a graph. It has an x coordinate and a y coordinate. These represent distances along two axes, the x axis and the y axis. Did I lose you? Good. We wont call it x and y axis. Instead, we will call it the "right direction" and the "forward direction". Also, we wont call them coordinates, rather we will call them steps. This mean, a vector represented like so (3,5) mean,s "3 steps to the right, 5 steps forward". Heck, you can even reverse it and say "5 steps forward" followed by "3 steps to the right". Either way, you will get to the same point. This is because of a fundamental property of "axis (or axes plural)". They have no influence on each other. It doesnt matter how many steps to the left or to the right you take, you will never move towards something that is in front of you. Mathematically this means "x axis and y axis are perpendicular (make 90 degrees) to each other", A deeper meaning means "when the axis is projected onto another axis, the value is 0". The two axes, A & B in this graph ARE NOT perpendicular to each other:

https://en.wikipedia.org/wiki/File:Dot_Product.svg

Notice that fancy lingo there? Dot Product, cosine (Theta).. these are a fancy way of saying what I just said. "cos(theta)" means, the fraction of influence that A and B have on each other, when you move along the direction of B, you will move along the direction A by "cos(theta)", and if you move along direction A, you will move allong direction B by "cos(theta). This is also what I mean by "projection onto another axis". earlier.

Let us now look at two very common axes, that you know of, the right and forward axes, or "x" and y". To move along the x axis by 3 steps, we denote that is (3,0). To move along the y axis by 5 steps, we denote that is (0,5). So, To take 3 steps to the right and then 5 steps forward, we get "(3,0) + (0,5) = (3,5)". Conversely, to take 5 steps forward first, and then 3 steps to the right is "(0,5) + (3,0) = (3,5)" again. So what is the axis we keep talking about? Simple, in the case of the right direction, it is (1,0), and in the case of the forward direction, it is (0,1).  so. (3,5) means "3 times the x axis, + 5 times the y axis", or 3 *(1,0) + 5*(0,1). Now, before I proceed further, I want to discuss the "projection" thingy i said.

There is something called the "dot product" (remember from the graph), and what that means is if you have 2 vectors, (a,b) and (x,y), this operation is performed as follows, "a*x +b*y". And you know what this returns? cos(theta)! Or the the influence the two vectors (a,b) and (x,y) have on eachother! It is going to be the most powerfull tool in your handbag as your journey into 2D/3D math continues! So remember the two axes we were talking about? The right direction and the forward direction? (1,0) and (0,1)? Let us perform a little dot product on these 2. (1,0) dot (0,1) is "1*0 + 0*1" or "0 + 0" or 0! THE DOT PRODUCT IS THE MATHEMATICAL WAY OF SAYING NO MATTER HOW MANY STEPS I TAKE TO THE RIGHT OR LEFT, IM NEVER GOING FORWARD OR BACKWARDS".

phew! thats a bit of a handful right? okay. So lets take our 3 steps right + 5 steps forward vector, (3,5). What is the dot product between (3,5) and the right vector (1,0)? can you guess? lets do some math, (3,5) dot (1,0) = (3*1 + 5*0) or 3. Holy shit! The dot product told us something (granted we already new it). It gave us the distance along that direction! How about y? How much influence does (3,5) have along the y axis? (3,*0 + 5*1) or 5! It told us we are 5 steps along the forward direction. What about some aribtrary vector, (a,b) or (x,y)? how much distance is 3 steps right + 5 steps forward have on the vector (a,b)? (which incidently means a steps right and b steps forward). Simple lets employ our handy dandy dot product:

We get (3,5) dot (a,b) = (3*a + 5*b). Awesome. We now now how to find the projection of an arbitrary vector onto another aribtrary vector. This you will find to be pretty cool when you are doing "inverse rotations". Actually, lets get into that now. What if our right direction and forward direction werent aligned with the poles of the earth, what if I was facing at some random angle, lets say our right axis is (a,b), and our forward direction is (x,y). But now, im going to throw a doosy. What if we were not located at (0,0), what instead we were located at (10,10). What would the point (3,5) in the world mean to us? Locally to us, my right is always (1,0), and my forward is always (0,1), and where I am, I'm always at 0,0. Im never over there! Im always where I am, right by myself! According to earth however right and forward (lets say East and North), I'm at (10,10). So what does (3,5) mean to me if I was at 0,0? Simple, (10,10) - (10,10) would put me at (0,0), so lets take (3,5) subtract (10,10), because its all relative! And we get (-7,-5). Cool. But my right and forward are not (1,0) and (0,1) so what is (-7,5) to me in terms of my right and forward directions. ALL HAIL THE DOT PRODUCT! It would be "(-7,-5) dot (a,b)" or "-7a -5b" for my right, and "(-7,-5) dot (x,y)" or "-7x - 5y". Do you kinda get it? At least kinda,kinda?

If you are still with me, great. Lets move on to your actual question. Well, we have inferred this much so far that any point, such as (3,5), really means 3 * x axis + 5 * y axis. This generality holds even when the axes aren't (1,0) and (0,1)! if our axis were (u,v) and (s,t). this would mean 3*(u,v) + 5*(s,t). This would also mean, (3u + 5s, 3v+5t). So lets not worry about the point at all! Lets just describe it is 3 * x axis + 5 * y axis, and Let us just rotate the axis!!

So to perform a 2D rotation on (3,5), we are going to go from "3 * (1,0) + 5*(0,1)  --> 3 * (u,v) + 5*(s,t)" where (u,v) is the rotated x axis by a certain number of degrees, and (s,t) is the rotated y axis by that number of degrees as well. All well and good, BUT HOW DO YOU ROTATE the axes? AAAGH! ITs like inception! axis inside of axis inside of axis... wont stop! Two words. Unit. Circle. You remember how I told you to hail the dot product as your overlord? Well He brought a very important friend with him, and this is your boss's boss. Learn about him to. https://en.wikipedia.org/wiki/File:Unit_circle.svg.

At this point I'm going to end my crazy rambling and open the floor to any questions. If you took the time to read all of this, I feel for you and I truely apologize. The lease I can do is hope this was beneficial. I did not answer your question, but I strongly believe I've given you enough pieces to figure this out! Go forth young padawan , and embrace not the darkness which is err.. not math!

##### Share on other sites

Thanks for the detailed reply fais. I did get it done what I wanted earlier

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 35
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631702
• Total Posts
3001812
×