fix dynamic grid broadphase being broken

This commit is contained in:
RandomityGuy 2024-11-05 20:25:58 +05:30
parent 0a56019a53
commit 3de0519d47
2 changed files with 35 additions and 25 deletions

View file

@ -34,6 +34,7 @@ class CollisionWorld {
public function build() { public function build() {
this.grid.build(); this.grid.build();
this.dynamicGrid.setBounds(this.grid.bounds);
this.dynamicGrid.build(); this.dynamicGrid.build();
} }

View file

@ -32,6 +32,7 @@ class GridBroadphase {
var searchKey:Int = 0; var searchKey:Int = 0;
var _built = false; var _built = false;
var hasBounds:Bool = false;
public function new() { public function new() {
// this.bounds = bounds.clone(); // this.bounds = bounds.clone();
@ -139,37 +140,45 @@ class GridBroadphase {
} }
} }
public function setBounds(bounds:Bounds) {
this.bounds = bounds.clone();
this.cellSize = new Vector(bounds.xSize / CELL_DIV.x, bounds.ySize / CELL_DIV.y);
this.hasBounds = true;
}
public function build() { public function build() {
if (_built) if (_built)
return; return;
_built = true; _built = true;
// Find the bounds // Find the bounds
var xMin = 1e8; if (!hasBounds) {
var xMax = -1e8; var xMin = 1e8;
var yMin = 1e8; var xMax = -1e8;
var yMax = -1e8; var yMin = 1e8;
var zMin = 1e8; var yMax = -1e8;
var zMax = -1e8; var zMin = 1e8;
for (i in 0...this.objects.length) { var zMax = -1e8;
if (this.objects[i] == null) for (i in 0...this.objects.length) {
continue; if (this.objects[i] == null)
var surface = this.objects[i].object; continue;
xMin = Math.min(xMin, surface.boundingBox.xMin); var surface = this.objects[i].object;
xMax = Math.max(xMax, surface.boundingBox.xMax); xMin = Math.min(xMin, surface.boundingBox.xMin);
yMin = Math.min(yMin, surface.boundingBox.yMin); xMax = Math.max(xMax, surface.boundingBox.xMax);
yMax = Math.max(yMax, surface.boundingBox.yMax); yMin = Math.min(yMin, surface.boundingBox.yMin);
zMin = Math.min(zMin, surface.boundingBox.zMin); yMax = Math.max(yMax, surface.boundingBox.yMax);
zMax = Math.max(zMax, surface.boundingBox.zMax); zMin = Math.min(zMin, surface.boundingBox.zMin);
zMax = Math.max(zMax, surface.boundingBox.zMax);
}
// Some padding
xMin -= 100;
xMax += 100;
yMin -= 100;
yMax += 100;
zMin -= 100;
zMax += 100;
this.bounds = Bounds.fromValues(xMin, yMin, zMin, xMax - xMin, yMax - yMin, zMax - zMin);
this.cellSize = new Vector(this.bounds.xSize / CELL_DIV.x, this.bounds.ySize / CELL_DIV.y);
} }
// Some padding
xMin -= 100;
xMax += 100;
yMin -= 100;
yMax += 100;
zMin -= 100;
zMax += 100;
this.bounds = Bounds.fromValues(xMin, yMin, zMin, xMax - xMin, yMax - yMin, zMax - zMin);
this.cellSize = new Vector(this.bounds.xSize / CELL_DIV.x, this.bounds.ySize / CELL_DIV.y);
// Insert the objects // Insert the objects
for (i in 0...CELL_SIZE) { for (i in 0...CELL_SIZE) {