From 44c15ab01110f94445ad6001e7d7bf6575a7defb Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Sat, 28 Mar 2026 14:33:04 +0000 Subject: [PATCH] more array reuse --- src/Marble.hx | 10 ++++++---- src/collision/CollisionEntity.hx | 24 ++++++++++++++---------- src/collision/CollisionWorld.hx | 2 +- src/collision/Grid.hx | 6 +----- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Marble.hx b/src/Marble.hx index 5873771a..aa3a3960 100644 --- a/src/Marble.hx +++ b/src/Marble.hx @@ -262,6 +262,7 @@ class Marble extends GameObject { public var bestContact:CollisionInfo; static var contactScratch:Array = []; + static var surfaceScratch:Array = []; var queuedContacts:Array = []; 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)); diff --git a/src/collision/CollisionEntity.hx b/src/collision/CollisionEntity.hx index 40dc552b..aad9f494 100644 --- a/src/collision/CollisionEntity.hx +++ b/src/collision/CollisionEntity.hx @@ -23,8 +23,7 @@ class CollisionEntity implements IOctreeObject implements IBVHObject { public var octree:Octree; - public var bvh:BVHTree; - + // public var bvh:BVHTree; var grid:Grid; public var surfaces:Array; @@ -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 = []; + public function sphereIntersection(collisionEntity:SphereCollisionEntity, timeState:TimeState, contacts:Array) { 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(); diff --git a/src/collision/CollisionWorld.hx b/src/collision/CollisionWorld.hx index 9f07adb0..da1a97f9 100644 --- a/src/collision/CollisionWorld.hx +++ b/src/collision/CollisionWorld.hx @@ -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); diff --git a/src/collision/Grid.hx b/src/collision/Grid.hx index 6c7b077e..40bf455d 100644 --- a/src/collision/Grid.hx +++ b/src/collision/Grid.hx @@ -75,7 +75,7 @@ class Grid { } // searchbox should be in LOCAL coordinates - public function boundingSearch(searchbox:Bounds) { + public function boundingSearch(searchbox:Bounds, foundSurfaces:Array) { 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) {