I'm making a Minecraft clone as the very first OpenGL project and get stuck in the box selection part. What would be the best way to make a reliable selection of boxes?
I go through some AABB algorithms, but none of them explains well enough what they are doing (especially super improved), and I don’t want to use things that I don’t understand.
Since the world is made up of cubes, I used octrees to remove some of the burden of computing the rays, basically the only thing I need is this function:
float cube_intersect(Vector ray, Vector origin, Vector min, Vector max) {
Ray and origin are easy to obtain with
Vector ray, origin, point_far; double mx, my, mz; gluUnProject(viewport[2]/2, viewport[3]/2, 1.0, (double*)modelview, (double*)projection, viewport, &mx, &my, &mz); point_far = Vector(mx, my, mz); gluUnProject(viewport[2]/2, viewport[3]/2, 0.0, (double*)modelview, (double*)projection, viewport, &mx, &my, &mz); origin = Vector(mx, my, mz); ray = point_far-origin;
min and max are the opposite corners of the cube.
I'm not even sure if this is the right way to do this, given the number of cubes I would have to test, even with octaves.
I also tried gluProject , it works, but is very unreliable and does not give me the selected face of the cube.
EDIT
So here is what I did: calculate the position in space with the beam:
float t = 0; for(int i=0; i<10; i++) { Vector p = ray*t+origin; while(visible octree) { if(p inside octree) { // then call recursive function until a cube is found break; } octree = octree->next; } if(found a cube) { break; } t += .5; }
It is actually amazingly fast and stops after the first cube found.

As you can see, the ray must go through several octets before it finds the cube (in fact, position in space) - there is a cross in the middle of the screen. The lower the increment, the more accurate the selection, but also slower.