Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion packages/core/src/physics/CharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class CharacterController extends Collider {

/**
* The step offset for the controller, the value must be greater than or equal to 0.
* @remarks Character can overcome obstacle less than the height(stepOffset + contractOffset of the shape).
* @remarks Character can overcome obstacle less than the height(stepOffset + contactOffset of the shape).
*/
get stepOffset(): number {
return this._stepOffset;
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/physics/Collider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICollider, IStaticCollider } from "@galacean/engine-design";
import { ICollider, IColliderShape, IStaticCollider } from "@galacean/engine-design";
import { BoolUpdateFlag } from "../BoolUpdateFlag";
import { deepClone, ignoreClone } from "../clone/CloneManager";
import { ICustomClone } from "../clone/ComponentCloner";
Expand Down Expand Up @@ -196,6 +196,21 @@ export class Collider extends Component implements ICustomClone {
}
}

/**
* Replace an attached native shape while keeping attachment ownership in the collider.
* @internal
*/
_replaceNativeShape(shape: ColliderShape, nativeShape: IColliderShape | null): void {
const previousShape = shape._nativeShape;
if (previousShape === nativeShape) return;

previousShape && this._nativeCollider.removeShape(previousShape);
shape._nativeShape = nativeShape;
nativeShape && this._nativeCollider.addShape(nativeShape);
previousShape?.destroy();
this._handleShapesChanged(ColliderShapeChangeFlag.Property);
}

private _setCollisionLayer(): void {
this._nativeCollider.setCollisionLayer(this._collisionLayerIndex);
}
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/physics/DynamicCollider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DynamicCollider extends Collider {
private _isKinematic = false;
private _constraints: DynamicColliderConstraints = 0;
private _collisionDetectionMode: CollisionDetectionMode = CollisionDetectionMode.Discrete;
private _sleepThreshold = 5e-3;
private _sleepThreshold: number | undefined;
private _automaticCenterOfMass = true;
private _automaticInertiaTensor = true;

Expand Down Expand Up @@ -221,9 +221,11 @@ export class DynamicCollider extends Collider {

/**
* The mass-normalized energy threshold, below which objects start going to sleep.
* @remarks The default is determined by the physics backend. PhysX uses
* `5e-5 * tolerancesScale.speed * tolerancesScale.speed`.
*/
get sleepThreshold(): number {
return this._sleepThreshold;
return this._sleepThreshold ?? Engine._nativePhysics?.getDefaultSleepThreshold?.() ?? 5e-3;
}

set sleepThreshold(value: number) {
Expand Down Expand Up @@ -498,7 +500,9 @@ export class DynamicCollider extends Collider {
}
(<IDynamicCollider>this._nativeCollider).setMaxAngularVelocity(this._maxAngularVelocity);
(<IDynamicCollider>this._nativeCollider).setMaxDepenetrationVelocity(this._maxDepenetrationVelocity);
(<IDynamicCollider>this._nativeCollider).setSleepThreshold(this._sleepThreshold);
if (this._sleepThreshold !== undefined) {
(<IDynamicCollider>this._nativeCollider).setSleepThreshold(this._sleepThreshold);
}
(<IDynamicCollider>this._nativeCollider).setSolverIterations(this._solverIterations);
(<IDynamicCollider>this._nativeCollider).setUseGravity(this._useGravity);
(<IDynamicCollider>this._nativeCollider).setIsKinematic(this._isKinematic);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/physics/PhysicsMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class PhysicsMaterial {
this._staticFriction,
this._dynamicFriction,
this._bounciness,
this._bounceCombine,
this._frictionCombine
this._frictionCombine,
this._bounceCombine
);
}

Expand Down
29 changes: 20 additions & 9 deletions packages/core/src/physics/shape/ColliderShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ export abstract class ColliderShape implements ICustomClone {

@ignoreClone
protected _id: number;
@ignoreClone
protected _material: PhysicsMaterial;
private _isTrigger: boolean = false;
@deepClone
private _rotation: Vector3 = new Vector3();
@deepClone
private _position: Vector3 = new Vector3();
private _contactOffset: number = 0.02;
private _contactOffset: number | undefined;

/**
* @internal
Expand All @@ -52,10 +53,10 @@ export abstract class ColliderShape implements ICustomClone {

/**
* Contact offset for this shape, the value must be greater than or equal to 0.
* @defaultValue 0.02
* @remarks The default is determined by the physics backend. PhysX uses `0.02 * tolerancesScale.length`.
*/
get contactOffset(): number {
return this._contactOffset;
return this._contactOffset ?? Engine._nativePhysics?.getDefaultContactOffset?.() ?? 0.02;
}

set contactOffset(value: number) {
Expand Down Expand Up @@ -165,7 +166,10 @@ export abstract class ColliderShape implements ICustomClone {
* @internal
*/
_cloneTo(target: ColliderShape) {
const defaultMaterial = target._material;
target._material = this._material;
target._syncNative();
defaultMaterial.destroy();
}

/**
Expand All @@ -180,16 +184,23 @@ export abstract class ColliderShape implements ICustomClone {
}

protected _syncNative(): void {
if (!this._nativeShape) return;
this._nativeShape.setPosition(this._position);
this._nativeShape.setRotation(this._rotation);
this._nativeShape.setContactOffset(this._contactOffset);
this._nativeShape.setIsTrigger(this._isTrigger);
this._nativeShape.setMaterial(this._material._nativeMaterial);
const nativeShape = this._nativeShape;
if (!nativeShape) return;
this._syncNativeShape(nativeShape);

this._collider?._handleShapesChanged(ColliderShapeChangeFlag.Property);
}

protected _syncNativeShape(nativeShape: IColliderShape): void {
nativeShape.setPosition(this._position);
nativeShape.setRotation(this._rotation);
if (this._contactOffset !== undefined) {
nativeShape.setContactOffset(this._contactOffset);
}
nativeShape.setIsTrigger(this._isTrigger);
nativeShape.setMaterial(this._material._nativeMaterial);
}

@ignoreClone
private _setPosition(): void {
this._nativeShape?.setPosition(this._position);
Expand Down
Loading
Loading