# Object matrices

## Recommended Posts

cutovoi    122
Hi fellows I'm developing a magiccube game. I'm having some difficulties to continue the project. I have 3 simple cubes, one for the left(rotating in the y axis and translating in the x axis), one for the right(rotating in the y axis and translating in the x negative axis) and one in the center ro tating in their y-axis. The two cubes rotating and translating fine, BUT these 2 guys doesn't translate around the middle cube, they cross the middle cube. I don't want to do this. I want to rotate the 2 cubes around the middle cube. Please help me in this fellows. Once again thanks a lot for the support. Below some part of my code:
the cube's constructor:

Cube::Cube(float fXVertex, float fYVertex, float fZVertex, float fXPos, float fYPos, float fZPos, LPDIRECT3DDEVICE8 DirectDevice, bool bSide)
{
m_fXVertex = fXVertex;
m_fYVertex = fYVertex;
m_fZVertex = fZVertex;
m_fXPos = fXPos;
m_fYPos = fYPos;
m_fZPos = fZPos;
m_DirectDevice = DirectDevice;
m_bSide = bSide;
CreateVerticesBuffer();
}

the values:
theCube0  = new Cube(3.0f, 3.0f, 3.0f, -6.5f, 0.0f, 0.0f, this->g_pDirect3DDevice, true);
theCube1  = new Cube(3.0f, 3.0f, 3.0f,  0.0f, 0.0f, 3.0f, this->g_pDirect3DDevice, false);
theCube2  = new Cube(3.0f, 3.0f, 3.0f,  6.5f, 0.0f, 0.0f, this->g_pDirect3DDevice, true);

Cube Functions:
D3DXMATRIX Cube::RotateX()
{
D3DXMATRIX TempMatrix;
D3DXMatrixRotationX(&TempMatrix, timeGetTime()/1000.0f);
return TempMatrix;
}
D3DXMATRIX Cube::RotateY()
{
D3DXMATRIX TempMatrix;
D3DXMatrixRotationY(&TempMatrix, timeGetTime()/1000.0f);
return TempMatrix;
}
D3DXMATRIX Cube::RotateZ()
{
D3DXMATRIX TempMatrix;
D3DXMatrixRotationZ(&TempMatrix, timeGetTime()/1000.0f);
return TempMatrix;
}

void Cube::ApplyIt()
{
D3DXMATRIX TempMatrix;
D3DXMatrixMultiply(&CubeMatrix, &(this->RotateY()), &CubeMatrix);
if(m_bSide)D3DXMatrixMultiply(&CubeMatrix, &(this->TranslateMatrix()), &CubeMatrix);
m_DirectDevice->SetTransform(D3DTS_WORLD, &CubeMatrix);
}

D3DXMATRIX Cube::TranslateMatrix()
{
D3DXMATRIX TempMatrix, TempMatrix2;
D3DXMatrixTranslation(&TempMatrix, m_fXPos, m_fYPos, m_fZPos);
//D3DXMatrixMultiply(&TempMatrix, &TempMatrix2, &TempMatrix);
return TempMatrix;
}

void Cube::RenderCube()
{
m_DirectDevice->SetStreamSource(0, m_DirectVertex, sizeof(VertexNonFVF));
this->SetPositions(m_fXPos, m_fYPos, m_fZPos);
this->ApplyIt();
m_DirectDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
m_DirectDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 14);
m_DirectDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2);
}

void Cube::SetPositions(float fX, float fY, float fZ)
{
D3DXMATRIX PositionMatrix;
D3DXMatrixTranslation(&CubeMatrix, fX, fY, fZ);
}



##### Share on other sites
Take a look at this

It should give you a clear indication of what you need to do. It seems like you want some kind of planetary system.

Example (Rotating around an axis - Planet/moon system)

Example (Rotating around an axis in place - The earth rotating)

I hope this helps.
Take care.

##### Share on other sites
cutovoi    122
Thanks Pieter, you've solved my problem.

now I can continue my project...

Thanks again