# Thread: Setting an object to predefined angle / orientation

1. ## Setting an object to predefined angle / orientation

This post being my first comes with lots of hope.

The situation till now :-

I have multiple objects in a scene. On a moment there is only 1 interact able object. The object is being rotated by the user input IN THE WORLD COORDINATE SYSTEM.

The free form rotation is doing JUST FINE.

The Problem :

Now while the user input ends I have to snap the object close to 90 degree or its multiples in any of the axes.

Any idea of how to do it ?

My first approach was to get the euler angle values. But after googling a bit I think that is impossible.

The transformation applied . . . . . .

Translatation, Rotation, Scalling,(GL_NOMALIZE is turned on). and there is a use of gluLookat.

Any help in this reagard will be greatly appriciated.

2. Without further context, I can't be sure how to be of help.

Are you using a matrix library or are you performing these rotations using the gl functions (glRotate, for example).

From a conceptual standpoint, the answer would depend on your method of defining the object's rotation; are you accepting input values, or 'rolling it' using a mouse?

This may be a good use for a quaternion. That is, in part, what it's for.

Perhaps you could include some code where you are manipulating this object's rotation, and it would help to pin down what you mean by snap. For this last point, what I mean is that your inquiry suggests selecting any axis might do - is that a world axis or a model axis, and how would to select which axis in which system?

3. I have multiple objects in a scene. On a moment there is only 1 interact able object. The object is being rotated by the user input IN THE WORLD COORDINATE SYSTEM.
This makes no sense since it is impossible to rotate an object around it's center only in world space. The rotation is happening in local space about the origin and then the object is being translated to it's world space position. Anything else would result in the object rotating around the point it was translated from (IE: 0,0,0 in local space).

I don't see why you could not use the standard up, right, and look combo for orienting your particular objects. Since you do not need smooth interpolation between two different 3D orientations I fail to see how or why quaternions would be necessary.

4. Originally Posted by Bubba
This makes no sense since it is impossible to rotate an object around it's center only in world space. The rotation is happening in local space about the origin and then the object is being translated to it's world space position. Anything else would result in the object rotating around the point it was translated from (IE: 0,0,0 in local space).

Yes you are right Fortunately I have crossed that stage.

Here is my code structure :

I am reading an obj file, and rendering it, This whole thing is a class. ie for every obj file there is a object, and in every object there is code to render that object separately.

I am calling this render functions from A MAIN RENDER LOOP.

Here is the code snippet for the render code for the INDIVIDUAL OBJECTS.

In Original Render Loop there is a glLoadIdentity();

Code:
```     // Save the current transformation by pushing it on the stack
glPushMatrix();

//////////////////////////////////////////////// Calculating the screen space orientation

glGetFloatv(GL_MODELVIEW_MATRIX, cameraMat);	// Save the current matrix containg the gluLookat data
glPushMatrix();

if(dRot.x)  // Delta Rotation Structure
glRotatef(dRot.x, 1.0f, 0.0f, 0.0f);
if(dRot.y)
glRotatef(dRot.y, 0.0f, 1.0f, 0.0f);
if(dRot.z)
glRotatef(dRot.z, 0.0f, 0.0f, 1.0f);
dRot.x = 0.0f;
dRot.y = 0.0f;
dRot.z = 0.0f;
glMultMatrixf (matrix);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);	// Saving the new matrix
glPopMatrix();

// Translate to the current position

glGetFloatv(GL_MODELVIEW_MATRIX, cameraMat);

glTranslatef(pos.x, pos.y, pos.z);

//Rotating the object

glRotatef(rot.x, 1.0f, 0.0f, 0.0f);			// In the object space
glRotatef(rot.y, 0.0f, 1.0f, 0.0f);
glRotatef(rot.z, 0.0f, 0.0f, 1.0f);

glMultMatrixf(matrix);			// Before first run this is initiaized to unit matrix
// In the world space

// Scaling the current object

glScalef(scale.x, scale.y, scale.z);

/////////////////////////////////////////////////////////////////////////////////////

Now With this code I am doing free form Rotation,'

At this point I need to snap the object to 90 degree or its multiple in any of the axes.

5. Well a snap to 90 is just a rotation to PI * 0.50. I'm not quite sure what you are trying to do.

6. That was the way I was doing before. . . . when I was rotating the objects in there own axes, snap to the closest angle, but as you can see I am now doing free form rotation in the WORLD SPACE, hence I don't have the euler angles I have the matrix. Recovering angles from that matrix will be a real pain. as you can see all the possible matrix trasnformationare applied to the matrix.

SO any more bright Ideas

7. Originally Posted by sapdev
I am reading an obj file, and rendering it, This whole thing is a class. ie for every obj file there is a object, and in every object there is code to render that object separately.
Please don't use class/object terminology with C because you will only confuse yourself and others. It would be better to explain what you are doing in a more concrete and normative way, eg, that basically you are calling a function to render a 3D thing.

I also believe this entire chunk of code accomplishes absolutely nothing:
Code:
```     // Save the current transformation by pushing it on the stack
glPushMatrix();

//////////////////////////////////////////////// Calculating the screen space orientation

glGetFloatv(GL_MODELVIEW_MATRIX, cameraMat);	// Save the current matrix containg the gluLookat data
glPushMatrix();

if(dRot.x)  // Delta Rotation Structure
glRotatef(dRot.x, 1.0f, 0.0f, 0.0f);
if(dRot.y)
glRotatef(dRot.y, 0.0f, 1.0f, 0.0f);
if(dRot.z)
glRotatef(dRot.z, 0.0f, 0.0f, 1.0f);
dRot.x = 0.0f;
dRot.y = 0.0f;
dRot.z = 0.0f;
glMultMatrixf (matrix);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);	// Saving the new matrix
glPopMatrix();```
Since all you do is transform the matrix without rendering anything, and then you pop the matrix out again -- making all that rotation meaningless.

Originally Posted by sapdev
SO any more bright Ideas

8. There are known formulas to extract angles from matrices however if you need to do this then you have probably done something wrong or are looking at the problem incorrectly.

And yet you continue to say you are doing the rotation in world space even when that is simply not possible without first rotating in local space. ANY translation from local space to world space prior to a rotation will cause the object to 'orbit' around the transformed origin. So if you translate to 100,100,100 and then rotate you will orbit at a distance of 100,100,100 on x y and z. If you translate the object to 2000,2000,2000 your object will orbit 100 units on x,y,z out from 2000,2000,2000.

9. @MK27

Sorry for a late reply . . . .

Since all you do is transform the matrix without rendering anything, and then you pop the matrix out again -- making all that rotation meaningless.

Have seen the whole code snippet.

I am repeating this agian, (ALAS I HOPED FOR SOME HELP, AND IT IS TAKING SO MUCH TIME MAKING PEOPLE UNDERSTAND MY POINT).

Let me quote part of my code snippet again.

Code:
```//Rotating the object

glRotatef(rot.x, 1.0f, 0.0f, 0.0f);			// In the object space
glRotatef(rot.y, 0.0f, 1.0f, 0.0f);
glRotatef(rot.z, 0.0f, 0.0f, 1.0f);

glMultMatrixf(matrix);			// Before first run this is initiaized to unit matrix. In the world space```
See the line marked in red.
That is the fate of the new matrix that is computed at top of the function.

@Bubba

And yet you continue to say you are doing the rotation in world space even when that is simply not possible without first rotating in local space. ANY translation from local space to world space prior to a rotation will cause the object to 'orbit' around the transformed origin. So if you translate to 100,100,100 and then rotate you will orbit at a distance of 100,100,100 on x y and z. If you translate the object to 2000,2000,2000 your object will orbit 100 units on x,y,z out from 2000,2000,2000.

Yes you are CORRECT. That's what I am doing. The last applied is scaling, then the rotation in object space + world space, then the translation. Since we all know that the matrix operation are applied from the back. That's what I am doing.

And about your comment, I was thinking in your path. . . .That's why I asked you people over here. I googled a lot and came out with the fact that, recovering the euler angled from matrix is complex and gives some error specially when gluLookAt is used. And I HAVE USED IT

So I actually don't want to convert back to euler angle but yet set the current orientation of the object 90 degree or its multiple in all the axes as the user interaction ends.

Again . . . .

ANY MORE BRIGHT IDEAS.

10. It might be possible to add up all your delta-angles as you go along, although it appears they are in new coordinates each time? (If so, that might be more work.)

As far as Euler angles go, I'm not sure why you're dismissing that out of hand. It is certainly true that a representation of a rotation in Euler angles is not unique, but that is completely irrelevant to your purposes, as I believe they would all "round" to the same 90 degree rotations. And although I've never done it, I believe the decomposition has been done before.

11. It might be possible to add up all your delta-angles as you go along, although it appears they are in new coordinates each time? (If so, that might be more work.)

Already tried and tested. NOT WORKING

As far as Euler angles go, I'm not sure why you're dismissing that out of hand. It is certainly true that a representation of a rotation in Euler angles is not unique, but that is completely irrelevant to your purposes, as I believe they would all "round" to the same 90 degree rotations. And although I've never done it, I believe the decomposition has been done before.

After this I think I am gonna give it a try. Will post my results within 5 hrs.

Thanks for giving a noobe some Hope

12. Originally Posted by sapdev
Let me quote part of my code snippet again.

See the line marked in red.
That is the fate of the new matrix that is computed at top of the function.
Code:
```glPushMatrix();

//////////////////////////////////////////////// Calculating the screen space orientation

glGetFloatv(GL_MODELVIEW_MATRIX, cameraMat);	// Save the current matrix containg the gluLookat data
glPushMatrix();```
If that big comment line represents something missing, great. If not, all you did is save the matrix to the stack, save the matrix to a variable, and then save the matrix to the stack again, pointlessly. This does not really inspire confidence in me, and depending on the context of your sequence of pops, might be an error.

To be honest, I am unlikely to be able to help anyway*, but you since some of cboard's finest minds also appear confused about your intention and you are complaining that you have not made yourself understood, I think you will have better luck if you put more effort into your explanation, rather than presuming your implementation of some abstract principles is clear (obviously it is not). Without doing that, it is also not clear that you understand either the principles or the implementation. If you are more specific, maybe someone will be able to see where you have gone wrong.

Also: have you tried "the official" openGL web forum?

*so just ignore me, really...

13. @MK27

Getting angry,abusing will NEVER solve anything.

PS : One More thing my code is 100 % Correct. This is for iPhone. the max frame rate possible on it is 60. and guess what, on my game (a very simple one) I am getting it, I tested my new Engine for 2 hrs CONTINUOUSLY. The Frame Rate never Droped.

@tabstop

I ran intosome work and didn't get the time to use your advice, Will check on that asap.

14. Originally Posted by sapdev
@MK27

Getting angry,abusing will NEVER solve anything.
I'm not angry. Are you angry? I'm not a GL wiz either; I'm more of an eavesdropper. Part of what I meant is, this is an advice forum -- I'm asking you for help in understanding exactly what it is you are doing. Even if I understood, I still probably can't help BUT while someone with all the cards may come along and grant your wish, it hasn't happened yet, so think a little about what I am trying to say. From my perspective (and possibly from others as well), it is not clear that the first part of your code is relevant to the issue you are trying to present, and it appears shoddy. So what you appear to be is someone who's done too much reading about methods but is in over their head applying them. I'm not trying to put you down. Don't get paranoid or angry, no one wants or needs it.

Ha! Don't thank me until you've tried it.
http://www.opengl.org/discussion_boards/

15. You are essentially asking us a question which means we presume you don't understand something about what it is you are doing. Some of the information in your posts has led many of us to believe that you lack some fundamental understanding of exactly what is happening in 3D via transformations. Some of what you posted is incorrect and we are merely trying to gauge where you are so we can take you to where you need to be in order to solve your problem.

I have seen nothing in this thread that would lead me to believe that anyone offering advice has become hostile towards you. What I do see is that every suggestion we give you give a rebuttal for. I guess my point is that if you cannot explain exactly what it is you are trying to do then perhaps you really don't know what you want to do. If you design something it is my firm opinion you should be able to communicate that design clearly to someone else and if you cannot then perhaps your design might be a bit flawed. If you cannot walk someone through it step by step without getting defensive then you probably ought to look at it a bit closer.

Lastly since you came to us for advice but yet seem to know the answers already since you brush ours off like they mean nothing it is my estimation that you are beyond help. Until you start listening to some of the advice in this thread instead of defending your code and your approach you cannot be helped because you will not help yourself.

Everything you want to do with snapping angles can be done via Euler angles. Quaternions and the like only solve the issue of interpolating smoothly between two 3D orientations. Gimbal lock is solvable via axis-angle rotations which can still be done with Euler angles. Regardless of the approach your final results will still end up in matrix form since that is what the video card understands (at least in the current hardware). I see nothing hard in this thread but I do see some communication breakdowns which we have asked you to clear up. Until you do I would say we cannot help you further. If you want further help we will give it but you must clear up some of the issues we have addressed. So in very direct terms: chill out and let us help you.

On a side note perhaps it is your use of CAPS that is making this thread hostile. Words in all CAPS accomplish nothing here and are interpreted as yelling. If it's emphasis you want then use italics or bold as they come across much better.

At first glance it appears to me that a simple fmodf() could be of some use to you.

Code:
`angle -= fmodf(angle,minimumSnapDist);`
Almost the same as attempting to snap an arbitrary value to a grid with a known cell width and cell height. Of course this only works if the point in question is actually inside the grid.
Code:
```screenX -= (screenX % cellWidth);
screenY -= (screenY % cellHeight);```