# misc/collision2d (vanilla-engine 0.4.0)

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

2D collision utility.
- A port of the original misc/collision2d.js. Gathers checks that were rebuilt in every action game, like circle-circle overlap separation (pushing apart) and finding the nearest within a radius.
- All functions are static.
- Differences from the original:
  resolve_circle_overlap returns None instead of null, and CircleOverlapPush instead of { push_a, push_b }.
  find_nearest_in_radius returns a reference to the target, and find_nearest_index_in_radius, which returns an index for modifying, is added.
  The is_static_b default (false) is always passed because Rust has no default arguments.

## CircleOverlapPush (struct) (Clone, Copy, Debug, PartialEq)

Offsets that resolve a circle-circle overlap. (original { push_a, push_b })
- Returned by Collision2D::resolve_circle_overlap. Adding each to its position resolves the overlap.

Example: Push two overlapping circles apart by half each

```rust
use vanilla::{Collision2D, Vector2};

let mut position_a = Vector2::create(0.0, 0.0);
let mut position_b = Vector2::create(6.0, 0.0);
if let Some(circle_overlap_push) = Collision2D::resolve_circle_overlap(position_a, 4.0, position_b, 4.0, false) {
	position_a += circle_overlap_push.push_a;
	position_b += circle_overlap_push.push_b;
}
assert_eq!(position_a, Vector2::create(-1.0, 0.0));
assert_eq!(position_b, Vector2::create(7.0, 0.0));
assert!(!Collision2D::test_circle_overlap(position_a, 4.0, position_b, 4.0));
```

- `pub push_a: Vector2` (field, since 0.3.2): Offset to add to A. (away from B)
  - returns Vector2
- `pub push_b: Vector2` (field, since 0.3.2): Offset to add to B. (away from A, 0 if B is static)
  - returns Vector2

## Collision2D (struct)

2D collision utility. (a set of static functions)

Example: Push a circle stuck in a wall (static object) out

```rust
use vanilla::{Collision2D, Vector2};

let wall_position = Vector2::create(10.0, 0.0);
let mut hero_position = Vector2::create(4.0, 0.0);
if let Some(circle_overlap_push) = Collision2D::resolve_circle_overlap(hero_position, 3.0, wall_position, 5.0, true) {
	hero_position += circle_overlap_push.push_a;
}
assert_eq!(hero_position, Vector2::create(2.0, 0.0));
```

Example: Pick the nearest enemy within a radius

```rust
use vanilla::{Collision2D, Vector2};

struct Enemy {
	position: Vector2,
	health: i32,
}

let mut enemy_list = vec![
	Enemy { position: Vector2::create(30.0, 0.0), health: 3 },
	Enemy { position: Vector2::create(8.0, 6.0), health: 3 },
];
let center = Vector2::zero();
if let Some(enemy_index) = Collision2D::find_nearest_index_in_radius(&enemy_list, |enemy| enemy.position, center, 20.0) {
	enemy_list[enemy_index].health -= 1;
}
assert_eq!(enemy_list[1].health, 2);
assert_eq!(enemy_list[0].health, 3);
```

- `pub fn test_circle_overlap(position_a: Vector2, radius_a: f64, position_b: Vector2, radius_b: f64) -> bool` (static, since 0.3.2): Circle-circle overlap test. (merely touching is not overlapping)
  - `position_a` Vector2: Center of circle A.
  - `radius_a` f64: Radius of circle A.
  - `position_b` Vector2: Center of circle B.
  - `radius_b` f64: Radius of circle B.
  - returns bool: true if the distance between the two centers is less than the sum of the radii.
- `pub fn resolve_circle_overlap(position_a: Vector2, radius_a: f64, position_b: Vector2, radius_b: f64, is_static_b: bool) -> Option<CircleOverlapPush>` (static, since 0.3.2): Circle-circle overlap separation. (offsets that push each other out by half if overlapping, None if not)
  - If is_static_b, B is treated as static and only A is pushed by the full overlap depth.
  - If the two centers are nearly the same, pushes along the x axis.
  - `position_a` Vector2: Center of circle A.
  - `radius_a` f64: Radius of circle A.
  - `position_b` Vector2: Center of circle B.
  - `radius_b` f64: Radius of circle B.
  - `is_static_b` bool: If true, B does not move and only A is pushed. (original default false)
  - returns Option<CircleOverlapPush>: Offsets to add to A and B if overlapping. None if merely touching or apart.
- `pub fn find_nearest_index_in_radius<T>(target_list: &[T], get_position_handler: impl Fn(&T) -> Vector2, center: Vector2, radius: f64) -> Option<usize>` (static, since 0.3.2): Finds the index of the nearest target within a radius. (the nearest of those within radius of the center, the later one on a tie)
  - Targets on the radius boundary are included.
  - `target_list` &[T]: List of targets to search.
  - `get_position_handler` impl Fn(&T) -> Vector2: Function that extracts a target's position.
  - `center` Vector2: Center to search from.
  - `radius` f64: Radius to search within.
  - returns Option<usize>: Index of the nearest target. None if none are within the radius.
- `pub fn find_nearest_in_radius<T>(target_list: &[T], get_position_handler: impl Fn(&T) -> Vector2, center: Vector2, radius: f64) -> Option<&T>` (static, since 0.3.2): Finds the nearest target within a radius. (like a projectile hit check)
  - The picking rule is the same as find_nearest_index_in_radius.
  - `target_list` &[T]: List of targets to search.
  - `get_position_handler` impl Fn(&T) -> Vector2: Function that extracts a target's position.
  - `center` Vector2: Center to search from.
  - `radius` f64: Radius to search within.
  - returns Option<&T>: Reference to the nearest target. None if none are within the radius.

  Example: The nearest point within a projectile's radius

  ```rust
  use vanilla::{Collision2D, Vector2};
  
  let target_list = [Vector2::create(10.0, 0.0), Vector2::create(3.0, 4.0), Vector2::create(0.0, 30.0)];
  let nearest_target = Collision2D::find_nearest_in_radius(&target_list, |target| *target, Vector2::zero(), 12.0);
  assert_eq!(nearest_target, Some(&Vector2::create(3.0, 4.0)));
  assert!(Collision2D::find_nearest_in_radius(&target_list, |target| *target, Vector2::create(100.0, 100.0), 5.0).is_none());
  ```

- `pub fn test_point_in_circle(point: Vector2, center: Vector2, radius: f64) -> bool` (static, since 0.3.2): Point-in-circle test. (the boundary counts as inside)
  - `point` Vector2: Point to test.
  - `center` Vector2: Center of the circle.
  - `radius` f64: Radius of the circle.
  - returns bool: true if the point is inside the circle or on its boundary.
