   snapping a object doing free form rotation to some specific angles
  Okay here is the situation.I have a object. Which is doing a free form rotation.I am saving the current model view matrix and then applying the rotational change on that matrix. These rotational change come from the user input and are generally very small (of size 2 to 3 degree). This is for iPhone. So this small rotational change is applied for a whole touch event.(which is generally a series of changes)That is fine up to now.The Problem When the user ends the touch event. The objects have to snap to nearest angle such that it is a multiple of 90 degree in x y and z axes.The way i figured it out to do :-get the modelview matrix,generate euler angle.Calculate the change change in angle to be made , dx , dy , dz. Apply rotation.NOW THE QUesTIONIS THERE ANY BETTER WAY TO DO IT ?presently I am yet to get my hands dirty for the matrix to euler angle conversion.Thanks for your time.Sapdev[Edited by - sapdev on May 18, 2009 3:12:21 AM]
  This is more math-related than OpenGL-related. Maybe you could get a mod to move it to Math & Physics.Here's what first comes to mind. There are n unique axis-aligned orientations in 3-space (24, I think). For each of these, find the rotational difference between it and your object's current orientation, in axis-angle form. Of the resulting axis-angle pairs, find the one whose angle has the smallest magnitude (if there's a tie, just pick one). Finally, snap the object's orientation to the axis-aligned orientation corresponding to the axis-angle pair you selected. This should effectively snap the object to the 'closest' axis-aligned orientation.This could probably be optimized considerably by removing redundant and/or unnecessary calculations. There may also be other ways to approach the problem that would be more efficient at the algorithmic level, but it's late, and nothing's coming to mind at the moment :) Maybe someone here or in Math & Physics will be able to offer some suggestions in that area. (On a PC, the brute-force approach would probably perform just fine, but I don't know enough about the iPhone to say whether it would be viable on that platform.)   ;  PS: Did I forgot to mention that I am the best NOOB in this field.Now I am trying to decipher your reply.Thanks for the reply :). [This is kind a my only hope.]   ;  jyk,I was going through your reply. That means you are suggesting that I need to get the euler angles converted from the Model View matrix. Calculate the difference and then apply the rotation. ?This CPU cost will be huge. Any more bright ideas.   ;Quote:Original post by sapdevjyk,I was going through your reply. That means you are suggesting that I need to get the euler angles converted from the Model View matrix. Calculate the difference and then apply the rotation. ?This CPU cost will be huge. Any more bright ideas.The approach I proposed doesn't involve Euler angles. The idea is that you compute the rotational difference between the current orientation and the candidate target orientation, like this:delta = target_orientation*orientation^-1Then convert to axis-angle form. Once you've done this for all the potential target orientations, the one for which the corresponding axis-angle pair has the angle with the least magnitude is the one you're looking for.This doesn't solve the run-time cost problem though. I'm quite certain the problem can be solved more optimally, either by optimizing the above method at the implementation level or by using a different method altogether, but unfortunately I don't have time to work though it right now.Again, you might PM a mod and see if you can get this moved to Math & Physics - you might get more in-depth feedback there.   ;  jyk,Sorry for a late reply . . .Was out of business(literally)Quote:delta = target_orientation*orientation^-1Can you clarify this a bit ? How can I calculate the target Orientation (which definitely is a matrix) from Euler angles, Do I need to pre-compute them and use them in the program.I PM the mod moderating OpenGL forum to move this Topic to the correct forum.   ;  Moved to Math and Physics, as requested.   ;  Any Body With Any new Bright Ideas ?   ;  I like jyk's approach. There are only 24 target configurations, and jyk's criterion to decide which one is closest is very natural.Do you want the snapping to be animated? If you do, you might be able to get good results by making a tiny mechanical simulation. Consider three particles at the points (1,0,0), (0,1,0) and (0,0,1). Rotate those particles through your rotation (effectively, these are the columns of your matrix). Now set up 6 attractors at the target positions for those points (which are the same 3 original points and their opposites (-1,0,0), (0,-1,0) and (0,0,-1)), with a decay that is proportional to 1/distance^2, like gravity, or perhaps 1/distance^4 (you'll have to experiment). Compute the combined force acting on each particle and update its position using verlet integration. There is a good description of verlet integration in this Gamasutra article. After you have updated the positions, the three points will no longer correspond to orthogonal unit vectors. You can force them back into these conditions by forcing the distance between them to be sqrt(2) and their distance to the origin to be 1. This is easy to do using the same kind of infinitely strong springs described in that Gamasutra article. Add some friction/drag to make sure that you converge to a valid position, instead of oscillating forever. After each step, the three points can be used as the columns of your rotation matrix.This is actually not that hard to do, not that computationally expensive, and I believe it can give the interface a very interesting feel.;  Hi,Alvaro.I am quite new in this field and the mathematics is not so strong as it used to be :PAfter some research, I got that jyk's way of solution is the only one and is a good one (A least logically).But I can't understand the "24 target configurations". Even Wikipedia is not doing much help :( Have u anything else in your BIG Bag.THANKS FOR THE HELP. This being my first big OpenGL project I should say. Rotation is one of the toughest operation in OpenGL.