@ue-too/dynamics 0.5.2 → 0.7.1

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.
Files changed (57) hide show
  1. package/README.md +7 -0
  2. package/benchmark.d.ts +47 -0
  3. package/collision-filter.d.ts +17 -0
  4. package/{.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/collision.d.ts → collision.d.ts} +23 -2
  5. package/dynamic-tree.d.ts +57 -0
  6. package/dynamics.tsbuildinfo +1 -0
  7. package/ecs-version/collision-system.d.ts +36 -0
  8. package/ecs-version/component.d.ts +40 -0
  9. package/ecs-version/index.d.ts +4 -0
  10. package/ecs-version/physics-system.d.ts +9 -0
  11. package/ecs-version/render-system.d.ts +8 -0
  12. package/index.d.ts +11 -0
  13. package/index.js +3165 -0
  14. package/index.js.map +1 -0
  15. package/package.json +10 -10
  16. package/pair-manager.d.ts +41 -0
  17. package/{dist/quadtree.d.ts → quadtree.d.ts} +6 -5
  18. package/{dist/rigidbody.d.ts → rigidbody.d.ts} +33 -4
  19. package/simple-benchmark-runner.d.ts +2 -0
  20. package/{.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/world.d.ts → world.d.ts} +20 -2
  21. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/collision.js +0 -330
  22. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/collision.js.map +0 -1
  23. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/constraint.js +0 -166
  24. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/constraint.js.map +0 -1
  25. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/dynamics.tsbuildinfo +0 -1
  26. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/index.d.ts +0 -3
  27. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/index.js +0 -4
  28. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/index.js.map +0 -1
  29. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/quadtree.d.ts +0 -26
  30. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/quadtree.js +0 -124
  31. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/quadtree.js.map +0 -1
  32. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/rigidbody.d.ts +0 -252
  33. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/rigidbody.js +0 -476
  34. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/rigidbody.js.map +0 -1
  35. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/world.js +0 -90
  36. package/.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/world.js.map +0 -1
  37. package/dist/collision.d.ts +0 -36
  38. package/dist/constraint.d.ts +0 -35
  39. package/dist/dynamics.tsbuildinfo +0 -1
  40. package/dist/index.d.ts +0 -3
  41. package/dist/index.js +0 -925
  42. package/dist/index.js.map +0 -1
  43. package/dist/package.json +0 -22
  44. package/dist/world.d.ts +0 -29
  45. package/jest.config.js +0 -18
  46. package/project.json +0 -33
  47. package/rollup.config.js +0 -20
  48. package/src/collision.ts +0 -359
  49. package/src/constraint.ts +0 -245
  50. package/src/index.ts +0 -3
  51. package/src/quadtree.ts +0 -145
  52. package/src/rigidbody.ts +0 -640
  53. package/src/world.ts +0 -116
  54. package/test/rigidbody.test.ts +0 -10
  55. package/tsconfig.json +0 -21
  56. package/tsconfig.spec.json +0 -12
  57. /package/{.rollup.cache/home/runner/work/ue-too/ue-too/packages/dynamics/dist/constraint.d.ts → constraint.d.ts} +0 -0
package/src/constraint.ts DELETED
@@ -1,245 +0,0 @@
1
- import { Point, PointCal } from "@ue-too/math";
2
- import { RigidBody } from "./rigidbody";
3
-
4
- export interface Constraint {
5
- enforce(dt: number): void;
6
- }
7
-
8
- export class FixedPinJoint implements Constraint {
9
-
10
- private anchorA: Point;
11
- private worldAnchorA: Point;
12
- private bodyA: RigidBody;
13
-
14
- constructor(bodyA: RigidBody, anchorA: Point, worldAnchorA: Point) {
15
- this.bodyA = bodyA;
16
- this.anchorA = anchorA;
17
- this.worldAnchorA = worldAnchorA;
18
- }
19
-
20
- enforce(dt: number): void {
21
- this.solveWorldPinJointConstraint(dt);
22
- }
23
-
24
- solveWorldPinJointConstraint(dt: number) {
25
- const body = this.bodyA;
26
- const localAnchor = this.anchorA;
27
- const worldAnchor = this.worldAnchorA;
28
-
29
- // Transform local anchor point to world space
30
- const worldAnchorOnBody = PointCal.addVector(body.center, PointCal.rotatePoint(localAnchor, body.orientationAngle));
31
-
32
- // Calculate the difference between the anchor points
33
- const diff = PointCal.subVector(worldAnchorOnBody, worldAnchor);
34
-
35
- // Calculate the relative velocity of the anchor point
36
- const r = PointCal.subVector(worldAnchorOnBody, body.center);
37
- const velocity = PointCal.addVector(
38
- body.linearVelocity,
39
- PointCal.crossProduct({x: 0, y: 0, z: body.angularVelocity}, r)
40
- );
41
-
42
- // Calculate the mass matrix
43
- const invMass = body.isStatic() ? 0 : 1 / body.mass;
44
- const invI = body.isStatic() ? 0 : 1 / body.momentOfInertia;
45
-
46
- const K = {
47
- x: invMass + invI * r.y * r.y,
48
- y: invMass + invI * r.x * r.x,
49
- xy: -invI * r.x * r.y
50
- };
51
-
52
- // Calculate the impulse
53
- const baumgarte = 1; // Baumgarte stabilization factor
54
- const impulse = {
55
- x: -K.x * diff.x - K.xy * diff.y - baumgarte * diff.x / dt - velocity.x,
56
- y: -K.xy * diff.x - K.y * diff.y - baumgarte * diff.y / dt - velocity.y
57
- };
58
-
59
- // Apply the impulse
60
- if (!body.isStatic()) {
61
- body.linearVelocity.x += invMass * impulse.x;
62
- body.linearVelocity.y += invMass * impulse.y;
63
- body.angularVelocity += invI * (r.x * impulse.y - r.y * impulse.x);
64
- }
65
- }
66
- }
67
-
68
- export class PinJoint implements Constraint {
69
-
70
- private anchorA: Point;
71
- private anchorB: Point;
72
- private bodyA: RigidBody;
73
- private bodyB: RigidBody;
74
-
75
- constructor(bodyA: RigidBody, bodyB: RigidBody, anchorA: Point, anchorB: Point) {
76
- this.bodyA = bodyA;
77
- this.bodyB = bodyB;
78
- this.anchorA = anchorA;
79
- this.anchorB = anchorB;
80
- }
81
-
82
- enforce(dt: number): void {
83
- this.solvePinJointConstraint(dt);
84
- }
85
-
86
- solvePinJointConstraint(dt: number) {
87
- const bodyA = this.bodyA;
88
- const bodyB = this.bodyB;
89
- const anchorA = this.anchorA;
90
- const anchorB = this.anchorB;
91
-
92
- // Transform local anchor points to world space
93
- const worldAnchorA = PointCal.addVector(bodyA.center, PointCal.rotatePoint(anchorA, bodyA.orientationAngle));
94
- const worldAnchorB = PointCal.addVector(bodyB.center, PointCal.rotatePoint(anchorB, bodyB.orientationAngle));
95
-
96
- // Calculate the difference between the two anchor points in world space
97
- const diff = PointCal.subVector(worldAnchorB, worldAnchorA);
98
-
99
- // Calculate the relative velocity of the anchor points
100
- const rA = PointCal.subVector(worldAnchorA, bodyA.center);
101
- const rB = PointCal.subVector(worldAnchorB, bodyB.center);
102
- const relativeVelocity = PointCal.subVector(
103
- PointCal.addVector(bodyB.linearVelocity, PointCal.crossProduct({x: 0, y: 0, z: bodyB.angularVelocity}, rB)),
104
- PointCal.addVector(bodyA.linearVelocity, PointCal.crossProduct({x: 0, y: 0, z: bodyA.angularVelocity}, rA))
105
- );
106
-
107
- // Calculate the mass matrix
108
- const invMassA = bodyA.isStatic() ? 0 : 1 / bodyA.mass;
109
- const invMassB = bodyB.isStatic() ? 0 : 1 / bodyB.mass;
110
- const invIA = bodyA.isStatic() ? 0 : 1 / bodyA.momentOfInertia;
111
- const invIB = bodyB.isStatic() ? 0 : 1 / bodyB.momentOfInertia;
112
-
113
- const K = {
114
- x: invMassA + invMassB + invIA * rA.y * rA.y + invIB * rB.y * rB.y,
115
- y: invMassA + invMassB + invIA * rA.x * rA.x + invIB * rB.x * rB.x,
116
- xy: -invIA * rA.x * rA.y - invIB * rB.x * rB.y
117
- };
118
-
119
- // Calculate the impulse
120
- const baumgarte = 1; // Baumgarte stabilization factor
121
- const impulse = {
122
- x: -K.x * diff.x - K.xy * diff.y - baumgarte * diff.x / dt - relativeVelocity.x,
123
- y: -K.xy * diff.x - K.y * diff.y - baumgarte * diff.y / dt - relativeVelocity.y
124
- };
125
-
126
- // Apply the impulse
127
- if (!bodyA.isStatic()) {
128
- bodyA.linearVelocity.x -= invMassA * impulse.x;
129
- bodyA.linearVelocity.y -= invMassA * impulse.y;
130
- bodyA.angularVelocity -= invIA * (rA.x * impulse.y - rA.y * impulse.x);
131
- }
132
-
133
- if (!bodyB.isStatic()) {
134
- bodyB.linearVelocity.x += invMassB * impulse.x;
135
- bodyB.linearVelocity.y += invMassB * impulse.y;
136
- bodyB.angularVelocity += invIB * (rB.x * impulse.y - rB.y * impulse.x);
137
- }
138
- }
139
-
140
- }
141
-
142
- export interface PinJointConstraint {
143
- bodyA: RigidBody;
144
- bodyB: RigidBody;
145
- anchorA: Point; // Local anchor point for bodyA
146
- anchorB: Point; // Local anchor point for bodyB
147
- }
148
-
149
- export function solvePinJointConstraint(constraint: PinJointConstraint, dt: number) {
150
- const { bodyA, bodyB, anchorA, anchorB } = constraint;
151
-
152
- // Transform local anchor points to world space
153
- const worldAnchorA = PointCal.addVector(bodyA.center, PointCal.rotatePoint(anchorA, bodyA.orientationAngle));
154
- const worldAnchorB = PointCal.addVector(bodyB.center, PointCal.rotatePoint(anchorB, bodyB.orientationAngle));
155
-
156
- // Calculate the difference between the two anchor points in world space
157
- const diff = PointCal.subVector(worldAnchorB, worldAnchorA);
158
-
159
- // Calculate the relative velocity of the anchor points
160
- const rA = PointCal.subVector(worldAnchorA, bodyA.center);
161
- const rB = PointCal.subVector(worldAnchorB, bodyB.center);
162
- const relativeVelocity = PointCal.subVector(
163
- PointCal.addVector(bodyB.linearVelocity, PointCal.crossProduct({x: 0, y: 0, z: bodyB.angularVelocity}, rB)),
164
- PointCal.addVector(bodyA.linearVelocity, PointCal.crossProduct({x: 0, y: 0, z: bodyA.angularVelocity}, rA))
165
- );
166
-
167
- // Calculate the mass matrix
168
- const invMassA = bodyA.isStatic() ? 0 : 1 / bodyA.mass;
169
- const invMassB = bodyB.isStatic() ? 0 : 1 / bodyB.mass;
170
- const invIA = bodyA.isStatic() ? 0 : 1 / bodyA.momentOfInertia;
171
- const invIB = bodyB.isStatic() ? 0 : 1 / bodyB.momentOfInertia;
172
-
173
- const K = {
174
- x: invMassA + invMassB + invIA * rA.y * rA.y + invIB * rB.y * rB.y,
175
- y: invMassA + invMassB + invIA * rA.x * rA.x + invIB * rB.x * rB.x,
176
- xy: -invIA * rA.x * rA.y - invIB * rB.x * rB.y
177
- };
178
-
179
- // Calculate the impulse
180
- const baumgarte = 0.5; // Baumgarte stabilization factor
181
- const impulse = {
182
- x: -K.x * diff.x - K.xy * diff.y - baumgarte * diff.x / dt - relativeVelocity.x,
183
- y: -K.xy * diff.x - K.y * diff.y - baumgarte * diff.y / dt - relativeVelocity.y
184
- };
185
-
186
- // Apply the impulse
187
- if (!bodyA.isStatic()) {
188
- bodyA.linearVelocity.x -= invMassA * impulse.x;
189
- bodyA.linearVelocity.y -= invMassA * impulse.y;
190
- bodyA.angularVelocity -= invIA * (rA.x * impulse.y - rA.y * impulse.x);
191
- }
192
-
193
- if (!bodyB.isStatic()) {
194
- bodyB.linearVelocity.x += invMassB * impulse.x;
195
- bodyB.linearVelocity.y += invMassB * impulse.y;
196
- bodyB.angularVelocity += invIB * (rB.x * impulse.y - rB.y * impulse.x);
197
- }
198
- }
199
-
200
- export interface WorldPinJointConstraint {
201
- body: RigidBody;
202
- localAnchor: Point; // Anchor point in body's local space
203
- worldAnchor: Point; // Fixed point in world space
204
- }
205
-
206
- export function solveWorldPinJointConstraint(constraint: WorldPinJointConstraint, dt: number) {
207
- const { body, localAnchor, worldAnchor } = constraint;
208
-
209
- // Transform local anchor point to world space
210
- const worldAnchorOnBody = PointCal.addVector(body.center, PointCal.rotatePoint(localAnchor, body.orientationAngle));
211
-
212
- // Calculate the difference between the anchor points
213
- const diff = PointCal.subVector(worldAnchorOnBody, worldAnchor);
214
-
215
- // Calculate the relative velocity of the anchor point
216
- const r = PointCal.subVector(worldAnchorOnBody, body.center);
217
- const velocity = PointCal.addVector(
218
- body.linearVelocity,
219
- PointCal.crossProduct({x: 0, y: 0, z: body.angularVelocity}, r)
220
- );
221
-
222
- // Calculate the mass matrix
223
- const invMass = body.isStatic() ? 0 : 1 / body.mass;
224
- const invI = body.isStatic() ? 0 : 1 / body.momentOfInertia;
225
-
226
- const K = {
227
- x: invMass + invI * r.y * r.y,
228
- y: invMass + invI * r.x * r.x,
229
- xy: -invI * r.x * r.y
230
- };
231
-
232
- // Calculate the impulse
233
- const baumgarte = 0.2; // Baumgarte stabilization factor
234
- const impulse = {
235
- x: -K.x * diff.x - K.xy * diff.y - baumgarte * diff.x / dt - velocity.x,
236
- y: -K.xy * diff.x - K.y * diff.y - baumgarte * diff.y / dt - velocity.y
237
- };
238
-
239
- // Apply the impulse
240
- if (!body.isStatic()) {
241
- body.linearVelocity.x += invMass * impulse.x;
242
- body.linearVelocity.y += invMass * impulse.y;
243
- body.angularVelocity += invI * (r.x * impulse.y - r.y * impulse.x);
244
- }
245
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./rigidbody";
2
- export * from "./quadtree";
3
- export * from "./collision";
package/src/quadtree.ts DELETED
@@ -1,145 +0,0 @@
1
- import { Point } from "@ue-too/math";
2
- import { RigidBody } from "./rigidbody";
3
-
4
- export class RectangleBound{
5
- private bottomLeft: Point;
6
- private width: number;
7
- private height: number;
8
-
9
- constructor(bottomLeft: Point, width: number, height: number){
10
- this.bottomLeft = bottomLeft;
11
- this.width = width;
12
- this.height = height;
13
- }
14
-
15
- getWidth(){
16
- return this.width;
17
- }
18
-
19
- getHeight(){
20
- return this.height;
21
- }
22
-
23
- getbottomLeft(){
24
- return this.bottomLeft;
25
- }
26
- }
27
-
28
-
29
- export class QuadTree {
30
- private MAX_OBJECTS = 10; // per node
31
- private MAX_LEVELS = 5;
32
-
33
- private level: number;
34
- private objects: RigidBody[] = [];
35
- private nodes: (QuadTree | undefined)[] = [];
36
- private bounds: RectangleBound;
37
-
38
-
39
- constructor(level: number, bounds: RectangleBound){
40
- this.level = level;
41
- this.objects = [];
42
- this.bounds = bounds;
43
- this.nodes = [undefined, undefined, undefined, undefined];
44
- }
45
-
46
- draw(context: CanvasRenderingContext2D){
47
- context.beginPath();
48
- context.rect(this.bounds.getbottomLeft().x, this.bounds.getbottomLeft().y, this.bounds.getWidth(), this.bounds.getHeight());
49
- context.stroke();
50
- // console.log("objects: ", this.objects.length, "level: ", this.level);
51
- for(let index = 0; index < this.nodes.length; index++){
52
- let node = this.nodes[index];
53
- if(node != undefined){
54
- node.draw(context);
55
- }
56
- }
57
- }
58
-
59
- clear(){
60
- this.objects = [];
61
- for(let index = 0; index < this.nodes.length; index++){
62
- let node = this.nodes[index];
63
- if(node != undefined){
64
- node.clear();
65
- node = undefined;
66
- }
67
- }
68
- }
69
-
70
- split(){
71
- // console.log("split");
72
- let subWidth = this.bounds.getWidth() / 2;
73
- let subHeight = this.bounds.getHeight() / 2;
74
- let bottomLeft = this.bounds.getbottomLeft();
75
- // bottom left is the first node and it goes clock wise
76
- this.nodes[0] = new QuadTree(this.level + 1, new RectangleBound({x: bottomLeft.x, y: bottomLeft.y}, subWidth, subHeight));
77
- this.nodes[1] = new QuadTree(this.level + 1, new RectangleBound({x: bottomLeft.x, y: bottomLeft.y + subHeight}, subWidth, subHeight));
78
- this.nodes[2] = new QuadTree(this.level + 1, new RectangleBound({x: bottomLeft.x + subWidth, y: bottomLeft.y + subHeight}, subWidth, subHeight));
79
- this.nodes[3] = new QuadTree(this.level + 1, new RectangleBound({x: bottomLeft.x + subWidth, y: bottomLeft.y}, subWidth, subHeight));
80
- }
81
-
82
- getIndex(vBody: RigidBody){
83
- let midPoint = {x: this.bounds.getbottomLeft().x + this.bounds.getWidth() / 2, y: this.bounds.getbottomLeft().y + this.bounds.getHeight() / 2};
84
- let points = vBody.AABB;
85
- let bottom = points.max.y < midPoint.y && points.min.y > this.bounds.getbottomLeft().y;
86
- let left = points.max.x < midPoint.x && points.min.x > this.bounds.getbottomLeft().x;
87
- let right = points.max.x > midPoint.x && points.min.x > midPoint.x;
88
- let top = points.max.y > midPoint.y && points.min.y > midPoint.y;
89
- // console.log("level", this.level);
90
- if (bottom && left){
91
- return 0;
92
- } else if (bottom && right){
93
- return 3;
94
- } else if (top && left){
95
- return 1;
96
- } else if (top && right){
97
- return 2;
98
- }
99
- return -1;
100
- }
101
-
102
- insert(vBody: RigidBody){
103
- let node = this.nodes[0];
104
- if (node != undefined){
105
- let index = this.getIndex(vBody);
106
-
107
- if (index !== -1){
108
- node = this.nodes[index];
109
- node?.insert(vBody);
110
- return;
111
- }
112
- }
113
-
114
- this.objects.push(vBody);
115
- if(this.objects.length > this.MAX_OBJECTS && this.level < this.MAX_LEVELS){
116
- if (this.nodes[0] == null || this.nodes[0] == undefined){
117
- this.split();
118
- }
119
- let i = 0;
120
- while (i < this.objects.length){
121
- let index = this.getIndex(this.objects[i]);
122
- let node = this.nodes[index];
123
- if (index != -1 && node !== undefined){
124
- let vBody = this.objects[i];
125
- this.objects.splice(i, 1);
126
- node.insert(vBody);
127
- } else{
128
- i++;
129
- }
130
- }
131
- }
132
- }
133
-
134
- retrieve(vBody: RigidBody): RigidBody[]{
135
- let index = this.getIndex(vBody);
136
- let res = [];
137
- let node = this.nodes[index];
138
- if(index !== -1 && node !== undefined){
139
- res.push(...node.retrieve(vBody));
140
- }
141
- res.push(...this.objects);
142
- return res;
143
- }
144
-
145
- }