Skip to content

Commit 4c9fcdd

Browse files
committed
cannon
1 parent 7c1cfa8 commit 4c9fcdd

40 files changed

+32737
-23390
lines changed

.release-it.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"releaseName": "Release ${version}"
4141
},
4242
"hooks": {
43-
"before:bump": "pnpm exec nx run-many --target=package --projects=core,soba,postprocessing --parallel=false",
43+
"before:bump": "pnpm exec nx package core",
4444
"after:bump": ["git checkout -- package.json"]
4545
}
4646
}

CHANGELOG.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
2-
31
## 2.0.0-beta.8 (2023-08-14)
42

5-
63
### Features
74

8-
* **core:** generate core ([e1cf6c7](https://github.com/angular-threejs/angular-three/commit/e1cf6c7422668afbbc0f767a444bcc591e1a6903))
5+
- **core:** generate core ([e1cf6c7](https://github.com/angular-threejs/angular-three/commit/e1cf6c7422668afbbc0f767a444bcc591e1a6903))

libs/cannon/.eslintrc.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts"],
7+
"rules": {
8+
"@angular-eslint/directive-selector": [
9+
"error",
10+
{
11+
"type": "attribute",
12+
"prefix": "platform",
13+
"style": "camelCase"
14+
}
15+
],
16+
"@angular-eslint/component-selector": [
17+
"error",
18+
{
19+
"type": "element",
20+
"prefix": "platform",
21+
"style": "kebab-case"
22+
}
23+
]
24+
},
25+
"extends": ["plugin:@nx/angular", "plugin:@angular-eslint/template/process-inline-templates"]
26+
},
27+
{
28+
"files": ["*.html"],
29+
"extends": ["plugin:@nx/angular-template"],
30+
"rules": {}
31+
}
32+
]
33+
}

libs/cannon/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# cannon
2+
3+
This library was generated with [Nx](https://nx.dev).
4+
5+
## Running unit tests
6+
7+
Run `nx test cannon` to execute the unit tests.

libs/cannon/debug/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# angular-three-cannon/debug
2+
3+
Secondary entry point of `angular-three-cannon`. It can be used by importing from `angular-three-cannon/debug`.

libs/cannon/debug/ng-package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"lib": {
3+
"entryFile": "src/index.ts"
4+
}
5+
}

libs/cannon/debug/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib/debug';

libs/cannon/debug/src/lib/debug.ts

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { Component, CUSTOM_ELEMENTS_SCHEMA, forwardRef, Input, type OnInit } from '@angular/core';
2+
import { propsToBody, type BodyProps, type BodyShapeType } from '@pmndrs/cannon-worker-api';
3+
import { createInjectionToken, injectBeforeRender, NgtArgs } from 'angular-three';
4+
import { injectNgtcPhysicsApi, provideNgtcPhysicsApi } from 'angular-three-cannon';
5+
import type { Body, Quaternion as CQuarternion, Vec3, World } from 'cannon-es';
6+
import CannonDebugger from 'cannon-es-debugger';
7+
import * as THREE from 'three';
8+
9+
const q = new THREE.Quaternion();
10+
const s = new THREE.Vector3(1, 1, 1);
11+
const v = new THREE.Vector3();
12+
const m = new THREE.Matrix4();
13+
14+
function getMatrix(o: THREE.Object3D): THREE.Matrix4 {
15+
if (o instanceof THREE.InstancedMesh) {
16+
o.getMatrixAt(parseInt(o.uuid.split('/')[1]), m);
17+
return m;
18+
}
19+
return o.matrix;
20+
}
21+
22+
export const [injectNgtcDebugApi, provideNgtcDebugApi] = createInjectionToken((debug: NgtcDebug) => debug.api, {
23+
isRoot: false,
24+
deps: [forwardRef(() => NgtcDebug)],
25+
});
26+
27+
@Component({
28+
selector: 'ngtc-debug',
29+
standalone: true,
30+
template: `
31+
<ngt-primitive *args="[scene]" />
32+
<ng-content />
33+
`,
34+
providers: [provideNgtcPhysicsApi()],
35+
imports: [NgtArgs],
36+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
37+
})
38+
export class NgtcDebug implements OnInit {
39+
@Input() color = 'black';
40+
@Input() scale = 1;
41+
@Input() impl = CannonDebugger;
42+
@Input() disabled = false;
43+
44+
private bodies: Body[] = [];
45+
private bodyMap: Record<string, Body> = {};
46+
47+
private physicsApi = injectNgtcPhysicsApi();
48+
49+
scene = new THREE.Scene();
50+
private cannonDebugger!: ReturnType<typeof CannonDebugger>;
51+
52+
api = {
53+
add: (uuid: string, props: BodyProps, type: BodyShapeType) => {
54+
const body = propsToBody({ uuid, props, type });
55+
this.bodies.push(body);
56+
this.bodyMap[uuid] = body;
57+
},
58+
remove: (id: string) => {
59+
const debugBodyIndex = this.bodies.indexOf(this.bodyMap[id]);
60+
if (debugBodyIndex > -1) this.bodies.splice(debugBodyIndex, 1);
61+
delete this.bodyMap[id];
62+
},
63+
};
64+
65+
constructor() {
66+
injectBeforeRender(() => {
67+
if (!this.cannonDebugger) return;
68+
const refs = this.physicsApi.get('refs');
69+
for (const uuid in this.bodyMap) {
70+
getMatrix(refs[uuid]).decompose(v, q, s);
71+
this.bodyMap[uuid].position.copy(v as unknown as Vec3);
72+
this.bodyMap[uuid].quaternion.copy(q as unknown as CQuarternion);
73+
}
74+
75+
for (const child of this.scene.children) {
76+
child.visible = !this.disabled;
77+
}
78+
79+
if (!this.disabled) {
80+
this.cannonDebugger.update();
81+
}
82+
});
83+
}
84+
85+
ngOnInit() {
86+
this.cannonDebugger = this.impl(this.scene, { bodies: this.bodies } as World, {
87+
color: this.color,
88+
scale: this.scale,
89+
});
90+
}
91+
}

libs/cannon/jest.config.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'cannon',
4+
preset: '../../jest.preset.js',
5+
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
6+
coverageDirectory: '../../coverage/libs/cannon',
7+
transform: {
8+
'^.+\\.(ts|mjs|js|html)$': [
9+
'jest-preset-angular',
10+
{
11+
tsconfig: '<rootDir>/tsconfig.spec.json',
12+
stringifyContentPathRegex: '\\.(html|svg)$',
13+
},
14+
],
15+
},
16+
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
17+
snapshotSerializers: [
18+
'jest-preset-angular/build/serializers/no-ng-attributes',
19+
'jest-preset-angular/build/serializers/ng-snapshot',
20+
'jest-preset-angular/build/serializers/html-comment',
21+
],
22+
};

libs/cannon/ng-package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
3+
"dest": "../../dist/libs/cannon",
4+
"lib": {
5+
"entryFile": "src/index.ts"
6+
}
7+
}

libs/cannon/package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "angular-three-cannon",
3+
"version": "0.0.0-replace",
4+
"publishConfig": {
5+
"access": "public"
6+
},
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/angular-threejs/angular-three/tree/main/libs/cannon"
10+
},
11+
"author": {
12+
"name": "Chau Tran",
13+
"email": "nartc7789@gmail.com",
14+
"url": "https://nartc.me"
15+
},
16+
"description": "Cannon.js physics integration with Angular Three",
17+
"keywords": [
18+
"angular",
19+
"threejs",
20+
"renderer",
21+
"cannonjs",
22+
"physics"
23+
],
24+
"license": "MIT",
25+
"peerDependencies": {
26+
"@angular/common": " ^16.0.0",
27+
"@angular/core": " ^16.0.0",
28+
"@pmndrs/cannon-worker-api": "^2.0.0",
29+
"angular-three": "^2.0.0",
30+
"cannon-es": "^0.20.0",
31+
"cannon-es-debugger": "^1.0.0",
32+
"three": ">=0.148.0"
33+
},
34+
"dependencies": {
35+
"tslib": "^2.3.0"
36+
},
37+
"sideEffects": false
38+
}

libs/cannon/project.json

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "cannon",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "libs/cannon/src",
5+
"prefix": "platform",
6+
"tags": [],
7+
"projectType": "library",
8+
"targets": {
9+
"build": {
10+
"executor": "@nx/angular:package",
11+
"outputs": ["{workspaceRoot}/dist/{projectRoot}"],
12+
"options": {
13+
"project": "libs/cannon/ng-package.json"
14+
},
15+
"configurations": {
16+
"production": {
17+
"tsConfig": "libs/cannon/tsconfig.lib.prod.json"
18+
},
19+
"development": {
20+
"tsConfig": "libs/cannon/tsconfig.lib.json"
21+
}
22+
},
23+
"defaultConfiguration": "production"
24+
},
25+
"test": {
26+
"executor": "@nx/jest:jest",
27+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
28+
"options": {
29+
"jestConfig": "libs/cannon/jest.config.ts",
30+
"passWithNoTests": true
31+
},
32+
"configurations": {
33+
"ci": {
34+
"ci": true,
35+
"codeCoverage": true
36+
}
37+
}
38+
},
39+
"lint": {
40+
"executor": "@nx/linter:eslint",
41+
"outputs": ["{options.outputFile}"],
42+
"options": {
43+
"lintFilePatterns": [
44+
"libs/cannon/**/*.ts",
45+
"libs/cannon/**/*.html",
46+
"libs/cannon/package.json",
47+
"libs/cannon/debug/**/*.ts",
48+
"libs/cannon/debug/**/*.html",
49+
"libs/cannon/services/**/*.ts",
50+
"libs/cannon/services/**/*.html"
51+
]
52+
}
53+
}
54+
}
55+
}

libs/cannon/services/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# angular-three-cannon/services
2+
3+
Secondary entry point of `angular-three-cannon`. It can be used by importing from `angular-three-cannon/services`.

libs/cannon/services/ng-package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"lib": {
3+
"entryFile": "src/index.ts"
4+
}
5+
}

libs/cannon/services/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from './lib/body';
2+
export * from './lib/constraints';
3+
export * from './lib/contact-material';
4+
export * from './lib/ray';
5+
export * from './lib/raycast-vehicle';
6+
export * from './lib/spring';

0 commit comments

Comments
 (0)