@@ -251,7 +251,8 @@ public B2DynamicTree(float aabbExtendSize = 1f / 32, int capacity = 256, Func<in
251251 capacity = Math . Max ( MinimumCapacity , capacity ) ;
252252
253253 _root = Proxy . Free ;
254- _nodes = new Node [ capacity ] ;
254+ // Maximum node count for a full binary tree is 2 * leafCount - 1.
255+ _nodes = new Node [ 2 * capacity - 1 ] ;
255256
256257 // Build a linked list for the free list.
257258 ref var node = ref _nodes [ 0 ] ;
@@ -294,6 +295,10 @@ private ref Node AllocateNode(out Proxy proxy)
294295 _freeList = allocNode . Next ;
295296 Assert ( _freeList == - 1 || _nodes [ _freeList ] . IsFree ) ;
296297 allocNode = default ;
298+ allocNode . Parent = Proxy . Free ;
299+ allocNode . Next = Proxy . Free ;
300+ allocNode . Child1 = Proxy . Free ;
301+ allocNode . Child2 = Proxy . Free ;
297302 ++ NodeCount ;
298303 proxy = alloc ;
299304 return ref allocNode ;
@@ -1897,7 +1902,7 @@ public int Rebuild(bool fullBuild)
18971902
18981903 var nodeIndex = _root ;
18991904 ref var baseRef = ref _nodes [ 0 ] ;
1900- var node = baseRef ;
1905+ ref var node = ref Unsafe . Add ( ref baseRef , nodeIndex ) ;
19011906
19021907 // These are the nodes that get sorted to rebuild the tree.
19031908 // I'm using indices because the node pool may grow during the build.
@@ -1942,7 +1947,7 @@ public int Rebuild(bool fullBuild)
19421947 stack . Push ( node . Child2 ) ;
19431948 }
19441949
1945- node = Unsafe . Add ( ref baseRef , nodeIndex ) ;
1950+ node = ref Unsafe . Add ( ref baseRef , nodeIndex ) ;
19461951
19471952 // Remove doomed node
19481953 FreeNode ( doomedNodeIndex ) ;
@@ -1956,7 +1961,7 @@ public int Rebuild(bool fullBuild)
19561961 }
19571962
19581963 nodeIndex = stack . Pop ( ) ;
1959- node = Unsafe . Add ( ref baseRef , nodeIndex ) ;
1964+ node = ref Unsafe . Add ( ref baseRef , nodeIndex ) ;
19601965 }
19611966
19621967 #if B2_VALIDATE
@@ -2068,6 +2073,9 @@ internal void RayCastNew(RayCastInput input, long mask, ref WorldRayCastContext
20682073 var p1 = input . Origin ;
20692074 var d = input . Translation ;
20702075
2076+ if ( d . LengthSquared ( ) < float . Epsilon )
2077+ return ;
2078+
20712079 var r = d . Normalized ( ) ;
20722080
20732081 // v is perpendicular to the segment.
@@ -2084,7 +2092,7 @@ internal void RayCastNew(RayCastInput input, long mask, ref WorldRayCastContext
20842092 // Build a bounding box for the segment.
20852093 var segmentAABB = new Box2 ( Vector2 . Min ( p1 , p2 ) , Vector2 . Max ( p1 , p2 ) ) ;
20862094
2087- var stack = new GrowableStack < Proxy > ( stackalloc Proxy [ 256 ] ) ;
2095+ var stack = new GrowableStack < Proxy > ( stackalloc Proxy [ TreeStackSize ] ) ;
20882096 ref var baseRef = ref _nodes [ 0 ] ;
20892097 stack . Push ( _root ) ;
20902098
@@ -2141,15 +2149,10 @@ internal void RayCastNew(RayCastInput input, long mask, ref WorldRayCastContext
21412149 }
21422150 else
21432151 {
2144- var stackCount = stack . GetCount ( ) ;
2145- Assert ( stackCount < 256 - 1 ) ;
2146- if ( stackCount < 256 - 1 )
2147- {
2148- // TODO_ERIN just put one node on the stack, continue on a child node
2149- // TODO_ERIN test ordering children by nearest to ray origin
2150- stack . Push ( node . Child1 ) ;
2151- stack . Push ( node . Child2 ) ;
2152- }
2152+ // TODO_ERIN just put one node on the stack, continue on a child node
2153+ // TODO_ERIN test ordering children by nearest to ray origin
2154+ stack . Push ( node . Child1 ) ;
2155+ stack . Push ( node . Child2 ) ;
21532156 }
21542157 }
21552158 }
@@ -2205,7 +2208,7 @@ internal void ShapeCast(ShapeCastInput input, long maskBits, TreeShapeCastCallba
22052208 var subInput = input ;
22062209
22072210 ref var baseRef = ref _nodes [ 0 ] ;
2208- var stack = new GrowableStack < Proxy > ( stackalloc Proxy [ 256 ] ) ;
2211+ var stack = new GrowableStack < Proxy > ( stackalloc Proxy [ TreeStackSize ] ) ;
22092212 stack . Push ( _root ) ;
22102213
22112214 while ( stack . GetCount ( ) > 0 )
@@ -2258,16 +2261,10 @@ internal void ShapeCast(ShapeCastInput input, long maskBits, TreeShapeCastCallba
22582261 }
22592262 else
22602263 {
2261- var stackCount = stack . GetCount ( ) ;
2262- Assert ( stackCount < 256 - 1 ) ;
2263-
2264- if ( stackCount < 255 )
2265- {
2266- // TODO_ERIN just put one node on the stack, continue on a child node
2267- // TODO_ERIN test ordering children by nearest to ray origin
2268- stack . Push ( node . Child1 ) ;
2269- stack . Push ( node . Child2 ) ;
2270- }
2264+ // TODO_ERIN just put one node on the stack, continue on a child node
2265+ // TODO_ERIN test ordering children by nearest to ray origin
2266+ stack . Push ( node . Child1 ) ;
2267+ stack . Push ( node . Child2 ) ;
22712268 }
22722269 }
22732270 }
0 commit comments