# Rotation around an arbitrary reference point

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

## 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 on other sites
Quote:
 Original post by megatron242I'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 on other sites
D3DX contains some functions for this...you can define a rotation matrix with any orgin point

##### 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 on other sites
The rotatePoint() function is what I was looking for.

thanks...

##### 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 on other sites
Quote:
 Original post by haegarrThe 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.

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633724
• Total Posts
3013555
×