Sign in to follow this  
megatron242

Rotation around an arbitrary reference point

Recommended Posts

Hi, I'm writing this simple game, where each entity is represented by a 2d origin point, and angle(which rotates the entity around it current point). Using this scheme it's extremly easy to manage the entities, except for the case where I need to rotate an entity around an arbitrary reference point. any ideas? thanks, Vince

Share this post


Link to post
Share on other sites
Quote:
Original post by megatron242
I'm writing this simple game, where each entity is represented by a 2d origin point, and angle(which rotates the entity around it current point).
So each entity has a location, and an orientation?
Quote:
except for the case where I need to rotate an entity around an arbitrary reference point.
So you need to change the entity's location and orientation, by rotating the entity around a point?

Rotations are additive, so the orientation is trivial: just add the rotation to the entity's existing rotation. As for the location, you need to use trigonometry (in particular sine and cosine). It might be easier to envision it as a rotation about the origin to start with, and later generalise to an arbitrary point.

Share this post


Link to post
Share on other sites
A simple way to rotate a point around another point is with basic trigonometry:

def rotatePoint(point, origin, radians):
displacement = point - origin
point.x = displacement.x * cos(radians) + displacement.y * sin(radians)
point.y = displacement.y * cos(radians) - displacement.x * sin(radians)

Share this post


Link to post
Share on other sites
The general way is to wrap the rotation (or scaling or whatever) by a translation that makes the desired origin temporarily the 0. Here the emphasis lies on "temporarily". This means that the translation has to be "undone" after the rotation is performed, namely by appliying another translation. E.g. in terms of a matrix product for column vectors the entire transformation looks like
T * R * T-1
where T denotes the translation matrix to the desired origin, and R the rotation matrix.

The rotatePoint(...) routine above considers the translation on the right but is missing the "undo" translation. IMHO it should correctly be:
def rotatePoint(point, origin, radians):
displacement = point - origin
point.x = displacement.x * cos(radians) + displacement.y * sin(radians)
point.y = displacement.y * cos(radians) - displacement.x * sin(radians)
point = point + origin

Share this post


Link to post
Share on other sites
Quote:
Original post by haegarr
The rotatePoint(...) routine above considers the translation on the right but is missing the "undo" translation. IMHO it should correctly be:
*** Source Snippet Removed ***
Oops, thanks, I was meant to apply that translation.

Also, in C++ for example, I'd return a new point rather than modifying the one passed in. I might consider doing that in Python too, although I'm not sure which is the most Pythonic.

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