|
1 | 1 | import type { IPhysicsEnginePluginV2, MassProperties } from "./IPhysicsEnginePlugin"; |
2 | 2 | import type { PhysicsShape } from "./physicsShape"; |
3 | | -import { Vector3, Quaternion } from "../../Maths/math.vector"; |
| 3 | +import { Vector3, Quaternion, TmpVectors } from "../../Maths/math.vector"; |
4 | 4 | import type { Scene } from "../../scene"; |
5 | 5 | import type { PhysicsEngine } from "./physicsEngine"; |
6 | 6 | import type { Mesh, TransformNode, AbstractMesh } from "../../Meshes"; |
7 | 7 | import type { Nullable } from "core/types"; |
8 | 8 | import type { PhysicsConstraint } from "./physicsConstraint"; |
| 9 | +import type { Bone } from "core/Bones/bone"; |
| 10 | +import { Space } from "core/Maths/math.axis"; |
9 | 11 | /** |
10 | 12 | * PhysicsBody is useful for creating a physics body that can be used in a physics engine. It allows |
11 | 13 | * 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 { |
430 | 432 | this._physicsPlugin.addConstraint(this, childBody, constraint); |
431 | 433 | } |
432 | 434 |
|
| 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 | + |
433 | 483 | /** |
434 | 484 | * Disposes the body from the physics engine. |
435 | 485 | * |
|
0 commit comments