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

Fix physics system stablity #1047

Merged
merged 6 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 12 additions & 2 deletions packages/core/src/physics/CharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ export class CharacterController extends Collider {
throw "only allow single shape on controller!";
}
super.addShape(shape);

const { transform } = this.entity;
const shapes = this.shapes;
(<ICharacterController>this._nativeCollider).setWorldPosition(transform.worldPosition);

const worldScale = transform.lossyWorldScale;
for (let i = 0, n = shapes.length; i < n; i++) {
shapes[i]._nativeShape.setWorldScale(worldScale);
}
}

/**
Expand All @@ -116,11 +125,12 @@ export class CharacterController extends Collider {
_onUpdate() {
if (this._updateFlag.flag) {
const { transform } = this.entity;
const shapes = this.shapes;
(<ICharacterController>this._nativeCollider).setWorldPosition(transform.worldPosition);

const worldScale = transform.lossyWorldScale;
for (let i = 0, n = this.shapes.length; i < n; i++) {
this.shapes[i]._nativeShape.setWorldScale(worldScale);
for (let i = 0, n = shapes.length; i < n; i++) {
shapes[i]._nativeShape.setWorldScale(worldScale);
}
this._updateFlag.flag = false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/physics/PhysicsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class PhysicsManager {
): boolean {
let hitResult: HitResult;

let distance = Number.MAX_VALUE;
let distance = 1.0e7;
if (typeof distanceOrResult === "number") {
distance = distanceOrResult;
} else if (distanceOrResult != undefined) {
Expand Down
13 changes: 10 additions & 3 deletions packages/physics-physx/src/PhysXCharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export class PhysXCharacterController implements ICharacterController {
* {@inheritDoc ICharacterController.setWorldPosition }
*/
setWorldPosition(position: Vector3): boolean {
return this._pxController.setPosition(position);
if (this._pxController) {
return this._pxController.setPosition(position);
} else {
return false;
}
}

/**
Expand Down Expand Up @@ -114,13 +118,16 @@ export class PhysXCharacterController implements ICharacterController {
desc.setMaterial(shape._pxMaterials[0]);

this._pxController = pxManager._getControllerManager().createController(desc);
this._pxController.setQueryFilterData(new PhysXPhysics._physX.PxFilterData(shape._id, 0, 0, 0));
}

/**
* @internal
*/
_destroyPXController(): void {
this._pxController.release();
this._pxController = null;
if (this._pxController) {
this._pxController.release();
this._pxController = null;
}
}
}
2 changes: 1 addition & 1 deletion packages/physics-physx/src/shape/PhysXColliderShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export abstract class PhysXColliderShape implements IColliderShape {
this._pxShape = PhysXPhysics._pxPhysics.createShape(
this._pxGeometry,
material._pxMaterial,
false,
true,
new PhysXPhysics._physX.PxShapeFlags(this._shapeFlags)
);
this._pxShape.setQueryFilterData(new PhysXPhysics._physX.PxFilterData(id, 0, 0, 0));
Expand Down