@gg-web-engine/matter 0.0.1 → 0.0.3
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/README.md
CHANGED
|
@@ -7,7 +7,3 @@
|
|
|
7
7
|
### Installation:
|
|
8
8
|
1) make sure **@gg-web-engine/core** installed
|
|
9
9
|
1) `npm install --save @gg-web-engine/matter`
|
|
10
|
-
1) Add to your `tsconfig.json` in the record `compilerOptions.paths`:
|
|
11
|
-
```json lines
|
|
12
|
-
"matter-js": ["./node_modules/@gg-web-engine/matter/node_modules/matter-js"]
|
|
13
|
-
```
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { IGg2dPhysicsWorld, GgDebugPhysicsDrawer, Point2, Gg2dWorld } from '@gg-web-engine/core';
|
|
2
|
-
import { World } from 'matter-js';
|
|
2
|
+
import { Engine, World } from 'matter-js';
|
|
3
3
|
import { Gg2dBodyFactory } from './gg-2d-body-factory';
|
|
4
4
|
export declare class Gg2dPhysicsWorld implements IGg2dPhysicsWorld {
|
|
5
|
-
|
|
5
|
+
protected matterEngine: Engine | null;
|
|
6
6
|
get matterWorld(): World | null;
|
|
7
7
|
readonly factory: Gg2dBodyFactory;
|
|
8
8
|
private _gravity;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gg-web-engine/matter",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.03",
|
|
4
4
|
"description": "An attempt to create open source game engine for browser",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/AndyGura/gg-web-engine#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@
|
|
30
|
+
"@types/matter-js": "^0.18.2",
|
|
31
|
+
"@gg-web-engine/core": "^0.0.3",
|
|
31
32
|
"matter-js": "^0.18.0"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
|
-
"@types/matter-js": "^0.18.2",
|
|
35
35
|
"prettier": "^2.8.6",
|
|
36
36
|
"typescript": "~4.7.2"
|
|
37
37
|
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { Body2DOptions, BodyShape2DDescriptor, IGg2dBodyFactory, Point2, Shape2DDescriptor } from '@gg-web-engine/core';
|
|
2
|
-
import { Gg2dBody } from './gg-2d-body';
|
|
3
|
-
import { Bodies, IBodyDefinition, Vector, Body } from 'matter-js';
|
|
4
|
-
|
|
5
|
-
export class Gg2dBodyFactory implements IGg2dBodyFactory<Gg2dBody, any> {
|
|
6
|
-
createRigidBody(
|
|
7
|
-
descriptor: BodyShape2DDescriptor,
|
|
8
|
-
transform?: {
|
|
9
|
-
position?: Point2;
|
|
10
|
-
rotation?: number;
|
|
11
|
-
},
|
|
12
|
-
): Gg2dBody {
|
|
13
|
-
let nativeBody: Body | null = null;
|
|
14
|
-
switch (descriptor.shape.shape) {
|
|
15
|
-
case 'SQUARE':
|
|
16
|
-
nativeBody = Bodies.rectangle(
|
|
17
|
-
0,
|
|
18
|
-
0,
|
|
19
|
-
descriptor.shape.dimensions.x,
|
|
20
|
-
descriptor.shape.dimensions.y,
|
|
21
|
-
this.transformOptions(descriptor.body),
|
|
22
|
-
);
|
|
23
|
-
break;
|
|
24
|
-
case 'CIRCLE':
|
|
25
|
-
nativeBody = Bodies.circle(0, 0, descriptor.shape.radius, this.transformOptions(descriptor.body));
|
|
26
|
-
break;
|
|
27
|
-
}
|
|
28
|
-
if (!nativeBody) {
|
|
29
|
-
throw new Error(`Shape "${descriptor.shape}" not implemented for Matter.js`);
|
|
30
|
-
}
|
|
31
|
-
nativeBody.position = Vector.create(transform?.position?.x || 0, transform?.position?.y || 0);
|
|
32
|
-
nativeBody.angle = transform?.rotation || 0;
|
|
33
|
-
return new Gg2dBody(nativeBody);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
createTrigger(
|
|
37
|
-
descriptor: Shape2DDescriptor,
|
|
38
|
-
transform?: {
|
|
39
|
-
position?: Point2;
|
|
40
|
-
rotation?: number;
|
|
41
|
-
},
|
|
42
|
-
): any {
|
|
43
|
-
throw new Error('Triggers not implemented for Matter.js');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private transformOptions(options: Partial<Body2DOptions>): IBodyDefinition {
|
|
47
|
-
const res: IBodyDefinition = {
|
|
48
|
-
isStatic: options.dynamic !== undefined ? !options.dynamic : !options.mass,
|
|
49
|
-
mass: options.mass,
|
|
50
|
-
restitution: options.restitution,
|
|
51
|
-
friction: options.friction,
|
|
52
|
-
};
|
|
53
|
-
for (const key in res) {
|
|
54
|
-
if ((res as any)[key] === undefined) {
|
|
55
|
-
delete (res as any)[key];
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return res;
|
|
59
|
-
}
|
|
60
|
-
}
|
package/src/impl/gg-2d-body.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { IGg2dBody, Point2, Gg2dEntity } from '@gg-web-engine/core';
|
|
2
|
-
import { Gg2dPhysicsWorld } from './gg-2d-physics-world';
|
|
3
|
-
import { Body, Composite, Vector } from 'matter-js';
|
|
4
|
-
|
|
5
|
-
export class Gg2dBody implements IGg2dBody {
|
|
6
|
-
public get position(): Point2 {
|
|
7
|
-
return this.nativeBody.position;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public set position(value: Point2) {
|
|
11
|
-
Body.translate(this.nativeBody, Vector.create(value.x, value.y));
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public get rotation(): number {
|
|
15
|
-
return this.nativeBody.angle;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public set rotation(value: number) {
|
|
19
|
-
this.nativeBody.angle = value;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public get scale(): Point2 {
|
|
23
|
-
// hmm, is it even possible to be different?
|
|
24
|
-
return { x: 1, y: 1 };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public name: string = '';
|
|
28
|
-
|
|
29
|
-
public entity: Gg2dEntity | null = null;
|
|
30
|
-
|
|
31
|
-
constructor(public nativeBody: Body) {}
|
|
32
|
-
|
|
33
|
-
clone(): Gg2dBody {
|
|
34
|
-
// TODO
|
|
35
|
-
throw new Error('Gg2dBody.clone() not implemented for Matter.js');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
addToWorld(world: Gg2dPhysicsWorld): void {
|
|
39
|
-
Composite.add(world.matterWorld!, this.nativeBody);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
removeFromWorld(world: Gg2dPhysicsWorld): void {
|
|
43
|
-
Composite.remove(world.matterWorld!, this.nativeBody);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
dispose(): void {}
|
|
47
|
-
|
|
48
|
-
resetMotion(): void {
|
|
49
|
-
Body.setVelocity(this.nativeBody, { x: 0, y: 0 });
|
|
50
|
-
Body.setAngularVelocity(this.nativeBody, 0);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { IGg2dPhysicsWorld, GgDebugPhysicsDrawer, Point2, Gg2dWorld } from '@gg-web-engine/core';
|
|
2
|
-
import { Engine, World } from 'matter-js';
|
|
3
|
-
import { Gg2dBodyFactory } from './gg-2d-body-factory';
|
|
4
|
-
|
|
5
|
-
export class Gg2dPhysicsWorld implements IGg2dPhysicsWorld {
|
|
6
|
-
private matterEngine: Engine | null = null;
|
|
7
|
-
|
|
8
|
-
public get matterWorld(): World | null {
|
|
9
|
-
return this.matterEngine && this.matterEngine.world;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public readonly factory: Gg2dBodyFactory = new Gg2dBodyFactory();
|
|
13
|
-
|
|
14
|
-
private _gravity: Point2 = { x: 0, y: 9.82 };
|
|
15
|
-
public get gravity(): Point2 {
|
|
16
|
-
return this._gravity;
|
|
17
|
-
}
|
|
18
|
-
public set gravity(value: Point2) {
|
|
19
|
-
this._gravity = value;
|
|
20
|
-
if (this.matterEngine) {
|
|
21
|
-
this.matterEngine.gravity.x = this._gravity.x;
|
|
22
|
-
this.matterEngine.gravity.y = this._gravity.y;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
private _timeScale: number = 1;
|
|
27
|
-
public get timeScale(): number {
|
|
28
|
-
return this._timeScale;
|
|
29
|
-
}
|
|
30
|
-
public set timeScale(value: number) {
|
|
31
|
-
this._timeScale = value;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public get physicsDebugViewActive(): boolean {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async init(): Promise<void> {
|
|
39
|
-
this.matterEngine = Engine.create({ gravity: { ...this._gravity, scale: 0.0001 } });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
simulate(delta: number): void {
|
|
43
|
-
Engine.update(this.matterEngine!, this._timeScale * delta);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
startDebugger(world: Gg2dWorld, drawer: GgDebugPhysicsDrawer<Point2, number>): void {
|
|
47
|
-
// TODO
|
|
48
|
-
throw new Error('Matter.js DebugDrawer not implemented');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
stopDebugger(): void {
|
|
52
|
-
// TODO
|
|
53
|
-
throw new Error('Matter.js DebugDrawer not implemented');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
dispose(): void {
|
|
57
|
-
Engine.clear(this.matterEngine!);
|
|
58
|
-
}
|
|
59
|
-
}
|
package/src/index.ts
DELETED