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 litePhysics raycast error #2321

Merged
merged 3 commits into from
Aug 8, 2024
Merged
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
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
Loading