rails-physics 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +13 -9
- package/package.json +2 -3
- package/ranges/segmenttree.js +7 -4
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
import {SegmentTree} from "./ranges/segmenttree.js";
|
|
4
|
+
import Matter from "matter-js";
|
|
4
5
|
|
|
5
6
|
const Rails = {
|
|
6
7
|
name: "rails",
|
|
@@ -20,11 +21,16 @@ const Rails = {
|
|
|
20
21
|
Body: {
|
|
21
22
|
createRail: function(body) {
|
|
22
23
|
if(body.plugin.rails_granularity !== undefined) {
|
|
23
|
-
let
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
let transverse_axis = Matter.Vector.create(1, 0);
|
|
25
|
+
let slide_axis = Matter.Vector.create(0, 1);
|
|
26
|
+
if(body.plugin.transverse_axis !== undefined) {
|
|
27
|
+
transverse_axis = body.plugin.transverse_axis;
|
|
28
|
+
}
|
|
29
|
+
if(body.plugin.slide_axis !== undefined) {
|
|
30
|
+
slide_axis = body.plugin.slide_axis;
|
|
26
31
|
}
|
|
27
|
-
body.plugin.centre_finder = new SegmentTree(this, this.rails_granularity,
|
|
32
|
+
body.plugin.centre_finder = new SegmentTree(this, this.rails_granularity, transverse_axis, slide_axis);
|
|
33
|
+
body.plugin.old_center = Matter.Vector.rotate(body.plugin.centre_finder.centerOfMass(), body.angle);
|
|
28
34
|
}
|
|
29
35
|
},
|
|
30
36
|
|
|
@@ -39,6 +45,4 @@ const Rails = {
|
|
|
39
45
|
|
|
40
46
|
};
|
|
41
47
|
|
|
42
|
-
Matter.Plugin.register(Rails);
|
|
43
|
-
|
|
44
|
-
module.exports = Rails;
|
|
48
|
+
Matter.Plugin.register(Rails);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rails-physics",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "A plugin for the matter-js physics library designed to efficiently simulate objects moving in a one-dimensional space whose projection onto the 2-dimensional space of the screen changes over time",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -28,10 +28,9 @@
|
|
|
28
28
|
],
|
|
29
29
|
"directories": {
|
|
30
30
|
"test": "tests"
|
|
31
|
-
|
|
32
31
|
},
|
|
33
32
|
"scripts": {
|
|
34
|
-
"test": "node tests/SegmentTreeTest.js"
|
|
33
|
+
"test": "node tests/SegmentTreeTest.js & node index.js"
|
|
35
34
|
},
|
|
36
35
|
"devDependencies": {
|
|
37
36
|
"@types/matter-js": "^0.20.2"
|
package/ranges/segmenttree.js
CHANGED
|
@@ -11,14 +11,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
11
|
};
|
|
12
12
|
var _SegmentTree_raw;
|
|
13
13
|
import { center, leftChild, rightChild, isLeaf } from "./segment.js";
|
|
14
|
+
import Matter from "matter-js";
|
|
14
15
|
//Implement a segment tree for O(log(n)) updates and O(log(n)) location of center of mass
|
|
15
16
|
export class SegmentTree {
|
|
16
|
-
constructor(container, element_count,
|
|
17
|
+
constructor(container, element_count, transverse_axis, slide_axis) {
|
|
17
18
|
_SegmentTree_raw.set(this, void 0);
|
|
18
|
-
this.surrounding = container;
|
|
19
|
-
this.external_width = external_width;
|
|
20
19
|
this.element_count = element_count;
|
|
20
|
+
this.slide_axis = slide_axis;
|
|
21
|
+
this.transverse_axis = transverse_axis;
|
|
21
22
|
element_count = Math.abs(Math.round(element_count));
|
|
23
|
+
let default_density = container.density;
|
|
22
24
|
__classPrivateFieldSet(this, _SegmentTree_raw, [{ mass: default_density * element_count, left: 0, right: element_count, parent: -1 }], "f");
|
|
23
25
|
let index = 0;
|
|
24
26
|
while (index < __classPrivateFieldGet(this, _SegmentTree_raw, "f").length) {
|
|
@@ -51,7 +53,8 @@ export class SegmentTree {
|
|
|
51
53
|
center += (1.0 * target) / __classPrivateFieldGet(this, _SegmentTree_raw, "f")[index].mass;
|
|
52
54
|
}
|
|
53
55
|
console.log(target, __classPrivateFieldGet(this, _SegmentTree_raw, "f")[index].left, center, "!");
|
|
54
|
-
|
|
56
|
+
let result = Matter.Vector.mult(this.slide_axis, center / this.element_count);
|
|
57
|
+
return Matter.Vector.add(result, this.transverse_axis);
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
_SegmentTree_raw = new WeakMap();
|