Pivot/center point in x.file

I'm a total newbie, so appoligize if this is a stupid question. How can I set the pivot point of my 3d model (x.file). I'm loading a car model and the pivot point is set to the left side of the car.
I want the pivot point to be at the center of the car for easier formulas for moving/rotating the car. How can I do this If I approaches this the wrong way pleas tell me.


Answer this question

Pivot/center point in x.file

  • MarcoL

    You can use this function:

    D3DXComputeBoundingSphere( (D3DXVECTOR3*)pData, numVertices, fvfSize, &center, &m_boundingRadius );

    It will tell you the center of your mesh and it will tell you the radius of the smallest sphere around your mesh...with this information you can treat your entire mesh has if it was a sphere...you remove the center point value to put your mesh at 0,0,0 (move it with a translation matrix in World Matrix with negative value of the center coordinate)and after that you can rotate it and move it back where you want it's center in the 3D world

    This demo is also in Managed code on their site

    void CMeshInstance::SetMesh( CMesh* pMesh )
    {

    Release();
    m_pMesh = pMesh;

    // Compute bounding sphere
    if ( m_pMesh )
    {
    D3DXVECTOR3 center;
    LPD3DXMESH pD3DXMesh = m_pMesh->GetMesh();
    DWORD numVertices = pD3DXMesh->GetNumVertices();
    DWORD fvfSize = D3DXGetFVFVertexSize( pD3DXMesh->GetFVF() );
    char* pData = NULL;
    if ( FAILED( pD3DXMesh->LockVertexBuffer( 0, (void**)&pData ) ) )
    {
    SHOWERROR( "Failed to lock mesh vertex buffer.", __FILE__, __LINE__ );
    return;
    }
    D3DXComputeBoundingSphere( (D3DXVECTOR3*)pData, numVertices, fvfSize, &center, &m_boundingRadius );
    if ( FAILED( pD3DXMesh->UnlockVertexBuffer() ) )
    {
    SHOWERROR( "Failed to unlock mesh vertex buffer.", __FILE__, __LINE__ );
    return;
    }
    }

    }

    http://www.c-unit.com/tutorials/directx/ t=24


  • Mumshelp

    Can you explain in more detail I know how I can calculate the bounding sphere of the mesh, but I don't understand how I can use that information to change my center/pivot point. I don't understand what you mean by this "you remove the center point value to put your mesh at 0,0,0 (move it with a translation matrix in World Matrix with negative value of the center coordinate)and after that you can rotate it and move it back where you want it's center in the 3D world"

    I'm using managed directx


  • Soaring Skies

    Thank you both of you:) I finaly understand. The point I have been missig is that rotations always happen about 0,0,0.
  • xlthim

    or load the model into the modelling package and center it on the origin



  • lchristensen

    You are setting the world matrix to a rotation and rotations always happen about 0,0,0. So if you 'move' your mesh such that what used to be a,b,c (where a,b,c is the center point from getboundingspehere) is now 0,0,0 then do the rotation, then move the mesh bak so that 0,0,0 is now a,b,c again then you will get the effect you want.

    The easiest way is to set your world matrix to do all 3 of these things

    world = translate(-a,-b,-c) * rotate(your original rotation) * translate(a,b,c)



  • Pivot/center point in x.file