# checking adjacent cubes

This topic is 2113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wasnt sure where to put this question. It's more of a design question than a code problem.

I have a list of cubes. The cubes are structs, with a World matrix that stores their positionand a bool isSolid that checks if it's solid it not.
My goal here is to nit draw any cubes where a six sides are adjacent by other cubes. I'm not worried about faces, just the cubes as a whole. If all cubes around the cube are solid,that cube isn't drawn.

How would i loop through my list of cubes to check if all the cubes around it are solid? This would require 6 if statements im assuming? Once i figure out which cubes should be solid and which aren't, i can store those cubes in another buffer and draw that buffer

##### Share on other sites

Each cube could store 6 pointers, each pointer points to the nearest cube in 6 directions.

Search for cubes that are aligned with the new cube and update pointers for your new cube and the 6 other cubes.

Edited by Tispe

##### Share on other sites
Could you give some pseudocode for that? The idea makes sense, but how exactly am i checking the pointers against another cube?

##### Share on other sites

Why not just research Occlusion Culling, and implement that?

##### Share on other sites
I would rather just cull whole cubes first, and if i need to ill cull individual faces. Since i use hardware instancing, i don't think it will make too much of a difference. Ill be culling chunks too when i get to that point

##### Share on other sites

You can brute search. I assume you have a huge container that manages all the cubes. If you spawn a new cube, traverse the "main" container and check every cube in relation to the new cube for all 6 directions.

Something like this:

Cube* pNewCube = new Cube(initstuff);

for(auto it = MyCubesContainer.begin(); it!=MyCubesContainer.end();it++){

//Check if new cube and *it are on the same YZ plane
if((*it)->Pos.y == pNewCube->Pos.y && (*it)->Pos.z == pNewCube->Pos.z){

//Check if *it are closer to pNewCube top face then the current one
if((*it)->Pos.x < pNewCube->pTop &&  (*it)->Pos.x > pNewCube->Pos.x){
pNewCube->pTop = &(*it);					//*it is closer to the Top face then the current cube

//also update *it's bottom to point to pNewCube

}

//Check if *it are closer to pNewCube bottom face then the current one
if((*it)->Pos.x < pNewCube->pBottom &&  (*it)->Pos.x > pNewCube->Pos.x){
pNewCube->pBottom = &(*it);					//*it is closer to the bottom face then the current cube

//also update *it's top to point to pNewCube
}

}

//Check for all planes
}

MyCubesContainer.push_back(pNewCube);


##### Share on other sites
That seems pretty simple to do in c#. would it make sense to copy the cubes to a new list and draw the new list after I've checked all thecubes?

##### Share on other sites
Turns out that you can get the six sides from the world matrix. I can set those sides based on location of the cube, check adjacency and voila I'm done

• 21
• 12
• 9
• 17
• 13