• 12
• 27
• 9
• 9
• 20

Magic Cube game

This topic is 4447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi for all I'm developing the magic cube game in DirectX just for learning. I having a big difficult to rotate my entire cube in the Y axis. I wrote a code that the big cube (that's formed by 64 little cubes) translates itself in the Y axis and the little cubes translating and rotating in the Y axis. But I WANT TO ROTATE THE ENTIRE CUBE. FOR THE LOVE OF GOD HEEEEELP. Thanks a lot
void DirectXFunctions::SetTranslate(float fX, float fY, float fZ)
{
D3DXMATRIX TranslationMatrix, MatrixY;
D3DXMatrixRotationY(&MatrixY, timeGetTime()/1000.0f);
D3DXMatrixTranslation(&TranslationMatrix, fX, fY, fZ);
m_DirectXMainDevice->SetTransform(D3DTS_WORLD, &TranslationMatrix);
BigWorldMatrix = TranslationMatrix;
}

void DirectXFunctions::SetRotate()
{
D3DXMATRIX WorldMatrix, TranslationMatrix, MatrixX, MatrixY;
D3DXMatrixRotationX(&MatrixX, timeGetTime()/1000.0f);
D3DXMatrixRotationY(&MatrixY, timeGetTime()/1000.0f);
D3DXMatrixMultiply(&BigWorldMatrix, &MatrixY, &BigWorldMatrix);
D3DXMatrixTranslation(&TranslationMatrix, 10.0f, 0.0f, 0.0f);
D3DXMatrixMultiply(&BigWorldMatrix, &TranslationMatrix, &BigWorldMatrix);
m_DirectXMainDevice->SetTransform(D3DTS_WORLD, &BigWorldMatrix);
}

void DirectXFunctions::Renderize()
{
float fX, fY, fZ;
float fValue = 7.0f;
char cVal[256];
fX = 0.0f;
fY = 0.0f;
fZ = 0.0f;
if(m_DirectXMainDevice == NULL)
{
return;
}
m_DirectXMainDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
m_DirectXMainDevice->BeginScene();
SetCamera();
SetPerspective();
for(int i = 0 ; i < 16 ; i++)
{
for(int j = 0 ; j < 4 ; j++)
{
SetTranslate(fX, fY, fZ);
ArrayCubes[j]->Renderize();
SetRotate();
m_DirectXMainDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,  0, 2);
m_DirectXMainDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,  4, 14);
m_DirectXMainDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2);
fX += fValue;
}
fX = 0.0f;
fY += fValue;
if((i+1)%4 == 0)
{
fValue = 7.0f;
fY = 0.0f;
fZ-=7.0f;
}
}
m_DirectXMainDevice->EndScene();
m_DirectXMainDevice->Present(NULL, NULL, NULL, NULL);
}



Share on other sites
I'm understanding you have a cube that is made up of 64 smaller cubes, and you want them all to rotate as one unit?

If so, what you want to do, is rotate each of the 64 cubes after it's been placed where it should be. You don't want to rotate it first, and then move it to the location. The big difference is that if you first move them, their position will also be modified due to the rotation. I hope this is clear enough :).

At any rate, you need to modify your matrix multiplication order, first translate the cube to a local translation, and then rotate it after it's in place.

Now, when I look at your code, you seem to be setting the world matrix twice, which is a bit confusing to me.

You should only have one of the two function, and you should start from a fresh identity matrix every time.

Here's the basics of what you want to do:

1) Make an identity matrix,
2) Multiply it by your translation matrix,
3) Multiply the result of the first step by your rotation matrix.

Hope this helps :).

Share on other sites
Sirob, I've maded that you told me, but now all the little blocks rotates in the Y axis. I put the code for you to see fi I done anything wrong.
Would you mind to help me again?

Thanks a lot.

void DirectXFunctions::SetTranslate(float fX, float fY, float fZ){   D3DXMATRIX TranslationMatrix;   D3DXMatrixTranslation(&TranslationMatrix, fX, fY, fZ);   m_DirectXMainDevice->SetTransform(D3DTS_WORLD, &TranslationMatrix);   BigWorldMatrix = TranslationMatrix;}void DirectXFunctions::SetRotate(){   D3DXMATRIX WorldMatrix, TranslationMatrix, IdentityMatrix, MatrixY;   D3DXMatrixIdentity(&IdentityMatrix);   D3DXMatrixMultiply(&IdentityMatrix, &BigWorldMatrix, &IdentityMatrix);   D3DXMatrixRotationY(&MatrixY, timeGetTime()/1000.0f);   D3DXMatrixMultiply(&IdentityMatrix, &MatrixY, &IdentityMatrix);   //D3DXMatrixMultiply(&BigWorldMatrix, &MatrixY, &BigWorldMatrix);   //D3DXMatrixTranslation(&TranslationMatrix, 10.0f, 0.0f, 0.0f);   //D3DXMatrixMultiply(&BigWorldMatrix, &TranslationMatrix, &BigWorldMatrix);   m_DirectXMainDevice->SetTransform(D3DTS_WORLD, &IdentityMatrix);}

Share on other sites
Ok, I think I mixed you up a bit with the indentity matrix thing. Right now, you don't need one.

If I understand what's going on correctly, it's actually the only thing causing your code not to do what you want it to.

Here's how I think this should look without the identity matrix (sorry if it mixed you up a bit):

void DirectXFunctions::SetRotate(){   D3DXMATRIX WorldMatrix, TranslationMatrix, IdentityMatrix, MatrixY;   //D3DXMatrixIdentity(&IdentityMatrix);   //D3DXMatrixMultiply(&IdentityMatrix, &BigWorldMatrix, &IdentityMatrix);   D3DXMatrixRotationY(&MatrixY, timeGetTime()/1000.0f);   //D3DXMatrixMultiply(&IdentityMatrix, &MatrixY, &IdentityMatrix);   D3DXMatrixMultiply(&BigWorldMatrix, &MatrixY, &BigWorldMatrix);   //D3DXMatrixTranslation(&TranslationMatrix, 10.0f, 0.0f, 0.0f);   //D3DXMatrixMultiply(&BigWorldMatrix, &TranslationMatrix, &BigWorldMatrix);   m_DirectXMainDevice->SetTransform(D3DTS_WORLD, &BigWorldMatrix);}

Hope this helps.