1

I am making a simple 3D Game in which I am moving in a maze. Now I want to implement collecting teapots which are randomly disturbed in the area. The random drawing of teapots is working perfectly. Now I need to make collisions. For that I would like to use AABB algorithm.

Here is how I initialize coords of teapots so they are disturbed in the maze where isn't wall:

 for(int i=0; i<5;i++) {

            x = random.nextInt(21);
            y = random.nextInt(21);

            while(wall[x][y]) {

                x = random.nextInt(21);
                y = random.nextInt(21);
            }

            if(!wall[x][y]) {
                teapot[x][y] = true;
            }   
        }

Here is how I render teapots:

gl.glLoadIdentity();
        //render konviček
        for(int i=0;i<21;i++) {
            for(int j=0; j<21; j++) {
                if(labyrinth.getDonut()[i][j]) {
                    gl.glTranslatef((float) i, 0f, (float) j);
                    wallTexture.disable(gl);
                    gl.glColor3f(1.0f, 0.0f, 0.0f);
                    glut.glutSolidTeapot(0.2);
                    gl.glTranslatef((float) -i, 0f, (float) -j);
                }
            }
        }

My question is, how to simply implement the AABB algorithm. I know I am supposed to draw a cube around those teapots, however I can't figure out how to code it.

I already have my checkCollision(double X, double Z); method prepared, so I really just need to draw the box around the teapot and get its coordinates.

  • you do not draw anything (only for debug to see if your AABB matches your teapot) the point is to compute intersection between bbox (your AABB) and what ever (player, dot, different AABB) so the first question is what are you colliding with ? Then what action you need to do (do not move, reflect, move as far as you can) your code does not have any meaning without the structures holding your sceene (and their description) btw you forget to translate your Czech comment into English :) I do the same all the time but as it is the only comment in your code I think it should be in English – Spektre Apr 12 '18 at 12:52
  • I am assuming your `wall[][]` is 2D uniform integral map/grid of your game in which case your teapots have only grid cell positions and no AABB is needed ... just compute distance of whatever (player) to teapod cell and if bigger than half or whole cell size you are colliding +/- your coordinate system and map topology tweaks. If you player is moving also only by cell grid steps than you just simply do `if (wall[player.x][player.y]) collision occur;` – Spektre Apr 12 '18 at 12:55
  • Hi @Spektre I'm also having same question. In my case it's 6 torus. But as u were suggesting if we are checking the distance between player and a torus we're not considering x, y coordinates. So if the camera moved to that distance (without considering x,y coordinates) it's always considering as a collision even there is no torus. – AbRe Jul 19 '20 at 07:41

0 Answers0