# base/obb (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/obb/

Oriented bounding box. (the original base/obb.js)
- 2D as in the original. A convex quadrilateral joining 4 corners (named edges in the original) in order.
- contains is the same ray crossing test as the original. Points on the left and top edges are inside, points on the right and bottom edges are outside.
- overlaps is the same separating axis theorem (SAT) as the original. Projects both sets of corners (get_edges) onto each edge normal of the two rectangles, and counts merely touching as overlapping. (< comparison)
- The original setEdges blocks null, non-arrays and wrong lengths with exceptions. In the Rust version, the [Vector2; 4] type does that job.

## OBB (struct) (Clone, Copy, Debug, Default, PartialEq)

Oriented bounding box. (2D, a convex quadrilateral joining 4 corners in order)

Example: Point test and overlap test

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

let mut square = OBB::new();
square.set_edges([Vector2::create(0.0, 0.0), Vector2::create(10.0, 0.0), Vector2::create(10.0, 10.0), Vector2::create(0.0, 10.0)]);
let mut diamond = OBB::new();
diamond.set_edges([Vector2::create(5.0, -2.0), Vector2::create(12.0, 5.0), Vector2::create(5.0, 12.0), Vector2::create(-2.0, 5.0)]);

assert!(square.contains(Vector2::create(5.0, 5.0)));
assert!(!square.contains(Vector2::create(11.0, 5.0)));
assert!(square.overlaps(&diamond));
```

- `pub fn new() -> OBB` (constructor, since 0.3.2): Constructor. (all corners (0, 0))
  - returns OBB: An OBB with all 4 corners at (0, 0).
- `pub fn contains(&self, view_position: Vector2) -> bool` (method, since 0.3.2): Whether a point is inside. (ray crossing test)
  - `view_position` Vector2: Point to test. (in the same coordinate system as the corners)
  - returns bool: true if inside. (for an axis-aligned rectangle, points on the left and top edges are inside, points on the right and bottom edges are outside)

  Example: Points on the edges

  ```rust
  use vanilla::{Vector2, OBB};
  
  let mut square = OBB::new();
  square.set_edges([Vector2::create(0.0, 0.0), Vector2::create(10.0, 0.0), Vector2::create(10.0, 10.0), Vector2::create(0.0, 10.0)]);
  assert!(square.contains(Vector2::create(0.0, 5.0)));
  assert!(!square.contains(Vector2::create(10.0, 5.0)));
  assert!(!square.contains(Vector2::create(5.0, 10.0)));
  ```

- `pub fn overlaps(&self, other: &OBB) -> bool` (method, since 0.3.2): Whether it overlaps another OBB. (separating axis theorem, projects onto every edge normal of both rectangles)
  - `other` &OBB: OBB to check overlap with.
  - returns bool: true if overlapping or touching. false if any edge normal of the two rectangles is a separating axis.

  Example: Two touching rectangles and a separate rectangle

  ```rust
  use vanilla::{Vector2, OBB};
  
  let mut left = OBB::new();
  left.set_edges([Vector2::create(0.0, 0.0), Vector2::create(10.0, 0.0), Vector2::create(10.0, 10.0), Vector2::create(0.0, 10.0)]);
  let mut right = OBB::new();
  right.set_edges([Vector2::create(10.0, 0.0), Vector2::create(20.0, 0.0), Vector2::create(20.0, 10.0), Vector2::create(10.0, 10.0)]);
  let mut far = OBB::new();
  far.set_edges([Vector2::create(30.0, 0.0), Vector2::create(40.0, 0.0), Vector2::create(40.0, 10.0), Vector2::create(30.0, 10.0)]);
  assert!(left.overlaps(&right));
  assert!(!left.overlaps(&far));
  ```

- `pub fn set_edges(&mut self, edges: [Vector2; 4])` (method, since 0.3.2): Sets the corner list.
  - `edges` [Vector2; 4]: 4 corners. (in an order that forms a convex quadrilateral when joined in sequence)
- `pub fn get_edges(&self) -> &[Vector2; 4]` (method, since 0.3.2): Returns the corner list.
  - returns &[Vector2; 4]: Read reference to the 4 corners.
