UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

96 lines (95 loc) 3.31 kB
import { vec2 } from "gl-matrix"; import CollisionObject from "../collisionObject"; import Ray from "../ray"; /** * Represents an axis aligned bounding box in 2D world space. * * A minimum (bottom left) and maximum (top right) vertex is used to represent the {@link AABB}. */ export default class AABB { min: vec2; max: vec2; margin: number; /** * A {@link CollisionObject} that is bound by the {@link AABB}. */ obj: CollisionObject; /** * Creates a {@link AABB} with the given minimum and and maximum vertices. * * @param min The minimum vertex (bottom left) * @param max The maximum vertex (top right) */ constructor(min: vec2, max: vec2); /** * Creates an {@link AABB} which contains both `a` and `b`, the created {@link AABB} is the union of `a` and `b` plus the `margin`. * * @param a An {@link AABB} * @param b An {@link AABB} * @param margin A margin to add to the min and max of the created AABB */ constructor(a: AABB, b: AABB, margin: number); /** * Creates a {@link AABB} that bounds the vertices of the given object's collider. * * @param obj {@link CollisionObject} to create AABB from * @param margin A margin to add to the new min and max points */ constructor(obj: CollisionObject, margin: number); /** * Sets the AABB's min and max so that the AABB contains the object's collider. * * @param obj The {@link CollisionObject} to bound * @param margin A margin to add to the new min and max points */ setMinMaxFromCollisionObj(obj: CollisionObject, margin: number): void; /** * Sets the AABB's min and max to the union of `a` and `b`. * * @param a An {@link AABB} * @param b An {@link AABB} * @param margin A margin to add to the new min and max points */ union(a: AABB, b: AABB, margin?: number): void; /** * Computes the area of the {@link AABB}. * * @returns The area of the {@link AABB} */ area(): number; /** * Determines wether or not this {@link AABB} intersects with the given {@link AABB}. * * @param b The {@link AABB} to check for intersection against */ intersects(b: AABB): boolean; /** * Determines wether or not the given point intersects with this {@link AABB}. * * @param point The point to check for intersection */ intersects(point: vec2): boolean; /** * Determines wether or not the given ray intersects with this {@link AABB}. * * @param ray The ray to check for intersection */ intersects(ray: Ray): boolean; /** * Determines wether or not the given {@link AABB} is contained within this {@link AABB}. * * @param b The {@link AABB} to check */ contains(b: AABB): boolean; /** * Clips a line based on the {@link AABB}'s min and max to determine if the line intersects with the AABB. * * @param dim The dimension to clip in (0 = x, 1 = y) * @param v0 The line's start point * @param v1 The line's end point * @param fLow The current fLow * @param fHigh The current fHigh * @returns false when no intersection possible, otherwise new fLow and fHigh */ private clipLine; }