Sign in to follow this  
tehXKnight

Circular rotation or Rotation about a point

Recommended Posts

tehXKnight    138
Okay, so maybe it's late and tropical storm Fay is getting to my brain, but I can't seem to devise the formula for rotation about a point. I have a center point in 2D for several objects that make up one object and I want to rotate each one around said point keeping the same distance. I was able to deduct that I needed to rotate the objects around the circumfrence of a circle with the distance to center point being the radius. I just want to rotate 2/PI or -2/PI, but I can't seem to locate the formula online or in my math books. I'm only trying to rotate 90 degrees every time so I know that I could brute force it with a switch statement and by swapping the X, Y and negative signs to go to the right rotation, but I was wondering if there was a more elegant solution since I would like to animate the rotation. I do know how to determine the amount of angular displacement based on time: theta = ((2 * PI * r)/(time for one rotation)) * time So I can find the radian based on time to give a smooth rotation (in say a quarter second), but how do I take that and translate point A around a center point C with a radius of r with a rotation of radian R? Simple problem I know. Thanks for any help.

Share this post


Link to post
Share on other sites
jyk    2094
You can find the equation for rotation of a 2-d vector here (about halfway down the page).

To rotate one point about another, you will generally apply a translation such that the pivot point becomes the origin, apply the rotation, and then translate back, e.g.:
vector2 RotatePointAboutPoint(vector2 p, vector2 pivot, float angle) {
vector2 v = p - pivot;
v = RotateVector(v, angle);
return pivot + v;
}
Note that it's usually easier to approach this sort of problem in terms of matrix transforms and transform hierarchies. (Ask if you would like more details on this.)

Share this post


Link to post
Share on other sites
Zipster    2365
Let's say you have point X that you want to rotate around point C. The correct mathematical formula in 2D is:

Y.x = [cos(θ) * (X.x - C.x)] - [sin(θ) * (X.y - C.y)] + C.x;
Y.y = [sin(θ) * (X.x - C.x)] + [cos(θ) * (X.y - C.y)] + C.y;


Although if your angle is always 90°, you can replace those sines and cosines with constants (they simplify to ±1 and 0).

EDIT: Typo [grin]

[Edited by - Zipster on August 21, 2008 12:20:17 PM]

Share this post


Link to post
Share on other sites
tehXKnight    138
Thanks to both of you guys. I hadn't even thought about vector math for some reason. I do like Zipsters solution because it was what I was looking for. Vector transforms would probably be faster but since I'm making a falling blocks ;) for my own personal learning I don't think performance will be much of an issue.

I'll probably use both and play around with them.

Share this post


Link to post
Share on other sites
tehXKnight    138
After looking I realized that they're both the same thing, Zipster simply put it in one equation and jyk split it up to make it more readable.

You did have one problem in your equation Zipster the Y.y equation should read:

Y.y = [sin(θ) * (X.x - C.x)] + [cos(θ) * (X.y - C.y)] + C.y;

Thanks again for the help guys.

Share this post


Link to post
Share on other sites
Zipster    2365
Quote:
Original post by tehXKnight
After looking I realized that they're both the same thing, Zipster simply put it in one equation and jyk split it up to make it more readable.

You did have one problem in your equation Zipster the Y.y equation should read:

Y.y = [sin(θ) * (X.x - C.x)] + [cos(θ) * (X.y - C.y)] + C.y;

Thanks again for the help guys.

Doh, that's right I forgot a cosine, stupid copy-paste [headshake]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this