# Simple 2D rotation

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

## Recommended Posts

Hi. I want to get a 2D object to rotate around another one. They are both in world coordinates. Here's what I have so far: Point t = center of object that I want to rotate around. Point p = position of the object that I want to rotate. I've concatenated 3 matrices (each 3x3): Matrix concat = TransInv*Rotation*Translate.
where Translate is:
1 0 -t.x
0 1 -t.y
0 0   1

Rotation is:
cos(a) -sin(a) 0
sin(a)  cos(a) 0
0       0    1

TransInv is:
1 0 t.x
0 1 t.y
0 0  1

and I get:
| cos(a) -sin(a)  -t.x*cos(a) + t.y*sin(a) + t.x |
| sin(a)  cos(a)  -t.x*sin(a) - t.y*cos(a) + t.y |
|   0       0                1                   |

So then I use that matrix and transform 'p':
Point temp;
temp.x = cos(a)*p.x + -sin(a)*p.y + -t.x*cos(a) + t.y*sin(a) + t.x;
temp.y = sin(a)*p.x +  cos(a)*p.y + -t.x*sin(a) - t.y*cos(a) + t.y;
p = temp;

[Every time before I apply that transform on p, I reposition it to it's original location (because angle 'a' accumulates each frame)] And so, what I get is that as the object rotates below position 't', it strays really far away from its circular path but as the object rotates above position 't', it's fine. What the heck am I doing wrong here? Thank you. [Edited by - 16bit_port on February 9, 2008 9:05:37 PM]

##### Share on other sites
I'm no expert at this kind of matrix transformation, but let me take a stab in the dark here.

It looks to me like your mistake begins when you multiply the matrices.

Point temp;temp.x = cos(a)*p.x + -sin(a)*p.y  + -t.x*cos(a) + t.y*sin(a) + t.x;temp.y = sin(a)*p.x +  cos(a)*p.y  + -t.x*sin(a) - t.y*cos(a) + t.y;p = temp;

Can you see what that part of the code is doing? Once you can, you'll know that its the problem. I think you should have summed the matrices or such, and ended up with this:

temp.x = cos(a)*p.x + -sin(a)*p.y + t.x;temp.y = sin(a)*p.x +  cos(a)*p.y + t.y;

##### Share on other sites
It looks like you may have reversed the order of your transformations, depending on whether your system uses premultiplication or postmultiplication of vectors.

##### Share on other sites
I tried reversing the orders of the matrices and even tried
temp.x = cos(a)*p.x + -sin(a)*p.y + t.x;temp.y = sin(a)*p.x +  cos(a)*p.y + t.y;

but neither works.

With the first one, it's all over the place on the screen. With the second one, it's no where to be seen.

So, I'm pretty sure mine is correct for the most part and I'm just forgetting a small minor detail. Anybody?

##### Share on other sites
Try:
temp.x = cos(a)*(p.x - t.x) - sin(a)*(p.y - t.y) + t.x;temp.y = sin(a)*(p.x - t.x) + cos(a)*(p.y - t.y) + t.y;

If this doesn't work, you probably have an error somewhere else.