Skip to content

Commit 3801a63

Browse files
committed
refactor: opt code
1 parent 9c4298b commit 3801a63

File tree

6 files changed

+17
-16
lines changed

6 files changed

+17
-16
lines changed

packages/core/src/physics/shape/ColliderShape.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,8 @@ export abstract class ColliderShape implements ICustomClone {
147147
console.warn("The collider is not active in scene.");
148148
return -1;
149149
}
150-
const tempQuat = ColliderShape._tempWorldRot;
151-
const tempPos = ColliderShape._tempWorldPos;
152-
Vector3.transformCoordinate(this._position, collider.entity.transform.worldMatrix, tempPos);
153150

154-
const rotation = this._rotation;
155-
Quaternion.rotationEuler(rotation.x, rotation.y, rotation.z, tempQuat);
156-
Quaternion.multiply(this._collider.entity.transform.rotationQuaternion, tempQuat, tempQuat);
157-
158-
const res = this._nativeShape.pointDistance(tempPos, tempQuat, point);
151+
const res = this._nativeShape.pointDistance(point);
159152
const distance = res.w;
160153
if (distance > 0) {
161154
outClosestPoint.set(res.x, res.y, res.z);

packages/design/src/physics/shape/IColliderShape.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ export interface IColliderShape {
4343

4444
/**
4545
* Get the distance between a point and the shape.
46-
* @param position - The position in world space
47-
* @param rotation - The rotation in world space
4846
* @param point - The point
4947
* @returns The distance information
5048
*/
51-
pointDistance(position: Vector3, rotation: Quaternion, point: Vector3): Vector4;
49+
pointDistance(point: Vector3): Vector4;
5250
/**
5351
* Decrements the reference count of a shape and releases it if the new reference count is zero.
5452
*/

packages/physics-lite/src/shape/LiteBoxColliderShape.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ export class LiteBoxColliderShape extends LiteColliderShape implements IBoxColli
6060
/**
6161
* {@inheritDoc IColliderShape.pointDistance }
6262
*/
63-
override pointDistance(position: Vector3, rotation: Quaternion, point: Vector3): Vector4 {
63+
override pointDistance(point: Vector3): Vector4 {
64+
const position = LiteColliderShape._tempPos;
65+
const rotation = LiteColliderShape._tempRot;
66+
this._transform.worldMatrix.decompose(position, rotation, LiteColliderShape._tempScale);
6467
const { position: shapePosition } = this._transform;
6568
const m = LiteBoxColliderShape._tempMatrix;
6669
const invM = LiteBoxColliderShape._tempInvMatrix;
@@ -71,10 +74,12 @@ export class LiteBoxColliderShape extends LiteColliderShape implements IBoxColli
7174
const { _boxMin, _boxMax } = this;
7275
p.copyFrom(_boxMin);
7376
p.subtract(shapePosition);
74-
boundingBox.min.set(p.x / scale.x, p.y / scale.y, p.z / scale.z);
77+
p.divide(scale);
78+
boundingBox.min.copyFrom(p);
7579
p.copyFrom(_boxMax);
7680
p.subtract(shapePosition);
77-
boundingBox.max.set(p.x / scale.x, p.y / scale.y, p.z / scale.z);
81+
p.divide(scale);
82+
boundingBox.max.copyFrom(p);
7883

7984
Matrix.affineTransformation(scale, rotation, position, m);
8085
Matrix.invert(m, invM);

packages/physics-lite/src/shape/LiteColliderShape.ts

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { LiteUpdateFlag } from "../LiteUpdateFlag";
99
* Abstract class for collider shapes.
1010
*/
1111
export abstract class LiteColliderShape implements IColliderShape {
12+
protected static _tempPos = new Vector3();
13+
protected static _tempRot = new Quaternion();
14+
protected static _tempScale = new Vector3();
1215
protected static _tempPoint = new Vector3();
1316
protected static _tempVector4 = new Vector4();
1417

packages/physics-lite/src/shape/LiteSphereColliderShape.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export class LiteSphereColliderShape extends LiteColliderShape implements ISpher
4747
/**
4848
* {@inheritDoc IColliderShape.pointDistance }
4949
*/
50-
override pointDistance(position: Vector3, rotation: Quaternion, point: Vector3): Vector4 {
50+
override pointDistance(point: Vector3): Vector4 {
51+
const position = LiteColliderShape._tempPos;
52+
this._transform.worldMatrix.decompose(position, LiteColliderShape._tempRot, LiteColliderShape._tempScale);
5153
const p = LiteColliderShape._tempPoint;
5254
Vector3.subtract(point, position, p);
5355
const direction = p.normalize();

packages/physics-physx/src/shape/PhysXColliderShape.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export abstract class PhysXColliderShape implements IColliderShape {
129129
/**
130130
* {@inheritDoc IColliderShape.pointDistance }
131131
*/
132-
pointDistance(translation: Vector3, rotation: Quaternion, point: Vector3): Vector4 {
132+
pointDistance(point: Vector3): Vector4 {
133133
const info = this._pxGeometry.pointDistance(this._pxShape.getGlobalPose(), point);
134134
const closestPoint = info.closestPoint;
135135
const res = PhysXColliderShape._tempVector4;

0 commit comments

Comments
 (0)