physicsBody.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. import { PhysicsMotionType } from "./IPhysicsEnginePlugin.js";
  2. import { Vector3, Quaternion, TmpVectors } from "../../Maths/math.vector.js";
  3. import { Space } from "../../Maths/math.axis.js";
  4. /**
  5. * PhysicsBody is useful for creating a physics body that can be used in a physics engine. It allows
  6. * the user to set the mass and velocity of the body, which can then be used to calculate the
  7. * motion of the body in the physics engine.
  8. */
  9. export class PhysicsBody {
  10. /**
  11. * Constructs a new physics body for the given node.
  12. * @param transformNode - The Transform Node to construct the physics body for. For better performance, it is advised that this node does not have a parent.
  13. * @param motionType - The motion type of the physics body. The options are:
  14. * - PhysicsMotionType.STATIC - Static bodies are not moving and unaffected by forces or collisions. They are good for level boundaries or terrain.
  15. * - PhysicsMotionType.DYNAMIC - Dynamic bodies are fully simulated. They can move and collide with other objects.
  16. * - PhysicsMotionType.ANIMATED - They behave like dynamic bodies, but they won't be affected by other bodies, but still push other bodies out of the way.
  17. * @param startsAsleep - Whether the physics body should start in a sleeping state (not a guarantee). Defaults to false.
  18. * @param scene - The scene containing the physics engine.
  19. *
  20. * This code is useful for creating a physics body for a given Transform Node in a scene.
  21. * It checks the version of the physics engine and the physics plugin, and initializes the body accordingly.
  22. * It also sets the node's rotation quaternion if it is not already set. Finally, it adds the body to the physics engine.
  23. */
  24. constructor(transformNode, motionType, startsAsleep, scene) {
  25. /**
  26. * V2 Physics plugin private data for single Transform
  27. */
  28. this._pluginData = undefined;
  29. /**
  30. * V2 Physics plugin private data for instances
  31. */
  32. this._pluginDataInstances = [];
  33. /**
  34. * If the collision callback is enabled
  35. */
  36. this._collisionCBEnabled = false;
  37. /**
  38. * If the collision ended callback is enabled
  39. */
  40. this._collisionEndedCBEnabled = false;
  41. /**
  42. * Disable pre-step that consists in updating Physics Body from Transform Node Translation/Orientation.
  43. * True by default for maximum performance.
  44. */
  45. this.disablePreStep = true;
  46. /**
  47. * Disable sync from physics to transformNode. This value is set to true at body creation or at motionType setting when the body is not dynamic.
  48. */
  49. this.disableSync = false;
  50. this._isDisposed = false;
  51. this._shape = null;
  52. if (!scene) {
  53. return;
  54. }
  55. const physicsEngine = scene.getPhysicsEngine();
  56. if (!physicsEngine) {
  57. throw new Error("No Physics Engine available.");
  58. }
  59. this._physicsEngine = physicsEngine;
  60. if (physicsEngine.getPluginVersion() != 2) {
  61. throw new Error("Plugin version is incorrect. Expected version 2.");
  62. }
  63. const physicsPlugin = physicsEngine.getPhysicsPlugin();
  64. if (!physicsPlugin) {
  65. throw new Error("No Physics Plugin available.");
  66. }
  67. this._physicsPlugin = physicsPlugin;
  68. if (!transformNode.rotationQuaternion) {
  69. transformNode.rotationQuaternion = Quaternion.FromEulerAngles(transformNode.rotation.x, transformNode.rotation.y, transformNode.rotation.z);
  70. }
  71. this.startAsleep = startsAsleep;
  72. this._motionType = motionType;
  73. // only dynamic and animated body needs sync from physics to transformNode
  74. this.disableSync = motionType == PhysicsMotionType.STATIC;
  75. // instances?
  76. const m = transformNode;
  77. if (m.hasThinInstances) {
  78. this._physicsPlugin.initBodyInstances(this, motionType, m);
  79. }
  80. else {
  81. // single instance
  82. if (transformNode.parent) {
  83. // Force computation of world matrix so that the parent transforms are correctly reflected in absolutePosition/absoluteRotationQuaternion.
  84. transformNode.computeWorldMatrix(true);
  85. }
  86. this._physicsPlugin.initBody(this, motionType, transformNode.absolutePosition, transformNode.absoluteRotationQuaternion);
  87. }
  88. this.transformNode = transformNode;
  89. transformNode.physicsBody = this;
  90. physicsEngine.addBody(this);
  91. this._nodeDisposeObserver = transformNode.onDisposeObservable.add(() => {
  92. this.dispose();
  93. });
  94. }
  95. /**
  96. * Returns the string "PhysicsBody".
  97. * @returns "PhysicsBody"
  98. */
  99. getClassName() {
  100. return "PhysicsBody";
  101. }
  102. /**
  103. * Clone the PhysicsBody to a new body and assign it to the transformNode parameter
  104. * @param transformNode transformNode that will be used for the cloned PhysicsBody
  105. * @returns the newly cloned PhysicsBody
  106. */
  107. clone(transformNode) {
  108. const clonedBody = new PhysicsBody(transformNode, this.getMotionType(), this.startAsleep, this.transformNode.getScene());
  109. clonedBody.shape = this.shape;
  110. clonedBody.setMassProperties(this.getMassProperties());
  111. clonedBody.setLinearDamping(this.getLinearDamping());
  112. clonedBody.setAngularDamping(this.getAngularDamping());
  113. return clonedBody;
  114. }
  115. /**
  116. * If a physics body is connected to an instanced node, update the number physic instances to match the number of node instances.
  117. */
  118. updateBodyInstances() {
  119. const m = this.transformNode;
  120. if (m.hasThinInstances) {
  121. this._physicsPlugin.updateBodyInstances(this, m);
  122. }
  123. }
  124. /**
  125. * This returns the number of internal instances of the physics body
  126. */
  127. get numInstances() {
  128. return this._pluginDataInstances.length;
  129. }
  130. /**
  131. * Get the motion type of the physics body. Can be STATIC, DYNAMIC, or ANIMATED.
  132. */
  133. get motionType() {
  134. return this._motionType;
  135. }
  136. /**
  137. * Sets the shape of the physics body.
  138. * @param shape - The shape of the physics body.
  139. *
  140. * This method is useful for setting the shape of the physics body, which is necessary for the physics engine to accurately simulate the body's behavior.
  141. * The shape is used to calculate the body's mass, inertia, and other properties.
  142. */
  143. set shape(shape) {
  144. this._shape = shape;
  145. if (shape) {
  146. this._physicsPlugin.setShape(this, shape);
  147. }
  148. }
  149. /**
  150. * Retrieves the physics shape associated with this object.
  151. *
  152. * @returns The physics shape associated with this object, or `undefined` if no
  153. * shape is associated.
  154. *
  155. * This method is useful for retrieving the physics shape associated with this object,
  156. * which can be used to apply physical forces to the object or to detect collisions.
  157. */
  158. get shape() {
  159. return this._shape;
  160. }
  161. /**
  162. * Sets the event mask for the physics engine.
  163. *
  164. * @param eventMask - A bitmask that determines which events will be sent to the physics engine.
  165. * @param instanceIndex - If this body is instanced, the index of the instance to set the event mask for.
  166. *
  167. * This method is useful for setting the event mask for the physics engine, which determines which events
  168. * will be sent to the physics engine. This allows the user to control which events the physics engine will respond to.
  169. */
  170. setEventMask(eventMask, instanceIndex) {
  171. this._physicsPlugin.setEventMask(this, eventMask, instanceIndex);
  172. }
  173. /**
  174. * Gets the event mask of the physics engine.
  175. * @param instanceIndex - If this body is instanced, the index of the instance to get the event mask for.
  176. * @returns The event mask of the physics engine.
  177. *
  178. * This method is useful for getting the event mask of the physics engine,
  179. * which is used to determine which events the engine will respond to.
  180. * This is important for ensuring that the engine is responding to the correct events and not
  181. * wasting resources on unnecessary events.
  182. */
  183. getEventMask(instanceIndex) {
  184. return this._physicsPlugin.getEventMask(this, instanceIndex);
  185. }
  186. /**
  187. * Sets the motion type of the physics body. Can be STATIC, DYNAMIC, or ANIMATED.
  188. * @param motionType - The motion type to set.
  189. * @param instanceIndex - If this body is instanced, the index of the instance to set the motion type for.
  190. */
  191. setMotionType(motionType, instanceIndex) {
  192. this.disableSync = motionType == PhysicsMotionType.STATIC;
  193. this._physicsPlugin.setMotionType(this, motionType, instanceIndex);
  194. }
  195. /**
  196. * Gets the motion type of the physics body. Can be STATIC, DYNAMIC, or ANIMATED.
  197. * @param instanceIndex - If this body is instanced, the index of the instance to get the motion type for.
  198. * @returns The motion type of the physics body.
  199. */
  200. getMotionType(instanceIndex) {
  201. return this._physicsPlugin.getMotionType(this, instanceIndex);
  202. }
  203. /**
  204. * Computes the mass properties of the physics object, based on the set of physics shapes this body uses.
  205. * This method is useful for computing the initial mass properties of a physics object, such as its mass,
  206. * inertia, and center of mass; these values are important for accurately simulating the physics of the
  207. * object in the physics engine, and computing values based on the shape will provide you with reasonable
  208. * initial values, which you can then customize.
  209. * @param instanceIndex - The index of the instance to compute the mass properties for.
  210. * @returns The mass properties of the object.
  211. */
  212. computeMassProperties(instanceIndex) {
  213. return this._physicsPlugin.computeMassProperties(this, instanceIndex);
  214. }
  215. /**
  216. * Sets the mass properties of the physics object.
  217. *
  218. * @param massProps - The mass properties to set.
  219. * @param instanceIndex - The index of the instance to set the mass properties for. If not defined, the mass properties will be set for all instances.
  220. *
  221. * This method is useful for setting the mass properties of a physics object, such as its mass,
  222. * inertia, and center of mass. This is important for accurately simulating the physics of the object in the physics engine.
  223. */
  224. setMassProperties(massProps, instanceIndex) {
  225. this._physicsPlugin.setMassProperties(this, massProps, instanceIndex);
  226. }
  227. /**
  228. * Retrieves the mass properties of the object.
  229. * @param instanceIndex - If this body is instanced, the index of the instance to get the mass properties for.
  230. * @returns The mass properties of the object.
  231. *
  232. * This method is useful for physics simulations, as it allows the user to
  233. * retrieve the mass properties of the object, such as its mass, center of mass,
  234. * and moment of inertia. This information is necessary for accurate physics
  235. * simulations.
  236. */
  237. getMassProperties(instanceIndex) {
  238. return this._physicsPlugin.getMassProperties(this, instanceIndex);
  239. }
  240. /**
  241. * Sets the linear damping of the physics body.
  242. *
  243. * @param damping - The linear damping value.
  244. * @param instanceIndex - If this body is instanced, the index of the instance to set the linear damping for.
  245. *
  246. * This method is useful for controlling the linear damping of the physics body,
  247. * which is the rate at which the body's velocity decreases over time. This is useful for simulating
  248. * the effects of air resistance or other forms of friction.
  249. */
  250. setLinearDamping(damping, instanceIndex) {
  251. this._physicsPlugin.setLinearDamping(this, damping, instanceIndex);
  252. }
  253. /**
  254. * Gets the linear damping of the physics body.
  255. * @param instanceIndex - If this body is instanced, the index of the instance to get the linear damping for.
  256. * @returns The linear damping of the physics body.
  257. *
  258. * This method is useful for retrieving the linear damping of the physics body, which is the amount of
  259. * resistance the body has to linear motion. This is useful for simulating realistic physics behavior
  260. * in a game.
  261. */
  262. getLinearDamping(instanceIndex) {
  263. return this._physicsPlugin.getLinearDamping(this, instanceIndex);
  264. }
  265. /**
  266. * Sets the angular damping of the physics body.
  267. * @param damping The angular damping of the body.
  268. * @param instanceIndex - If this body is instanced, the index of the instance to set the angular damping for.
  269. *
  270. * This method is useful for controlling the angular velocity of a physics body.
  271. * By setting the damping, the body's angular velocity will be reduced over time, simulating the effect of friction.
  272. * This can be used to create realistic physical behavior in a physics engine.
  273. */
  274. setAngularDamping(damping, instanceIndex) {
  275. this._physicsPlugin.setAngularDamping(this, damping, instanceIndex);
  276. }
  277. /**
  278. * Gets the angular damping of the physics body.
  279. * @param instanceIndex - If this body is instanced, the index of the instance to get the angular damping for.
  280. *
  281. * @returns The angular damping of the physics body.
  282. *
  283. * This method is useful for getting the angular damping of the physics body,
  284. * which is the rate of reduction of the angular velocity over time.
  285. * This is important for simulating realistic physics behavior in a game.
  286. */
  287. getAngularDamping(instanceIndex) {
  288. return this._physicsPlugin.getAngularDamping(this, instanceIndex);
  289. }
  290. /**
  291. * Sets the linear velocity of the physics object.
  292. * @param linVel - The linear velocity to set.
  293. * @param instanceIndex - If this body is instanced, the index of the instance to set the linear velocity for.
  294. *
  295. * This method is useful for setting the linear velocity of a physics object,
  296. * which is necessary for simulating realistic physics in a game engine.
  297. * By setting the linear velocity, the physics object will move in the direction and speed specified by the vector.
  298. * This allows for realistic physics simulations, such as simulating the motion of a ball rolling down a hill.
  299. */
  300. setLinearVelocity(linVel, instanceIndex) {
  301. this._physicsPlugin.setLinearVelocity(this, linVel, instanceIndex);
  302. }
  303. /**
  304. * Gets the linear velocity of the physics body and stores it in the given vector3.
  305. * @param linVel - The vector3 to store the linear velocity in.
  306. * @param instanceIndex - If this body is instanced, the index of the instance to get the linear velocity for.
  307. *
  308. * This method is useful for getting the linear velocity of a physics body in a physics engine.
  309. * This can be used to determine the speed and direction of the body, which can be used to calculate the motion of the body.
  310. */
  311. getLinearVelocityToRef(linVel, instanceIndex) {
  312. this._physicsPlugin.getLinearVelocityToRef(this, linVel, instanceIndex);
  313. }
  314. /**
  315. * Gets the linear velocity of the physics body as a new vector3.
  316. * @param instanceIndex - If this body is instanced, the index of the instance to get the linear velocity for.
  317. * @returns The linear velocity of the physics body.
  318. *
  319. * This method is useful for getting the linear velocity of a physics body in a physics engine.
  320. * This can be used to determine the speed and direction of the body, which can be used to calculate the motion of the body.
  321. */
  322. getLinearVelocity(instanceIndex) {
  323. const ref = new Vector3();
  324. this.getLinearVelocityToRef(ref, instanceIndex);
  325. return ref;
  326. }
  327. /**
  328. * Sets the angular velocity of the physics object.
  329. * @param angVel - The angular velocity to set.
  330. * @param instanceIndex - If this body is instanced, the index of the instance to set the angular velocity for.
  331. *
  332. * This method is useful for setting the angular velocity of a physics object, which is necessary for
  333. * simulating realistic physics behavior. The angular velocity is used to determine the rate of rotation of the object,
  334. * which is important for simulating realistic motion.
  335. */
  336. setAngularVelocity(angVel, instanceIndex) {
  337. this._physicsPlugin.setAngularVelocity(this, angVel, instanceIndex);
  338. }
  339. /**
  340. * Gets the angular velocity of the physics body and stores it in the given vector3.
  341. * @param angVel - The vector3 to store the angular velocity in.
  342. * @param instanceIndex - If this body is instanced, the index of the instance to get the angular velocity for.
  343. *
  344. * This method is useful for getting the angular velocity of a physics body, which can be used to determine the body's
  345. * rotational speed. This information can be used to create realistic physics simulations.
  346. */
  347. getAngularVelocityToRef(angVel, instanceIndex) {
  348. this._physicsPlugin.getAngularVelocityToRef(this, angVel, instanceIndex);
  349. }
  350. /**
  351. * Gets the angular velocity of the physics body as a new vector3.
  352. * @param instanceIndex - If this body is instanced, the index of the instance to get the angular velocity for.
  353. * @returns The angular velocity of the physics body.
  354. *
  355. * This method is useful for getting the angular velocity of a physics body, which can be used to determine the body's
  356. * rotational speed. This information can be used to create realistic physics simulations.
  357. */
  358. getAngularVelocity(instanceIndex) {
  359. const ref = new Vector3();
  360. this.getAngularVelocityToRef(ref, instanceIndex);
  361. return ref;
  362. }
  363. /**
  364. * Applies an impulse to the physics object.
  365. *
  366. * @param impulse The impulse vector.
  367. * @param location The location of the impulse.
  368. * @param instanceIndex For a instanced body, the instance to where the impulse should be applied. If not specified, the impulse is applied to all instances.
  369. *
  370. * This method is useful for applying an impulse to a physics object, which can be used to simulate physical forces such as gravity,
  371. * collisions, and explosions. This can be used to create realistic physics simulations in a game or other application.
  372. */
  373. applyImpulse(impulse, location, instanceIndex) {
  374. this._physicsPlugin.applyImpulse(this, impulse, location, instanceIndex);
  375. }
  376. /**
  377. * Add torque to a physics body
  378. * @param angularImpulse The angular impulse vector.
  379. * @param instanceIndex For a instanced body, the instance to where the impulse should be applied. If not specified, the impulse is applied to all instances.
  380. */
  381. applyAngularImpulse(angularImpulse, instanceIndex) {
  382. this._physicsPlugin.applyAngularImpulse(this, angularImpulse, instanceIndex);
  383. }
  384. /**
  385. * Applies a force to the physics object.
  386. *
  387. * @param force The force vector.
  388. * @param location The location of the force.
  389. * @param instanceIndex For a instanced body, the instance to where the force should be applied. If not specified, the force is applied to all instances.
  390. *
  391. * This method is useful for applying a force to a physics object, which can be used to simulate physical forces such as gravity,
  392. * collisions, and explosions. This can be used to create realistic physics simulations in a game or other application.
  393. */
  394. applyForce(force, location, instanceIndex) {
  395. this._physicsPlugin.applyForce(this, force, location, instanceIndex);
  396. }
  397. /**
  398. * Retrieves the geometry of the body from the physics plugin.
  399. *
  400. * @returns The geometry of the body.
  401. *
  402. * This method is useful for retrieving the geometry of the body from the physics plugin, which can be used for various physics calculations.
  403. */
  404. getGeometry() {
  405. return this._physicsPlugin.getBodyGeometry(this);
  406. }
  407. /**
  408. * Returns an observable that will be notified for when a collision starts or continues for this PhysicsBody
  409. * @returns Observable
  410. */
  411. getCollisionObservable() {
  412. return this._physicsPlugin.getCollisionObservable(this);
  413. }
  414. /**
  415. * Returns an observable that will be notified when the body has finished colliding with another body
  416. * @returns
  417. */
  418. getCollisionEndedObservable() {
  419. return this._physicsPlugin.getCollisionEndedObservable(this);
  420. }
  421. /**
  422. * Enable or disable collision callback for this PhysicsBody.
  423. * @param enabled true if PhysicsBody's collision will rise a collision event and notifies the observable
  424. */
  425. setCollisionCallbackEnabled(enabled) {
  426. this._collisionCBEnabled = enabled;
  427. this._physicsPlugin.setCollisionCallbackEnabled(this, enabled);
  428. }
  429. /**
  430. * Enable or disable collision ended callback for this PhysicsBody.
  431. * @param enabled true if PhysicsBody's collision ended will rise a collision event and notifies the observable
  432. */
  433. setCollisionEndedCallbackEnabled(enabled) {
  434. this._collisionEndedCBEnabled = enabled;
  435. this._physicsPlugin.setCollisionEndedCallbackEnabled(this, enabled);
  436. }
  437. /**
  438. * Get the center of the object in world space.
  439. * @param instanceIndex - If this body is instanced, the index of the instance to get the center for.
  440. * @returns geometric center of the associated mesh
  441. */
  442. getObjectCenterWorld(instanceIndex) {
  443. const ref = new Vector3();
  444. return this.getObjectCenterWorldToRef(ref, instanceIndex);
  445. }
  446. /**
  447. * Get the center of the object in world space.
  448. * @param ref - The vector3 to store the result in.
  449. * @param instanceIndex - If this body is instanced, the index of the instance to get the center for.
  450. * @returns geometric center of the associated mesh
  451. */
  452. getObjectCenterWorldToRef(ref, instanceIndex) {
  453. if (this._pluginDataInstances?.length > 0) {
  454. const index = instanceIndex || 0;
  455. const matrixData = this.transformNode._thinInstanceDataStorage.matrixData;
  456. if (matrixData) {
  457. ref.set(matrixData[index * 16 + 12], matrixData[index * 16 + 13], matrixData[index * 16 + 14]);
  458. }
  459. }
  460. else {
  461. ref.copyFrom(this.transformNode.position);
  462. }
  463. return ref;
  464. }
  465. /**
  466. * Adds a constraint to the physics engine.
  467. *
  468. * @param childBody - The body to which the constraint will be applied.
  469. * @param constraint - The constraint to be applied.
  470. * @param instanceIndex - If this body is instanced, the index of the instance to which the constraint will be applied. If not specified, no constraint will be applied.
  471. * @param childInstanceIndex - If the child body is instanced, the index of the instance to which the constraint will be applied. If not specified, no constraint will be applied.
  472. *
  473. */
  474. addConstraint(childBody, constraint, instanceIndex, childInstanceIndex) {
  475. this._physicsPlugin.addConstraint(this, childBody, constraint, instanceIndex, childInstanceIndex);
  476. }
  477. /**
  478. * Sync with a bone
  479. * @param bone The bone that the impostor will be synced to.
  480. * @param boneMesh The mesh that the bone is influencing.
  481. * @param jointPivot The pivot of the joint / bone in local space.
  482. * @param distToJoint Optional distance from the impostor to the joint.
  483. * @param adjustRotation Optional quaternion for adjusting the local rotation of the bone.
  484. * @param boneAxis Optional vector3 axis the bone is aligned with
  485. */
  486. syncWithBone(bone, boneMesh, jointPivot, distToJoint, adjustRotation, boneAxis) {
  487. const mesh = this.transformNode;
  488. if (mesh.rotationQuaternion) {
  489. if (adjustRotation) {
  490. const tempQuat = TmpVectors.Quaternion[0];
  491. bone.getRotationQuaternionToRef(Space.WORLD, boneMesh, tempQuat);
  492. tempQuat.multiplyToRef(adjustRotation, mesh.rotationQuaternion);
  493. }
  494. else {
  495. bone.getRotationQuaternionToRef(Space.WORLD, boneMesh, mesh.rotationQuaternion);
  496. }
  497. }
  498. const pos = TmpVectors.Vector3[0];
  499. const boneDir = TmpVectors.Vector3[1];
  500. if (!boneAxis) {
  501. boneAxis = TmpVectors.Vector3[2];
  502. boneAxis.x = 0;
  503. boneAxis.y = 1;
  504. boneAxis.z = 0;
  505. }
  506. bone.getDirectionToRef(boneAxis, boneMesh, boneDir);
  507. bone.getAbsolutePositionToRef(boneMesh, pos);
  508. if ((distToJoint === undefined || distToJoint === null) && jointPivot) {
  509. distToJoint = jointPivot.length();
  510. }
  511. if (distToJoint !== undefined && distToJoint !== null) {
  512. pos.x += boneDir.x * distToJoint;
  513. pos.y += boneDir.y * distToJoint;
  514. pos.z += boneDir.z * distToJoint;
  515. }
  516. mesh.setAbsolutePosition(pos);
  517. }
  518. /**
  519. * Executes a callback on the body or all of the instances of a body
  520. * @param callback the callback to execute
  521. */
  522. iterateOverAllInstances(callback) {
  523. if (this._pluginDataInstances?.length > 0) {
  524. for (let i = 0; i < this._pluginDataInstances.length; i++) {
  525. callback(this, i);
  526. }
  527. }
  528. else {
  529. callback(this, undefined);
  530. }
  531. }
  532. /**
  533. * Sets the gravity factor of the physics body
  534. * @param factor the gravity factor to set
  535. * @param instanceIndex the instance of the body to set, if undefined all instances will be set
  536. */
  537. setGravityFactor(factor, instanceIndex) {
  538. this._physicsPlugin.setGravityFactor(this, factor, instanceIndex);
  539. }
  540. /**
  541. * Gets the gravity factor of the physics body
  542. * @param instanceIndex the instance of the body to get, if undefined the value of first instance will be returned
  543. * @returns the gravity factor
  544. */
  545. getGravityFactor(instanceIndex) {
  546. return this._physicsPlugin.getGravityFactor(this, instanceIndex);
  547. }
  548. /**
  549. * Set the target transformation (position and rotation) of the body, such that the body will set its velocity to reach that target
  550. * @param position The target position
  551. * @param rotation The target rotation
  552. * @param instanceIndex The index of the instance in an instanced body
  553. */
  554. setTargetTransform(position, rotation, instanceIndex) {
  555. this._physicsPlugin.setTargetTransform(this, position, rotation, instanceIndex);
  556. }
  557. /**
  558. * Returns if the body has been disposed.
  559. * @returns true if disposed, false otherwise.
  560. */
  561. get isDisposed() {
  562. return this._isDisposed;
  563. }
  564. /**
  565. * Disposes the body from the physics engine.
  566. *
  567. * This method is useful for cleaning up the physics engine when a body is no longer needed. Disposing the body will free up resources and prevent memory leaks.
  568. */
  569. dispose() {
  570. if (this._isDisposed) {
  571. return;
  572. }
  573. // Disable collisions CB so it doesn't fire when the body is disposed
  574. if (this._collisionCBEnabled) {
  575. this.setCollisionCallbackEnabled(false);
  576. }
  577. if (this._collisionEndedCBEnabled) {
  578. this.setCollisionEndedCallbackEnabled(false);
  579. }
  580. if (this._nodeDisposeObserver) {
  581. this.transformNode.onDisposeObservable.remove(this._nodeDisposeObserver);
  582. this._nodeDisposeObserver = null;
  583. }
  584. this._physicsEngine.removeBody(this);
  585. this._physicsPlugin.removeBody(this);
  586. this._physicsPlugin.disposeBody(this);
  587. this.transformNode.physicsBody = null;
  588. this._pluginData = null;
  589. this._pluginDataInstances.length = 0;
  590. this._isDisposed = true;
  591. this.shape = null;
  592. }
  593. }
  594. //# sourceMappingURL=physicsBody.js.map