Archived

This topic is now archived and is closed to further replies.

alberto_balsalm

Matrix question in D3D

Recommended Posts

Hi! I want to rotate a cube around its center coordinate but at different lokation than origo. However, instead of rotating around it''s center coordinate (2,3,0 for example) my cube rotates around a different coordinate (4.6,0).. What is wrong with my code?
  

D3DXMatrixTranslation(&WorldMatrix,2.0f, 3.0f, 0.0f);
D3DXMatrixRotationZ(&RotMat, Radians);
D3DXMatrixMultiply(&RotMat, &RotMat, &WorldMatrix);

D3DXMatrixTranslation(&WorldMatrix,2.0f, 3.0f, 0.0f);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &RotMat);
c3DDevice->SetTransform(D3DTS_WORLD, &WorldMatrix);
DrawCube();

  

Share this post


Link to post
Share on other sites
a person    118
try reverse your transformations. in other words, you must translate the cubes center to 0,0,0 thne rotate, then translate cube back to original position. the translations should cancel out (they are not in your case)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
it looks to me like your adding (2,3,0) and then rotating. and then adding (2,3,0) again.

what you want to do is add (2,3,0) and then rotoate -- and then *subtract* (2,3,0). Or, more likely, subtract (2,3,0), rotate, and then add (2,3,0).

-Wyriel

Share this post


Link to post
Share on other sites
steg    390
As ''a person'' as said, you must translate the cubes centre to the origin (0,0,0) if using an identity world matrix then apply rotation and then translate back to the original position - you can use an inverse translation matrix.

Reg''ds,
Steve

Share this post


Link to post
Share on other sites
I thought that depending on the where i wanted the object to rotate around, i translated the world matrix to that possision in world space, but apperantly thats not the case. So if i want to rotate around a point -1.0f on the x row. i simply translate the world matrix (-1.0f, 0.0f, 0.0f), right??

//Mikael -

Share this post


Link to post
Share on other sites
steg    390
You have to translate the object not the world matrix, thus store your objects positions in world space, translate it to 0,0,0 then rotate it then translate back to original position.

Reg''ds,
Steve

Share this post


Link to post
Share on other sites