HELP: moving X files around in my world

Started by
8 comments, last by supagu 22 years, 5 months ago
im new to directx, and i got a problem, ive loaded 2 x-files, i do a few matrix operations on the wolrd and it does what i want sort of, but what i cant seem to do id work out how to move each x-file instead of moving the world, all ive done is loaded another mesh into the tut06_meshes that comes with the SDK. i looked in the dolphon example onhow they move the dolhin, they dont even use the command SetTransform() they seem to use Transpose() or something like it, i looked in the help and it was some stupid definition like "returns the transpose of a transpose" wtf is a transpose? so if ya can help that would be k3wl thanks
Advertisement
Changing the world matrix is the correct way to do things. The world matrix is perhaps somewhat misleadingly named. What it specifies is an objects transformation in world space. You should store one transformation maxtrix for each object you are going to render, then you change the world matrix to the current object''s transformation object before you render. So in your case, one tranformation matrix for each x file. Set the world matrix to the transformation matrix for each mesh before rendering.

The transpose of a matrix exchanges its row and column coordinates (ex: m(1,2) becomes m(2,1).
thanks, man, ill see what i can do, but how do you relate the mesh and the world matrix to each other... they seem to be unrelated.
D3DXMATRIX matWorld;
D3DXMATRIX test1;
D3DXMATRIX test2;
D3DXMATRIX scale;


D3DXMatrixRotationY( &test1, timeGetTime()/1000.0f );
D3DXMatrixTranslation( &test2, 1, 0, 0 );
D3DXMatrixMultiply(&matWorld, &test1, &test2 );
D3DXMatrixScaling( &scale, 0.5, 0.5, 0.5 );
D3DXMatrixMultiply(&matWorld, &matWorld, &scale );


g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );


this is the chunk of code what i use to do my moving and stuff
there is no reference of my mesh which is called &g_pMesh and the other mesh file with i called &g_pMesh

so er, im stumped... thanks for bearing with me...
lo? plz, ene 1
What you are doing looks right, except you should always scale, then rotate, then translate. If you do not wierd things can happen. For instance, if you mean to make something twice as tall, but start by rotating it on its back, it will be rotated first, then scaled, which means it will be twice as fat instead (because the stomach is now facing up when you do the scale).
The order of the matrices matters -- always scale, rotate, translate. Let me show you an example from my mesh class. Each mesh has 3 vectors, one for position (translation), one for scaling, and one for rotation. Here are some samples of the functions I have to update these values:

      void CMesh::MoveTo ( FLOAT x, FLOAT y, FLOAT z ){	m_vLocation.x = x;	m_vLocation.y = y;	m_vLocation.z = z;	UpdateMatrix ();}void CMesh::RotateBy ( FLOAT x, FLOAT y, FLOAT z ){	m_vRotation.x += x;	m_vRotation.y += y;	m_vRotation.z += z;	UpdateMatrix ();}  

You can see that all these functions do is manipulate the vector, then call UpdateMatrix. UpdateMatrix constructs a translation world matrix from the position, rotation and scaling vectors and stores it in the mesh's m_mWorld matrix:
        void CMesh::UpdateMatrix ( void ){	D3DXMATRIX matRot, matTrans, matScale ;	D3DXMatrixScaling( &matScale, m_vScale.x, m_vScale.y, m_vScale.z);	D3DXMatrixTranslation(&matTrans, m_vLocation.x, m_vLocation.y, m_vLocation.z);	D3DXMatrixRotationYawPitchRoll(&matRot, m_vRotation.y, m_vRotation.x, m_vRotation.z);	m_mWorld = matScale * matRot * matTrans;}  

On render, just set the world matrix to the mesh's matrix:
       void CMesh::Render(){    m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_mWorld );    m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );    m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );    m_pd3dDevice->SetIndices( m_pIB, 0 );    m_pd3dDevice->DrawIndexedPrimitive( m_renderType, 0, dwNumberofVertices, 0, m_dwNumberofIndices / 3 ); }  


Since everything is nicely encapsulated in the class you don't have to worry about the order of transforms (Updatematrix always does it in the right order), and you can make readable code like:

CMesh player = new CMesh ("player.x");
player->MoveTo (0.0f, 0.0f, 50.0f);
player->RotateY (PI/2);
player->Render();


Edited by - invective on November 10, 2001 2:03:09 AM
w0w thanks heaps man... makes more sense now...

ill give it a sh0t
oh man... looks like you copied and pasted from a tutorial... if so can i get the link of ya...

im having real trouble with this... i got some of the class set up like youve got... but the linkto the tut if it is would be o so lovely

thanks 1''ce again
oh man... looks like you copied and pasted from a tutorial... if so can i get the link of ya...

im having real trouble with this... i got some of the class set up like youve got... but the linkto the tut if it is would be o so lovely

thanks 1''ce again
sorry, i didn''t get it from a tutorial, just years of playing with 3d modeling programs. I can send you my code if you want, but its kind of quirky because its meant to be used with the common files framework, and its not really commented or organized. Still, if you just take the matrix and transform functions you might be able to integrate it into your program. Here is the relavant part. If you need the whole thing, icq me 15869327.
  class CMesh {protected:	D3DXVECTOR3	m_vRotation;	D3DXVECTOR3	m_vLocation;	D3DXVECTOR3 m_vScale;	D3DXMATRIX m_mWorld;	void UpdateMatrix ( void );public:	CMesh ();	void MoveBy   ( FLOAT x, FLOAT y, FLOAT z );	void MoveTo   ( FLOAT x, FLOAT y, FLOAT z );	void RotateBy ( FLOAT x, FLOAT y, FLOAT z );	void RotateX  ( FLOAT x );	void RotateY  ( FLOAT y );	void RotateZ  ( FLOAT z );	void ScaleBy  ( FLOAT x, FLOAT y, FLOAT z );	void Scale    ( FLOAT x, FLOAT y, FLOAT z );	~CMesh ();};void CMesh::MoveBy ( FLOAT x, FLOAT y, FLOAT z ){	m_vLocation.x += x;	m_vLocation.y += y;	m_vLocation.z += z;	UpdateMatrix ();}void CMesh::MoveTo ( FLOAT x, FLOAT y, FLOAT z ){	m_vLocation.x = x;	m_vLocation.y = y;	m_vLocation.z = z;	UpdateMatrix ();}void CMesh::RotateBy ( FLOAT x, FLOAT y, FLOAT z ){	m_vRotation.x += x;	m_vRotation.y += y;	m_vRotation.z += z;	UpdateMatrix ();}void CMesh::RotateX ( FLOAT x ){	m_vRotation.x = x;	UpdateMatrix ();}void CMesh::RotateY ( FLOAT y ){	m_vRotation.y = y;	UpdateMatrix ();}void CMesh::RotateZ ( FLOAT z ){	m_vRotation.z = z;	UpdateMatrix ();}void CMesh::ScaleBy  ( FLOAT x, FLOAT y, FLOAT z ){	m_vScale.x += x;	m_vScale.y += y;	m_vScale.z += z;	UpdateMatrix ();}void CMesh::Scale    ( FLOAT x, FLOAT y, FLOAT z ){	m_vScale.x = x;	m_vScale.y = y;	m_vScale.z = z;	UpdateMatrix ();}void CMesh::UpdateMatrix ( void ){	D3DXMATRIX matRot, matTrans, matScale ;	D3DXMatrixScaling( &matScale, m_vScale.x, m_vScale.y, m_vScale.z);	D3DXMatrixTranslation(&matTrans, m_vLocation.x, m_vLocation.y, m_vLocation.z);	D3DXMatrixRotationYawPitchRoll(&matRot, m_vRotation.y, m_vRotation.x, m_vRotation.z);	m_mWorld = matScale * matRot * matTrans;}CMesh::CMesh (){	m_pd3dDevice = NULL;	m_pvVertices = NULL;	m_pwIndices = NULL;	m_dwSizeofVertices = 0;    m_dwSizeofIndices = 0;	m_dwNumberofVertices = 0;	m_dwNumberofIndices = 0;	m_pTexture = NULL;	m_pVB = NULL;    m_pIB = NULL;	m_vRotation = D3DXVECTOR3 ( 0.0, 0.0, 0.0 );	m_vLocation = D3DXVECTOR3 ( 0.0, 0.0, 0.0 );	m_vScale = D3DXVECTOR3 ( 1.0, 1.0, 1.0 );	UpdateMatrix ();	m_renderType = D3DPT_LINELIST;	return;}  

This topic is closed to new replies.

Advertisement