Skip to content

Commit

Permalink
feat: new version with body.move() and updated dependencies and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Prozi committed Jul 15, 2024
1 parent 1da9ea2 commit 3e6c247
Show file tree
Hide file tree
Showing 147 changed files with 10,969 additions and 68,854 deletions.
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,34 @@ system.insert(box2);
Manipulate body attributes and update the collision system:

```ts
box.setPosition(x, y);
box.setScale(scaleX, scaleY);
box.setAngle(angle);
box.setOffset({ x, y });
system.update(); // Update the system after manipulation
box.group = group; // Immediate effect, no system.update needed
// if omitted updateNow is true
const updateNow = false;

// teleport
box.setPosition(x, y, updateNow);
box.setScale(scaleX, scaleY, updateNow);
box.setAngle(angle, updateNow);
box.move(1, updateNow);
box.setOffset({ x, y }, updateNow);
console.log(box.dirty); // true

box.updateBody(); // Update the body once, when all manipulations are done
console.log(box.dirty); // false

box.group = group; // Immediate effect, no body/system update needed
console.log(box.dirty); // false
```

### Step 5: Collision Detection and Resolution

Detect collisions and respond accordingly:

```ts
if (system.checkAll()) {
if (system.checkAll(callback)) {
// Do something yourself
}

if (system.checkOne(body, callback)) {
// Do something yourself
}

Expand Down
8 changes: 4 additions & 4 deletions dist/base-system.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Body, BodyOptions, ChildrenData, Data, InTest, Leaf, PotentialVector, RBush, TraverseFunction, Vector } from "./model";
import { Box } from "./bodies/box";
import { Circle } from "./bodies/circle";
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, InTest, Leaf, PotentialVector, RBush, TraverseFunction, Vector } from "./model";
/**
* very base collision system (create, insert, update, draw, remove)
*/
export declare class BaseSystem<TBody extends Body = Body> extends RBush<TBody> implements Data<TBody> {
export declare class BaseSystem<TBody extends Body = Body> extends RBush implements Data<TBody> {
data: ChildrenData<TBody>;
/**
* create point at position with options and add to system
Expand Down Expand Up @@ -38,7 +38,7 @@ export declare class BaseSystem<TBody extends Body = Body> extends RBush<TBody>
* re-insert body into collision tree and update its bbox
* every body can be part of only one system
*/
insert(body: TBody): RBush<TBody>;
insert(body: TBody): this;
/**
* updates body in collision tree
*/
Expand All @@ -58,7 +58,7 @@ export declare class BaseSystem<TBody extends Body = Body> extends RBush<TBody>
/**
* remove body aabb from collision tree
*/
remove(body: TBody, equals?: InTest<TBody>): RBush<TBody>;
remove(body: TBody, equals?: InTest<TBody>): this;
/**
* get object potential colliders
* @deprecated because it's slower to use than checkOne() or checkAll()
Expand Down
6 changes: 3 additions & 3 deletions dist/base-system.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseSystem = void 0;
const model_1 = require("./model");
const utils_1 = require("./utils");
const optimized_1 = require("./optimized");
const box_1 = require("./bodies/box");
const circle_1 = require("./bodies/circle");
const ellipse_1 = require("./bodies/ellipse");
const line_1 = require("./bodies/line");
const point_1 = require("./bodies/point");
const polygon_1 = require("./bodies/polygon");
const model_1 = require("./model");
const optimized_1 = require("./optimized");
const utils_1 = require("./utils");
/**
* very base collision system (create, insert, update, draw, remove)
*/
Expand Down
60 changes: 30 additions & 30 deletions dist/benchmarks/insertion.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.insertionBenchmark = void 0;
/* tslint:disable:no-implicit-dependencies */
const tinybench_1 = require("tinybench");
const circle_1 = require("../bodies/circle");
const polygon_1 = require("../bodies/polygon");
const model_1 = require("../model");
const system_1 = require("../system");
const circle_js_1 = require("../bodies/circle.js");
const polygon_js_1 = require("../bodies/polygon.js");
const model_js_1 = require("../model.js");
const system_js_1 = require("../system.js");
const insertionBenchmark = () => {
const benchmark = new tinybench_1.Bench({});
const nonoverlappingBodies = [];
Expand All @@ -17,64 +17,64 @@ const insertionBenchmark = () => {
const overlappingRectangles = [];
const BODY_COUNT = 1000;
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
nonoverlappingBodies.push(new circle_1.Circle(new model_1.SATVector(ndx, 0), 0.25));
overlappingBodies.push(new circle_1.Circle(new model_1.SATVector(0, 0), 0.25));
nonoverlappingTriangles.push(new polygon_1.Polygon(new model_1.SATVector(ndx * 2, 0), [
new model_1.SATVector(0, 0),
new model_1.SATVector(0, 1),
new model_1.SATVector(1, 0)
nonoverlappingBodies.push(new circle_js_1.Circle(new model_js_1.SATVector(ndx, 0), 0.25));
overlappingBodies.push(new circle_js_1.Circle(new model_js_1.SATVector(0, 0), 0.25));
nonoverlappingTriangles.push(new polygon_js_1.Polygon(new model_js_1.SATVector(ndx * 2, 0), [
new model_js_1.SATVector(0, 0),
new model_js_1.SATVector(0, 1),
new model_js_1.SATVector(1, 0)
]));
overlappingTriangles.push(new polygon_1.Polygon(new model_1.SATVector(0, 0), [
new model_1.SATVector(0, 0),
new model_1.SATVector(0, 1),
new model_1.SATVector(1, 0)
overlappingTriangles.push(new polygon_js_1.Polygon(new model_js_1.SATVector(0, 0), [
new model_js_1.SATVector(0, 0),
new model_js_1.SATVector(0, 1),
new model_js_1.SATVector(1, 0)
]));
nonoverlappingRectangles.push(new polygon_1.Polygon(new model_1.SATVector(0, 0), [
new model_1.SATVector(0, 0),
new model_1.SATVector(0, 1),
new model_1.SATVector(1, 1),
new model_1.SATVector(1, 0)
nonoverlappingRectangles.push(new polygon_js_1.Polygon(new model_js_1.SATVector(0, 0), [
new model_js_1.SATVector(0, 0),
new model_js_1.SATVector(0, 1),
new model_js_1.SATVector(1, 1),
new model_js_1.SATVector(1, 0)
]));
overlappingRectangles.push(new polygon_1.Polygon(new model_1.SATVector(0, 0), [
new model_1.SATVector(0, 0),
new model_1.SATVector(0, 1),
new model_1.SATVector(1, 1),
new model_1.SATVector(1, 0)
overlappingRectangles.push(new polygon_js_1.Polygon(new model_js_1.SATVector(0, 0), [
new model_js_1.SATVector(0, 0),
new model_js_1.SATVector(0, 1),
new model_js_1.SATVector(1, 1),
new model_js_1.SATVector(1, 0)
]));
}
benchmark
.add("non overlapping circles", () => {
const uut = new system_1.System(BODY_COUNT);
const uut = new system_js_1.System(BODY_COUNT);
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
uut.insert(nonoverlappingBodies[ndx]);
}
})
.add("overlapping circles", () => {
const uut = new system_1.System(BODY_COUNT);
const uut = new system_js_1.System(BODY_COUNT);
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
uut.insert(overlappingBodies[ndx]);
}
})
.add("non-overlapping triangles", () => {
const uut = new system_1.System(BODY_COUNT);
const uut = new system_js_1.System(BODY_COUNT);
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
uut.insert(nonoverlappingTriangles[ndx]);
}
})
.add("overlapping triangles", () => {
const uut = new system_1.System(BODY_COUNT);
const uut = new system_js_1.System(BODY_COUNT);
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
uut.insert(overlappingTriangles[ndx]);
}
})
.add("non-overlapping quad", () => {
const uut = new system_1.System(BODY_COUNT);
const uut = new system_js_1.System(BODY_COUNT);
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
uut.insert(nonoverlappingRectangles[ndx]);
}
})
.add("overlapping quad", () => {
const uut = new system_1.System(BODY_COUNT);
const uut = new system_js_1.System(BODY_COUNT);
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
uut.insert(overlappingRectangles[ndx]);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/benchmarks/stress.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports.stressBenchmark = void 0;
/* tslint:disable:no-implicit-dependencies variable-name no-any */
const tinybench_1 = require("tinybench");
const stressBenchmark = () => __awaiter(void 0, void 0, void 0, function* () {
const { default: Stress } = yield Promise.resolve().then(() => __importStar(require("../demo/stress")));
const { default: Stress } = yield Promise.resolve().then(() => __importStar(require("../demo/stress.js")));
let stressTest;
const benchmark = new tinybench_1.Bench({
time: 1000,
Expand Down
2 changes: 1 addition & 1 deletion dist/bodies/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.Box = void 0;
const model_1 = require("../model");
const utils_1 = require("../utils");
const polygon_1 = require("./polygon");
const utils_1 = require("../utils");
/**
* collider - box
*/
Expand Down
21 changes: 12 additions & 9 deletions dist/bodies/circle.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BBox } from "rbush";
import { BBox, BodyGroup, BodyOptions, BodyProps, BodyType, PotentialVector, SATVector, Vector } from "../model";
import { Circle as SATCircle } from "sat";
import { System } from "../system";
import { BodyGroup, BodyOptions, BodyProps, BodyType, PotentialVector, SATVector, Vector } from "../model";
/**
* collider - circle
*/
Expand Down Expand Up @@ -121,21 +120,25 @@ export declare class Circle extends SATCircle implements BBox, BodyProps {
get group(): number;
set group(group: number);
/**
* update position
* update position BY MOVING FORWARD IN ANGLE DIRECTION
*/
setPosition(x: number, y: number, update?: boolean): Circle;
move(speed?: number, updateNow?: boolean): Circle;
/**
* update position BY TELEPORTING
*/
setPosition(x: number, y: number, updateNow?: boolean): Circle;
/**
* update scale
*/
setScale(scaleX: number, _scaleY?: number, update?: boolean): Circle;
setScale(scaleX: number, _scaleY?: number, updateNow?: boolean): Circle;
/**
* set rotation
*/
setAngle(angle: number, update?: boolean): Circle;
setAngle(angle: number, updateNow?: boolean): Circle;
/**
* set offset from center
*/
setOffset(offset: Vector, update?: boolean): Circle;
setOffset(offset: Vector, updateNow?: boolean): Circle;
/**
* get body bounding box, without padding
*/
Expand All @@ -151,11 +154,11 @@ export declare class Circle extends SATCircle implements BBox, BodyProps {
/**
* inner function for after position change update aabb in system
*/
updateBody(update?: boolean): void;
updateBody(updateNow?: boolean): void;
/**
* update instantly or mark as dirty
*/
protected markAsDirty(update?: boolean): void;
protected markAsDirty(updateNow?: boolean): void;
/**
* internal for getting offset with applied angle
*/
Expand Down
35 changes: 21 additions & 14 deletions dist/bodies/circle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Circle = void 0;
const sat_1 = require("sat");
const model_1 = require("../model");
const utils_1 = require("../utils");
const sat_1 = require("sat");
/**
* collider - circle
*/
Expand Down Expand Up @@ -100,43 +100,50 @@ class Circle extends sat_1.Circle {
this._group = (0, utils_1.getGroup)(group);
}
/**
* update position
* update position BY MOVING FORWARD IN ANGLE DIRECTION
*/
move(speed = 1, updateNow = true) {
(0, utils_1.move)(this, speed, updateNow);
return this;
}
/**
* update position BY TELEPORTING
*/
setPosition(x, y, update = true) {
setPosition(x, y, updateNow = true) {
this.pos.x = x;
this.pos.y = y;
this.markAsDirty(update);
this.markAsDirty(updateNow);
return this;
}
/**
* update scale
*/
setScale(scaleX, _scaleY = scaleX, update = true) {
setScale(scaleX, _scaleY = scaleX, updateNow = true) {
this.r = this.unscaledRadius * Math.abs(scaleX);
this.markAsDirty(update);
this.markAsDirty(updateNow);
return this;
}
/**
* set rotation
*/
setAngle(angle, update = true) {
setAngle(angle, updateNow = true) {
this.angle = angle;
const { x, y } = this.getOffsetWithAngle();
this.offset.x = x;
this.offset.y = y;
this.markAsDirty(update);
this.markAsDirty(updateNow);
return this;
}
/**
* set offset from center
*/
setOffset(offset, update = true) {
setOffset(offset, updateNow = true) {
this.offsetCopy.x = offset.x;
this.offsetCopy.y = offset.y;
const { x, y } = this.getOffsetWithAngle();
this.offset.x = x;
this.offset.y = y;
this.markAsDirty(update);
this.markAsDirty(updateNow);
return this;
}
/**
Expand Down Expand Up @@ -185,18 +192,18 @@ class Circle extends sat_1.Circle {
/**
* inner function for after position change update aabb in system
*/
updateBody(update = this.dirty) {
updateBody(updateNow = this.dirty) {
var _a;
if (update) {
if (updateNow) {
(_a = this.system) === null || _a === void 0 ? void 0 : _a.insert(this);
this.dirty = false;
}
}
/**
* update instantly or mark as dirty
*/
markAsDirty(update = false) {
if (update) {
markAsDirty(updateNow = false) {
if (updateNow) {
this.updateBody(true);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion dist/bodies/ellipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ellipse = void 0;
const model_1 = require("../model");
const utils_1 = require("../utils");
const polygon_1 = require("./polygon");
const utils_1 = require("../utils");
/**
* collider - ellipse
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/bodies/line.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Vector as SATVector } from "sat";
import { BodyGroup, BodyOptions, BodyType, Vector } from "../model";
import { Polygon } from "./polygon";
import { Vector as SATVector } from "sat";
/**
* collider - line
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/bodies/line.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Line = void 0;
const sat_1 = require("sat");
const model_1 = require("../model");
const polygon_1 = require("./polygon");
const sat_1 = require("sat");
/**
* collider - line
*/
Expand Down
Loading

0 comments on commit 3e6c247

Please sign in to comment.