Skip to content

Commit

Permalink
feat: refactor / small optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacek Pietal committed Feb 28, 2024
1 parent 31f7e08 commit 29f027e
Show file tree
Hide file tree
Showing 39 changed files with 675 additions and 601 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ npm install detect-collisions

For detailed documentation on the library's API, refer to the following link:

https://prozi.github.io/detect-collisions/modules.html
https://prozi.github.io/detect-collisions/

## Usage

Expand Down
50 changes: 39 additions & 11 deletions dist/base-system.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ import { Ellipse } from "./bodies/ellipse";
import { Line } from "./bodies/line";
import { Point } from "./bodies/point";
import { Polygon } from "./bodies/polygon";
import { Body, BodyOptions, ChildrenData, Data, PotentialVector, RBush, Vector } from "./model";
import { Body, BodyOptions, ChildrenData, Data, Leaf, PotentialVector, RBush, TraverseFunction, Vector } from "./model";
/**
* very base collision system
* very base collision system (create, insert, update, draw, remove)
*/
export declare class BaseSystem<TBody extends Body> extends RBush<TBody> implements Data<TBody> {
export declare class BaseSystem<TBody extends Body = Body> extends RBush<TBody> implements Data<TBody> {
data: ChildrenData<TBody>;
/**
* draw exact bodies colliders outline
*/
draw(context: CanvasRenderingContext2D): void;
/**
* draw bounding boxes hierarchy outline
*/
drawBVH(context: CanvasRenderingContext2D): void;
/**
* create point at position with options and add to system
*/
Expand All @@ -42,4 +34,40 @@ export declare class BaseSystem<TBody extends Body> extends RBush<TBody> impleme
* create polygon at position with options and add to system
*/
createPolygon(position: PotentialVector, points: PotentialVector[], options?: BodyOptions): Polygon;
/**
* re-insert body into collision tree and update its aabb
* every body can be part of only one system
*/
insert(body: TBody): RBush<TBody>;
/**
* updates body in collision tree
*/
updateBody(body: TBody): void;
/**
* update all bodies aabb
*/
update(): void;
/**
* draw exact bodies colliders outline
*/
draw(context: CanvasRenderingContext2D): void;
/**
* draw bounding boxes hierarchy outline
*/
drawBVH(context: CanvasRenderingContext2D): void;
/**
* remove body aabb from collision tree
*/
remove(body: TBody, equals?: (a: TBody, b: TBody) => boolean): RBush<TBody>;
/**
* get object potential colliders
* @deprecated because it's slower to use than checkOne() or checkAll()
*/
getPotentials(body: TBody): TBody[];
/**
* used to find body deep inside data with finder function returning boolean found or not
*/
traverse(traverseFunction: TraverseFunction<TBody>, { children }?: {
children?: Leaf<TBody>[];
}): TBody | undefined;
}
112 changes: 91 additions & 21 deletions dist/base-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,9 @@ const model_1 = require("./model");
const optimized_1 = require("./optimized");
const utils_1 = require("./utils");
/**
* very base collision system
* very base collision system (create, insert, update, draw, remove)
*/
class BaseSystem extends model_1.RBush {
/**
* draw exact bodies colliders outline
*/
draw(context) {
(0, optimized_1.forEach)(this.all(), (body) => {
body.draw(context);
});
}
/**
* draw bounding boxes hierarchy outline
*/
drawBVH(context) {
const drawChildren = (body) => {
(0, utils_1.drawBVH)(context, body);
if (body.children) {
(0, optimized_1.forEach)(body.children, drawChildren);
}
};
(0, optimized_1.forEach)(this.data.children, drawChildren);
}
/**
* create point at position with options and add to system
*/
Expand Down Expand Up @@ -82,5 +62,95 @@ class BaseSystem extends model_1.RBush {
this.insert(polygon);
return polygon;
}
/**
* re-insert body into collision tree and update its aabb
* every body can be part of only one system
*/
insert(body) {
body.bbox = body.getAABBAsBBox();
if (body.system) {
// allow end if body inserted and not moved
if (!(0, utils_1.bodyMoved)(body)) {
return this;
}
// old bounding box *needs* to be removed
body.system.remove(body);
}
// only then we update min, max
body.minX = body.bbox.minX - body.padding;
body.minY = body.bbox.minY - body.padding;
body.maxX = body.bbox.maxX + body.padding;
body.maxY = body.bbox.maxY + body.padding;
// set system for later body.system.updateBody(body)
body.system = this;
// reinsert bounding box to collision tree
return super.insert(body);
}
/**
* updates body in collision tree
*/
updateBody(body) {
body.updateBody();
}
/**
* update all bodies aabb
*/
update() {
(0, optimized_1.forEach)(this.all(), (body) => {
this.updateBody(body);
});
}
/**
* draw exact bodies colliders outline
*/
draw(context) {
(0, optimized_1.forEach)(this.all(), (body) => {
body.draw(context);
});
}
/**
* draw bounding boxes hierarchy outline
*/
drawBVH(context) {
const drawChildren = (body) => {
(0, utils_1.drawBVH)(context, body);
if (body.children) {
(0, optimized_1.forEach)(body.children, drawChildren);
}
};
(0, optimized_1.forEach)(this.data.children, drawChildren);
}
/**
* remove body aabb from collision tree
*/
remove(body, equals) {
body.system = undefined;
return super.remove(body, equals);
}
/**
* get object potential colliders
* @deprecated because it's slower to use than checkOne() or checkAll()
*/
getPotentials(body) {
// filter here is required as collides with self
return (0, optimized_1.filter)(this.search(body), (candidate) => candidate !== body);
}
/**
* used to find body deep inside data with finder function returning boolean found or not
*/
traverse(traverseFunction, { children } = this.data) {
return children === null || children === void 0 ? void 0 : children.find((body, index) => {
if (!body) {
return false;
}
if (body.type && traverseFunction(body, children, index)) {
return true;
}
// if callback returns true, ends forEach
if (body.children) {
this.traverse(traverseFunction, body);
}
});
}
}
exports.BaseSystem = BaseSystem;
4 changes: 2 additions & 2 deletions dist/bodies/circle.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BBox } from "rbush";
import { Circle as SATCircle } from "sat";
import { BodyOptions, BodyProps, PotentialVector, SATVector, BodyType, Vector } from "../model";
import { System } from "../system";
import { BaseSystem } from "../base-system";
/**
* collider - circle
*/
Expand Down Expand Up @@ -53,7 +53,7 @@ export declare class Circle extends SATCircle implements BBox, BodyProps {
/**
* reference to collision system
*/
system?: System;
system?: BaseSystem;
/**
* was the polygon modified and needs update in the next checkCollision
*/
Expand Down
Loading

0 comments on commit 29f027e

Please sign in to comment.