   Problem with Rotations
  Hi again guys...sorry to bother you again guys...na I'm sure you like me :pI'm having a little problem with Rotations in DirectX 9.0c and MFC. Here's my function :void CObjet::RotationX(IDirect3DDevice9* pDevice){D3DXMATRIX matRotX, matTranslation;D3DXMatrixRotationX(&matRotX, m_fRotationX*(D3DX_PI/180.0f));D3DXMatrixMultiply(&matWorld, &matRotX, &matWorld );}I got 2 others for Y and Z but they're the same code so useless to post. Anyway, I got an object that I move using Translation function and then can rotate anywhere in my world, like any 3d editors, using the mouse.When I created the Rotation, I was modifying directly the matWorld, but the object was always returning to the point of origin (0,0,0) when I was beginning to rotate it. Because I was modifying the matWorld and not multiplying it :D3DXMatrixRotationX(&matWorld, m_fRotationX*(D3DX_PI/180.0f));I then decided to multiply 2 matrix, the matWorld and a Temp matrix which would hold my rotation. It works very well, the object rotate where it has been translated BUT the rotation is messed up.- The object doesn't rotate regulary. Sometimes it'll be very slow and sometimes very fast. I don't change my mouse speed and it accelerates itself.- I got a property window which tells me the exact angle where the object is and while it was working great when modifying directly the matWorld, now the angles are messed up and when the chair is physically 90 degrees, it would show me like 15-20.-And another problem explained here: Here's my code which call the RotationX :if (point.y &gt; m_pMouseMove.y)m_oObjSelection-&gt;SetRotationX(m_oObjSelection-&gt;GetRotationX()-1);else if (point.y &lt; m_pMouseMove.y)m_oObjSelection-&gt;SetRotationX(m_oObjSelection-&gt;GetRotationX()+1); m_oObjSelection-&gt;RotationX(m_p3DDevice);So basically, if I move my mouse up from where I clicked, the rotation will decrement by 1. I update the new position by doing m_pMouseMove = point at the end of these conditions, so that when I decide to change my mouse direction, the object will follow and I won't have to get to the position I clicked before my object change direction. I don't know if you follow me on this one. This is pretty hard to explain...anyway this was working fine when I was using the matWorld directly, but now it doesn't work. Well I mean if I go up and down the object follow but higher (or lower) I'll go before going back to the point, longer it will take for my object to change it's direction. Like if the m_pMouseMove = point wouldn't work but it works fine, I checked while in debug.And for the record, I do the SetTransform of the matWorld in the Draw()Bottom line, my function RotationX(pDevice) is missing something I think, but I don't see what. Just looks like that my multiply doesn't work well or something like that.Again, sorry for my bad english and weird explanations of my problem...it's easy to explain in french but in english it's harder for me ;)Thanks again guys,Bbs[Edited by - Bbs on December 10, 2005 9:23:13 PM]
  bump for emergency help? ;)   ;Quote:Original post by Bbsbump for emergency help? ;)Subtle hint... it's usually frowned upon to bump unless your thread has been cold for 24hrs (or fallen off the topic listing) [smile]Anyway, three things I can think of:1. Are you resetting your world matrix to the identity each frame? You can get some odd results if you continue to re-use/increment the same variables. 2. Are you using radians all the way through? mixing-and-matching radians/degrees can make for easy to miss bugs.3. Is there any degree of timing involved in your calculations? timer inaccuracies can cause transforms/animations to slow down and/or suddenly jump forwards (and backwards!).hthJack   ;Quote:Original post by jollyjeffersQuote:Original post by Bbsbump for emergency help? ;)Subtle hint... it's usually frowned upon to bump unless your thread has been cold for 24hrs (or fallen off the topic listing) [smile]Anyway, three things I can think of:1. Are you resetting your world matrix to the identity each frame? You can get some odd results if you continue to re-use/increment the same variables. 2. Are you using radians all the way through? mixing-and-matching radians/degrees can make for easy to miss bugs.3. Is there any degree of timing involved in your calculations? timer inaccuracies can cause transforms/animations to slow down and/or suddenly jump forwards (and backwards!).hthJackoups sorry for the quick bump...1. My matWorld is set to identity only 1 time, when I create the Object using CObject class. Otherwise, it's never called.2. I'm using degrees. My RotationX function uses m_fRotationX which is a float degrees and then I convert them to radian (m_fRotationX*(D3DX_PI/180.0f)). It works very well, well while using the matWorld directly.3.There is no timing at all. I only use my mouse as a "timer". As long as I move my mouse while the item is selected, m_fRotationX while be increased by 1 degree each pixels.Should I put D3DXMatrixIdentity (&matWorld) each time I render? ;  Generally speaking you should completely re-construct your world matrix each time it needs to change. Although, I'm not sure that this will solve your problem - but worth trying [smile]instead of:1. Identity2. Rotate by 103. Render4. Rotate by 10  (now @ 20 degrees)5. Render6. Rotate by 10  (now @ 30 degrees)7. Render8. Rotate by 10  (now @ 40 degrees)9. RenderYou should do:1. Identity2. Rotate by 103. Render4. Identity5. Rotate by 206. Render7. Identity8. Rotate by 309. Render10. Identity11. Rotate by 4012. RenderThe reasoning for this is that, in the first example, over a large enough number of frames (particularly possible if you have a high frame-rate or fast moving mouse) the tiny accuracy errors can accumulate to give noticeable results.Another thing that I got a long time ago is the use of angles outside of 0..360 (are you clamping/wrapping yours?). Mathematically the trig functions repeat themselves, but the standard libraries implementation (that I used at the time) started to break down after you'd repeated 10-20 times (in my code, 10-20 rotations wasn't uncommon). Could be worth outputting some debug of your values to make sure they're not going out of bounds.hthJack   ;Quote:Original post by jollyjeffersGenerally speaking you should completely re-construct your world matrix each time it needs to change. Although, I'm not sure that this will solve your problem - but worth trying [smile]instead of:1. Identity2. Rotate by 103. Render4. Rotate by 10  (now @ 20 degrees)5. Render6. Rotate by 10  (now @ 30 degrees)7. Render8. Rotate by 10  (now @ 40 degrees)9. RenderYou should do:1. Identity2. Rotate by 103. Render4. Identity5. Rotate by 206. Render7. Identity8. Rotate by 309. Render10. Identity11. Rotate by 4012. RenderThe reasoning for this is that, in the first example, over a large enough number of frames (particularly possible if you have a high frame-rate or fast moving mouse) the tiny accuracy errors can accumulate to give noticeable results.Another thing that I got a long time ago is the use of angles outside of 0..360 (are you clamping/wrapping yours?). Mathematically the trig functions repeat themselves, but the standard libraries implementation (that I used at the time) started to break down after you'd repeated 10-20 times (in my code, 10-20 rotations wasn't uncommon). Could be worth outputting some debug of your values to make sure they're not going out of bounds.hthJackI do check if the m_fRotationX is between 0 and 360. If it's below 0 (obviously -1), the condition affects m_fRotation to 259. If it's higher than 360, it goes back to 0.As for the steps you gave me, I'm pretty sure I'm doing the right thing. Because I'm adding 1 to m_fRotationX and then I rotate it. So my rotation isn't working on the "+1", but on the m_fRotationX directly, which is incremented at each mouse move (or decremented).Reconstructing my matWorld each time? You mean that I recreate my matWorld each time I call OnMouseMove and then multiply the Translation, Rotation and all stuff before rendering? If so, there must be an easier way... :/   ;  If your adding translation and rotation to your world matrix it'll get all wobly because you are not rotating around the center anymore.also it looks like you keep adding to the amount to rotate obj at 0 deg, rotate 1 degobj at 1 deg, rotate 2 degobj at 3 deg  rotate 3 degobj at 6 deg  rotate 4 degdo you mean to accelerate the rotation speed, or do you just want to move 1 degree per frame.I think you algorithm should look something likeeach frame rotate the world matrix by 0 1 or -1 deglike you haveD3DXMatrixRotationX(&matRotX, m_fRotationX*(D3DX_PI/180.0f));D3DXMatrixMultiply(&matWorld, &matRotX, &matWorld );except that m_fRotationX should only be 0, -1 or 1or you should recalculate the world matrix   D3DXMatrixIdentity(&matWorld)   D3DXMatrixRotationX(&matRotX, m_fRotationX*(D3DX_PI/180.0f))   D3DXMatrixTranslation(&matTrans, trans)   matWorld = matRotX * matTranswhere m_fRotationX should be in the 0-360 range.   ;Quote:Original post by robert_por you should recalculate the world matrix   D3DXMatrixIdentity(&matWorld)   D3DXMatrixRotationX(&matRotX, m_fRotationX*(D3DX_PI/180.0f))   D3DXMatrixTranslation(&matTrans, trans)   matWorld = matRotX * matTranswhere m_fRotationX should be in the 0-360 range.This is working like a charm. I put all my D3DXMATRIX members of my class CObject and then multiply everything before rendering...result is awesome and even corrected another bug that I had :DThanks robert and all of you for helping me !Guillaume :)   