Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opt physics experience and opt physics unit test #2420

Merged
merged 55 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
376abf7
test: change floss to vitest
luzhuang Oct 18, 2024
b5fcbaa
test: update test case
luzhuang Oct 18, 2024
1cc6a9e
Merge branch dev/1.4 of github.com:oasis-engine/engine into test/refa…
luzhuang Oct 18, 2024
0bc262c
test: update dependence
luzhuang Oct 18, 2024
213958a
test: update config
luzhuang Oct 18, 2024
d7647be
test: update config
luzhuang Oct 18, 2024
47bb76d
test: update config
luzhuang Oct 18, 2024
696e7d5
test: update config
luzhuang Oct 18, 2024
b59711d
test: update config
luzhuang Oct 18, 2024
9431ab7
test: update config
luzhuang Oct 18, 2024
979e2e6
test: update config
luzhuang Oct 18, 2024
7a02b4b
test: update config
luzhuang Oct 18, 2024
2a8e038
feat: stash
luzhuang Oct 18, 2024
e44f652
test: update config
luzhuang Oct 18, 2024
9527d5c
Merge branch 'test/refactor' into test/physics
luzhuang Oct 18, 2024
8031abb
refactor: opt dynamicCollider experience
luzhuang Oct 21, 2024
8a3b7c1
refactor: opt characterController experience
luzhuang Oct 23, 2024
80c4afb
refactor: opt joint experience
luzhuang Nov 4, 2024
ad85911
test: opt physicsMaterial unit tests
luzhuang Nov 5, 2024
1ed1b1b
Merge branch dev/1.4 of github.com:oasis-engine/engine into test/physics
luzhuang Nov 5, 2024
790e7e5
refactor: opt code
luzhuang Nov 5, 2024
4e564b9
refactor: opt code
luzhuang Nov 5, 2024
06dce2c
refactor: opt code
luzhuang Nov 6, 2024
d8bf792
refactor: opt code
luzhuang Nov 6, 2024
c1da8d7
refactor: opt code
luzhuang Nov 6, 2024
a11cbc9
refactor: opt code
luzhuang Nov 6, 2024
4a00069
refactor: opt code
luzhuang Nov 6, 2024
c3ccafe
refactor: opt code
luzhuang Nov 6, 2024
6a5f6ce
refactor: opt code
luzhuang Nov 6, 2024
ef3c228
refactor: opt code
luzhuang Nov 11, 2024
ae63786
test: opt unit test
luzhuang Nov 21, 2024
5bda3ac
refactor: opt code
luzhuang Nov 21, 2024
6d4deab
refactor: opt code
luzhuang Nov 21, 2024
288b627
refactor: opt code
luzhuang Nov 21, 2024
aabeada
test: opt unit test
luzhuang Nov 22, 2024
988c10f
test: opt unit test
luzhuang Nov 22, 2024
059af77
refactor: opt code
luzhuang Nov 26, 2024
48f876d
refactor: opt code
luzhuang Nov 26, 2024
8407e3c
refactor: opt code
luzhuang Nov 27, 2024
e95ce84
refactor: opt code
luzhuang Nov 27, 2024
55b618e
refactor: opt code
luzhuang Nov 27, 2024
4e3ea94
refactor: opt code
luzhuang Nov 27, 2024
e7922ee
refactor: opt code
luzhuang Nov 27, 2024
e410f89
refactor: opt code
luzhuang Nov 27, 2024
10f552b
refactor: opt code
luzhuang Nov 27, 2024
022f838
refactor: opt code
luzhuang Nov 27, 2024
13cee3e
refactor: opt code
luzhuang Nov 27, 2024
0130cee
refactor: opt code
luzhuang Nov 28, 2024
78fa5ad
refactor: opt code
luzhuang Nov 28, 2024
d5f9aab
refactor: opt code
luzhuang Nov 28, 2024
94cefcf
refactor: opt code
luzhuang Nov 28, 2024
d3fb9bb
refactor: opt code
luzhuang Nov 28, 2024
c768cc9
refactor: opt code
luzhuang Nov 28, 2024
1672ab8
refactor: opt code
luzhuang Nov 28, 2024
f8dc9c4
refactor: opt code
luzhuang Nov 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export class Engine extends EventDispatcher {
static _noDepthTextureMacro: ShaderMacro = ShaderMacro.getByName("ENGINE_NO_DEPTH_TEXTURE");
/** @internal Conversion of space units to pixel units for 2D. */
static _pixelsPerUnit: number = 100;
/** @internal */
static _physicalObjectsMap: Record<number, ColliderShape> = {};

/** Input manager of Engine. */
readonly inputManager: InputManager;
Expand All @@ -66,8 +68,6 @@ export class Engine extends EventDispatcher {
/** @internal */
_physicsInitialized: boolean = false;
/** @internal */
_physicalObjectsMap: Record<number, ColliderShape> = {};
/** @internal */
_nativePhysicsManager: IPhysicsManager;
/* @internal */
_hardwareRenderer: IHardwareRenderer;
Expand Down
41 changes: 17 additions & 24 deletions packages/core/src/physics/CharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import { Collider } from "./Collider";
import { PhysicsScene } from "./PhysicsScene";
import { ControllerNonWalkableMode } from "./enums/ControllerNonWalkableMode";
import { ColliderShape } from "./shape";
import { deepClone } from "../clone/CloneManager";

/**
* The character controllers.
*/
export class CharacterController extends Collider {
private _stepOffset: number = 0.5;
private _stepOffset = 0.5;
private _nonWalkableMode: ControllerNonWalkableMode = ControllerNonWalkableMode.PreventClimbing;
@deepClone
private _upDirection = new Vector3(0, 1, 0);
private _slopeLimit: number = 0.707;
private _slopeLimit = 0.707;

/**
* The step offset for the controller.
* @remarks Character can overcome obstacle less than the height(stepOffset + contractOffset(0.1)).
*/
get stepOffset(): number {
return this._stepOffset;
Expand Down Expand Up @@ -57,7 +60,8 @@ export class CharacterController extends Collider {
}

/**
* The slope limit for the controller.
* The slope limit for the controller, the value is the cosine value of the maximum slope angle.
* @defaultValue 0.707(the cosine value of 45 degrees)
*/
get slopeLimit(): number {
return this._slopeLimit;
Expand Down Expand Up @@ -110,15 +114,6 @@ export class CharacterController extends Collider {
this._updateFlag.flag = true;
}

/**
* Remove all shape attached.
*/
override clearShapes(): void {
if (this._shapes.length > 0) {
super.removeShape(this._shapes[0]);
}
}

/**
* @internal
*/
Expand Down Expand Up @@ -148,24 +143,22 @@ export class CharacterController extends Collider {
* @internal
*/
override _onEnableInScene() {
const physics = this.scene.physics;
physics._addCharacterController(this);
const shapes = this.shapes;
for (let i = 0, n = shapes.length; i < n; i++) {
physics._addColliderShape(shapes[i]);
}
this.scene.physics._addCharacterController(this);
}

/**
* @internal
*/
override _onDisableInScene() {
const physics = this.scene.physics;
physics._removeCharacterController(this);
const shapes = this.shapes;
for (let i = 0, n = shapes.length; i < n; i++) {
physics._removeColliderShape(shapes[i]);
}
this.scene.physics._removeCharacterController(this);
}

protected override _syncNative(): void {
super._syncNative();
(<ICharacterController>this._nativeCollider).setStepOffset(this._stepOffset);
(<ICharacterController>this._nativeCollider).setNonWalkableMode(this._nonWalkableMode);
(<ICharacterController>this._nativeCollider).setUpDirection(this._upDirection);
(<ICharacterController>this._nativeCollider).setSlopeLimit(this._slopeLimit);
}

private _syncWorldPositionFromPhysicalSpace(): void {
Expand Down
53 changes: 24 additions & 29 deletions packages/core/src/physics/Collider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ export class Collider extends Component implements ICustomClone {
if (oldCollider) {
oldCollider.removeShape(shape);
}

this._shapes.push(shape);
shape._collider = this;
this._nativeCollider.addShape(shape._nativeShape);
this._phasedActiveInScene && this.scene.physics._addColliderShape(shape);
this._addNativeShape(shape);
}
}

Expand All @@ -66,9 +63,7 @@ export class Collider extends Component implements ICustomClone {
const index = this._shapes.indexOf(shape);
if (index !== -1) {
this._shapes.splice(index, 1);
this._phasedActiveInScene && this.scene.physics._removeColliderShape(shape);
shape._collider = null;
this._nativeCollider.removeShape(shape._nativeShape);
this._removeNativeShape(shape);
}
}

Expand All @@ -79,9 +74,7 @@ export class Collider extends Component implements ICustomClone {
const shapes = this._shapes;
for (let i = 0, n = shapes.length; i < n; i++) {
const shape = shapes[i];
this._phasedActiveInScene && this.scene.physics._removeColliderShape(shape);
shape._destroy();
this._nativeCollider.removeShape(shape._nativeShape);
this._removeNativeShape(shape);
}
shapes.length = 0;
}
Expand Down Expand Up @@ -114,33 +107,26 @@ export class Collider extends Component implements ICustomClone {
* @internal
*/
override _onEnableInScene(): void {
const physics = this.scene.physics;
physics._addCollider(this);
const shapes = this.shapes;
for (let i = 0, n = shapes.length; i < n; i++) {
physics._addColliderShape(shapes[i]);
}
this.scene.physics._addCollider(this);
}

/**
* @internal
*/
override _onDisableInScene(): void {
const physics = this.scene.physics;
physics._removeCollider(this);
const shapes = this.shapes;
for (let i = 0, n = shapes.length; i < n; i++) {
physics._removeColliderShape(shapes[i]);
}
this.scene.physics._removeCollider(this);
}

/**
* @internal
*/
_cloneTo(target: Collider): void {
const shapes = target._shapes;
for (let i = 0, n = shapes.length; i < n; i++) {
target._addPhysicsShape(shapes[i]);
target._syncNative();
}

protected _syncNative(): void {
for (let i = 0, n = this.shapes.length; i < n; i++) {
this._addNativeShape(this.shapes[i]);
}
}

Expand All @@ -149,14 +135,23 @@ export class Collider extends Component implements ICustomClone {
*/
protected override _onDestroy(): void {
super._onDestroy();
this.clearShapes();
const shapes = this._shapes;
for (let i = 0, n = shapes.length; i < n; i++) {
const shape = shapes[i];
this._removeNativeShape(shape);
shape._destroy();
}
shapes.length = 0;
this._nativeCollider.destroy();
}

protected _addPhysicsShape(shape: ColliderShape): void {
protected _addNativeShape(shape: ColliderShape): void {
shape._collider = this;

this._nativeCollider.addShape(shape._nativeShape);
this._phasedActiveInScene && this.scene.physics._addColliderShape(shape);
}

protected _removeNativeShape(shape: ColliderShape): void {
shape._collider = null;
this._nativeCollider.removeShape(shape._nativeShape);
}
}
Loading
Loading