does this look right?
Posted: March 15th, 2017, 2:52 pm
Hey folks, I'm doing some 3D work and I am setting up collision planes in a partitioned world. Each cell holds a list of triangles from the world, which is a giant room with vertical walls. I am using this algorithm to place triangles in the cells. It works 95% of the time but seems to miss some. I made sure that the cell size is larger than any triangle (save for the ceiling). I am using this but it seems to miss the odd one ?.. any suggestions ?
The cells are an array based on the x and z axis, as y is irrelevant in this case
The cells are an array based on the x and z axis, as y is irrelevant in this case
Code: Select all
void AddPlane(Utilities::Plane* plane)
{
// ?? hmm.. adding one plane to possible 3 cells, i think
// i have to do this in order to make sure
// we can intersect that plane, no matter how
// little of it is in the cell
// get indices of plane points
size_t index[3];
size_t cSize = (size_t)m_cellSize;
size_t row1 = (size_t)plane->pt0.z / cSize;
size_t col1 = (size_t)plane->pt0.x / cSize;
size_t row2 = (size_t)plane->pt1.z / cSize;
size_t col2 = (size_t)plane->pt1.x / cSize;
size_t row3 = (size_t)plane->pt2.z / cSize;
size_t col3 = (size_t)plane->pt2.x / cSize;
index[0] = row1 * m_columns + col1;
index[1] = row2 * m_columns + col2;
index[2] = row3 * m_columns + col3;
for (size_t i = 0; i < 3; i++)
{
auto& it = std::find_if(m_cells[index[i]].m_planes.begin(),
m_cells[index[i]].m_planes.end(), [plane](Utilities::Plane& p)
{
return plane->index == p.index;
//index set when reading in data from .obj file
});
// not in cell's plane list so add it
if(it == m_cells[index[i]].m_planes.end())
m_cells[index[i]].m_planes.push_back(*plane);
}
}