Free Spot Finder 2D

curio124

(0)
$5
Publisher curio124
File size 90.19kB
Number of files 5
Latest version 1.0.0
Latest release date 2024-02-14 05:47:11
First release date 2024-02-14 05:47:11
Supported Unity versions 2018.4.2 or higher

Highly preformant detection algorythm encapsulated in the single class with 3 main capabilities:

1. calculate % of occupation in rectangular or circular area,

2. retrieve random free spot from rectangular or circular area,

3. check if defined rectangular or circular area is unoccupied or not.


Used algorythm is 5-20x faster than using any Physics.Cast approach and works either with internal shapes or existing colliders. Area is analyzed and random free spot retrieved usually within few miliseconds! Works only in 2D space.


Supported concepts:

- fixed boundaries to retrieve free spot

- padding for placed object not to overlap with occupied areas

- rectangular or circular detection area

- flexible resolution of the detection algorythm

- rectangles, circles, ellipses and points as occupied areas

- Collider2D (any) as occupied areas

- rotation and scaling of Collider2D

- DebugDraw functions to display results of analysis


Provided class is very easy to use and every public function, method and property is well documented. Free spot can be retrieve in 3 very easy steps:

1. create an instance with preffered settings (or use properties later)

        var scanArea = new ScanArea2D(boundariesSize: new Vector2(40, 20));

2a. add occupied areas manually by providing shapes:

        scanArea.AddPoint(new Vector2(4, 2));

        scanArea.AddCircle(new Vector2(-2, 1), 2);

        scanArea.AddRectangle(new Vector2(1, -3), new Vector2(3, 2));

2b. and/or enable detection of existing colliders for occupied spots

        scanArea.ignoreAllColliders = false;

3. request new random available spot for object of defined size

        Vector2 freeSpot =  scanArea.GetRandomFreePositionForRectangle(objectSize, areaCenter, areaRadius);