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?