more array reuse

This commit is contained in:
RandomityGuy 2026-03-28 14:33:04 +00:00
parent 8d35663f3d
commit 44c15ab011
4 changed files with 22 additions and 20 deletions

View file

@ -262,6 +262,7 @@ class Marble extends GameObject {
public var bestContact:CollisionInfo;
static var contactScratch:Array<CollisionEntity> = [];
static var surfaceScratch:Array<CollisionSurface> = [];
var queuedContacts:Array<CollisionInfo> = [];
var appliedImpulses:Array<{impulse:Vector, contactImpulse:Bool}> = [];
@ -1320,7 +1321,7 @@ class Marble extends GameObject {
// var iterationFound = false;
for (obj in foundObjs) {
// Its an MP so bruh
if (obj.go != null && !obj.go.isCollideable)
if (obj.go == this || (obj.go != null && !obj.go.isCollideable))
continue;
var isDts = obj.go is DtsObject;
@ -1348,11 +1349,12 @@ class Marble extends GameObject {
Math.max(Math.max(sphereRadius.x, sphereRadius.y), sphereRadius.z) * 2);
var currentFinalPos = position.add(relVel.multiply(finalT)); // localpos.add(relLocalVel.multiply(finalT));
var surfaces = @:privateAccess obj.grid != null ? @:privateAccess obj.grid.boundingSearch(boundThing) : (obj.bvh == null ? obj.octree.boundingSearch(boundThing)
.map(x -> cast x) : obj.bvh.boundingSearch(boundThing));
surfaceScratch.resize(0);
@:privateAccess obj.grid.boundingSearch(boundThing, surfaceScratch);
var surfaces = surfaceScratch;
for (surf in surfaces) {
var surface:CollisionSurface = cast surf;
var surface:CollisionSurface = surf;
currentFinalPos = position.add(relVel.multiply(finalT));

View file

@ -23,8 +23,7 @@ class CollisionEntity implements IOctreeObject implements IBVHObject {
public var octree:Octree;
public var bvh:BVHTree<CollisionSurface>;
// public var bvh:BVHTree<CollisionSurface>;
var grid:Grid;
public var surfaces:Array<CollisionSurface>;
@ -67,12 +66,12 @@ class CollisionEntity implements IOctreeObject implements IBVHObject {
// Generates the bvh
public function finalize() {
this.generateBoundingBox();
#if hl
this.bvh = new BVHTree();
for (surface in this.surfaces) {
this.bvh.add(surface);
}
#end
// #if hl
// this.bvh = new BVHTree();
// for (surface in this.surfaces) {
// this.bvh.add(surface);
// }
// #end
var bbox = new Bounds();
for (surface in this.surfaces)
bbox.add(surface.boundingBox);
@ -90,7 +89,8 @@ class CollisionEntity implements IOctreeObject implements IBVHObject {
}
go = null;
surfaces = null;
bvh = null;
grid = null;
// bvh = null;
octree = null;
}
@ -200,6 +200,8 @@ class CollisionEntity implements IOctreeObject implements IBVHObject {
this.priority = priority;
}
static var surfaceSearchPool:Array<CollisionSurface> = [];
public function sphereIntersection(collisionEntity:SphereCollisionEntity, timeState:TimeState, contacts:Array<CollisionInfo>) {
var position = collisionEntity.transform.getPosition();
var radius = collisionEntity.radius + 0.001;
@ -215,7 +217,9 @@ class CollisionEntity implements IOctreeObject implements IBVHObject {
var invScale = invMatrix.getScale();
var sphereRadius = new Vector(radius * invScale.x, radius * invScale.y, radius * invScale.z);
sphereBounds.addSpherePos(localPos.x, localPos.y, localPos.z, Math.max(Math.max(sphereRadius.x, sphereRadius.y), sphereRadius.z) * 1.1);
var surfaces = grid.boundingSearch(sphereBounds); // bvh == null ? octree.boundingSearch(sphereBounds).map(x -> cast x) : bvh.boundingSearch(sphereBounds);
surfaceSearchPool.resize(0);
grid.boundingSearch(sphereBounds, surfaceSearchPool);
var surfaces = surfaceSearchPool;
var invtform = invMatrix.clone();
invtform.transpose();

View file

@ -81,7 +81,7 @@ class CollisionWorld {
+ rayDirection.x * rayLength, rayStart.y
+ rayDirection.y * rayLength, rayStart.z
+ rayDirection.z * rayLength);
this.intersectionList.splice(0, this.intersectionList.length);
this.intersectionList.resize(0);
this.grid.boundingSearch(bounds, this.intersectionList);
dynamicGrid.boundingSearch(bounds, this.intersectionList);

View file

@ -75,7 +75,7 @@ class Grid {
}
// searchbox should be in LOCAL coordinates
public function boundingSearch(searchbox:Bounds) {
public function boundingSearch(searchbox:Bounds, foundSurfaces:Array<CollisionSurface>) {
var queryMinX = Math.max(searchbox.xMin, bounds.xMin);
var queryMinY = Math.max(searchbox.yMin, bounds.yMin);
var queryMaxX = Math.min(searchbox.xMax, bounds.xMax);
@ -94,8 +94,6 @@ class Grid {
if (yEnd > CELL_SIZE)
yEnd = CELL_SIZE;
var foundSurfaces = [];
searchKey++;
// Insert the surface references from [xStart, yStart, zStart] to [xEnd, yEnd, zEnd] into the map
@ -113,8 +111,6 @@ class Grid {
}
}
}
return foundSurfaces;
}
function elegantPair(x:Int, y:Int) {