views:

1063

answers:

2

Hi,

(This is all in ortho mode, origin is in the top left corner, x is positive to the right, y is positive down the y axis)

I have a rectangle in world space, which can have a rotation m_rotation (in degrees).

I can work with the rectangle fine, it rotates, scales, everything you could want it to do.

The part that I am getting really confused on is calculating the rectangles world coordinates from its local coordinates.

I've been trying to use the formula:

x' = x*cos(t) - y*sin(t) 
y' = x*sin(t) + y*cos(t)

where (x, y) are the original points,
(x', y') are the rotated coordinates,
and t is the angle measured in radians

from the x-axis. The rotation is
counter-clockwise as written. 
-credits duffymo

I tried implementing the formula like this:

//GLfloat Ax = getLocalVertices()[BOTTOM_LEFT].x * cosf(DEG_TO_RAD( m_orientation )) - getLocalVertices()[BOTTOM_LEFT].y * sinf(DEG_TO_RAD( m_orientation ));
//GLfloat Ay = getLocalVertices()[BOTTOM_LEFT].x * sinf(DEG_TO_RAD( m_orientation )) + getLocalVertices()[BOTTOM_LEFT].y * cosf(DEG_TO_RAD( m_orientation ));

//Vector3D BL = Vector3D(Ax,Ay,0);

I create a vector to the translated point, store it in the rectangles world_vertice member variable. That's fine. However, in my main draw loop, I draw a line from (0,0,0) to the vector BL, and it seems as if the line is going in a circle from the point on the rectangle (the rectangles bottom left corner) around the origin of the world coordinates.

Basically, as m_orientation gets bigger it draws a huge circle around the (0,0,0) world coordinate system origin. edit: when m_orientation = 360, it gets set back to 0.

I feel like I am doing this part wrong:

and t is the angle measured in radians from the x-axis.

Possibly I am not supposed to use m_orientation (the rectangles rotation angle) in this formula?

Thanks!

edit: the reason I am doing this is for collision detection. I need to know where the coordinates of the rectangles (soon to be rigid bodies) lie in the world coordinate place for collision detection.

+2  A: 
rekli
Thank you. I will work on this later tonight! :)
Ben Adamson
OpenGL handles graphics, not linear algebra operations. If he needs the transformed coordinates for collision detection, that's a bad advice in my opinion. But +1 for a good explanation.If you use OpenGL to transform the mesh during the actual rendering, remember to rotate about the Z axis.
Mads Elvheim
+1  A: 

This seems to be overcomplicating things somewhat: typically you would store an object's world position and orientation separately from its set of own local coordinates. Rotating the object is done in model space and therefore the position is unchanged. The world position of each coordinate is the same whether you do a rotation or not - add the world position to the local position to translate the local coordinates to world space.

Any rotation occurs around a specific origin, and the typical sin/cos formula presumes (0,0) is your origin. If the coordinate system in use doesn't currently have (0,0) as the origin, you must translate it to one that does, perform the rotation, then transform back. Usually model space is defined so that (0,0) is the origin for the model, making this step trivial.

Kylotan