PDA

View Full Version : Mesh roll With Pivot at Top


whizkid667
08-03-2007, 11:24 PM
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.com/doc/95a57f00-c793-4ec8-b1c7-fa3c03e28a72/Mesh_Roll_Pivot_Top

Reedbeta
08-04-2007, 10:46 AM
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.

whizkid667
08-05-2007, 04:28 AM
First of all, thanks 4 the reply.....
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.com/doc/5a86dd69-142f-45e1-afe2-2f3361a34a8c/Mesh_Roll_Pivot_Top01

Reedbeta
08-05-2007, 11:23 AM
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...

whizkid667
08-06-2007, 07:37 AM
instead, you should construct the world matrix from scratch each frame
I tried it out but it gave the same result :wacko:
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.com/doc/ccbab099-0598-4f5e-a47d-468a98255bb6/Mesh_Roll_Pivot_Top01

Reedbeta
08-06-2007, 09:24 AM
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.

whizkid667
08-07-2007, 06:56 AM
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.