UNPKG

blaze-2d

Version:

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

92 lines (91 loc) 3 kB
import { vec2 } from "gl-matrix"; import AABBTree from "../aabb/aabbTree"; import CollisionObject from "../collisionObject"; import CollisionPair from "../collisionPair"; import Manifold from "../manifold"; import ManifoldMap from "../manifoldMap"; import { CollisionSolver } from "../solvers/solver"; import Space from "./space"; /** * Represents an infinite space containing {@link CollisionObject}s. * * Can be used to test for and solve collisions between objects in the space. */ export default class CollisionsSpace extends Space<CollisionObject, CollisionSolver> { gravity: vec2; collisionManifolds: Manifold[]; triggerManifolds: Manifold[]; /** * Collision manifolds map. */ collisions: ManifoldMap; /** * Trigger manifolds map. */ triggers: ManifoldMap; /** * Array used to store pairs of objects which may be colliding. */ collisionPairs: CollisionPair[]; /** * The {@link AABBTree} used for broadphase and raycasting. */ aabbTree: AABBTree; /** * Creates a {@link CollisionSpace} instance. * * @param gravity The gravity vector to use in the collision space */ constructor(gravity: vec2); /** * Executes a solver on every collision manifold in the space. * * Make sure to call `this.obtainManifolds` before executing any solvers. * * @param id The id of the solver to execute * @param delta The time since the last update */ solve(id: string, delta: number): void; /** * Fire "collision" and "trigger" events on objects in `this.manifolds`. * * If both objects in a collision are triggers object `a` will recieve the "trigger" the event. */ fireEvents(): void; /** * Performs the broadphase collision detection. * * The broadphase will update the {@link AABBTree} and then obtain possible collision pairs for the narrow phase. */ broadphase(): void; /** * Obtains all collision and trigger manifolds for the current frame. * * Manifolds are stored in `this.manifolds` * * obtainManifolds should only be called once per frame. * * @param delta Time since last frame */ obtainManifolds(delta: number): void; /** * Gets all the manifolds in the collisions {@link ManifoldMap} and stores them in `this.collisionManifolds`. */ private getCollisionManifolds; /** * Gets all the manifolds in the triggers {@link ManifoldMap} and stores them in `this.triggersManifolds`. */ private getTriggerManifolds; /** * Adds an object to the space. * * @param obj The {@link CollisionObject} to add to the space */ addObject(obj: CollisionObject): void; /** * Removes an object from the space and deletes any manifolds associated with it. * * @param obj The {@link CollisionObject} to remove from the space */ removeObject(obj: CollisionObject): boolean; }