Skip to content

Commit ebd9036

Browse files
authoredFeb 22, 2024··
Physics raycast hitResult add shape (#2004)
* feat: hitResult add shape
1 parent 95a3b05 commit ebd9036

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed
 

‎packages/core/src/physics/HitResult.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Entity } from "../Entity";
22
import { Vector3 } from "@galacean/engine-math";
3+
import { ColliderShape } from "./shape";
34

45
/**
56
* Structure used to get information back from a raycast or a sweep.
@@ -13,4 +14,6 @@ export class HitResult {
1314
point: Vector3 = new Vector3();
1415
/** The normal of the surface the ray hit. */
1516
normal: Vector3 = new Vector3();
17+
/** The shape of the collider that was hit. */
18+
shape: ColliderShape = null;
1619
}

‎packages/core/src/physics/PhysicsScene.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ export class PhysicsScene {
315315

316316
if (hitResult != undefined) {
317317
const result = this._nativePhysicsScene.raycast(ray, distance, onRaycast, (idx, distance, position, normal) => {
318-
hitResult.entity = this._scene.engine._physicalObjectsMap[idx]._collider.entity;
318+
const hitShape = this._scene.engine._physicalObjectsMap[idx];
319+
hitResult.entity = hitShape._collider.entity;
320+
hitResult.shape = hitShape;
319321
hitResult.distance = distance;
320322
hitResult.normal.copyFrom(normal);
321323
hitResult.point.copyFrom(position);
@@ -325,6 +327,7 @@ export class PhysicsScene {
325327
return true;
326328
} else {
327329
hitResult.entity = null;
330+
hitResult.shape = null;
328331
hitResult.distance = 0;
329332
hitResult.point.set(0, 0, 0);
330333
hitResult.normal.set(0, 0, 0);

‎tests/src/core/physics/PhysicsManager.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ describe("Physics Test", () => {
190190
expect(outHitResult.point.z).to.be.closeTo(0.5, 0.01);
191191
expect(outHitResult.normal).to.be.deep.include({ x: 0, y: 0, z: 0 });
192192
expect(outHitResult.entity).to.be.eq(raycastTestRoot);
193+
expect(outHitResult.shape).to.be.eq(box);
193194

194195
// Test that raycast with outHitResult works correctly.
195196
expect(engineLite.physicsManager.raycast(ray, Number.MAX_VALUE, outHitResult)).to.eq(true);
@@ -199,20 +200,23 @@ describe("Physics Test", () => {
199200
expect(outHitResult.point.z).to.be.closeTo(0.5, 0.01);
200201
expect(outHitResult.normal).to.be.deep.include({ x: 0, y: 0, z: 0 });
201202
expect(outHitResult.entity).to.be.eq(raycastTestRoot);
203+
expect(outHitResult.shape).to.be.eq(box);
202204

203205
// Test that raycast nothing if layer is not match.
204206
expect(engineLite.physicsManager.raycast(ray, Number.MAX_VALUE, Layer.Layer1, outHitResult)).to.eq(false);
205207
expect(outHitResult.distance).to.be.eq(0);
206208
expect(outHitResult.point).to.be.deep.include({ x: 0, y: 0, z: 0 });
207209
expect(outHitResult.normal).to.be.deep.include({ x: 0, y: 0, z: 0 });
208210
expect(outHitResult.entity).to.be.null;
211+
expect(outHitResult.shape).to.be.null;
209212

210213
// Test that return origin point if origin is inside collider.
211214
ray = new Ray(new Vector3(0.25, -0.5, 0.5), new Vector3(0, -1, 0));
212215
expect(engineLite.physicsManager.raycast(ray, outHitResult)).to.eq(true);
213216
expect(outHitResult.distance).to.be.eq(0);
214217
expect(outHitResult.point).to.be.deep.include({ x: 0.25, y: -0.5, z: 0.5 });
215218
expect(outHitResult.entity).to.be.eq(raycastTestRoot);
219+
expect(outHitResult.shape).to.be.eq(box);
216220

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

0 commit comments

Comments
 (0)
Please sign in to comment.