Thread: Setting an object to predefined angle / orientation

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    kolkata, India
    Posts
    7

    Unhappy 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.
    Last edited by sapdev; 05-15-2009 at 06:30 AM. Reason: Missed some point

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    147
    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. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #4
    Registered User
    Join Date
    May 2009
    Location
    kolkata, India
    Posts
    7

    Cool

    Quote Originally Posted by Bubba View Post
    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();
    	glLoadIdentity();
    	
            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();
    	
    	//glLoadIdentity();
    	// 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);
    	
    
    /////////////////////////////////////////////////////////////////////////////////////
    //vertex array loaded and rendered

    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.

    THANKS FOR YOUR REPLY.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #6
    Registered User
    Join Date
    May 2009
    Location
    kolkata, India
    Posts
    7
    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. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sapdev View Post
    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();
    glLoadIdentity();
    	
            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.

    Quote Originally Posted by sapdev View Post
    SO any more bright Ideas
    Last edited by MK27; 05-16-2009 at 07:04 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #9
    Registered User
    Join Date
    May 2009
    Location
    kolkata, India
    Posts
    7
    @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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #11
    Registered User
    Join Date
    May 2009
    Location
    kolkata, India
    Posts
    7

    Smile


    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. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sapdev View Post
    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.
    Okay, fair enough (and I apologize). But what about this:
    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...
    Last edited by MK27; 05-18-2009 at 09:47 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    May 2009
    Location
    kolkata, India
    Posts
    7
    @MK27

    Getting angry,abusing will NEVER solve anything.


    I like your idea about official opengl forum. :P



    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. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sapdev View Post
    @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.

    I like your idea about official opengl forum. :P
    Ha! Don't thank me until you've tried it.
    http://www.opengl.org/discussion_boards/
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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);
    Last edited by VirtualAce; 05-18-2009 at 05:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Include Problems
    By loopshot in forum Game Programming
    Replies: 13
    Last Post: 02-17-2006, 03:22 PM
  2. Module Development for Game Design - By Jeff Verkoeyen
    By kermi3 in forum Article Discussions
    Replies: 23
    Last Post: 03-07-2005, 08:05 AM
  3. Setting Fonts for all of the window object
    By bman1176 in forum Windows Programming
    Replies: 1
    Last Post: 01-24-2005, 02:57 PM
  4. C++ Class Object Collection
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2002, 04:48 PM
  5. Setting Object Data *double* For Class ComboBox :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 06:20 PM

Tags for this Thread