Skip to content

Commit

Permalink
Fix litePhysics raycast error (#2321)
Browse files Browse the repository at this point in the history
* fix: litePhysics raycast error
  • Loading branch information
luzhuang authored Aug 8, 2024
1 parent ae08a6b commit 1fe6043
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
42 changes: 16 additions & 26 deletions packages/physics-lite/src/LitePhysicsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ export class LitePhysicsScene implements IPhysicsScene {
);
} else {
const raycastStaticRes = this._raycast(ray, distance, onRaycast, this._staticColliders, hit);
const raycastDynamicRes = this._raycast(
ray,
LitePhysicsScene._currentHit.distance,
onRaycast,
this._dynamicColliders,
hit
);

if (raycastStaticRes) {
distance = LitePhysicsScene._currentHit.distance;
}

const raycastDynamicRes = this._raycast(ray, distance, onRaycast, this._dynamicColliders, hit);
const isHit = raycastStaticRes || raycastDynamicRes;
const hitResult = LitePhysicsScene._hitResult;

Expand Down Expand Up @@ -323,28 +322,19 @@ export class LitePhysicsScene implements IPhysicsScene {
colliders: LiteCollider[],
hit?: (shapeUniqueID: number, distance: number, position: Vector3, normal: Vector3) => void
): boolean {
let hitResult: LiteHitResult;
if (hit) {
hitResult = LitePhysicsScene._hitResult;
}

let isHit = false;
const curHit = LitePhysicsScene._currentHit;
for (let i = 0, len = colliders.length; i < len; i++) {
const collider = colliders[i];

if (collider._raycast(ray, onRaycast, curHit)) {
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 (colliders[i]._raycast(ray, onRaycast, curHit) && curHit.distance < distance) {
if (hit) {
isHit = true;
const hitResult = LitePhysicsScene._hitResult;
hitResult.normal.copyFrom(curHit.normal);
hitResult.point.copyFrom(curHit.point);
hitResult.distance = distance = curHit.distance;
hitResult.shapeID = curHit.shapeID;
} else {
return true;
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions tests/src/core/physics/PhysicsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ describe("Physics Test", () => {
ray = new Ray(new Vector3(0, 3, 0), new Vector3(0, -1, 0));
expect(engineLite.physicsManager.raycast(ray, Number.MAX_VALUE, outHitResult)).to.eq(true);
expect(outHitResult.shape.id).to.eq(box2.id);

ray = new Ray(new Vector3(0, -3, 0), new Vector3(0, 1, 0));
box.position = new Vector3(1, 0, 0);

// Test that raycast nothing if distance is less than distance of origin to detected collider.
expect(engineLite.physicsManager.raycast(ray, -3, outHitResult)).to.eq(false);

box.position = new Vector3(0, 0, 0);
collider2.destroy();
// Test that raycast with outHitResult works correctly.
ray = new Ray(new Vector3(3, 3, 3), new Vector3(-1, -1.25, -1));
Expand Down Expand Up @@ -297,8 +305,8 @@ describe("Physics Test", () => {
expect(outHitResult.shape).to.be.eq(box);

// Test that raycast nothing if distance is less than distance of origin to detected collider.
expect(engineLite.physicsManager.raycast(ray, 0, Layer.Everything, outHitResult)).to.eq(true);
expect(engineLite.physicsManager.raycast(ray, -1, Layer.Everything, outHitResult)).to.eq(true);
expect(engineLite.physicsManager.raycast(ray, 0, Layer.Everything, outHitResult)).to.eq(false);
expect(engineLite.physicsManager.raycast(ray, -1, Layer.Everything, outHitResult)).to.eq(false);

collider.removeShape(box);
expect(engineLite.physicsManager.raycast(ray)).to.eq(false);
Expand Down

0 comments on commit 1fe6043

Please sign in to comment.