View Full Version : Dragging rotated object

ps_23

06-12-2008, 01:53 AM

Hello,

I am not sure if I am just thinking wrong or where exactly to start with solving the problem:

I have a graph with nodes (spheres) and edges which the user can rotate around the middle point of the system (QT + glu OpenGL library). Now I implemented the dragging itself (using glUnProject), so the user can drag an object (a node) normally as long as the graph has not been rotated, as the x and y axes for the graph and the mouse are the same. However, when the graph is rotated, the 3 axes of the graph and the 2 axes of the window obviously differ. I am not sure what I should do to make the dragging work, thus, how to translate the 2 window axes into the 3D system and taking into account how much the graph was already rotated. Any hint?

ZbuffeR

06-12-2008, 02:11 AM

So translation must be node after rotating. Due to how matrix multiplication works, I am pretty sure it means that you have to place the translate instructions before the rotations. But maybe I am wrong and it it the other way round :) .

ps_23

06-12-2008, 02:28 AM

But when I translate after rotating, then I just change the position of the graph or the distance to the rotation point, or am I wrong?

Actually I do not translate the rotation, I just use glTranslate in the resize function:

void GLWidget::resizeGL(int width, int height) {

GLfloat aspect = (GLfloat)width/height;

glViewport(0,0,(GLint)width,(GLint)height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(20.0, aspect, 0.1, 200.0);

glMatrixMode(GL_MODELVIEW);

//glLoadIdentity();

glTranslated(0.0, 0.0, -10.0);

}

What I want is, that when the user for example turns the graph 180 degrees around the y-axis, a node is going to the right when the user moves the mouse to the right. At the moment the node would go left when the user drags right, as I do not know how to translate the movement of the mouse to the graph which does not use the same system any more as the user window after the rotation.

dletozeun

06-12-2008, 03:16 AM

You have to consider 2 spaces:

- a world space with coordinates system (Xw, Yw, Zw).

- an object space with coordinates system (Xo, Yo, Zo).

The object space is attached to you graph and you can consider that the center of this space is its middle point. So the object space rotates and translates like your grapth related to the world space.

You will need to build a matrix Mwo that transforms a point in world space to object space. So Mwo contains inverse of all object space transformations.

the problem is that all your graph objects (spheres and edges) coordinates are given in object space, so you will need to move the world transformations Tw in object space to obtain a transformation matrix To.

With a little math you have :

To = Mwo * Tw * inverse(Mwo)

ps_23

06-12-2008, 03:24 AM

Ok, I expected a matrix transformation problem. Is it a complete manual thing, or does OpenGL provide any help here, or a code snippet at least somewhere?

dletozeun

06-12-2008, 03:33 AM

Opengl api doesn't provide any function to inverse matrices but you should easily find a code to do 4x4 matrix inverse.

dletozeun

06-12-2008, 03:37 AM

In your case, if you do just rotations and translations, this would be useful:

http://gpwiki.org/index.php/MathGem:Fast_Matrix_Inversion

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.