Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group multiple objects for purpose of rotating them as a unit?

I need some pointers on the best approach for a rotation task in OpenGL. I know how to rotate objects in 3D space using quaternions, I can translate them, all well and good.

But I want to know the best way to treat a collection of different objects as a single entity for the purpose of rotation. For example, suppose you have a desk with objects on it. Each has its own translation and rotation, but now I want to rotate the entire desk and everything on it, while keeping other objects in the room in place. This means objects on the outer edge of the desk will rotate and also translate around the center of the desk, while objects at the center might just rotate but otherwise stay in place, or simply not translate by as much, depending on their distance from the axis of rotation.

It seems that a rather inelegant way to do this is to individually rotate and translate each object on the desk, as well as the desk itself, knowing where the axis of rotation is. Perhaps this is the only good way to do it, but is there a technique to "group" disparate objects for this purpose?

like image 481
johnbakers Avatar asked Mar 22 '13 05:03

johnbakers


2 Answers

This is generally accomplished by using some kind of scene-graph which incorporates a transform hierarchy.

A simple minimal version would be to have a tree where each node contains an object, a transform, and a list of child nodes. The transform is relative to the parent node.

So the objects on the desk would be children of the desk itself.

The transform for any given object is the concatenation of that object's transform with all its parents in the tree. There are many ways to accomplish that, but the 'old school' GL functionality provides a matrix stack for this purpose. You multiply in the local matrix for a node, draw the geometry for that node, recursively draw all the child nodes, and then pop the matrix back off.

like image 39
JasonD Avatar answered Nov 15 '22 03:11

JasonD


What you're looking for are transformation hierachies. The objects on your desk are positioned relative to the desk, or in other words in the coordinate system of the desk. So lets designate M_Desk as the transformation defining the placement of the desk and the local coordinate system of it. Next let be M_Pencilbox the transformation of the pencil box standing on the desk in relation to the desk. And a pencil in the pencil box would be placed in relation to the pencil box.

So the pencil goes through a hierachy of transformations. Remember that in the column major notation used by OpenGL things "flow" through the transformation chain from last transformation to first (or right to left when written down).

Each transformation, like M_Desk for example, is a 4×4 matrix that can be constructed the usual way: Rotations, translations, scalings, etc.

So to transform the vertices of a pencil you'd apply the following transformation

… · M_Desk · M_Pencilbox · v_Pencil

Of course the desk itself may be in relation to something different, like a room. At the very beginning of that transformation chain would be the view transformation. So effectively we're building a modelview matrix here.

In terms of modern OpenGL, everytime you encounter a branch in the transformation hierachy (think of directories in a file system), you'd create a copy of the transformation chain built so far, so that you don't have to restart from scratch for each branch.

Let me know if you need further clearification.

like image 134
datenwolf Avatar answered Nov 15 '22 03:11

datenwolf