# misc/collision2d (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/collision2d/

2D collision utility.

## Collision2D extends Object

2D collision utility.
- Gathers tests reinvented in every action game, like circle-circle overlap separation (pushing apart) and nearest search within a radius.

Example: Test two circles for overlap and push them apart

```js
import { Collision2D, Vector2 } from "./build/vanilla.js";

let positionA = Vector2.create(0, 0);
let positionB = Vector2.create(15, 0);
console.log(Collision2D.testCircleOverlap(positionA, 10, positionB, 10));

const push = Collision2D.resolveCircleOverlap(positionA, 10, positionB, 10);
if (push !== null) {
	positionA = positionA.add(push.pushA);
	positionB = positionB.add(push.pushB);
}
console.log(positionA.x, positionB.x);
console.log(Collision2D.testCircleOverlap(positionA, 10, positionB, 10));
```

- `static testCircleOverlap(positionA, radiusA, positionB, radiusB)` (static, since 0.3.0): Circle-circle overlap check. (static)
  - `positionA` Vector2: Center of circle A.
  - `radiusA` number: Radius of circle A.
  - `positionB` Vector2: Center of circle B.
  - `radiusB` number: Radius of circle B.
  - returns boolean: True if the two circles overlap. (false if they only touch)
- `static resolveCircleOverlap(positionA, radiusA, positionB, radiusB, isStaticB = false)` (static, since 0.3.0): Circle-circle overlap separation. (static)
  - If overlapping, returns the displacement that pushes each apart by half. null if not overlapping.
  - If isStaticB is true, B is treated as fixed and only A is pushed the full distance.
  - If the two centers are exactly the same, pushes apart along an arbitrary axis (x).
  - `positionA` Vector2: Center of circle A.
  - `radiusA` number: Radius of circle A.
  - `positionB` Vector2: Center of circle B.
  - `radiusB` number: Radius of circle B.
  - `isStaticB` boolean: Whether to treat B as a static object that does not move. (default false)
  - returns object | null: { pushA: Vector2, pushB: Vector2 }

  Example: Push only A out of a static wall circle

  ```js
  import { Collision2D, Vector2 } from "./build/vanilla.js";
  
  let playerPosition = Vector2.create(18, 0);
  const wallPosition = Vector2.create(0, 0);
  const push = Collision2D.resolveCircleOverlap(playerPosition, 8, wallPosition, 12, true);
  if (push !== null) {
  	playerPosition = playerPosition.add(push.pushA);
  }
  console.log(playerPosition.x, playerPosition.y);
  ```

- `static findNearestInRadius(targetList, getPositionHandler, center, radius)` (static, since 0.3.0): Finds the nearest target within a radius. (static)
  - Finds "the closest one among those within radius of the center", as in projectile hit tests.
  - `targetList` T[]: List of targets to search.
  - `getPositionHandler` Function: (target) => Vector2
  - `center` Vector2: Search center.
  - `radius` number: Search radius. (inclusive of the boundary)
  - returns T | null: Nearest target within the radius. (null if none, the later one on equal distance)

  Example: Find the nearest enemy within a radius

  ```js
  import { Collision2D, Vector2 } from "./build/vanilla.js";
  
  const enemyList = [
  	{ name: "slime", position: Vector2.create(40, 0) },
  	{ name: "bat", position: Vector2.create(10, 5) },
  	{ name: "golem", position: Vector2.create(300, 0) },
  ];
  const center = Vector2.create(0, 0);
  const nearest = Collision2D.findNearestInRadius(enemyList, (enemy) => enemy.position, center, 50);
  console.log(nearest.name);
  ```

- `static findNearestIndexInRadius(targetList, getPositionHandler, center, radius)` (static, since 0.4.0): Finds the index of the nearest target within a radius. (static, returns the index by the same test as findNearestInRadius, -1 if none)
  - On equal distance, the later one is chosen.
  - `targetList` T[]: List of targets to search.
  - `getPositionHandler` Function: (target) => Vector2
  - `center` Vector2: Search center.
  - `radius` number: Search radius. (inclusive of the boundary)
  - returns number: Index of the nearest target within the radius. (-1 if none)
- `static testPointInCircle(point, center, radius)` (static, since 0.3.0): Point-in-circle check. (static)
  - `point` Vector2: Point to test.
  - `center` Vector2: Center of the circle.
  - `radius` number: Radius of the circle.
  - returns boolean: True if the point is inside the circle or on its boundary.
