views:

50

answers:

3

Hi,

I need to access the X,Y values of a vertex object after Ive rotated it (with glRotate3f). How is it possible? I need em to solve an object collision problem where collisions will be sending objects in an angle related direction based upon objects rotation degrees.

edit::

Ok, Ive come close to find a way. Lets assume X and Y and degrees 0, 90 , 180, 270. I set a point as the center of the object, lets assume X=30, Y=70 and the X_size is 60 and Y_size is 140.

If I make new_X = X*cos(angle) - Y*sen(angle) and new_Y = X*sen(angle) + Y*cos(angle) if then I add or subtract the object center X and Y to new_X and new_Y I actually get to the new point except when angle is 180 where I would need to go with -new_X + X.

Is this the way to solve it?

A: 

Not directly, you can retrieve the modelview matrix using float modelview[16]; glGetFloatv(GL_MODELVIEW, modelview) and do the matrix multiplication manually between your vectors and the modelview matrix.

There's no other method in GL to do it.

Matias Valdenegro
He wants rotated object, not a current matrix.
SigTerm
Yes but with the current matrix I can use a rotate matrix to get there. Thanks for info
d0pe
+1  A: 

You are misunderstanding things a bit.

You didn't rotate anything by calling glRotatef. When you call glRotate, you modify transformation matrix, the object data remains unchanged. When you render object, the vertex data goes through vertex processing pipeline, gets multiplied by transformation matrix (every time you render object) and technically, you can't get result of calculations back - because object wasn't changed.

Well, if you really want it, you could try using gl feedback buffers (see glFeedbackBuffer), but I don't see a reason for that. I think that "OpenGL red book" may have feedback buffer examples, but I'm not sure about that. Feedback buffers MAY provide functionality you need, but I really haven't used them much, so I'm not sure about it either.

Also, moving lots of data back and forward between system memory and video memory (when your object is stored in video memory) leads to performance losses, so you shouldn't do that even if you can.

If you want to get object state after rotation, you will have to multiply it by hand - using object's matrix and store a copy in system memory. Since OpenGL combines object matrix with view matrix (when you call glRotatef), then you'll need to store rotation matrix separately.

Is this the way to solve it?

Maybe only if you're working with 2D, and rendering one triangle or two.

Normally when you do transformations, you build combined rotation/translations matrix, then multiply data you need using that matrix - this will be more efficient, because it allows to combine multiple transformations into one. And if you work with non-graphical 3D routines (collision detection), you do matrix multiplications/vector transformation in your code, yourself.

AFAIK OpenGL doesn't provide matrix manipulation routines (similar to D3DXMatrixMultiply in D3DX), but they are easy to write from scratch, and can be taken from multiple places. And you can use D3DX matrix functions, if you're desperate (D3D matrices have same memory layout). Besides, if you're serious about working with 3D, you'll have to learn matrices eventually.

SigTerm
Thanks, I know the combined matrixes and the simple ones, for now they will do and good insights on on things really work behind the code lines :)
d0pe
A: 

It sounds like what you're after is gluProject. You supply x, y, and z coordinates of an object, current matrices (modelview and projection), and viewport coords, and it gives you back the x, y coordinates to which that point would be mapped by those matrices. The linked documentation includes the formulas for the job, if you prefer to do it yourself.

Jerry Coffin
Im going to bed but thanks for the comment and in your description seems itll do what I need so thanks again.
d0pe