Skip to content

Commit

Permalink
Fix a series of physical system problems (#1712)
Browse files Browse the repository at this point in the history
* fix: onCollisionEnter not triggered.

* fix: onCollisionEnter not triggered.

* fix: collider setPosition error

* fix: lite Physics rayCast error when distance is lower than hit distance

* feat: opt code

* feat: opt code

* feat: add unit test

* feat: opt code
  • Loading branch information
luzhuang authored Aug 28, 2023
1 parent 3d6bd03 commit 2a788ca
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 55 deletions.
20 changes: 9 additions & 11 deletions packages/physics-lite/src/LitePhysicsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,17 @@ export class LitePhysicsManager implements IPhysicsManager {
for (let i = 0, len = colliders.length; i < len; i++) {
const collider = colliders[i];

if (collider._raycast(ray, onRaycast, curHit)) {
if (collider._raycast(ray, onRaycast, curHit) && curHit.distance < distance) {
isHit = true;
if (curHit.distance < distance) {
if (hitResult) {
hitResult.normal.copyFrom(curHit.normal);
hitResult.point.copyFrom(curHit.point);
hitResult.distance = curHit.distance;
hitResult.shapeID = curHit.shapeID;
} else {
return true;
}
distance = curHit.distance;
if (hitResult) {
hitResult.normal.copyFrom(curHit.normal);
hitResult.point.copyFrom(curHit.point);
hitResult.distance = curHit.distance;
hitResult.shapeID = curHit.shapeID;
} else {
return true;
}
distance = curHit.distance;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/physics-lite/src/shape/LiteSphereColliderShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class LiteSphereColliderShape extends LiteColliderShape implements ISpher
* {@inheritDoc IColliderShape.setWorldScale }
*/
setWorldScale(scale: Vector3): void {
this._maxScale = Math.max(scale.x, Math.max(scale.x, scale.y));
this._maxScale = Math.max(scale.x, scale.y, scale.z);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/physics-physx/libs/physx.release.js

Large diffs are not rendered by default.

64 changes: 32 additions & 32 deletions packages/physics-physx/libs/physx.release.js.js

Large diffs are not rendered by default.

Binary file modified packages/physics-physx/libs/physx.release.js.mem
Binary file not shown.
Binary file added packages/physics-physx/libs/physx.release.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/physics-physx/src/PhysXCharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ export class PhysXCharacterController implements ICharacterController {
*/
_setLocalPosition(position: Vector3, scale: Vector3): void {
Vector3.multiply(position, scale, this._scaledOffset);
this.setWorldPosition(this._position);
this.setWorldPosition(position);
}
}
8 changes: 3 additions & 5 deletions packages/physics-physx/src/PhysXPhysics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ISpringJoint,
IStaticCollider
} from "@galacean/engine-design";
import { Quaternion, Vector3 } from "@galacean/engine";
import { Quaternion, Vector3, version } from "@galacean/engine";
import { PhysXRuntimeMode } from "./enum/PhysXRuntimeMode";
import { PhysXFixedJoint } from "./joint/PhysXFixedJoint";
import { PhysXHingeJoint } from "./joint/PhysXHingeJoint";
Expand Down Expand Up @@ -87,11 +87,9 @@ export class PhysXPhysics implements IPhysics {
}

if (runtimeMode == PhysXRuntimeMode.JavaScript) {
script.src =
"https://gw.alipayobjects.com/os/lib/oasis-engine/physics-physx/1.0.0-alpha.4/libs/physx.release.js.js";
script.src = `https://gw.alipayobjects.com/os/lib/galacean/engine-physics-physx/${version}/libs/physx.release.js.js`;
} else if (runtimeMode == PhysXRuntimeMode.WebAssembly) {
script.src =
"https://gw.alipayobjects.com/os/lib/oasis-engine/physics-physx/1.0.0-alpha.4/libs/physx.release.js";
script.src = `https://gw.alipayobjects.com/os/lib/galacean/engine-physics-physx/${version}/libs/physx.release.js`;
}
});

Expand Down
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 @@ -72,7 +72,7 @@ export abstract class PhysXColliderShape implements IColliderShape {
}
const controllers = this._controllers;
for (let i = 0, n = controllers.length; i < n; i++) {
controllers.get(i)._pxController.setLocalPosition(this._position, this._scale);
controllers.get(i)._setLocalPosition(this._position, this._scale);
}

this._setLocalPose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class PhysXSphereColliderShape extends PhysXColliderShape implements ISph
override setWorldScale(scale: Vector3): void {
super.setWorldScale(scale);

this._maxScale = Math.max(scale.x, Math.max(scale.x, scale.y));
this._maxScale = Math.max(scale.x, scale.y, scale.z);
this._pxGeometry.radius = this._radius * this._maxScale;
this._pxShape.setGeometry(this._pxGeometry);
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"@galacean/engine-design": "workspace:*",
"@galacean/engine-math": "workspace:*",
"@galacean/engine-rhi-webgl": "workspace:*",
"@galacean/engine-physics-lite": "workspace:*"
"@galacean/engine-physics-lite": "workspace:*",
"@galacean/engine-physics-physx": "workspace:*"

}
}
}
38 changes: 38 additions & 0 deletions tests/src/core/physics/CharacterController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Entity, CapsuleColliderShape, CharacterController } from "@galacean/engine-core";
import { Ray, Vector3 } from "@galacean/engine-math";
import { PhysXPhysics } from "@galacean/engine-physics-physx";
import { WebGLEngine } from "@galacean/engine-rhi-webgl";
import { expect } from "chai";

describe("physics collider test", () => {
let engine: WebGLEngine;
let rootEntity: Entity;
let controllerEntity: Entity;

before(async () => {
engine = await WebGLEngine.create({ canvas: document.createElement("canvas"), physics: new PhysXPhysics() });
const scene = engine.sceneManager.activeScene;
rootEntity = scene.createRootEntity("root");

engine.run();
});

beforeEach(() => {
rootEntity.clearChildren();

controllerEntity = rootEntity.createChild("controller");
});

it("Set Position", async () => {
const physicsCapsule = new CapsuleColliderShape();
physicsCapsule.radius = 0.15;
physicsCapsule.height = 0.2;
const characterController = controllerEntity.addComponent(CharacterController);
characterController.addShape(physicsCapsule);

physicsCapsule.position = new Vector3(5, 3, 3);
expect(physicsCapsule.position.x).to.equal(5);
expect(physicsCapsule.position.y).to.equal(3);
expect(physicsCapsule.position.z).to.equal(3);
});
});
Loading

0 comments on commit 2a788ca

Please sign in to comment.