Hussien Hussien
Hussien Hussien

Reputation: 180

Rotating vertices about point

I'm reverse engineering a level map for a game. Each object within the level has 3 floats for positioning (x,y,z) and 3 floats for rotation (x,y,z).

A lot of the objects, when pulled into a 3D Program, axis sit at 0,0,0 (world axis) and the object is drawn away from that point rather than being pulled in and centered at the world axis.

Here is 2D example of what I mean:

You have a square with 4 vertex's (5,5),(5,10),(10,10),(10,5) and its center is (7.5,7,5)

I need the 4 vertex's to be rotated around 0,0 and not its center (7.5,7.5)

I've been rotating the objects using the following python code:

#vX,vY,vZ is the vector coords
#rx,rY,rZ is the rotation angles in degrees
#Xrotation
xX = vX
xY = vY * math.cos(rX) - vZ * math.sin(rX)
xZ = vY * math.sin(rX) + vZ * math.cos(rX)
#Yrotation
yX = xZ * math.sin(rY) + xX * math.cos(rY)
yY = xY
yZ = xZ * math.cos(rY) - xX * math.sin(rY)
#Zrotation
zX = yX * math.cos(rZ) - yY * math.sin(rZ)
zY = yX * math.sin(rZ) + yY * math.cos(rZ)
zZ = yZ
#zX,zY,zZ is what gets outputted

The above code rotates the objects around its center rather than the world axis of 0,0,0. I'm at a loss of how to modify the above calculations to rotate the object around the world axis.

I've been looking over a lot of other similar questions on here and other places and my brain is about to explode into a pool of greek symbols.

Any help or direction would be very much appreciated!

Upvotes: 0

Views: 1039

Answers (1)

cividesk
cividesk

Reputation: 454

Have you tried translating the object center to the world axis, applying your rotation, and then translating back by the opposite amount?

In your 2D example you would do a translation of (-7.5,-7.5), apply your rotation, and then translate by (7.5,7.5).

Upvotes: 2

Related Questions