Rotating a mesh

Started by
16 comments, last by ZitherMan 20 years ago
Thank you very very very much for your replies, there are many things you've made more clear for me. However, the mesh still doesn't rotate right

Here's a screenshot:
http://zith.cisko.org/screen2.gif

So it still rotates around the same point as before, while i want it to rotate around its own pivot-point / axis. The speed of the rotation also increases every frame, making the object spin faster and faster and i really can't figure out why..



I made a frame-class pretty much like the one you showed me.

          // This is the headerclass CFrame{private:    D3DXMATRIX  m_mLocal;    D3DXVECTOR3 m_vPosition;    D3DXVECTOR3 m_vVelocity;    D3DXVECTOR3 m_vOrientation;    LPDIRECT3DDEVICE8 m_D3DDevice;    D3DXMATRIX mtxRot;    CMesh *m_Mesh;public:    CFrame(LPDIRECT3DDEVICE8 lpD3DDevice, CMesh *Mesh);    ~CFrame();    //All your set and get functions for position, velocity, etc    void Rotate(D3DXVECTOR3 axis, long Speed);    void Update();    HRESULT Render();};// This is the cpp fileCFrame::CFrame(LPDIRECT3DDEVICE8 lpD3DDevice, CMesh *Mesh){	m_vOrientation = D3DXVECTOR3(0.00000001,0.00000001,0.00000001);	m_vVelocity = D3DXVECTOR3(0,0,0);	m_vPosition = D3DXVECTOR3(0,0,0);	D3DXMatrixIdentity(&m_mLocal);	m_D3DDevice = lpD3DDevice;	m_Mesh = Mesh;}HRESULT CFrame::Render(){	HRESULT hr;	m_D3DDevice->SetTransform(D3DTS_WORLD, &m_mLocal);	hr = m_Mesh->Render();	return hr;}void CFrame::Rotate(D3DXVECTOR3 axis, long speed){	m_vOrientation += axis * (float)speed) / 10000.0f;	D3DXMatrixRotationYawPitchRoll(&mtxRot, m_vOrientation.x, m_vOrientation.y, m_vOrientation.z);	D3DXMatrixMultiply(&m_mLocal, &mtxRot, &m_mLocal);}    


As you can see, i'm using 0.00000001x0.00000001x0.00000001 as the orientation-vector. If i use a bigger value, the rotation will be much faster. If i use 0, there is no rotation.


The mesh-class have changed quite a lot so i'll post that too.

     // This is the header (some old parts might still be in it)class CMesh{public:	CMesh(LPDIRECT3DDEVICE8 lpD3DDevice, LPSTR Filename);	~CMesh();	HRESULT Render();private:        LPD3DXMESH			m_Mesh;	D3DMATERIAL8		*m_Materials;	LPDIRECT3DTEXTURE8	*m_Textures;	DWORD				m_NumMaterials;	LPD3DXBUFFER		m_MaterialsBuffer;	LPDIRECT3DDEVICE8	m_D3DDevice;};// This is the cpp-file, pretty much init-stuff, the only function that is left except for the constructor and the deconstructor is Render()CMesh::CMesh(LPDIRECT3DDEVICE8 lpD3DDevice, LPSTR Filename){	HRESULT hr;	m_D3DDevice = lpD3DDevice;	Log.Log("Loading Model ",Filename);	if(FAILED(D3DXLoadMeshFromX(Filename, D3DXMESH_SYSTEMMEM, m_D3DDevice, NULL, &m_MaterialsBuffer, &m_NumMaterials, &m_Mesh)))	{		Log.Log("Could not load model ",Filename);	}	D3DXMATERIAL *d3dxMaterials = (D3DXMATERIAL*)m_MaterialsBuffer->GetBufferPointer();	m_Materials = new D3DMATERIAL8[m_NumMaterials];	m_Textures = new LPDIRECT3DTEXTURE8[m_NumMaterials];	for(DWORD i = 0; i < m_NumMaterials; i++)	{		m_Materials[i] = d3dxMaterials[i].MatD3D;		m_Materials[i].Ambient = m_Materials[i].Diffuse;		hr = D3DXCreateTextureFromFile(m_D3DDevice, d3dxMaterials[i].pTextureFilename, &m_Textures[i]);		if(hr==D3DERR_NOTAVAILABLE)		{			m_Textures[i] = NULL;			Log.Log("  Error, Not Avavible");		}		else if(hr==D3DERR_OUTOFVIDEOMEMORY)		{			m_Textures[i] = NULL;			Log.Log("  Error, Out of videomemory");		}		else if(hr==D3DERR_INVALIDCALL)		{			m_Textures[i] = NULL;			Log.Log("  Error, Invalid Call");		}		else if(hr==D3DXERR_INVALIDDATA)		{			m_Textures[i] = NULL;			Log.Log("  Error, Couldn't find texture", d3dxMaterials[i].pTextureFilename);		}		else if(hr==E_OUTOFMEMORY)		{			m_Textures[i] = NULL;			Log.Log("  Error, Out of memory");		}	}	m_MaterialsBuffer->Release();	Log.Log("Model Loaded");}CMesh::~CMesh(){	if(m_Materials != NULL)		delete[] m_Materials;	if(m_Textures != NULL)	{		for(DWORD i = 0; i < m_NumMaterials; i++)		{			if(m_Textures[i])				m_Textures[i]->Release();		}		delete[] m_Textures;	}		if(m_Mesh != NULL)		m_Mesh->Release();}HRESULT CMesh::Render(){    HRESULT r = E_FAIL;    for (int i = 0; i < m_NumMaterials; i++)    {        //m_Materials.Update();<br></font><br>		m_D3DDevice-&gt;SetMaterial(&m_Materials[<font color="purple">i</font>]);<br>        m_D3DDevice-&gt;SetTexture(0, m_Textures[<font color="purple">i</font>]);<br>        r = m_Mesh-&gt;DrawSubset(i);<br>    }<br><br>    <font color="gray">// Reset the vertexshader here when (if) i will use one<br></font><br>    <font color="blue">return</font> r;<br>}<br><br><br>          </pre></DIV><!–ENDSCRIPT–><br><br><br><SPAN CLASS=editedby>[edited by - ZitherMan &#111;n May 13, 2003 11:58:56 AM]</SPAN><br><br><SPAN CLASS=editedby>[edited by - ZitherMan &#111;n May 16, 2003 10:56:44 AM]</SPAN>    
----------------------------
When hell freezes over it will be a pretty cool place to snowboard
Advertisement
I just wanted you guys to know that i''m still stuck

It seems like the mesh rotates around the point where the camera starts.
----------------------------
When hell freezes over it will be a pretty cool place to snowboard

  void CFrame::Rotate(D3DXVECTOR3 axis, long speed){	m_vOrientation += axis * (float)speed) / 10000.0f;	D3DXMatrixRotationYawPitchRoll(&mtxRot, m_vOrientation.x, m_vOrientation.y, m_vOrientation.z);	D3DXMatrixMultiply(&m_mLocal, &mtxRot, &m_mLocal);}   


try resetting your mesh matrix to identity before myltiplying
since your are rotating with acceleration ...

also you can't just add orientation forever ... it should wrap around some value .... I use degrees it should be reset after 360



[edited by - thy MC on May 20, 2003 12:50:15 PM]
Please AGREE or I will LIBERATE you
..first of all...I have to admit...I didn''t read all the way through your last snippet of source code...so forgive me if my assumption is incorrect....

..but...from your last screenshot...it looks like your building your matrix wrong.

The order of transformation is very important. You don''t get the same results if you do the translation first and then the rotation or if you do the rotation and then the translation.

From your screenshot, it looks like you''re doint the translation first and then the rotation....which would be wrong...

...you have to do the rotation first and then do the translation...that should work just fine....

...the rotation happens around the World 0,0,0 ....


....maybe I missed the point....hope it was the solution to your problem though....
Thanks for your replies.

djc: you''re right. I did translate before i rotated, but unfortenantly it didnt make any difference

I have positioned it on 0,0,0 already, but it''s still spinning around the cameras original position (which i also set to 0,0,0, and it is not positioned in the same place as the mesh, and yes, my pivot-point is correct (if the 3ds to .x tool which comes with dxsdk 7 doesnt change this horribly)

However, when i try to set the mesh on some other place in the init, it still shows up in the same place.



argh, i cant get my head around this

----------------------------
When hell freezes over it will be a pretty cool place to snowboard
I''m still stuck with this problem


Is there anyone who maybe could give me their source for THEIR mesh-handling (including rotation) so i can see how you did it? I have already made the base for mine, so i wont steal the whole thing. The only thing i want to do is to see how you did it.
----------------------------
When hell freezes over it will be a pretty cool place to snowboard
I''ve got the same problem. It''s just that the pivot is wrong. Pivot (0, 0, 0) doesn''t mean world coordinates. It means local coordinates. So how do you turn the local pivot into world coordinates?
*News tagenigma.com is my new domain.
im only just starting with the 3D stuff, but try

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/TutorialsAndSamples/Tutorials/tutorials.asp

for help. thats what i used to get my head around the meshes along with the transformation tutorials from two kings:

http://www.riaz.de/index.html

have a look through all that and if it still doesnt help then i''ll paste some of my code in here. I would do it now, but its such a mess that i doubt it will be any help to you.

This topic is closed to new replies.

Advertisement