Jump to content


Mesh roll With Pivot at Top


6 replies to this topic

#1 whizkid667

    New Member

  • Members
  • Pip
  • 9 posts

Posted 04 August 2007 - 06:24 AM

I was trying to rotate a mesh about its z-axis with the pivot of rotation being the top most part of the mesh. In other words, I want the mesh to move about a circle with the center at the top of the mesh and radius being the bounding radius of the mesh. I translated the mesh to the world origin(y-offseted by the bounding radius) . Applied rotation about Z axis and then translated it back to its previous position. Instead of moving about a circle, the mesh kept rolling in a wierd manner. I hope the code below gives a better picture as to what I tried to do. I searched for a solution everywhere but no luck. :wacko: Any help will be deeply appreciated.

void OffsetedRollObject_PivotTop(const FLOAT fUnits)

{

	D3DXMATRIX		matWorld, matTrans ;

	D3DXVECTOR3		vec3Pos ;


	m_TheWorld.GetWorldMatrix(&matWorld) ;

	vec3Pos.x = matWorld._41 ;	vec3Pos.y = matWorld._42 + m_fRadius ;	vec3Pos.z = matWorld._43 ;


	D3DXMatrixTranslation(&matTrans, -vec3Pos.x, -vec3Pos.y, -vec3Pos.z) ;

	D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans) ;

	

	D3DXMatrixRotationZ(&matTrans, fUnits) ;

	D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans) ;


	D3DXMatrixTranslation(&matTrans, vec3Pos.x, vec3Pos.y, vec3Pos.z) ;

	D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans) ;

	

	m_TheWorld.SetWorld(&matWorld) ;

}

Pls check out the URL. This is the result I had expected.http://www.esnips.co..._Roll_Pivot_Top

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 04 August 2007 - 05:46 PM

So you want the mesh to move in a circle, but not change its own orientation, i.e. always face the same direction? If that's the case, you've got the right idea, but instead of translate, rotate, untranslate you'll want to do rotate, translate, unrotate. Basically, rotate by the negative of the angle you want, then translate along the -Y axis, then rotate by the angle you want.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 whizkid667

    New Member

  • Members
  • Pip
  • 9 posts

Posted 05 August 2007 - 11:28 AM

First of all, thanks 4 the reply.....

Quote

So you want the mesh to move in a circle, but not change its own orientation, i.e. always face the same direction?
I think I might have wrongly phrased my query...The orientation of the mesh should change, as in a normal Z axis rotation. I just want it to rotate in such a manner, that the path traversed will be a circle with the center at the top portion of the mesh. (Just like a second hand moving in a clock, but in a 3d Sense). I think the link that I'd given in the previous post was not a proper representation my expected output. I think this one will give a better idea.http://www.esnips.co...oll_Pivot_Top01

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 05 August 2007 - 06:23 PM

If that's the case, then the transformations you originally posted look right. The one thing I notice is that you are accumulating the transformation over time, by taking the previous world matrix and multiplying some transformations into it to get the new world matrix. This can cause precision problems; instead, you should construct the world matrix from scratch each frame, using a variable to hold the current angle of rotation which is updated each frame. That might give you better results.

If that doesn't help it, you're going to have to post a video of what you're getting, since "rolling in a weird manner" doesn't give much to go on...
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 whizkid667

    New Member

  • Members
  • Pip
  • 9 posts

Posted 06 August 2007 - 02:37 PM

Quote

instead, you should construct the world matrix from scratch each frame
I tried it out but it gave the same result :wacko:

Quote

If that doesn't help it, you're going to have to post a video of what you're getting, since "rolling in a weird manner" doesn't give much to go on...
The link below gives a video capture of the rather unexpected roll output. I have kept the camera stationary. After a fixed number of frames(in this case 400) I am reversing the direction of the roll so that it rolls both sides.
http://www.esnips.co...oll_Pivot_Top01

#6 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 06 August 2007 - 04:24 PM

Hmm.. the only thing I can think of is that the mesh is not perfectly aligned with the axis. Either that or something elsewhere in your code is causing unexpected transformations to be added.
reedbeta.com - developer blog, OpenGL demos, and other projects

#7 whizkid667

    New Member

  • Members
  • Pip
  • 9 posts

Posted 07 August 2007 - 01:56 PM

Finally got a solution....
The problem was that I was just offseting the mesh along the world Y for every roll....The correct method was to to offset the mesh along its up vector.


	vec3Up.x = matWorld._21 ;	vec3Up.y = matWorld._22 ;	vec3Up.z = matWorld._23 ;

	D3DXVec3Scale(&vec3Up, &vec3Up, m_fRadius) ;

	vec3Pos.x = matWorld._41 ;	vec3Pos.y = matWorld._42 ;	vec3Pos.z = matWorld._43 ;


	D3DXMatrixTranslation(&matTrans, -vec3Pos.x, -vec3Pos.y, -vec3Pos.z) ;

	D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans) ;


	matWorld._41 += -vec3Up.x ;	matWorld._42 += -vec3Up.y  ;	matWorld._43 += -vec3Up.z ;


	vec3YAxis.x = matWorld._31 ;	vec3YAxis.y = matWorld._32 ;	vec3YAxis.z = matWorld._33 ;


	D3DXQuaternionRotationAxis(&quatRoll, &vec3YAxis, fUnits) ;

	D3DXMatrixRotationQuaternion(&matTrans, &quatRoll) ;

	D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans) ;

	

	matWorld._41 += vec3Up.x ;	matWorld._42 += vec3Up.y  ;	matWorld._43 += vec3Up.z ;


	D3DXMatrixTranslation(&matTrans, vec3Pos.x, vec3Pos.y, vec3Pos.z) ;

	D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans) ;


Thanks a lot Reedbeta for helping me out. :worthy: I don't see anything wrong with this solution as yet. Just hope that this will work under all conditions.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users