Best Minecraft Clone Box Selection Method

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.

alt text

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.

+6
c ++ opengl
source share
3 answers

Working with boxes as primitives is unnecessary in terms of memory and processing power. Cubes are great for rendering, and even there you can find more complex algorithms that give you a better final image (marching cubes). Minecraft graphs are very primitive in this sense, as voxel rendering has been around for a long time, and significant progress has been made.

Basically you should use the fact that all your boxes are equally distributed and the same size. They are called voxels. Raycasting in a grid is trivial compared to what you have - a large-scale oct-tree and a narrow phase AABB test. I suggest you work a bit on detecting collisions of voxels and voxels, and also find algorithms that are easier to implement and that will work faster.

+5
source share

You do not need an explicit octet structure in memory. All that is needed is byte [,]. Just generate 8 children in the field lazily during the search (for example, the chess engine generates the game states of the children).

0
source share

I also claim that you do not need to rely on real raycasts to determine what to do. Given that you are in a predefined grid shape, you really are not held hostage to the “exact visible” requirements. If you can track the position of your camera and highlight some NSWE compass, you can also use this to determine if the GPU buffer should even look at the vertex array for rendering.

I describe this theory in detail here fooobar.com/questions/878817 / ...

But using the positioning of Octree and Camera + the distance / frame of the camera, do you basically know what the user sees without resorting to raytrace (s) for accurate data? If you can combine your triangles into larger ones for rendering purposes, and then use a texture to break the appearance of a large back in a cubic shape (a little hand), you will significantly reduce the number of vertices. Then it’s just a matter of visualizing the case and tracking what your camera direction is and where xyz is sitting, you can leave by letting some people who should not be displayed as this will have minimal impact on performance (especially if your shaders also do part work)

I experiment further by tracking the center point of the camera to determine its horizontal focal point, and from it you can determine the angle, which in turn determines the depth of the piece, which you will probably see in the direction in which it is

0
source share

All Articles