diff --git a/dist/base-system.d.ts b/dist/base-system.d.ts index 4837827..20ac2c0 100644 --- a/dist/base-system.d.ts +++ b/dist/base-system.d.ts @@ -35,7 +35,7 @@ export declare class BaseSystem
extends RBush */ createPolygon(position: PotentialVector, points: PotentialVector[], options?: BodyOptions): Polygon; /** - * re-insert body into collision tree and update its aabb + * re-insert body into collision tree and update its bbox * every body can be part of only one system */ insert(body: TBody): RBush; diff --git a/dist/base-system.js b/dist/base-system.js index 2c51640..31ab232 100644 --- a/dist/base-system.js +++ b/dist/base-system.js @@ -63,7 +63,7 @@ class BaseSystem extends model_1.RBush { return polygon; } /** - * re-insert body into collision tree and update its aabb + * re-insert body into collision tree and update its bbox * every body can be part of only one system */ insert(body) { @@ -81,8 +81,6 @@ class BaseSystem extends model_1.RBush { 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); } diff --git a/dist/bodies/circle.d.ts b/dist/bodies/circle.d.ts index 782ab8e..427848f 100644 --- a/dist/bodies/circle.d.ts +++ b/dist/bodies/circle.d.ts @@ -1,6 +1,6 @@ import { BBox } from "rbush"; import { Circle as SATCircle } from "sat"; -import { BaseSystem } from "../base-system"; +import { System } from "../system"; import { BodyGroup, BodyOptions, BodyProps, BodyType, PotentialVector, SATVector, Vector } from "../model"; /** * collider - circle @@ -53,7 +53,7 @@ export declare class Circle extends SATCircle implements BBox, BodyProps { /** * reference to collision system */ - system?: BaseSystem; + system?: System; /** * was the polygon modified and needs update in the next checkCollision */ diff --git a/dist/demo/demo.js b/dist/demo/demo.js index 4b22658..42d4b55 100644 --- a/dist/demo/demo.js +++ b/dist/demo/demo.js @@ -2124,7 +2124,7 @@ class BaseSystem extends model_1.RBush { return polygon; } /** - * re-insert body into collision tree and update its aabb + * re-insert body into collision tree and update its bbox * every body can be part of only one system */ insert(body) { @@ -2142,8 +2142,6 @@ class BaseSystem extends model_1.RBush { 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); } @@ -3319,7 +3317,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.BodyGroup = exports.getGroup = exports.BodyType = exports.SATCircle = exports.SATPolygon = exports.SATVector = exports.Response = exports.RBush = exports.isSimple = exports.bin2dec = void 0; +exports.BodyGroup = exports.getGroup = exports.BodyType = exports.SATCircle = exports.SATPolygon = exports.SATVector = exports.Response = exports.RBush = exports.isSimple = void 0; const rbush_1 = __importDefault(__webpack_require__(/*! rbush */ "./node_modules/rbush/rbush.min.js")); Object.defineProperty(exports, "RBush", ({ enumerable: true, get: function () { return rbush_1.default; } })); const sat_1 = __webpack_require__(/*! sat */ "./node_modules/sat/SAT.js"); @@ -3327,13 +3325,6 @@ Object.defineProperty(exports, "SATCircle", ({ enumerable: true, get: function ( Object.defineProperty(exports, "SATPolygon", ({ enumerable: true, get: function () { return sat_1.Polygon; } })); Object.defineProperty(exports, "Response", ({ enumerable: true, get: function () { return sat_1.Response; } })); Object.defineProperty(exports, "SATVector", ({ enumerable: true, get: function () { return sat_1.Vector; } })); -/** - * binary string to decimal number - */ -function bin2dec(binary) { - return Number(`0b${binary}`.replace(/\s/g, "")); -} -exports.bin2dec = bin2dec; var poly_decomp_es_1 = __webpack_require__(/*! poly-decomp-es */ "./node_modules/poly-decomp-es/dist/poly-decomp-es.js"); Object.defineProperty(exports, "isSimple", ({ enumerable: true, get: function () { return poly_decomp_es_1.isSimple; } })); /** @@ -3482,6 +3473,16 @@ class System extends base_system_1.BaseSystem { */ this.response = new model_1.Response(); } + /** + * re-insert body into collision tree and update its bbox + * every body can be part of only one system + */ + insert(body) { + const insertResult = super.insert(body); + // set system for later body.system.updateBody(body) + body.system = this; + return insertResult; + } /** * separate (move away) bodies */ @@ -3524,6 +3525,16 @@ class System extends base_system_1.BaseSystem { }; return (0, optimized_1.some)(bodies, checkCollision); } + /** + * callback all bodies in area + */ + checkArea(area, callback = utils_1.returnTrue, response = this.response) { + const bodies = this.search(area); + const checkOne = (body) => { + return this.checkOne(body, callback, response); + }; + return (0, optimized_1.some)(bodies, checkOne); + } /** * check all bodies collisions with callback */ @@ -3630,7 +3641,7 @@ exports.System = System; "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.returnTrue = exports.cloneResponse = exports.drawBVH = exports.drawPolygon = exports.dashLineTo = exports.getSATTest = exports.getBounceDirection = exports.mapArrayToVector = exports.mapVectorToArray = exports.clonePointsArray = exports.checkAInB = exports.areSameGroup = exports.intersectAABB = exports.notIntersectAABB = exports.bodyMoved = exports.extendBody = exports.clockwise = exports.distance = exports.ensurePolygonPoints = exports.ensureVectorPoint = exports.createBox = exports.createEllipse = exports.rad2deg = exports.deg2rad = exports.RAD2DEG = exports.DEG2RAD = void 0; +exports.bin2dec = exports.returnTrue = exports.cloneResponse = exports.drawBVH = exports.drawPolygon = exports.dashLineTo = exports.getSATTest = exports.getBounceDirection = exports.mapArrayToVector = exports.mapVectorToArray = exports.clonePointsArray = exports.checkAInB = exports.areSameGroup = exports.intersectAABB = exports.notIntersectAABB = exports.bodyMoved = exports.extendBody = exports.clockwise = exports.distance = exports.ensurePolygonPoints = exports.ensureVectorPoint = exports.createBox = exports.createEllipse = exports.rad2deg = exports.deg2rad = exports.RAD2DEG = exports.DEG2RAD = void 0; const sat_1 = __webpack_require__(/*! sat */ "./node_modules/sat/SAT.js"); const intersect_1 = __webpack_require__(/*! ./intersect */ "./src/intersect.ts"); const model_1 = __webpack_require__(/*! ./model */ "./src/model.ts"); @@ -3917,6 +3928,13 @@ function returnTrue() { return true; } exports.returnTrue = returnTrue; +/** + * binary string to decimal number + */ +function bin2dec(binary) { + return Number(`0b${binary}`.replace(/\s/g, "")); +} +exports.bin2dec = bin2dec; /***/ }), @@ -4030,34 +4048,34 @@ module.exports.height = height; \****************************/ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { -const { BodyGroup } = __webpack_require__(/*! ../model */ "./src/model.ts") -const { System } = __webpack_require__(/*! ../system */ "./src/system.ts") -const { getBounceDirection } = __webpack_require__(/*! ../utils */ "./src/utils.ts") -const { width, height, loop } = __webpack_require__(/*! ./canvas */ "./src/demo/canvas.js") -const seededRandom = (__webpack_require__(/*! random-seed */ "./node_modules/random-seed/index.js").create)("@Prozi").random +const { BodyGroup } = __webpack_require__(/*! ../model */ "./src/model.ts"); +const { System } = __webpack_require__(/*! ../system */ "./src/system.ts"); +const { getBounceDirection } = __webpack_require__(/*! ../utils */ "./src/utils.ts"); +const { width, height, loop } = __webpack_require__(/*! ./canvas */ "./src/demo/canvas.js"); +const seededRandom = (__webpack_require__(/*! random-seed */ "./node_modules/random-seed/index.js").create)("@Prozi").random; function random(min, max) { - return Math.floor(seededRandom() * max) + min + return Math.floor(seededRandom() * max) + min; } class Stress { constructor(count = 2000) { - this.size = Math.sqrt((width * height) / (count * 50)) - - this.physics = new System(5) - this.bodies = [] - this.polygons = 0 - this.boxes = 0 - this.circles = 0 - this.ellipses = 0 - this.lines = 0 - this.lastVariant = 0 - this.count = count - this.bounds = this.getBounds() - this.enableFiltering = false + this.size = Math.sqrt((width * height) / (count * 50)); + + this.physics = new System(5); + this.bodies = []; + this.polygons = 0; + this.boxes = 0; + this.circles = 0; + this.ellipses = 0; + this.lines = 0; + this.lastVariant = 0; + this.count = count; + this.bounds = this.getBounds(); + this.enableFiltering = false; for (let i = 0; i < count; ++i) { - this.createShape(!random(0, 20)) + this.createShape(!random(0, 20)); } this.legend = `- re-insert body into collision tree and update its aabb every + re-insert body into collision tree and update its bbox every body can be part of only one system
callback all bodies in area
+