diff --git a/packages/physics-lite/src/LitePhysicsScene.ts b/packages/physics-lite/src/LitePhysicsScene.ts index 5edeacf039..0768191d1c 100644 --- a/packages/physics-lite/src/LitePhysicsScene.ts +++ b/packages/physics-lite/src/LitePhysicsScene.ts @@ -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; @@ -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; } } } diff --git a/tests/src/core/physics/PhysicsManager.test.ts b/tests/src/core/physics/PhysicsManager.test.ts index 1a9555d116..013a8f3d7a 100644 --- a/tests/src/core/physics/PhysicsManager.test.ts +++ b/tests/src/core/physics/PhysicsManager.test.ts @@ -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)); @@ -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);