Skip to content

Commit 82ae2a7

Browse files
committed
fix: issue #3
1 parent e4095c5 commit 82ae2a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+3308
-3294
lines changed

.github/workflows/benchmark.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ on:
44
push:
55
branches: [master, develop]
66
paths:
7-
- "src/**"
8-
- ".github/workflows/benchmarks.yml"
7+
- 'src/**'
8+
- '.github/workflows/benchmarks.yml'
99
pull_request:
1010
branches: [master, develop]
1111
paths:
12-
- "src/**"
13-
- ".github/workflows/benchmarks.yml"
12+
- 'src/**'
13+
- '.github/workflows/benchmarks.yml'
1414
schedule:
15-
- cron: "0 1 * * 1,3,5"
15+
- cron: '0 1 * * 1,3,5'
1616

1717
jobs:
1818
bench:
19-
name: "Run Benchmarks"
19+
name: 'Run Benchmarks'
2020
runs-on: ubuntu-latest
2121

2222
defaults:

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "none",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

README.md

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ For detailed documentation on the library's API, refer to the following link:
3434
Initialize a unique collision system using Detect-Collisions:
3535

3636
```ts
37-
const { System } = require("detect-collisions");
38-
const system = new System();
37+
const { System } = require('detect-collisions')
38+
const system = new System()
3939
```
4040

4141
### Step 2: Understand Body Attributes
@@ -62,15 +62,15 @@ const {
6262
Ellipse,
6363
Line,
6464
Point,
65-
Polygon,
66-
} = require("detect-collisions");
65+
Polygon
66+
} = require('detect-collisions')
6767

6868
// Example: Create and insert box1 body
69-
const box1 = system.createBox(position, width, height, options);
69+
const box1 = system.createBox(position, width, height, options)
7070
// Example: Create box2 body
71-
const box2 = new Box(position, width, height, options);
71+
const box2 = new Box(position, width, height, options)
7272
// Example: Insert box2 body
73-
system.insert(box2);
73+
system.insert(box2)
7474
```
7575

7676
### Step 4: Manipulate Bodies
@@ -79,23 +79,23 @@ Manipulate body attributes and update the collision system:
7979

8080
```ts
8181
// if omitted updateNow is true
82-
const updateNow = false;
82+
const updateNow = false
8383
// this should be time scaled, 1 for example
84-
const speed = 1;
84+
const speed = 1
8585

8686
// teleport
87-
box.setPosition(x, y, updateNow);
88-
box.setScale(scaleX, scaleY, updateNow);
89-
box.setAngle(angle, updateNow);
90-
box.move(speed, updateNow);
91-
box.setOffset({ x, y }, updateNow);
92-
console.log(box.dirty); // true
93-
94-
box.updateBody(); // Update the body once, when all manipulations are done
95-
console.log(box.dirty); // false
96-
97-
box.group = group; // Immediate effect, no body/system update needed
98-
console.log(box.dirty); // false
87+
box.setPosition(x, y, updateNow)
88+
box.setScale(scaleX, scaleY, updateNow)
89+
box.setAngle(angle, updateNow)
90+
box.move(speed, updateNow)
91+
box.setOffset({ x, y }, updateNow)
92+
console.log(box.dirty) // true
93+
94+
box.updateBody() // Update the body once, when all manipulations are done
95+
console.log(box.dirty) // false
96+
97+
box.group = group // Immediate effect, no body/system update needed
98+
console.log(box.dirty) // false
9999
```
100100

101101
### Step 5: Collision Detection and Resolution
@@ -104,7 +104,7 @@ Detect collisions and respond accordingly:
104104

105105
```ts
106106
const callback = (result) => {
107-
console.info(result);
107+
console.info(result)
108108
}
109109

110110
if (system.checkAll(callback)) {
@@ -116,13 +116,13 @@ if (system.checkOne(body, callback)) {
116116
}
117117

118118
// Or separate bodies based on isStatic/isTrigger
119-
system.separate();
119+
system.separate()
120120
```
121121

122122
Get exact collision points:
123123

124124
```ts
125-
const { a, b } = result;
125+
const { a, b } = result
126126
const points = system.getCollisionPoints(a, b)
127127
```
128128

@@ -131,7 +131,7 @@ const points = system.getCollisionPoints(a, b)
131131
Remove bodies when they're no longer needed:
132132

133133
```ts
134-
system.remove(body);
134+
system.remove(body)
135135
```
136136

137137
And that's it! You're now ready to utilize the Detect-Collisions library in your project.
@@ -141,46 +141,46 @@ And that's it! You're now ready to utilize the Detect-Collisions library in your
141141
To facilitate debugging, Detect-Collisions allows you to visually represent the collision bodies. By invoking the `draw()` method and supplying a 2D context of a `<canvas>` element, you can draw all the bodies within a collision system. You can also opt to draw individual bodies.
142142

143143
```ts
144-
const canvas = document.createElement("canvas");
145-
const context = canvas.getContext("2d");
144+
const canvas = document.createElement('canvas')
145+
const context = canvas.getContext('2d')
146146

147-
context.strokeStyle = "#FFFFFF";
148-
context.beginPath();
147+
context.strokeStyle = '#FFFFFF'
148+
context.beginPath()
149149
// draw specific body
150-
body.draw(context);
150+
body.draw(context)
151151
// draw whole system
152-
system.draw(context);
153-
context.stroke();
152+
system.draw(context)
153+
context.stroke()
154154
```
155155

156156
To assess the [Bounding Volume Hierarchy](https://en.wikipedia.org/wiki/Bounding_volume_hierarchy), you can draw the BVH.
157157

158158
```ts
159-
context.strokeStyle = "#FFFFFF";
160-
context.beginPath();
159+
context.strokeStyle = '#FFFFFF'
160+
context.beginPath()
161161
// draw specific body bounding box
162-
body.drawBVH(context);
162+
body.drawBVH(context)
163163
// draw bounding volume hierarchy of the system
164-
system.drawBVH(context);
165-
context.stroke();
164+
system.drawBVH(context)
165+
context.stroke()
166166
```
167167

168168
## Raycasting
169169

170170
Detect-Collisions provides the functionality to gather raycast data. Here's how:
171171

172172
```ts
173-
const start = { x: 0, y: 0 };
174-
const end = { x: 0, y: -10 };
173+
const start = { x: 0, y: 0 }
174+
const end = { x: 0, y: -10 }
175175
const hit = system.raycast(start, end, (body, ray) => {
176176
// if you don't want the body to be hit by raycast return false
177-
return true;
178-
});
177+
return true
178+
})
179179

180180
if (hit) {
181-
const { point, body } = hit;
181+
const { point, body } = hit
182182

183-
console.log({ point, body });
183+
console.log({ point, body })
184184
}
185185
```
186186

@@ -189,7 +189,7 @@ In this example, `point` is a `Vector` with the coordinates of the nearest inter
189189
## Usage in Browsers
190190

191191
```js
192-
import { System } from "https://esm.sh/detect-collisions";
192+
import { System } from 'https://esm.sh/detect-collisions'
193193
```
194194

195195
## Contributing to the Project

dist/base-system.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Body, BodyOptions, ChildrenData, Data, InTest, Leaf, PotentialVector, RBush, TraverseFunction, Vector } from "./model";
2-
import { Box, BoxConstructor } from "./bodies/box";
3-
import { Circle, CircleConstructor } from "./bodies/circle";
4-
import { Ellipse, EllipseConstructor } from "./bodies/ellipse";
5-
import { Line, LineConstructor } from "./bodies/line";
6-
import { Point, PointConstructor } from "./bodies/point";
7-
import { Polygon, PolygonConstructor } from "./bodies/polygon";
1+
import { Body, BodyOptions, ChildrenData, Data, InTest, Leaf, PotentialVector, RBush, TraverseFunction, Vector } from './model';
2+
import { Box, BoxConstructor } from './bodies/box';
3+
import { Circle, CircleConstructor } from './bodies/circle';
4+
import { Ellipse, EllipseConstructor } from './bodies/ellipse';
5+
import { Line, LineConstructor } from './bodies/line';
6+
import { Point, PointConstructor } from './bodies/point';
7+
import { Polygon, PolygonConstructor } from './bodies/polygon';
88
/**
99
* very base collision system (create, insert, update, draw, remove)
1010
*/

dist/benchmarks/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { insertionBenchmark } from "./insertion.bench";
2-
export { stressBenchmark } from "./stress.bench";
1+
export { insertionBenchmark } from './insertion.bench';
2+
export { stressBenchmark } from './stress.bench';

dist/benchmarks/insertion.bench.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,58 +22,58 @@ const insertionBenchmark = () => {
2222
nonoverlappingTriangles.push(new polygon_js_1.Polygon(new model_js_1.SATVector(ndx * 2, 0), [
2323
new model_js_1.SATVector(0, 0),
2424
new model_js_1.SATVector(0, 1),
25-
new model_js_1.SATVector(1, 0),
25+
new model_js_1.SATVector(1, 0)
2626
]));
2727
overlappingTriangles.push(new polygon_js_1.Polygon(new model_js_1.SATVector(0, 0), [
2828
new model_js_1.SATVector(0, 0),
2929
new model_js_1.SATVector(0, 1),
30-
new model_js_1.SATVector(1, 0),
30+
new model_js_1.SATVector(1, 0)
3131
]));
3232
nonoverlappingRectangles.push(new polygon_js_1.Polygon(new model_js_1.SATVector(0, 0), [
3333
new model_js_1.SATVector(0, 0),
3434
new model_js_1.SATVector(0, 1),
3535
new model_js_1.SATVector(1, 1),
36-
new model_js_1.SATVector(1, 0),
36+
new model_js_1.SATVector(1, 0)
3737
]));
3838
overlappingRectangles.push(new polygon_js_1.Polygon(new model_js_1.SATVector(0, 0), [
3939
new model_js_1.SATVector(0, 0),
4040
new model_js_1.SATVector(0, 1),
4141
new model_js_1.SATVector(1, 1),
42-
new model_js_1.SATVector(1, 0),
42+
new model_js_1.SATVector(1, 0)
4343
]));
4444
}
4545
benchmark
46-
.add("non overlapping circles", () => {
46+
.add('non overlapping circles', () => {
4747
const uut = new system_js_1.System(BODY_COUNT);
4848
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
4949
uut.insert(nonoverlappingBodies[ndx]);
5050
}
5151
})
52-
.add("overlapping circles", () => {
52+
.add('overlapping circles', () => {
5353
const uut = new system_js_1.System(BODY_COUNT);
5454
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
5555
uut.insert(overlappingBodies[ndx]);
5656
}
5757
})
58-
.add("non-overlapping triangles", () => {
58+
.add('non-overlapping triangles', () => {
5959
const uut = new system_js_1.System(BODY_COUNT);
6060
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
6161
uut.insert(nonoverlappingTriangles[ndx]);
6262
}
6363
})
64-
.add("overlapping triangles", () => {
64+
.add('overlapping triangles', () => {
6565
const uut = new system_js_1.System(BODY_COUNT);
6666
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
6767
uut.insert(overlappingTriangles[ndx]);
6868
}
6969
})
70-
.add("non-overlapping quad", () => {
70+
.add('non-overlapping quad', () => {
7171
const uut = new system_js_1.System(BODY_COUNT);
7272
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
7373
uut.insert(nonoverlappingRectangles[ndx]);
7474
}
7575
})
76-
.add("overlapping quad", () => {
76+
.add('overlapping quad', () => {
7777
const uut = new system_js_1.System(BODY_COUNT);
7878
for (let ndx = 0; ndx < BODY_COUNT; ndx++) {
7979
uut.insert(overlappingRectangles[ndx]);
@@ -85,16 +85,16 @@ const insertionBenchmark = () => {
8585
console.table(benchmark.tasks.map(({ name, result }) => {
8686
var _a, _b, _c, _d, _e;
8787
return ({
88-
"Task Name": name,
89-
"Average Time (s)": parseFloat(((_a = result === null || result === void 0 ? void 0 : result.mean) !== null && _a !== void 0 ? _a : 0).toFixed(3)),
90-
"Standard Deviation (s)": parseFloat(((_b = result === null || result === void 0 ? void 0 : result.sd) !== null && _b !== void 0 ? _b : 0).toFixed(3)),
88+
'Task Name': name,
89+
'Average Time (s)': parseFloat(((_a = result === null || result === void 0 ? void 0 : result.mean) !== null && _a !== void 0 ? _a : 0).toFixed(3)),
90+
'Standard Deviation (s)': parseFloat(((_b = result === null || result === void 0 ? void 0 : result.sd) !== null && _b !== void 0 ? _b : 0).toFixed(3)),
9191
hz: parseFloat(((_c = result === null || result === void 0 ? void 0 : result.hz) !== null && _c !== void 0 ? _c : 0).toFixed(3)),
92-
"p99 (s)": parseFloat(((_d = result === null || result === void 0 ? void 0 : result.p99) !== null && _d !== void 0 ? _d : 0).toFixed(3)),
93-
"p995 (s)": parseFloat(((_e = result === null || result === void 0 ? void 0 : result.p995) !== null && _e !== void 0 ? _e : 0).toFixed(3)),
92+
'p99 (s)': parseFloat(((_d = result === null || result === void 0 ? void 0 : result.p99) !== null && _d !== void 0 ? _d : 0).toFixed(3)),
93+
'p995 (s)': parseFloat(((_e = result === null || result === void 0 ? void 0 : result.p995) !== null && _e !== void 0 ? _e : 0).toFixed(3))
9494
});
9595
}));
9696
})
97-
.catch(err => {
97+
.catch((err) => {
9898
console.warn(err.message || err);
9999
});
100100
};

dist/benchmarks/stress.bench.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const stressBenchmark = () => __awaiter(void 0, void 0, void 0, function* () {
2121
let stressTest;
2222
const benchmark = new tinybench_1.Bench({
2323
time: 1000,
24-
warmupIterations: 0,
24+
warmupIterations: 0
2525
});
2626
amounts.forEach((items) => {
2727
benchmark.add(`stress test, items=${items}`, () => {
@@ -33,7 +33,7 @@ const stressBenchmark = () => __awaiter(void 0, void 0, void 0, function* () {
3333
},
3434
afterEach: () => {
3535
stressTest.physics.clear();
36-
},
36+
}
3737
});
3838
});
3939
yield benchmark.run();

dist/bodies/box.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { BodyGroup, BodyOptions, BodyType, PotentialVector } from "../model";
2-
import { Polygon } from "./polygon";
1+
import { BodyGroup, BodyOptions, BodyType, PotentialVector } from '../model';
2+
import { Polygon } from './polygon';
33
export interface BoxConstructor<TBox extends Box> {
44
new (position: PotentialVector, width: number, height: number, options?: BodyOptions): TBox;
55
}

dist/bodies/circle.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { BBox, BodyGroup, BodyOptions, BodyProps, BodyType, PotentialVector, SATCircle, SATVector, Vector } from "../model";
2-
import { System } from "../system";
1+
import { BBox, BodyGroup, BodyOptions, BodyProps, BodyType, PotentialVector, SATCircle, SATVector, Vector } from '../model';
2+
import { System } from '../system';
33
export interface CircleConstructor<TCircle extends Circle> {
44
new (position: PotentialVector, radius: number, options?: BodyOptions): TCircle;
55
}
@@ -62,7 +62,7 @@ export declare class Circle<UserDataType = any> extends SATCircle implements BBo
6262
/**
6363
* allows the user to set any misc data for client use
6464
*/
65-
userData?: BodyProps<UserDataType>["userData"];
65+
userData?: BodyProps<UserDataType>['userData'];
6666
readonly isConvex = true;
6767
/**
6868
* circle type

dist/bodies/circle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class Circle extends model_1.SATCircle {
154154
minX: x - this.r,
155155
maxX: x + this.r,
156156
minY: y - this.r,
157-
maxY: y + this.r,
157+
maxY: y + this.r
158158
};
159159
}
160160
/**

0 commit comments

Comments
 (0)