• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

rotation and translation, what's wrong?

4 posts in this topic

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:

 

bool CD3dmeshInst::CalcRadiusAndWorldPos(CD3dmesh *pMesh)
{
	TVERTEX *verticesPointer;
	TVERTEX *vertices = new TVERTEX[pMesh->mMesh->GetNumVertices()];

	pMesh->mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0); //D3DLOCK_READONLY);
	memcpy(vertices, verticesPointer, pMesh->mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0)); 
	pMesh->mVtxBuffer->Unlock();

	// SCALE AND ROTATE ALL MESH VERTICES
	D3DXMatrixScaling(&mMatScale, mScale, mScale, mScale);
	D3DXMatrixRotationX(&mMatRotateX, D3DXToRadian(mRot.x));
	D3DXMatrixRotationY(&mMatRotateY, D3DXToRadian(mRot.y));
	D3DXMatrixRotationZ(&mMatRotateZ, D3DXToRadian(mRot.z));
	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):

 

bool CD3dmeshInst::CalcRadiusAndWorldPos(CD3dmesh *pMesh)
{
	TVERTEX *verticesPointer;
	TVERTEX *vertices = new TVERTEX[pMesh->mMesh->GetNumVertices()];

	pMesh->mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0); //D3DLOCK_READONLY);
	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);
	// D3DXMatrixRotationX(&mMatRotateX, D3DXToRadian(mRot.x));
	// D3DXMatrixRotationY(&mMatRotateY, D3DXToRadian(mRot.y));
	// D3DXMatrixRotationZ(&mMatRotateZ, D3DXToRadian(mRot.z));
	// mMatWorld = (D3DXMATRIXA16)(mMatScale*mMatRotateX*mMatRotateY*mMatRotateZ*mMatTranslate);

	return true;
}

 

Really like to hear your thoughts.

Edited by cozzie
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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:

bool CD3dmeshInst::CalcRadiusAndWorldPos(CD3dmesh *pMesh)
{
	TVERTEX *verticesPointer;
	TVERTEX *vertices = new TVERTEX[pMesh->mMesh->GetNumVertices()];

	pMesh->mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0); //D3DLOCK_READONLY);
	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(); 

	mBoundingRadius *= mScale;

	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
0

Share this post


Link to post
Share on other sites

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).

0

Share this post


Link to post
Share on other sites

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:

 

bool CD3dmeshInst::CalcRadiusAndWorldPos(CD3dmesh *pMesh)
{
	TVERTEX *verticesPointer;
	TVERTEX *vertices = new TVERTEX[pMesh->mMesh->GetNumVertices()];

	pMesh->mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0); //D3DLOCK_READONLY);
	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(); 

	mBoundingRadius *= mScale;

	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
	mBoundingRadius *= mScale;

	if(mIsMoved || mIsRotated || mIsScaled)
	{
		mBoundingRadius *= mScale;
		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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0