Collision Detection
In the keyboard event callback, when doing translation along the major axes in the world space using the arrow keys and + -:
check the next step camera position
Create a small AABB box centered at the camera position.
Test intersection the box with the Uniform Grid or Octree using QueryAABB
If there is an intersection, the collision is detected. The the movement is rejected.
Note: this demo does not handle collision detection when you are moving or rotating in the camera space.
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
....
// check collision detection
AABB mybox{ nextViewPos - 0.2f, nextViewPos + 0.2f };
std::vector<int> out;
bool bCollide = false;
for (std::shared_ptr<Mesh> pMesh : meshList)
{
pMesh->pSpatial->QueryAABB(mybox, out);
if (out.empty()) {
std::cout << "No collision" << std::endl;
} else {
bCollide = true;
std::cout << "Collision detected: " << out.size() << std::endl;
}
}
if (!bCollide) {
matView = nextMatView;
viewPos = nextViewPos;
}
}Last updated