Sign in to follow this  
xynapse

AABB position

Recommended Posts

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




[code]

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;


[/code]

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:

[code]

// 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();

[/code]




Then i iterate and look for intersections like this:

[code]// 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);
}
}


[/code]






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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Hidden
Sorry, here are the details:



[img]http://extraordinaire.pl/gd/approach7.jpg[/img]




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

[code]<0,0> <32,32>

<0,32> <32,64>

<32,0> <64,32>

<32,32> <64,64>

[/code]


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





[code]



// 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;


[/code]




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

[code]

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>


[/code]




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





[code]

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();

}

[/code]

[img]http://extraordinaire.pl/gd/approach8.jpg[/img]










BoundingBox Class ( dhpoware )

[code]

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();
}

[/code]




Share this post


Link to post
Hidden
Is this correct that for an AABB




[code]minimal value = (0,0,0)

maximal value = (32,32,32)

[/code]




Dimensions are:




[code]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: [b]Size(55.425625)[/b][b] Radius(27.712812)[/b] Center(16.000000,16.000000,16.000000)
[/code]




With

[code]

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();
}

[/code]

Share this post


Link to post

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