Skip to content

Commit a8bd717

Browse files
authored
add debug view on inspector and add method to sync physics body with bone (#13601)
1 parent 3a1a25e commit a8bd717

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

packages/dev/core/src/Physics/v2/physicsBody.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { IPhysicsEnginePluginV2, MassProperties } from "./IPhysicsEnginePlugin";
22
import type { PhysicsShape } from "./physicsShape";
3-
import { Vector3, Quaternion } from "../../Maths/math.vector";
3+
import { Vector3, Quaternion, TmpVectors } from "../../Maths/math.vector";
44
import type { Scene } from "../../scene";
55
import type { PhysicsEngine } from "./physicsEngine";
66
import type { Mesh, TransformNode, AbstractMesh } from "../../Meshes";
77
import type { Nullable } from "core/types";
88
import type { PhysicsConstraint } from "./physicsConstraint";
9+
import type { Bone } from "core/Bones/bone";
10+
import { Space } from "core/Maths/math.axis";
911
/**
1012
* PhysicsBody is useful for creating a physics body that can be used in a physics engine. It allows
1113
* the user to set the mass and velocity of the body, which can then be used to calculate the
@@ -430,6 +432,54 @@ export class PhysicsBody {
430432
this._physicsPlugin.addConstraint(this, childBody, constraint);
431433
}
432434

435+
/**
436+
* Sync with a bone
437+
* @param bone The bone that the impostor will be synced to.
438+
* @param boneMesh The mesh that the bone is influencing.
439+
* @param jointPivot The pivot of the joint / bone in local space.
440+
* @param distToJoint Optional distance from the impostor to the joint.
441+
* @param adjustRotation Optional quaternion for adjusting the local rotation of the bone.
442+
* @param boneAxis Optional vector3 axis the bone is aligned with
443+
*/
444+
public syncWithBone(bone: Bone, boneMesh: AbstractMesh, jointPivot: Vector3, distToJoint?: number, adjustRotation?: Quaternion, boneAxis?: Vector3) {
445+
const mesh = this.transformNode;
446+
447+
if (mesh.rotationQuaternion) {
448+
if (adjustRotation) {
449+
const tempQuat = TmpVectors.Quaternion[0];
450+
bone.getRotationQuaternionToRef(Space.WORLD, boneMesh, tempQuat);
451+
tempQuat.multiplyToRef(adjustRotation, mesh.rotationQuaternion);
452+
} else {
453+
bone.getRotationQuaternionToRef(Space.WORLD, boneMesh, mesh.rotationQuaternion);
454+
}
455+
}
456+
457+
const pos = TmpVectors.Vector3[0];
458+
const boneDir = TmpVectors.Vector3[1];
459+
460+
if (!boneAxis) {
461+
boneAxis = TmpVectors.Vector3[2];
462+
boneAxis.x = 0;
463+
boneAxis.y = 1;
464+
boneAxis.z = 0;
465+
}
466+
467+
bone.getDirectionToRef(boneAxis, boneMesh, boneDir);
468+
bone.getAbsolutePositionToRef(boneMesh, pos);
469+
470+
if ((distToJoint === undefined || distToJoint === null) && jointPivot) {
471+
distToJoint = jointPivot.length();
472+
}
473+
474+
if (distToJoint !== undefined && distToJoint !== null) {
475+
pos.x += boneDir.x * distToJoint;
476+
pos.y += boneDir.y * distToJoint;
477+
pos.z += boneDir.z * distToJoint;
478+
}
479+
480+
mesh.setAbsolutePosition(pos);
481+
}
482+
433483
/**
434484
* Disposes the body from the physics engine.
435485
*

packages/dev/inspector/src/components/actionTabs/tabs/debugTabComponent.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { MaterialFlags } from "core/Materials/materialFlags";
1111

1212
import "core/Physics/physicsEngineComponent";
1313
import "core/Physics/v1/physicsEngineComponent";
14-
import "core/Physics/v1/physicsEngineComponent";
14+
import "core/Physics/v2/physicsEngineComponent";
1515

1616
export class DebugTabComponent extends PaneComponent {
1717
private _physicsViewersEnabled = false;
@@ -44,6 +44,24 @@ export class DebugTabComponent extends PaneComponent {
4444
if (mesh.physicsImpostor) {
4545
const debugMesh = physicsViewer.showImpostor(mesh.physicsImpostor, mesh as Mesh);
4646

47+
if (debugMesh) {
48+
debugMesh.reservedDataStore = { hidden: true };
49+
debugMesh.material!.reservedDataStore = { hidden: true };
50+
}
51+
} else if (mesh.physicsBody) {
52+
const debugMesh = physicsViewer.showBody(mesh.physicsBody);
53+
54+
if (debugMesh) {
55+
debugMesh.reservedDataStore = { hidden: true };
56+
debugMesh.material!.reservedDataStore = { hidden: true };
57+
}
58+
}
59+
}
60+
61+
for (const transformNode of scene.transformNodes) {
62+
if (transformNode.physicsBody) {
63+
const debugMesh = physicsViewer.showBody(transformNode.physicsBody);
64+
4765
if (debugMesh) {
4866
debugMesh.reservedDataStore = { hidden: true };
4967
debugMesh.material!.reservedDataStore = { hidden: true };

0 commit comments

Comments
 (0)