- Home /
Create and divide a bounding box into cells
I'm implementing fire propagation described in this blog post. http://jflevesque.com/2012/12/06/far-cry-how-the-fire-burns-and-spreads/
I want to create a 3D grid that surrounds an object, like this http://jflevesque.com/wp-content/uploads/2012/12/3DGrid.jpg.
Extract from the blog post:
First, we create an AABB that entirely surrounds the object. Second, we somehow need to detect the shape of that object. After all, it could be a chair, an oil barrel or a whole house for all we know. To accomplish that, the bounding box is divided in equally spaced cubes. An iterative algorithm then go through all those cubes and test their location against the collision shape of the object. If the test return a positive result, this cube is kept, otherwise it is discarded. At the end, we have a collection of cubes that approximately represent the shape of the object to be burned.
How can I accomplish this?
What I have this far:
public class Flammable : MonoBehaviour {
private List<FireCell> fireGrid = new List<FireCell>();
public FireCell fireCell;
public static float cellSize = 0.1f;
void Start() {
Bounds meshBounds = GetComponent<MeshCollider>().bounds;
Vector3 min = meshBounds.min;
Vector3 max = meshBounds.max;
for (float x = min.x; x <= max.x; x = x + cellSize) {
for (float y = min.y; y <= max.y; y = y + cellSize) {
for (float z = min.z; z <= max.z; z = z + cellSize) {
Vector3 pos = new Vector3(x, y, z);
if (meshBounds.Contains(pos)) {
FireCell cell = (FireCell) Instantiate(fireCell, pos, Quaternion.identity);
fireGrid.Add(cell);
}
}
}
}
}
void Update() {
}
}
public class FireCell : MonoBehaviour {
void Start() {
transform.localScale = new Vector3(Flammable.cellSize, Flammable.cellSize, Flammable.cellSize);
}
void Update() {
}
void OnDrawGizmos() {
Gizmos.DrawWireCube(transform.position, new Vector3(Flammable.cellSize, Flammable.cellSize, Flammable.cellSize));
}
}
This creates a 3D grid between the min and max points of the MeshCollider. However, meshBounds.Contains() seems to return true, even for cells that are outside of the object. As such, all cells are added to the list.
FireCell is a prefab with a Transform and script containing different fire properties. I'm drawing the cell using Gizmos.
I hope there is a better way of solving this.
Thanks.
That all looks correct. Do you have an example of where meshBounds.Contains() returns true where you don't expect it to? In the pasted code, I would expect it to always return true because you're iterating through the limits of the bounding box.
Your answer
Follow this Question
Related Questions
Having trouble with grid movement 1 Answer
How do I move a 3D player on a tile based grid smoothly? 0 Answers
How to move character in a grid-based area? 1 Answer
3D level generator on a grid C# 1 Answer
Unity Probuilder mesh brush and grids. 0 Answers