Rotation around an arbitrary reference point
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
Quote:Original post by megatron242So each entity has a location, and an orientation?
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).
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.
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 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:
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 haegarrOops, thanks, I was meant to apply that translation.
The rotatePoint(...) routine above considers the translation on the right but is missing the "undo" translation. IMHO it should correctly be:
*** Source Snippet Removed ***
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
Popular Topics
Advertisement