Rotation around an arbitrary reference point

Started by
6 comments, last by dmatter 15 years, 6 months ago
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
Advertisement
What exactly is your question?
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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

D3DX contains some functions for this...you can define a rotation matrix with any orgin point
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)
The rotatePoint() function is what I was looking for.

thanks...
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
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.

This topic is closed to new replies.

Advertisement