Unity-Technologies / EntityComponentSystemSamples

Other
7.09k stars 1.6k forks source link

Closest Target Scene with KD Tree Algorithm not always finds the nearest target #275

Open houshuo opened 11 months ago

houshuo commented 11 months ago

Apprently, in the screenshot below, the green boxes find wrong nearest target. With None and Simple algorithms, such error won't happen. image

cihanozcelik commented 10 months ago

Hello,

I've noticed a potential issue in the TargetingSystem.cs file on line 141 (TargetingSystem.cs). The GetEntriesInRangeWithHeap method is called with unfilteredChunkIndex as the first parameter. This parameter is intended to be an entity index within kdQuery, to ensure the algorithm does not find the entity that is performing the search. However, in this sample, the seeker entities are not included in kdQuery, so there is no need to skip any entity. Passing unfilteredChunkIndex seems arbitrary here, and we could instead pass -1 as a quick fix for the sample:

Tree.GetEntriesInRangeWithHeap(-1, transforms[i].Position, float.MaxValue, ref Scratch.Neighbours); If the intention is to include all entities in range and if you need the seeker entities' query index within IJobChunk, it would be more appropriate to first create an index mapping to the job using kdQuery.CalculateBaseEntityIndexArrayAsync.