   Question regarding rotation of an Object.
  Hi guys,as the title says, I have a question regarding the rotation of an object.When I render an object right now I create its rotation matrix at render time like this:D3DXMatrixRotationYawPitchRoll(&rotationmatrix, y , x , z );For this I need to have the individual x,y,z angles at render-time to create the rotation matrix.The problem I have now is that I want to rotate an object several times (always based on the preceeding rotation). I cannot simply add the angles onto another (x on x, y on y, etc...) because then they are not aware of the other axis angles and the rotation turns out wrong.Its like a merry-go-round that keeps rotating around its z-axis and even if you tilt the ground (x or y axis) the z-axis tilts as well and it keeps rotating around the tilted z-axis.What I want is that at subsequent rotations the angles are calculated as if the already rotated object was not rotated and the rotation affects now all 3 axis accordingly.It would be much easier if I would simply calculate several rotation matrices and apply them one after the other (I think that should work) but I do some complicated math with the angles so I cannot change my approach and need to create the whole rotation (as mentioned above) at render time.I am not sure how I can do this and would really appreciate some help.   
  Not sure if I understand the question completely, but if you store the orientation in matrix or quaternion form (rather than as Euler angles), you can apply whatever rotations you want to it on a per-update basis. For example, you could apply a rotation every update to keep it spinning about its own local z axis, while applying other rotations as needed to change its overall orientation.Remember that with incremental rotations, you'll need to orthogonalize/normalize the orientation matrix/quaternion periodically to prevent numerical drift.   ;Quote:Original post by jykNot sure if I understand the question completely, but if you store the orientation in matrix or quaternion form (rather than as Euler angles), you can apply whatever rotations you want to it on a per-update basis. Thats exactly what I cannot do as I need to keep the rotation as numbers (euler angles) for other things that are going on. As I understood it I cannot go back from a rotation matrix to the angles and I need them so I have to do it this way. ;Quote:Original post by Zaph-0Quote:Original post by jykNot sure if I understand the question completely, but if you store the orientation in matrix or quaternion form (rather than as Euler angles), you can apply whatever rotations you want to it on a per-update basis. Thats exactly what I cannot do as I need to keep the rotation as numbers (euler angles) for other things that are going on. As I understood it I cannot go back from a rotation matrix to the angles and I need them so I have to do it this way.I'm always a little skeptical when people say they 'have to have' Euler angles - can you say anything about what you need them for?Anyway, it is possible to convert a rotation matrix to a set of Euler angles. It's a little tricky, and the solution is often non-unique, but if you need to go back and forth for some reason, it's certainly possible.   ;Quote:Original post by jykQuote:Original post by Zaph-0Quote:Original post by jykNot sure if I understand the question completely, but if you store the orientation in matrix or quaternion form (rather than as Euler angles), you can apply whatever rotations you want to it on a per-update basis. Thats exactly what I cannot do as I need to keep the rotation as numbers (euler angles) for other things that are going on. As I understood it I cannot go back from a rotation matrix to the angles and I need them so I have to do it this way.I'm always a little skeptical when people say they 'have to have' Euler angles - can you say anything about what you need them for?Anyway, it is possible to convert a rotation matrix to a set of Euler angles. It's a little tricky, and the solution is often non-unique, but if you need to go back and forth for some reason, it's certainly possible.I need the euler angles for a sort of steering behaviour where I need to know the angle the object is facing in all 3 directions to calculate its behaviour relative to other objects in the area.Is there no way where I can simply rotate the 3 angles as if I were applying rotation matrices one after the other ?;Quote:Original post by Zaph-0It would be much easier if I would simply calculate several rotation matrices and apply them one after the other (I think that should work)Could you just keep the previous frame/stage's rotation matrix and multiply it with the current rotation matrix made from your Euler angles?   ;Quote:I need the euler angles for a sort of steering behaviour where I need to know the angle the object is facing in all 3 directions to calculate its behaviour relative to other objects in the area.I don't believe it :-)Ok, maybe I'm wrong, and you do need Euler angles here, but I'll continue to play devil's advocate anyway.Say you have an object, A, and want to determine its behavior based on the relative position (and possibly orientation) of object B. Take A's transform, invert it, and multiply it with B's transform; this will give you B's transform expressed in A's local space.It seems to me that this would give you pretty much all the information you'd need for the purposes of AI. From this, you can determine whether the object of interest is above or below, to the right or to the left, in front or behind, facing away or facing towards, etc., and can then apply steering behaviors as appropriate. It's not really clear to me how this would be easier to accomplish using Euler angles.But, I realize you're not asking whether Euler angles are the right choice here, but simply if and how they can be used to do what you're wanting. That I can't really answer, 'cause I'm not really sure what you're trying to do. However, I will say that you can certainly think in terms of Euler angles for the purpose of local-space AI calculations, if that's useful to you.  Based on what you've posted though, I'm not sure if that's what you're looking for.   ;Quote:Original post by jykQuote:I need the euler angles for a sort of steering behaviour where I need to know the angle the object is facing in all 3 directions to calculate its behaviour relative to other objects in the area.I don't believe it :-)Ok, maybe I'm wrong, and you do need Euler angles here, but I'll continue to play devil's advocate anyway.Say you have an object, A, and want to determine its behavior based on the relative position (and possibly orientation) of object B. Take A's transform, invert it, and multiply it with B's transform; this will give you B's transform expressed in A's local space.It seems to me that this would give you pretty much all the information you'd need for the purposes of AI. From this, you can determine whether the object of interest is above or below, to the right or to the left, in front or behind, facing away or facing towards, etc., and can then apply steering behaviors as appropriate. It's not really clear to me how this would be easier to accomplish using Euler angles.Euler angles would be easier because I don't only need how the locations relative to another but also the angles they are facing each other in global numbers. And even if it would work with matrices I made a huge chunk of AI and steering behaviour that took me ages to perfect and don't really want to change it.So euler angles it is...^_^;  I am going to sound like a hypocrite for asking a question about one thing and griping that my replies were suggestive of another method, but jyk is correct; everything you are doing can be done just as easily using another method (specifically storing forward/up/right directional vectors and a positional vector (which is exactly what a matrix is)) but without the headache of converting to matrices some special way.In over 10 years of professional game-programming experience I have never encountered a case where storing the rotations of an object in Euler and converting to a matrix was the correct solution.You only think you need to do that because you understand Euler better and you can visualize the math in your head better.This is important for some people, but the downside is just what you have encountered.  Matrix and vector math is too abstract for you and you cannot convert from your system to theirs correctly.You are basically just trading one evil for another.  Rather than enduring the hardship of storing the data in a way that seems abstract to you, you endure the hardship (and performance hit!) of a conversion you don’t understand and can’t get working right.The only correct way to proceed is to saddle up and wrap your head around matrix and vector applications to the world of 3D.  That would have also saved you the trouble of all the effort you put into that steering mechanism, as it would have seemed trivial to you.As for your problem, read about Euler angles here: http://www.euclideanspace.com/maths/geometry/rotations/euler/index.htmThat page links to this discussion how to combine Euler angles: http://www.euclideanspace.com/maths/geometry/rotations/for/index.htmAs that page puts it, combining Euler angles is so difficult it is often best to simply convert to a matrix or quaternion, perform the rotation, and convert back.As I said before, you are not escaping any difficulty at all by running away from the proper methods.  Instead you suffer a huge penalty in performance.L. Spiro   ;  First of all, I second what jyk and YogurtEmperor have suggested.Quote:Original post by Zaph-0Euler angles would be easier because I don't only need how the locations relative to another but also the angles they are facing each other in global numbers. ...Not to be offending, but: They are obviously not really easy, because you ran into a serious problem, didn't you? AI isn't an island, so the simpleness cannot be substantiated by looking at just one domain, IMHO.Quote:Original post by Zaph-0... And even if it would work with matrices I made a huge chunk of AI and steering behaviour that took me ages to perfect and don't really want to change it.And you probably don't need to change it all. An issue is a bit that we don't know how you deal with the Euler angles in your AI. From what I can imagine, you use distinct differences in heading and differences in pitching. The minimal invasive act would be to re-factor your code so that the scene uses matrices as usual, and at the entry points of the AI sub-system a conversion happens. Maybe you have restrictions in the angles anyhow, so that conversion would be a little bit simpler. Maybe you'll consider to re-factor step-by-step, e.g. line-of-sight computations and similar "small stuff" to use the vectors from the object's scene description as well, and some day you may drop Euler angles at all...[Edited by - haegarr on April 8, 2010 10:17:57 AM]