• Create Account

## rotation and translation, what's wrong?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1cozzie  Members

4627
Like
0Likes
Like

Posted 09 March 2013 - 06:21 AM

Hi,

Just can't figure out why I can't get my matrix rotation/ translation correct.

I try to achieve the following:

- for a mesh I have 'x' vertices

- they are in model/ local space

- calculate the bounding box min and max vertices (in model space)

- transform the min and max vertices into worldspace, by using the rotation and translation matrices

I can't get the expected result unless I:

- rotate/scale the vertices in model space

- then calculate the bounding box min and max vertices (model space, but rotated/scaled)

- transform the min and max vertices using only the translation matrix (world position)

Although it works the 1st way above, this makes me have to go through all vertices when an object is rotated/ scaled/ moved, and I need to update the min and max vertices. This I want to prevent.

Pseudo "updatebox" code:

transformcoordinate(min vertex, world matrix);

transformcoordinate(max vertex, world matrix);

Here is the code of both the OK and the not OK situation.

OK:

{
TVERTEX *verticesPointer;
TVERTEX *vertices = new TVERTEX[pMesh->mMesh->GetNumVertices()];

memcpy(vertices, verticesPointer, pMesh->mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0));
pMesh->mVtxBuffer->Unlock();

// SCALE AND ROTATE ALL MESH VERTICES
D3DXMatrixScaling(&mMatScale, mScale, mScale, mScale);
mMatWorld = (D3DXMATRIXA16)(mMatScale*mMatRotateX*mMatRotateY*mMatRotateZ);

for(DWORD vc=0;vc<pMesh->mMesh->GetNumVertices();++vc)
D3DXVec3TransformCoord(&vertices[vc].position, &vertices[vc].position, &mMatWorld);

// VERTICES ARE STILL IN LOCAL SPACE (MODEL), BUT SCALED AND ROTATED

// bounding radius and AABB for full mesh
D3DXComputeBoundingSphere(&vertices[0].position, pMesh->mMesh->GetNumVertices(), D3DXGetDeclVertexSize(vtxdecl, 0), &pMesh->mMeshCenter, &mBoundingRadius);
CalcBoundingBox(vertices, 0, pMesh->mMesh->GetNumVertices(), &mBoundingBox);

// NOTE: MWORLDPOS IS NOW NOT RELATED TO mMESHCENTER
delete[] vertices;

// CREATE worldspace matrix and transform bounding volumes to worldspace (AABB, sphere)
CreateWorldMatrix();

D3DXVec3TransformCoord(&mBoundingBox.min, &mBoundingBox.min, &mMatTranslate);
D3DXVec3TransformCoord(&mBoundingBox.max, &mBoundingBox.max, &mMatTranslate);

return true;
}

Not OK (and goal):

{
TVERTEX *verticesPointer;
TVERTEX *vertices = new TVERTEX[pMesh->mMesh->GetNumVertices()];

memcpy(vertices, verticesPointer, pMesh->mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0));
pMesh->mVtxBuffer->Unlock();

// VERTICES ARE IN LOCAL SPACE (MODEL), NOT SCALED AND ROTATED

// bounding radius and AABB for full mesh
D3DXComputeBoundingSphere(&vertices[0].position, pMesh->mMesh->GetNumVertices(), D3DXGetDeclVertexSize(vtxdecl, 0), &pMesh->mMeshCenter, &mBoundingRadius);
CalcBoundingBox(vertices, 0, pMesh->mMesh->GetNumVertices(), &mBoundingBox);

// NOTE: MWORLDPOS IS NOW NOT RELATED TO mMESHCENTER

// CREATE worldspace matrix and transform bounding volumes to worldspace (AABB, sphere)
CreateWorldMatrix();

D3DXVec3TransformCoord(&mBoundingBox.min, &mBoundingBox.min, &mMatWorld);
D3DXVec3TransformCoord(&mBoundingBox.max, &mBoundingBox.max, &mMatWorld);

// create worldmatrix does:
// D3DXMatrixTranslation(&mMatTranslate, mWorldPos.x, mWorldPos.y, mWorldPos.z);
// D3DXMatrixScaling(&mMatScale, mScale, mScale, mScale);
// mMatWorld = (D3DXMATRIXA16)(mMatScale*mMatRotateX*mMatRotateY*mMatRotateZ*mMatTranslate);

return true;
}

Really like to hear your thoughts.

Edited by cozzie, 09 March 2013 - 06:26 AM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #2Zaoshi Kaba  Members

7790
Like
0Likes
Like

Posted 09 March 2013 - 06:53 AM

1. Find min/max (8 vertices)

2. Transform min/max (8 vertices)

3. Find min/max of transformed min/max

However this min/max won't match your current method, but it'll definitely fit whole model inside.

### #3cozzie  Members

4627
Like
0Likes
Like

Posted 09 March 2013 - 07:37 AM

Hi zaoshi.
Thanks for the quick answer. I now use only 2 points for an AABB with min/max over all axes.
(I reuse those when culling against the frustum, using them for creating a p- and -n vertex)

I'm not sure if I can just find min/max of only the 2 vertices (step 3).
Unless I do it per axis, where the new min should have lowest x, y and z of both vertices (and for max , the max).
If this wont work I might have to just store all 8 points and doing with all 8, finding min max afterwards.

Update; got it working 95%, if I re-arrange X, Y, Z on the min and max extents of the AABB, culling gets almost as it should.
Only thing left is something strange with rotations. If I rotate a mesh with i.e. 90, 180, 270 degrees culling is all good, if I use practically any other number, the mesh is culled to early at the corners of the frustum/screen (like min/max of AABB are not correct anymore).
I'll try to find why that is, thanks for the help again.

Here's the result for now:

{
TVERTEX *verticesPointer;
TVERTEX *vertices = new TVERTEX[pMesh->mMesh->GetNumVertices()];

memcpy(vertices, verticesPointer, pMesh->mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0));
pMesh->mVtxBuffer->Unlock();
// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED

// CREATE BOUNDING VOLUMES (SPHERE & AABB) FOR FULL MESH, MODEL SPACE
D3DXComputeBoundingSphere(&vertices[0].position, pMesh->mMesh->GetNumVertices(), D3DXGetDeclVertexSize(vtxdecl, 0), &pMesh->mMeshCenter, &mBoundingRadius);
CalcBoundingBox(vertices, 0, pMesh->mMesh->GetNumVertices(), &mBoundingBox);
// NOTE: MWORLDPOS IS NOW NOT RELATED TO mMESHCENTER ?!?!

delete[] vertices;

// CREATE WORLD MATRIX AND TRANFORM BOUNDING VOLUMES TO WORLDSPACE (SPHERE, AABB)
CreateWorldMatrix();

D3DXVec3TransformCoord(&mBoundingBox.min, &mBoundingBox.min, &mMatWorld);
D3DXVec3TransformCoord(&mBoundingBox.max, &mBoundingBox.max, &mMatWorld);
D3DXV3Ascending(&mBoundingBox.min, &mBoundingBox.max);
return true;
}

void D3DXV3Ascending(D3DXVECTOR3 *pVec1, D3DXVECTOR3 *pVec2)
{
D3DXVECTOR3 temp;

if(pVec1->x > pVec2->x)
{
temp.x		= pVec2->x;
pVec2->x	= pVec1->x;
pVec1->x	= temp.x;
}
if(pVec1->y > pVec2->y)
{
temp.y		= pVec2->y;
pVec2->y	= pVec1->y;
pVec1->y	= temp.y;
}
if(pVec1->z > pVec2->z)
{
temp.z		= pVec2->z;
pVec2->z	= pVec1->z;
pVec1->z	= temp.z;
}
}

Edited by cozzie, 09 March 2013 - 08:45 AM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #4Zaoshi Kaba  Members

7790
Like
0Likes
Like

Posted 09 March 2013 - 09:22 AM

Expand your min/max into 8 vertices, transform them and calculate AABB for them. With just 2 points and 45 degree rotation you can force them into single line (0 volume AABB).

### #5cozzie  Members

4627
Like
0Likes
Like

Posted 09 March 2013 - 06:16 PM

So little lines of comment and so much result

Thanks, objects are now not culled to early anymore, I did what you said and it works.

Left to do: determine normal per plane (+/-, +/-, +/-) once per frame, instead of for each AABB check (to find P and N vertex).

Only thing that frustrates me a bit is the accuracy when I have rotated meshes with a different degree rotation then 90/180/270.

Culling happens, but when the mesh is way out of the frustum. Maybe still something weird with the rotations or just an acceptable margin.

Here's the code, might you see something strange left:

{
TVERTEX *verticesPointer;
TVERTEX *vertices = new TVERTEX[pMesh->mMesh->GetNumVertices()];

memcpy(vertices, verticesPointer, pMesh->mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0));
pMesh->mVtxBuffer->Unlock();
// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED

// CREATE BOUNDING VOLUMES (SPHERE & AABB) FOR FULL MESH, MODEL SPACE
D3DXComputeBoundingSphere(&vertices[0].position, pMesh->mMesh->GetNumVertices(), D3DXGetDeclVertexSize(vtxdecl, 0), &pMesh->mMeshCenter, &mBoundingRadius);
CreateAABB(vertices, 0, pMesh->mMesh->GetNumVertices(), &mBoundingBox);
// NOTE: MWORLDPOS IS NOW NOT RELATED TO mMESHCENTER ?!?!

// CREATE WORLD MATRIX AND TRANFORM BOUNDING VOLUMES TO WORLDSPACE (SPHERE, AABB)
CreateWorldMatrix();

for(int i=0;i<8;++i) D3DXVec3TransformCoord(&mBoundingBox.boxArray[i], &mBoundingBox.boxArray[i], &mMatWorld);
if(!UpdateAABB(&mBoundingBox)) return false;

return true;
}

bool CD3dmeshInst::UpdateBVWorldSpace()
{
// WORLD MATRIX should be already updated

if(!mDynamic) return false;

// FULL mesh

if(mIsMoved || mIsRotated || mIsScaled)
{
D3DXVec3TransformCoord(&mBoundingBox.min, &mBoundingBox.min, &mMatWorld);
D3DXVec3TransformCoord(&mBoundingBox.max, &mBoundingBox.max, &mMatWorld);
if(!UpdateAABB(&mBoundingBox)) return false;
}
return true;
}

bool UpdateAABB(BOUNDINGBOX *pBox)
{
if(!pBox->created) return false;

//	8 AABB corners need to be transformed by the latest world matrix
//	now find the 2 extents

pBox->min.x = pBox->boxArray[0].x;
pBox->min.y = pBox->boxArray[0].y;
pBox->min.z = pBox->boxArray[0].z;

pBox->max.x = pBox->boxArray[0].x;
pBox->max.y = pBox->boxArray[0].y;
pBox->max.z = pBox->boxArray[0].z;

for(int vc=0;vc<8;++vc)
{
if(pBox->boxArray[vc].x < pBox->min.x) pBox->min.x = pBox->boxArray[vc].x;
if(pBox->boxArray[vc].x > pBox->max.x) pBox->max.x = pBox->boxArray[vc].x;

if(pBox->boxArray[vc].y < pBox->min.y) pBox->min.y = pBox->boxArray[vc].y;
if(pBox->boxArray[vc].y > pBox->max.y) pBox->max.y = pBox->boxArray[vc].y;

if(pBox->boxArray[vc].z < pBox->min.z) pBox->min.z = pBox->boxArray[vc].z;
if(pBox->boxArray[vc].z > pBox->max.z) pBox->max.z = pBox->boxArray[vc].z;
}
return true;
}

void CreateAABB(TVERTEX *pVtxArray, DWORD pStart, DWORD pNr, BOUNDINGBOX *pBox)
{
// MIN and MAX vertices
pBox->min.x = pVtxArray[pStart].position.x;
pBox->min.y = pVtxArray[pStart].position.y;
pBox->min.z = pVtxArray[pStart].position.z;

pBox->max.x = pVtxArray[pStart].position.x;
pBox->max.y = pVtxArray[pStart].position.y;
pBox->max.z = pVtxArray[pStart].position.z;

for(DWORD vc=pStart;vc<pStart+pNr;++vc)
{
if(pVtxArray[vc].position.x < pBox->min.x) pBox->min.x = pVtxArray[vc].position.x;
if(pVtxArray[vc].position.x > pBox->max.x) pBox->max.x = pVtxArray[vc].position.x;

if(pVtxArray[vc].position.y < pBox->min.y) pBox->min.y = pVtxArray[vc].position.y;
if(pVtxArray[vc].position.y > pBox->max.y) pBox->max.y = pVtxArray[vc].position.y;

if(pVtxArray[vc].position.z < pBox->min.z) pBox->min.z = pVtxArray[vc].position.z;
if(pVtxArray[vc].position.z > pBox->max.z) pBox->max.z = pVtxArray[vc].position.z;
}

// FULL 8 vertices of the AABB
pBox->boxArray[0].x = pBox->min.x;
pBox->boxArray[0].y = pBox->min.y;
pBox->boxArray[0].z = pBox->min.z;

pBox->boxArray[1].x = pBox->max.x;
pBox->boxArray[1].y = pBox->max.y;
pBox->boxArray[1].z = pBox->max.z;

pBox->boxArray[2].x = pBox->max.x;
pBox->boxArray[2].y = pBox->max.y;
pBox->boxArray[2].z = pBox->min.z;

pBox->boxArray[3].x = pBox->min.x;
pBox->boxArray[3].y = pBox->max.y;
pBox->boxArray[3].z = pBox->min.z;

pBox->boxArray[4].x = pBox->min.x;
pBox->boxArray[4].y = pBox->min.y;
pBox->boxArray[4].z = pBox->max.z;

pBox->boxArray[5].x = pBox->max.x;
pBox->boxArray[5].y = pBox->min.y;
pBox->boxArray[5].z = pBox->max.z;

pBox->boxArray[6].x = pBox->max.x;
pBox->boxArray[6].y = pBox->min.y;
pBox->boxArray[6].z = pBox->min.z;

pBox->boxArray[7].x = pBox->min.x;
pBox->boxArray[7].y = pBox->max.y;
pBox->boxArray[7].z = pBox->max.z;

pBox->created = true;
}

By the way, the accuracy when rotating meshes with other angles then 90/180/270 was also the case when I checked al 8 points of the boundingbox.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.