@cyclonium/physics-2d 0.0.100
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/lib/body-collider-link.d.ts +32 -0
- package/lib/body-collider-link.js +164 -0
- package/lib/character-controller-2d.d.ts +69 -0
- package/lib/character-controller-2d.js +399 -0
- package/lib/collider-2d.d.ts +81 -0
- package/lib/collider-2d.js +364 -0
- package/lib/colliders/box-collider-2d.d.ts +20 -0
- package/lib/colliders/box-collider-2d.js +84 -0
- package/lib/colliders/capsule-collider-2d.d.ts +17 -0
- package/lib/colliders/capsule-collider-2d.js +78 -0
- package/lib/colliders/circle-collider-2d.d.ts +14 -0
- package/lib/colliders/circle-collider-2d.js +59 -0
- package/lib/colliders/polygon-collider-2d.d.ts +17 -0
- package/lib/colliders/polygon-collider-2d.js +77 -0
- package/lib/collision-matrix.d.ts +12 -0
- package/lib/collision-matrix.js +70 -0
- package/lib/contact.d.ts +7 -0
- package/lib/contact.js +2 -0
- package/lib/exchange.d.ts +5 -0
- package/lib/exchange.js +23 -0
- package/lib/gizmo-headless.d.ts +7 -0
- package/lib/gizmo-headless.js +7 -0
- package/lib/gizmo.d.ts +13 -0
- package/lib/gizmo.js +122 -0
- package/lib/index.d.ts +20 -0
- package/lib/index.js +20 -0
- package/lib/physics-2d-debugger-headless.d.ts +6 -0
- package/lib/physics-2d-debugger-headless.js +9 -0
- package/lib/physics-2d-debugger.d.ts +11 -0
- package/lib/physics-2d-debugger.js +146 -0
- package/lib/physics-2d-settings.d.ts +13 -0
- package/lib/physics-2d-settings.js +53 -0
- package/lib/physics-component-2d-base.d.ts +22 -0
- package/lib/physics-component-2d-base.js +85 -0
- package/lib/physics-world-2d-scene-component.d.ts +20 -0
- package/lib/physics-world-2d-scene-component.js +107 -0
- package/lib/physics-world-2d.d.ts +103 -0
- package/lib/physics-world-2d.js +506 -0
- package/lib/physics-world-registry.d.ts +6 -0
- package/lib/physics-world-registry.js +26 -0
- package/lib/px2-impl.d.ts +4 -0
- package/lib/px2-impl.js +18 -0
- package/lib/rigid-body-2d.d.ts +72 -0
- package/lib/rigid-body-2d.js +391 -0
- package/lib/scene-query-probe-2d.d.ts +102 -0
- package/lib/scene-query-probe-2d.js +223 -0
- package/lib/scene-query-probes/box-scene-query-probe-2d.d.ts +21 -0
- package/lib/scene-query-probes/box-scene-query-probe-2d.js +62 -0
- package/lib/scene-query-probes/capsule-scene-query-probe-2d.d.ts +27 -0
- package/lib/scene-query-probes/capsule-scene-query-probe-2d.js +96 -0
- package/lib/scene-query-probes/circle-scene-query-probe-2d.d.ts +20 -0
- package/lib/scene-query-probes/circle-scene-query-probe-2d.js +64 -0
- package/lib/scene-query.d.ts +67 -0
- package/lib/scene-query.js +128 -0
- package/lib/shared.d.ts +9 -0
- package/lib/shared.js +42 -0
- package/lib/utils/3-to-2.d.ts +3 -0
- package/lib/utils/3-to-2.js +12 -0
- package/lib/wasm-binary-helper/index.d.ts +6 -0
- package/lib/wasm-binary-helper/index.js +14 -0
- package/lib/wasm-binary-helper/wasm-binary-id.d.ts +2 -0
- package/lib/wasm-binary-helper/wasm-binary-id.js +2 -0
- package/lib/wasm-binary-helper.d.ts +6 -0
- package/lib/wasm-binary-helper.js +6 -0
- package/package.json +50 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { editable, executeInEditMode, idem, serializable } from '@cyclonium/core/legacy-decorator';
|
|
8
|
+
import { Collider2D } from '../collider-2d.js';
|
|
9
|
+
import { px2Impl } from '../px2-impl.js';
|
|
10
|
+
import { EDITOR_NOT_IN_PREVIEW } from 'cc/env';
|
|
11
|
+
import { drawCircleCollider2DGizmo } from '#gizmo';
|
|
12
|
+
import { cycloBuiltinClass } from '@cyclonium/core/internal';
|
|
13
|
+
import { Vec2 } from '@cyclonium/core/math/vec2';
|
|
14
|
+
let CircleCollider2D = class CircleCollider2D extends Collider2D {
|
|
15
|
+
get radius() {
|
|
16
|
+
return this._radius;
|
|
17
|
+
}
|
|
18
|
+
set radius(value) {
|
|
19
|
+
this._radius = value;
|
|
20
|
+
this._implCollider?.setRadius(this._getRadius());
|
|
21
|
+
}
|
|
22
|
+
getShape() {
|
|
23
|
+
return new px2Impl.Ball(this._getRadius());
|
|
24
|
+
}
|
|
25
|
+
computeShapeBounds(out) {
|
|
26
|
+
out.setCenterSize(Vec2.ZERO, Vec2.splat(this._getRadius() * 2));
|
|
27
|
+
}
|
|
28
|
+
onUpdate() {
|
|
29
|
+
if (EDITOR_NOT_IN_PREVIEW) {
|
|
30
|
+
drawCircleCollider2DGizmo(this);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
updateSceneGraphScale() {
|
|
34
|
+
this._implCollider?.setRadius(this._getRadius());
|
|
35
|
+
}
|
|
36
|
+
_radius = 1;
|
|
37
|
+
_getRadius() {
|
|
38
|
+
const scale = this.sceneGraphScale;
|
|
39
|
+
if (scale.x !== scale.y) {
|
|
40
|
+
console.warn('CircleCollider2D should have uniform scale');
|
|
41
|
+
}
|
|
42
|
+
return this._radius * scale.x;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
__decorate([
|
|
46
|
+
editable({
|
|
47
|
+
min: 0,
|
|
48
|
+
}),
|
|
49
|
+
idem
|
|
50
|
+
], CircleCollider2D.prototype, "radius", null);
|
|
51
|
+
__decorate([
|
|
52
|
+
serializable
|
|
53
|
+
], CircleCollider2D.prototype, "_radius", void 0);
|
|
54
|
+
CircleCollider2D = __decorate([
|
|
55
|
+
cycloBuiltinClass('CircleCollider2D'),
|
|
56
|
+
executeInEditMode
|
|
57
|
+
], CircleCollider2D);
|
|
58
|
+
export { CircleCollider2D };
|
|
59
|
+
//# sourceMappingURL=circle-collider-2d.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Collider2D } from '../collider-2d.js';
|
|
2
|
+
import { px2Impl } from '../px2-impl.js';
|
|
3
|
+
import { Vec2 } from '@cyclonium/core/math/vec2';
|
|
4
|
+
import { Bounds2D } from '@cyclonium/core/math/bounds-2d';
|
|
5
|
+
export declare class PolygonCollider2D extends Collider2D {
|
|
6
|
+
get points(): Vec2[];
|
|
7
|
+
setPolygon(points: Vec2[], indices?: ArrayLike<number>): void;
|
|
8
|
+
protected getShape(): px2Impl.Shape | undefined;
|
|
9
|
+
protected computeShapeBounds(out: Bounds2D): void;
|
|
10
|
+
protected updateSceneGraphScale(): void;
|
|
11
|
+
private _points;
|
|
12
|
+
private _indices;
|
|
13
|
+
private _isValidShape;
|
|
14
|
+
private _updateShape;
|
|
15
|
+
private _makeShape;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=polygon-collider-2d.d.ts.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { Collider2D } from '../collider-2d.js';
|
|
8
|
+
import { px2Impl } from '../px2-impl.js';
|
|
9
|
+
import { Vec2 } from '@cyclonium/core/math/vec2';
|
|
10
|
+
import { cycloBuiltinClass } from '@cyclonium/core/internal';
|
|
11
|
+
import { Bounds2D } from '@cyclonium/core/math/bounds-2d';
|
|
12
|
+
let PolygonCollider2D = class PolygonCollider2D extends Collider2D {
|
|
13
|
+
get points() {
|
|
14
|
+
return this._points;
|
|
15
|
+
}
|
|
16
|
+
setPolygon(points, indices) {
|
|
17
|
+
this._points = points.slice().map((v) => v.clone());
|
|
18
|
+
this._indices = indices ? Uint32Array.from(indices) : undefined;
|
|
19
|
+
this._updateShape();
|
|
20
|
+
}
|
|
21
|
+
getShape() {
|
|
22
|
+
if (!this._isValidShape()) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
return this._makeShape();
|
|
26
|
+
}
|
|
27
|
+
computeShapeBounds(out) {
|
|
28
|
+
const scale = this.sceneGraphScale;
|
|
29
|
+
out.setMinMax(Vec2.POSITIVE_INFINITY, Vec2.NEGATIVE_INFINITY);
|
|
30
|
+
const p = new Vec2();
|
|
31
|
+
for (let i = 0; i < this._points.length; i++) {
|
|
32
|
+
const point = this._points[i];
|
|
33
|
+
out.extend(p.copyFrom(point).mulSelf(scale));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
updateSceneGraphScale() {
|
|
37
|
+
this._updateShape();
|
|
38
|
+
}
|
|
39
|
+
_points = [];
|
|
40
|
+
_indices = undefined;
|
|
41
|
+
_isValidShape() {
|
|
42
|
+
return this._points.length > 0;
|
|
43
|
+
}
|
|
44
|
+
_updateShape() {
|
|
45
|
+
if (!this._implCollider) {
|
|
46
|
+
this.recreateCollider();
|
|
47
|
+
}
|
|
48
|
+
else if (this._isValidShape()) {
|
|
49
|
+
this._implCollider.setShape(this._makeShape());
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.recreateCollider();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
_makeShape() {
|
|
56
|
+
const scale = this.sceneGraphScale;
|
|
57
|
+
const flatVertices = new Float32Array(this._points.length * 2);
|
|
58
|
+
for (let i = 0; i < this._points.length; i++) {
|
|
59
|
+
const point = this._points[i];
|
|
60
|
+
flatVertices[i * 2] = point.x * scale.x;
|
|
61
|
+
flatVertices[i * 2 + 1] = point.y * scale.y;
|
|
62
|
+
}
|
|
63
|
+
const polyline = new px2Impl.Polyline(flatVertices, this._indices);
|
|
64
|
+
// try {
|
|
65
|
+
// polyline.intoRaw();
|
|
66
|
+
// } catch (e) {
|
|
67
|
+
// console.error(`Invalid polygon: , name: ${this.node.getPathInHierarchy()} points: ${this._points.length}, indices: ${this._indices?.length}`);
|
|
68
|
+
// throw e;
|
|
69
|
+
// }
|
|
70
|
+
return polyline;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
PolygonCollider2D = __decorate([
|
|
74
|
+
cycloBuiltinClass('PolygonCollider2D')
|
|
75
|
+
], PolygonCollider2D);
|
|
76
|
+
export { PolygonCollider2D };
|
|
77
|
+
//# sourceMappingURL=polygon-collider-2d.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class CollisionMatrix {
|
|
2
|
+
static readonly EXTENT = 16;
|
|
3
|
+
private get values();
|
|
4
|
+
private set values(value);
|
|
5
|
+
clone(): CollisionMatrix;
|
|
6
|
+
get(i: number, j: number): boolean;
|
|
7
|
+
set(i: number, j: number, value: boolean): void;
|
|
8
|
+
getCollisionsBitsOf(i: number): number;
|
|
9
|
+
private _values;
|
|
10
|
+
private _locate;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=collision-matrix.d.ts.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var CollisionMatrix_1;
|
|
8
|
+
import { editable, serializable } from '@cyclonium/core/legacy-decorator';
|
|
9
|
+
import { cycloBuiltinClass } from '@cyclonium/core/internal';
|
|
10
|
+
import { dumpRaw } from '@cyclonium/core/utils';
|
|
11
|
+
const EXTENT = 16;
|
|
12
|
+
let CollisionMatrix = class CollisionMatrix {
|
|
13
|
+
static { CollisionMatrix_1 = this; }
|
|
14
|
+
static EXTENT = EXTENT;
|
|
15
|
+
get values() {
|
|
16
|
+
return this._values;
|
|
17
|
+
}
|
|
18
|
+
set values(values) {
|
|
19
|
+
this._values = [...values];
|
|
20
|
+
}
|
|
21
|
+
clone() {
|
|
22
|
+
const clone = new CollisionMatrix_1();
|
|
23
|
+
clone._values = this._values.slice();
|
|
24
|
+
return clone;
|
|
25
|
+
}
|
|
26
|
+
get(i, j) {
|
|
27
|
+
return this._values[this._locate(i, j)];
|
|
28
|
+
}
|
|
29
|
+
set(i, j, value) {
|
|
30
|
+
const index = this._locate(i, j);
|
|
31
|
+
this._values[index] = value;
|
|
32
|
+
}
|
|
33
|
+
getCollisionsBitsOf(i) {
|
|
34
|
+
let bits = 0;
|
|
35
|
+
for (let j = 0; j < EXTENT; j++) {
|
|
36
|
+
const value = this.get(i, j);
|
|
37
|
+
if (value) {
|
|
38
|
+
bits |= 1 << j;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const rowStart = i * (i + 1) / 2;
|
|
42
|
+
for (let j = 0; j <= i; j++) {
|
|
43
|
+
bits |= this._values[rowStart + j] ? 1 << j : 0;
|
|
44
|
+
}
|
|
45
|
+
return bits;
|
|
46
|
+
}
|
|
47
|
+
_values = new Array(EXTENT * (EXTENT + 1) / 2).fill(false);
|
|
48
|
+
_locate(i, j) {
|
|
49
|
+
if (i < 0 || i >= EXTENT || j < 0 || j >= EXTENT) {
|
|
50
|
+
throw new Error(`Index out of range.`);
|
|
51
|
+
}
|
|
52
|
+
if (i > j) {
|
|
53
|
+
[i, j] = [j, i];
|
|
54
|
+
}
|
|
55
|
+
const rowStart = i * (2 * EXTENT - i - 1) / 2;
|
|
56
|
+
return rowStart + j;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
__decorate([
|
|
60
|
+
editable,
|
|
61
|
+
dumpRaw
|
|
62
|
+
], CollisionMatrix.prototype, "values", null);
|
|
63
|
+
__decorate([
|
|
64
|
+
serializable
|
|
65
|
+
], CollisionMatrix.prototype, "_values", void 0);
|
|
66
|
+
CollisionMatrix = CollisionMatrix_1 = __decorate([
|
|
67
|
+
cycloBuiltinClass('CollisionMatrix')
|
|
68
|
+
], CollisionMatrix);
|
|
69
|
+
export { CollisionMatrix };
|
|
70
|
+
//# sourceMappingURL=collision-matrix.js.map
|
package/lib/contact.d.ts
ADDED
package/lib/contact.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Vec2 } from '@cyclonium/core/math/vec2';
|
|
2
|
+
import { px2Impl } from './px2-impl.js';
|
|
3
|
+
export declare function toPx2ImplVec2(input: Vec2, out?: px2Impl.Vector2): px2Impl.Vector2;
|
|
4
|
+
export declare function fromPx2ImplVec2(input: px2Impl.Vector2, out?: Vec2): Vec2;
|
|
5
|
+
//# sourceMappingURL=exchange.d.ts.map
|
package/lib/exchange.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Vec2 } from '@cyclonium/core/math/vec2';
|
|
2
|
+
import { px2Impl } from './px2-impl.js';
|
|
3
|
+
export function toPx2ImplVec2(input, out) {
|
|
4
|
+
if (out) {
|
|
5
|
+
out.x = input.x;
|
|
6
|
+
out.y = input.y;
|
|
7
|
+
return out;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
return new px2Impl.Vector2(input.x, input.y);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function fromPx2ImplVec2(input, out) {
|
|
14
|
+
if (out) {
|
|
15
|
+
out.x = input.x;
|
|
16
|
+
out.y = input.y;
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
return new Vec2(input.x, input.y);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=exchange.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function drawBoxCollider2DGizmo(): void;
|
|
2
|
+
export declare function drawCircleCollider2DGizmo(): void;
|
|
3
|
+
export declare function drawCapsuleCollider2DGizmo(): void;
|
|
4
|
+
export declare function drawBoxSceneQueryProbe2DGizmo(): void;
|
|
5
|
+
export declare function drawCircleSceneQueryProbe2DGizmo(): void;
|
|
6
|
+
export declare function drawCapsuleSceneQueryProbe2DGizmo(): void;
|
|
7
|
+
//# sourceMappingURL=gizmo-headless.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export function drawBoxCollider2DGizmo() { }
|
|
2
|
+
export function drawCircleCollider2DGizmo() { }
|
|
3
|
+
export function drawCapsuleCollider2DGizmo() { }
|
|
4
|
+
export function drawBoxSceneQueryProbe2DGizmo() { }
|
|
5
|
+
export function drawCircleSceneQueryProbe2DGizmo() { }
|
|
6
|
+
export function drawCapsuleSceneQueryProbe2DGizmo() { }
|
|
7
|
+
//# sourceMappingURL=gizmo-headless.js.map
|
package/lib/gizmo.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BoxCollider2D } from './colliders/box-collider-2d.js';
|
|
2
|
+
import type { CircleCollider2D } from './colliders/circle-collider-2d.js';
|
|
3
|
+
import type { CapsuleCollider2D } from './colliders/capsule-collider-2d.js';
|
|
4
|
+
import type { BoxSceneQueryProbe2D } from './scene-query-probes/box-scene-query-probe-2d.js';
|
|
5
|
+
import type { CapsuleSceneQueryProbe2D } from './scene-query-probes/capsule-scene-query-probe-2d.js';
|
|
6
|
+
import type { CircleSceneQueryProbe2D } from './scene-query-probes/circle-scene-query-probe-2d.js';
|
|
7
|
+
export declare function drawBoxCollider2DGizmo(collider: BoxCollider2D): void;
|
|
8
|
+
export declare function drawCircleCollider2DGizmo(collider: CircleCollider2D): void;
|
|
9
|
+
export declare function drawCapsuleCollider2DGizmo(_collider: CapsuleCollider2D): void;
|
|
10
|
+
export declare function drawBoxSceneQueryProbe2DGizmo(probe: BoxSceneQueryProbe2D): void;
|
|
11
|
+
export declare function drawCircleSceneQueryProbe2DGizmo(probe: CircleSceneQueryProbe2D): void;
|
|
12
|
+
export declare function drawCapsuleSceneQueryProbe2DGizmo(probe: CapsuleSceneQueryProbe2D): void;
|
|
13
|
+
//# sourceMappingURL=gizmo.d.ts.map
|
package/lib/gizmo.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Color } from 'cc';
|
|
2
|
+
import { EDITOR_NOT_IN_PREVIEW } from 'cc/env';
|
|
3
|
+
import { tryGetDebugGeometryRenderer, drawBox2D, drawCircle2D, drawLineFromTo2D } from '@cyclonium/debug-draw';
|
|
4
|
+
import { Vec2 } from '@cyclonium/core/math/vec2';
|
|
5
|
+
import { Transform2DComponent } from '@cyclonium/core/2d';
|
|
6
|
+
const depthTest = false;
|
|
7
|
+
const color = Color.YELLOW;
|
|
8
|
+
const probeColor = new Color(0, 204, 255, 255);
|
|
9
|
+
const wireframe = true;
|
|
10
|
+
export function drawBoxCollider2DGizmo(collider) {
|
|
11
|
+
if (EDITOR_NOT_IN_PREVIEW) {
|
|
12
|
+
const g = tryGetDebugGeometryRenderer();
|
|
13
|
+
if (g) {
|
|
14
|
+
const transform = getCollider2DTransform(collider);
|
|
15
|
+
drawBox2D(g, collider.center, new Vec2(collider.width / 2, collider.height / 2), color, {
|
|
16
|
+
wireframe,
|
|
17
|
+
depthTest,
|
|
18
|
+
transform,
|
|
19
|
+
});
|
|
20
|
+
drawLineFromTo2D(g, collider.center, collider.center.addMulScalar(Vec2.UNIT_X, collider.width / 2), Color.RED, {
|
|
21
|
+
depthTest,
|
|
22
|
+
transform,
|
|
23
|
+
});
|
|
24
|
+
drawLineFromTo2D(g, collider.center, collider.center.addMulScalar(Vec2.UNIT_Y, collider.height / 2), Color.GREEN, {
|
|
25
|
+
depthTest,
|
|
26
|
+
transform,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function drawCircleCollider2DGizmo(collider) {
|
|
32
|
+
if (EDITOR_NOT_IN_PREVIEW) {
|
|
33
|
+
const g = tryGetDebugGeometryRenderer();
|
|
34
|
+
if (g) {
|
|
35
|
+
const transform = Transform2DComponent.of(collider);
|
|
36
|
+
drawCircle2D(g, {
|
|
37
|
+
center: collider.center,
|
|
38
|
+
radius: collider.radius * transform.scale.x,
|
|
39
|
+
color: color,
|
|
40
|
+
wireframe,
|
|
41
|
+
depthTest,
|
|
42
|
+
transform: getCollider2DTransform(collider),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function drawCapsuleCollider2DGizmo(_collider) {
|
|
48
|
+
if (EDITOR_NOT_IN_PREVIEW) {
|
|
49
|
+
const g = tryGetDebugGeometryRenderer();
|
|
50
|
+
if (g) {
|
|
51
|
+
// todo
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export function drawBoxSceneQueryProbe2DGizmo(probe) {
|
|
56
|
+
if (EDITOR_NOT_IN_PREVIEW) {
|
|
57
|
+
const g = tryGetDebugGeometryRenderer();
|
|
58
|
+
if (g) {
|
|
59
|
+
drawBox2D(g, Vec2.ZERO, probe.halfExtents, probeColor, {
|
|
60
|
+
wireframe,
|
|
61
|
+
depthTest,
|
|
62
|
+
transform: getSceneQueryProbe2DTransform(probe),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function drawCircleSceneQueryProbe2DGizmo(probe) {
|
|
68
|
+
if (EDITOR_NOT_IN_PREVIEW) {
|
|
69
|
+
const g = tryGetDebugGeometryRenderer();
|
|
70
|
+
if (g) {
|
|
71
|
+
drawCircle2D(g, {
|
|
72
|
+
center: Vec2.ZERO.clone(),
|
|
73
|
+
radius: probe.radius,
|
|
74
|
+
color: probeColor,
|
|
75
|
+
wireframe,
|
|
76
|
+
depthTest,
|
|
77
|
+
transform: getSceneQueryProbe2DTransform(probe),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export function drawCapsuleSceneQueryProbe2DGizmo(probe) {
|
|
83
|
+
if (EDITOR_NOT_IN_PREVIEW) {
|
|
84
|
+
const g = tryGetDebugGeometryRenderer();
|
|
85
|
+
if (g) {
|
|
86
|
+
const transform = getSceneQueryProbe2DTransform(probe);
|
|
87
|
+
const top = new Vec2(0, probe.halfHeight);
|
|
88
|
+
const bottom = new Vec2(0, -probe.halfHeight);
|
|
89
|
+
drawCircle2D(g, {
|
|
90
|
+
center: top,
|
|
91
|
+
radius: probe.radius,
|
|
92
|
+
color: probeColor,
|
|
93
|
+
wireframe,
|
|
94
|
+
depthTest,
|
|
95
|
+
transform,
|
|
96
|
+
});
|
|
97
|
+
drawCircle2D(g, {
|
|
98
|
+
center: bottom,
|
|
99
|
+
radius: probe.radius,
|
|
100
|
+
color: probeColor,
|
|
101
|
+
wireframe,
|
|
102
|
+
depthTest,
|
|
103
|
+
transform,
|
|
104
|
+
});
|
|
105
|
+
drawLineFromTo2D(g, new Vec2(-probe.radius, -probe.halfHeight), new Vec2(-probe.radius, probe.halfHeight), probeColor, {
|
|
106
|
+
depthTest,
|
|
107
|
+
transform,
|
|
108
|
+
});
|
|
109
|
+
drawLineFromTo2D(g, new Vec2(probe.radius, -probe.halfHeight), new Vec2(probe.radius, probe.halfHeight), probeColor, {
|
|
110
|
+
depthTest,
|
|
111
|
+
transform,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function getCollider2DTransform(collider) {
|
|
117
|
+
return Transform2DComponent.of(collider).matrix;
|
|
118
|
+
}
|
|
119
|
+
function getSceneQueryProbe2DTransform(probe) {
|
|
120
|
+
return Transform2DComponent.of(probe).matrix;
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=gizmo.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export { PhysicsWorld2D, RaycastResult, type CircleShapeDesc, type CapsuleShapeDesc, type RectShapeDesc, type ShapeTransformDesc, } from './physics-world-2d.js';
|
|
2
|
+
export { RigidBody2D, RigidBody2DType } from './rigid-body-2d.js';
|
|
3
|
+
export { Collider2D } from './collider-2d.js';
|
|
4
|
+
export { Physics2DSettings } from './physics-2d-settings.js';
|
|
5
|
+
export { BoxCollider2D } from './colliders/box-collider-2d.js';
|
|
6
|
+
export { CircleCollider2D } from './colliders/circle-collider-2d.js';
|
|
7
|
+
export { PolygonCollider2D } from './colliders/polygon-collider-2d.js';
|
|
8
|
+
export { CapsuleCollider2D } from './colliders/capsule-collider-2d.js';
|
|
9
|
+
export { SceneQueryProbe2D, type SceneQueryProbeSweepOptions } from './scene-query-probe-2d.js';
|
|
10
|
+
export { BoxSceneQueryProbe2D } from './scene-query-probes/box-scene-query-probe-2d.js';
|
|
11
|
+
export { CircleSceneQueryProbe2D } from './scene-query-probes/circle-scene-query-probe-2d.js';
|
|
12
|
+
export { CapsuleSceneQueryProbe2D } from './scene-query-probes/capsule-scene-query-probe-2d.js';
|
|
13
|
+
export { KinematicCharacterController2D } from './character-controller-2d.js';
|
|
14
|
+
export { initializePx2Impl as __init } from './px2-impl.js';
|
|
15
|
+
export { Physics2DDebugger } from '#physics-2d-debugger';
|
|
16
|
+
export { CollisionMatrix } from './collision-matrix.js';
|
|
17
|
+
export { PhysicsWorld2DSceneComponent } from './physics-world-2d-scene-component.js';
|
|
18
|
+
export { type Contact2DInfo } from './contact.js';
|
|
19
|
+
export { SceneQueryFilter, type ShapeCastQueryOptions, ColliderShapeCastHit, type ShapeIntersectionQueryOptions, ColliderShapeIntersection, RayColliderIntersection, } from './scene-query.js';
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export { PhysicsWorld2D, RaycastResult, } from './physics-world-2d.js';
|
|
2
|
+
export { RigidBody2D, RigidBody2DType } from './rigid-body-2d.js';
|
|
3
|
+
export { Collider2D } from './collider-2d.js';
|
|
4
|
+
export { Physics2DSettings } from './physics-2d-settings.js';
|
|
5
|
+
export { BoxCollider2D } from './colliders/box-collider-2d.js';
|
|
6
|
+
export { CircleCollider2D } from './colliders/circle-collider-2d.js';
|
|
7
|
+
export { PolygonCollider2D } from './colliders/polygon-collider-2d.js';
|
|
8
|
+
export { CapsuleCollider2D } from './colliders/capsule-collider-2d.js';
|
|
9
|
+
export { SceneQueryProbe2D } from './scene-query-probe-2d.js';
|
|
10
|
+
export { BoxSceneQueryProbe2D } from './scene-query-probes/box-scene-query-probe-2d.js';
|
|
11
|
+
export { CircleSceneQueryProbe2D } from './scene-query-probes/circle-scene-query-probe-2d.js';
|
|
12
|
+
export { CapsuleSceneQueryProbe2D } from './scene-query-probes/capsule-scene-query-probe-2d.js';
|
|
13
|
+
export { KinematicCharacterController2D } from './character-controller-2d.js';
|
|
14
|
+
export { initializePx2Impl as __init } from './px2-impl.js';
|
|
15
|
+
export { Physics2DDebugger } from '#physics-2d-debugger';
|
|
16
|
+
export { CollisionMatrix } from './collision-matrix.js';
|
|
17
|
+
export { PhysicsWorld2DSceneComponent } from './physics-world-2d-scene-component.js';
|
|
18
|
+
export {} from './contact.js';
|
|
19
|
+
export { SceneQueryFilter, ColliderShapeCastHit, ColliderShapeIntersection, RayColliderIntersection, } from './scene-query.js';
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Scene } from 'cc';
|
|
2
|
+
import { px2Impl } from './px2-impl.js';
|
|
3
|
+
export declare class Physics2DDebugger {
|
|
4
|
+
private _implWorld;
|
|
5
|
+
constructor(_implWorld: px2Impl.World, scene: Scene);
|
|
6
|
+
destroy(): void;
|
|
7
|
+
render(): void;
|
|
8
|
+
private _node;
|
|
9
|
+
private _renderer;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=physics-2d-debugger.d.ts.map
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { assetManager, EffectAsset, gfx, Material, Node, renderer, Renderer, RenderingSubMesh, Scene } from 'cc';
|
|
2
|
+
import { px2Impl } from './px2-impl.js';
|
|
3
|
+
class Physics2DDebugRenderer extends Renderer {
|
|
4
|
+
constructor() {
|
|
5
|
+
super();
|
|
6
|
+
const material = new Material('physics-2d-debugger');
|
|
7
|
+
material.reset({
|
|
8
|
+
effectName: 'builtin-unlit',
|
|
9
|
+
states: {
|
|
10
|
+
primitive: gfx.PrimitiveMode.LINE_LIST,
|
|
11
|
+
rasterizerState: {
|
|
12
|
+
lineWidth: 100,
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
this._material = material;
|
|
17
|
+
this._loadMaterial();
|
|
18
|
+
}
|
|
19
|
+
onEnable() {
|
|
20
|
+
if (this._renderRecord) {
|
|
21
|
+
this._renderRecord.model.enabled = true;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
onDisable() {
|
|
25
|
+
if (this._renderRecord) {
|
|
26
|
+
this._renderRecord.model.enabled = false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
render(world) {
|
|
30
|
+
const { vertices, colors } = world.debugRender();
|
|
31
|
+
const vertexCount = vertices.length / 2;
|
|
32
|
+
this._ensureModelCapacity(vertexCount);
|
|
33
|
+
const renderRecord = this._renderRecord;
|
|
34
|
+
if (renderRecord) {
|
|
35
|
+
this._setDrawVertexCount(renderRecord, vertexCount);
|
|
36
|
+
if (vertexCount === 0) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
// @ts-expect-error Cocos buffer typings omit the runtime Float32Array update overload.
|
|
40
|
+
renderRecord.vertexBuffer.update(vertices, vertices.byteLength);
|
|
41
|
+
// @ts-expect-error Cocos buffer typings omit the runtime Float32Array update overload.
|
|
42
|
+
renderRecord.colorVertexBuffer.update(colors, colors.byteLength);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
_material;
|
|
46
|
+
_materialLoaded = false;
|
|
47
|
+
_renderRecord;
|
|
48
|
+
_ensureModelCapacity(requiredVertexCount) {
|
|
49
|
+
if (requiredVertexCount === 0) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const renderRecord = this._renderRecord;
|
|
53
|
+
if (renderRecord) {
|
|
54
|
+
if (renderRecord.vertexCapacity >= requiredVertexCount) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
renderRecord.model.scene?.removeModel(renderRecord.model);
|
|
58
|
+
renderRecord.model.destroy();
|
|
59
|
+
renderRecord.vertexBuffer.destroy();
|
|
60
|
+
renderRecord.colorVertexBuffer.destroy();
|
|
61
|
+
this._renderRecord = undefined;
|
|
62
|
+
}
|
|
63
|
+
const renderScene = this._getRenderScene();
|
|
64
|
+
const device = renderScene.root.device;
|
|
65
|
+
const vertexBuffer = device.createBuffer(new gfx.BufferInfo(gfx.BufferUsageBit.VERTEX, gfx.MemoryUsageBit.DEVICE, requiredVertexCount * 2 * Float32Array.BYTES_PER_ELEMENT, 2 * Float32Array.BYTES_PER_ELEMENT, gfx.BufferFlagBit.NONE));
|
|
66
|
+
const colorVertexBuffer = device.createBuffer(new gfx.BufferInfo(gfx.BufferUsageBit.VERTEX, gfx.MemoryUsageBit.DEVICE, requiredVertexCount * 4 * Float32Array.BYTES_PER_ELEMENT, 4 * Float32Array.BYTES_PER_ELEMENT, gfx.BufferFlagBit.NONE));
|
|
67
|
+
const renderingSubMesh = new RenderingSubMesh([vertexBuffer, colorVertexBuffer], [
|
|
68
|
+
new gfx.Attribute(gfx.AttributeName.ATTR_POSITION, gfx.Format.RG32F, false, 0, false, undefined),
|
|
69
|
+
new gfx.Attribute(gfx.AttributeName.ATTR_COLOR, gfx.Format.RGBA32F, false, 1, false, undefined),
|
|
70
|
+
], gfx.PrimitiveMode.LINE_LIST, undefined, undefined);
|
|
71
|
+
const drawInfo = new gfx.DrawInfo();
|
|
72
|
+
renderingSubMesh.drawInfo = drawInfo;
|
|
73
|
+
const model = new renderer.scene.Model();
|
|
74
|
+
model.node = this.node;
|
|
75
|
+
model.transform = this.node;
|
|
76
|
+
model.initSubModel(0, renderingSubMesh, this._material);
|
|
77
|
+
renderScene.addModel(model);
|
|
78
|
+
this._renderRecord = {
|
|
79
|
+
vertexCapacity: requiredVertexCount,
|
|
80
|
+
drawInfo,
|
|
81
|
+
model,
|
|
82
|
+
renderingSubMesh,
|
|
83
|
+
vertexBuffer,
|
|
84
|
+
colorVertexBuffer,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
_setDrawVertexCount(renderRecord, vertexCount) {
|
|
88
|
+
renderRecord.drawInfo.vertexCount = vertexCount;
|
|
89
|
+
renderRecord.renderingSubMesh.drawInfo = renderRecord.drawInfo;
|
|
90
|
+
renderRecord.model.subModels[0]?.onGeometryChanged();
|
|
91
|
+
}
|
|
92
|
+
_loadMaterial() {
|
|
93
|
+
if (this._materialLoaded) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this._materialLoaded = true;
|
|
97
|
+
new Promise((resolve, reject) => {
|
|
98
|
+
assetManager.loadAny('0352391c-bc3d-4674-bed1-3472beacecc1', (err, effect) => {
|
|
99
|
+
if (err) {
|
|
100
|
+
reject(err);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
resolve(effect);
|
|
104
|
+
});
|
|
105
|
+
})
|
|
106
|
+
.then((effect) => {
|
|
107
|
+
const material = new Material('physics-2d-debugger');
|
|
108
|
+
material.reset({
|
|
109
|
+
effectAsset: effect,
|
|
110
|
+
states: {
|
|
111
|
+
primitive: gfx.PrimitiveMode.LINE_LIST,
|
|
112
|
+
rasterizerState: {
|
|
113
|
+
lineWidth: 100,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
material.setProperty('z', 1);
|
|
118
|
+
this._material = material;
|
|
119
|
+
if (this._renderRecord) {
|
|
120
|
+
this._renderRecord.model.setSubModelMaterial(0, material);
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
.catch((err) => {
|
|
124
|
+
console.error('Failed to load physics-2d-debugger material', err);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export class Physics2DDebugger {
|
|
129
|
+
_implWorld;
|
|
130
|
+
constructor(_implWorld, scene) {
|
|
131
|
+
this._implWorld = _implWorld;
|
|
132
|
+
const node = this._node = new Node(`physics-2d-debugger`);
|
|
133
|
+
this._node.parent = scene;
|
|
134
|
+
const renderer = node.addComponent(Physics2DDebugRenderer);
|
|
135
|
+
this._renderer = renderer;
|
|
136
|
+
}
|
|
137
|
+
destroy() {
|
|
138
|
+
this._node.destroy();
|
|
139
|
+
}
|
|
140
|
+
render() {
|
|
141
|
+
this._renderer.render(this._implWorld);
|
|
142
|
+
}
|
|
143
|
+
_node;
|
|
144
|
+
_renderer;
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=physics-2d-debugger.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Asset } from 'cc';
|
|
2
|
+
import { CollisionMatrix } from './collision-matrix.js';
|
|
3
|
+
export declare class Physics2DSettings extends Asset {
|
|
4
|
+
fps: number;
|
|
5
|
+
get tags(): Record<string, number>;
|
|
6
|
+
private get tags_editor();
|
|
7
|
+
private set tags_editor(value);
|
|
8
|
+
get collisionMatrix(): CollisionMatrix;
|
|
9
|
+
findTagIndex(tag: string): number;
|
|
10
|
+
private _tags;
|
|
11
|
+
private _collisionMatrix;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=physics-2d-settings.d.ts.map
|