AABB position

Started by
2 comments, last by xynapse 12 years, 6 months ago
Guys,

I am now building HitTest method that as input takes in the vNear and vFar vector obtained via Screen to World space for mouse position on the terrain.



As my terrain is divided into Cells ( each of size 32 ) - i need to build AABB covering each cell so when i click LMB, my Ray checks for intersections for those cells ( checks for intersections with AABB for each visible cell ).




I generate the AABB for each cell like this






for(iCellX)

for(iCellZ)

// Generate AABB for this cell
CVector3 vAABMin = CVector3(0,0,0);
CVector3 vAABMax = CVector3(m_iCellSize,iCellSize,m_iCellSize);

CVector3 center = vAABMin + ( vAABMax - vAABMin ) * 0.5f;
CVector3 offset = CVector3(iCellX * m_iCellSize,0,iCellZ * m_iCellSize) + center;

newCell.m_AABB.min=vAABMin + offset;
newCell.m_AABB.max=vAABMax + offset;




Now when a LMB is clicked, i do a Screen to World space by passing mouse coordinates ( works great ) and need to cast a ray

to find with which AABB it intersects.




I do a Ray from zNear to zFar like this:



// vNear and vFar are screen space to world space projected vectors - mouse position in world space saying simpler.

// Create a direction
CVector3 vDirection = ( vNear + vFar ) * 0.5;
vDirection.normalize();






Then i iterate and look for intersections like this:

// Create a ray from zNear to zFar
CRay RayIntersect(vNear,vDirection);

// We are checking for intersection with visible cells
std::vector<tTerrainCell*>::iterator it;
for(it = m_vVisibleCells.begin(); it != m_vVisibleCells.end(); ++it)
{
if(RayIntersect.hasIntersected(it[0]->m_AABB)==true)
{
return CVector2(it[0]->m_iCellX,it[0]->m_iCellZ);
}
}









Of course the results are wrong and it seems like my AABB positions are wrong and i can't figure out why..

So here comes the question:


Should i multiply my AABB with Model matrix or this i am missing the spot somewhere?




As my terrain cells are not moving ( static mesh ) and won't rotate - i don't think i should multiply anything here, but i might be wrong .. can i ask for an advise please?
perfection.is.the.key
Advertisement
*bump* oh, please - nobody ?
perfection.is.the.key
Why don’t you add code to draw your AABB’s and confirm they are wrong? Or at least print some coordinates on a single object and check the math logically.
Because an AABB is very hard to get wrong, and so simple to fix if it is.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Sorry, here are the details:



approach7.jpg




Here are 4 first cells that are starting and ending as follows:

<0,0> <32,32>

<0,32> <32,64>

<32,0> <64,32>

<32,32> <64,64>




I build the AABB for each one of them as per below:









// Generate AABB for this cell

// m_iCellSize = 32


// iCellX cell number in X ranging from 0 to m_iMapSize / m_iCellSize

// iCellZ cell number in Z ranging from 0 to m_iMapSize / m_iCellSize




CVector3 vAABMin = CVector3(0,0,0);
CVector3 vAABMax = CVector3(m_iCellSize / 2,0,m_iCellSize / 2);

CVector3 center = CVector3(m_iCellSize / 2,0,m_iCellSize / 2);

// offset to move the aabb position into the middle of the cell


CVector3 offset = CVector3(iCellX * m_iCellSize,0,iCellZ * m_iCellSize) + center ;

newCell.m_AABB.min=vAABMin + offset;
newCell.m_AABB.max=vAABMax + offset;







So first four as seen, would have AABB's as here:



Terrain debug: Cell[0][0] AAB: min<16.000000,0.000000,16.000000> max<32.000000,0.000000,32.000000>


Terrain debug: Cell[0][1] AAB: min<16.000000,0.000000,48.000000> max<32.000000,0.000000,64.000000>


Terrain debug: Cell[1][0] AAB: min<48.000000,0.000000,16.000000> max<64.000000,0.000000,32.000000>


Terrain debug: Cell[1][1] AAB: min<48.000000,0.000000,48.000000> max<64.000000,0.000000,64.000000>







Now the AABB debug rendering part - i do it in old style for debug purposes, so no shader is involved to render the wirecube boxes







for(it = m_vVisibleCells.begin(); it != m_vVisibleCells.end(); ++it)
{

// Draw AABB

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(&m_mat4ProjectionMatrix[0][0]);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(&m_mat4ViewMatrix[0][0]);

CMatrix4x4 mModel;
mModel.translate(it[0]->m_AABB.min.x,it[0]->m_AABB.min.y,it[0]->m_AABB.min.z);

glMultMatrixf(&mModel[0][0]);

glPushMatrix();
glutWireCube(it[0]->m_AABB.getSize());
glPopMatrix();

}



approach8.jpg










BoundingBox Class ( dhpoware )



class CBoundingBox
{
public:
CVector3 min;
CVector3 max;

CBoundingBox();
CBoundingBox(const CVector3 &min_, const CVector3 &max_);
~CBoundingBox();

CVector3 getCenter() const;
float getRadius() const;
float getSize() const;
};


CBoundingBox::CBoundingBox()
{
min.set(0.0f, 0.0f, 0.0f);
max.set(0.0f, 0.0f, 0.0f);
}

CBoundingBox::CBoundingBox(const CVector3 &min_, const CVector3 &max_)
{
min = min_;
max = max_;
}

CBoundingBox::~CBoundingBox()
{
}

CVector3 CBoundingBox::getCenter() const
{
return (min + max) * 0.5f;
}

float CBoundingBox::getRadius() const
{
return getSize() * 0.5f;
}

float CBoundingBox::getSize() const
{
return (max - min).magnitude();
}






perfection.is.the.key
Is this correct that for an AABB




minimal value = (0,0,0)

maximal value = (32,32,32)






Dimensions are:




Terrain debug: Cell[0][0] AAB: min<0.000000,0.000000,0.000000> max<32.000000,32.000000,32.000000>
Terrain debug: Cell[0][0] AAB: Size(55.425625) Radius(27.712812) Center(16.000000,16.000000,16.000000)





With



CVector3 CBoundingBox::getCenter() const
{
return (min + max) * 0.5f;
}

float CBoundingBox::getRadius() const
{
return getSize() * 0.5f;
}

float CBoundingBox::getSize() const
{
return (max - min).magnitude();
}

perfection.is.the.key
Ok got it solved. Thanks.
perfection.is.the.key

This topic is closed to new replies.

Advertisement