physicsBody.d.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import type { IBasePhysicsCollisionEvent, IPhysicsCollisionEvent, PhysicsMassProperties } from "./IPhysicsEnginePlugin";
  2. import { PhysicsMotionType } from "./IPhysicsEnginePlugin";
  3. import type { PhysicsShape } from "./physicsShape";
  4. import { Vector3, Quaternion } from "../../Maths/math.vector";
  5. import type { Scene } from "../../scene";
  6. import type { Nullable } from "../../types.js";
  7. import type { PhysicsConstraint } from "./physicsConstraint";
  8. import type { Bone } from "../../Bones/bone.js";
  9. import type { Observable } from "../../Misc/observable";
  10. import type { AbstractMesh } from "../../Meshes/abstractMesh";
  11. import type { TransformNode } from "../../Meshes/transformNode";
  12. /**
  13. * PhysicsBody is useful for creating a physics body that can be used in a physics engine. It allows
  14. * the user to set the mass and velocity of the body, which can then be used to calculate the
  15. * motion of the body in the physics engine.
  16. */
  17. export declare class PhysicsBody {
  18. /**
  19. * V2 Physics plugin private data for single Transform
  20. */
  21. _pluginData: any;
  22. /**
  23. * V2 Physics plugin private data for instances
  24. */
  25. _pluginDataInstances: Array<any>;
  26. /**
  27. * The V2 plugin used to create and manage this Physics Body
  28. */
  29. private _physicsPlugin;
  30. /**
  31. * The engine used to create and manage this Physics Body
  32. */
  33. private _physicsEngine;
  34. /**
  35. * If the collision callback is enabled
  36. */
  37. private _collisionCBEnabled;
  38. /**
  39. * If the collision ended callback is enabled
  40. */
  41. private _collisionEndedCBEnabled;
  42. /**
  43. * The transform node associated with this Physics Body
  44. */
  45. transformNode: TransformNode;
  46. /**
  47. * Disable pre-step that consists in updating Physics Body from Transform Node Translation/Orientation.
  48. * True by default for maximum performance.
  49. */
  50. disablePreStep: boolean;
  51. /**
  52. * 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.
  53. */
  54. disableSync: boolean;
  55. /**
  56. * Physics engine will try to make this body sleeping and not active
  57. */
  58. startAsleep: boolean;
  59. private _nodeDisposeObserver;
  60. private _isDisposed;
  61. private _shape;
  62. private _motionType;
  63. /**
  64. * Constructs a new physics body for the given node.
  65. * @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.
  66. * @param motionType - The motion type of the physics body. The options are:
  67. * - PhysicsMotionType.STATIC - Static bodies are not moving and unaffected by forces or collisions. They are good for level boundaries or terrain.
  68. * - PhysicsMotionType.DYNAMIC - Dynamic bodies are fully simulated. They can move and collide with other objects.
  69. * - 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.
  70. * @param startsAsleep - Whether the physics body should start in a sleeping state (not a guarantee). Defaults to false.
  71. * @param scene - The scene containing the physics engine.
  72. *
  73. * This code is useful for creating a physics body for a given Transform Node in a scene.
  74. * It checks the version of the physics engine and the physics plugin, and initializes the body accordingly.
  75. * It also sets the node's rotation quaternion if it is not already set. Finally, it adds the body to the physics engine.
  76. */
  77. constructor(transformNode: TransformNode, motionType: PhysicsMotionType, startsAsleep: boolean, scene: Scene);
  78. /**
  79. * Returns the string "PhysicsBody".
  80. * @returns "PhysicsBody"
  81. */
  82. getClassName(): string;
  83. /**
  84. * Clone the PhysicsBody to a new body and assign it to the transformNode parameter
  85. * @param transformNode transformNode that will be used for the cloned PhysicsBody
  86. * @returns the newly cloned PhysicsBody
  87. */
  88. clone(transformNode: TransformNode): PhysicsBody;
  89. /**
  90. * If a physics body is connected to an instanced node, update the number physic instances to match the number of node instances.
  91. */
  92. updateBodyInstances(): void;
  93. /**
  94. * This returns the number of internal instances of the physics body
  95. */
  96. get numInstances(): number;
  97. /**
  98. * Get the motion type of the physics body. Can be STATIC, DYNAMIC, or ANIMATED.
  99. */
  100. get motionType(): PhysicsMotionType;
  101. /**
  102. * Sets the shape of the physics body.
  103. * @param shape - The shape of the physics body.
  104. *
  105. * 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.
  106. * The shape is used to calculate the body's mass, inertia, and other properties.
  107. */
  108. set shape(shape: Nullable<PhysicsShape>);
  109. /**
  110. * Retrieves the physics shape associated with this object.
  111. *
  112. * @returns The physics shape associated with this object, or `undefined` if no
  113. * shape is associated.
  114. *
  115. * This method is useful for retrieving the physics shape associated with this object,
  116. * which can be used to apply physical forces to the object or to detect collisions.
  117. */
  118. get shape(): Nullable<PhysicsShape>;
  119. /**
  120. * Sets the event mask for the physics engine.
  121. *
  122. * @param eventMask - A bitmask that determines which events will be sent to the physics engine.
  123. * @param instanceIndex - If this body is instanced, the index of the instance to set the event mask for.
  124. *
  125. * This method is useful for setting the event mask for the physics engine, which determines which events
  126. * will be sent to the physics engine. This allows the user to control which events the physics engine will respond to.
  127. */
  128. setEventMask(eventMask: number, instanceIndex?: number): void;
  129. /**
  130. * Gets the event mask of the physics engine.
  131. * @param instanceIndex - If this body is instanced, the index of the instance to get the event mask for.
  132. * @returns The event mask of the physics engine.
  133. *
  134. * This method is useful for getting the event mask of the physics engine,
  135. * which is used to determine which events the engine will respond to.
  136. * This is important for ensuring that the engine is responding to the correct events and not
  137. * wasting resources on unnecessary events.
  138. */
  139. getEventMask(instanceIndex?: number): number;
  140. /**
  141. * Sets the motion type of the physics body. Can be STATIC, DYNAMIC, or ANIMATED.
  142. * @param motionType - The motion type to set.
  143. * @param instanceIndex - If this body is instanced, the index of the instance to set the motion type for.
  144. */
  145. setMotionType(motionType: PhysicsMotionType, instanceIndex?: number): void;
  146. /**
  147. * Gets the motion type of the physics body. Can be STATIC, DYNAMIC, or ANIMATED.
  148. * @param instanceIndex - If this body is instanced, the index of the instance to get the motion type for.
  149. * @returns The motion type of the physics body.
  150. */
  151. getMotionType(instanceIndex?: number): PhysicsMotionType;
  152. /**
  153. * Computes the mass properties of the physics object, based on the set of physics shapes this body uses.
  154. * This method is useful for computing the initial mass properties of a physics object, such as its mass,
  155. * inertia, and center of mass; these values are important for accurately simulating the physics of the
  156. * object in the physics engine, and computing values based on the shape will provide you with reasonable
  157. * initial values, which you can then customize.
  158. * @param instanceIndex - The index of the instance to compute the mass properties for.
  159. * @returns The mass properties of the object.
  160. */
  161. computeMassProperties(instanceIndex?: number): PhysicsMassProperties;
  162. /**
  163. * Sets the mass properties of the physics object.
  164. *
  165. * @param massProps - The mass properties to set.
  166. * @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.
  167. *
  168. * This method is useful for setting the mass properties of a physics object, such as its mass,
  169. * inertia, and center of mass. This is important for accurately simulating the physics of the object in the physics engine.
  170. */
  171. setMassProperties(massProps: PhysicsMassProperties, instanceIndex?: number): void;
  172. /**
  173. * Retrieves the mass properties of the object.
  174. * @param instanceIndex - If this body is instanced, the index of the instance to get the mass properties for.
  175. * @returns The mass properties of the object.
  176. *
  177. * This method is useful for physics simulations, as it allows the user to
  178. * retrieve the mass properties of the object, such as its mass, center of mass,
  179. * and moment of inertia. This information is necessary for accurate physics
  180. * simulations.
  181. */
  182. getMassProperties(instanceIndex?: number): PhysicsMassProperties;
  183. /**
  184. * Sets the linear damping of the physics body.
  185. *
  186. * @param damping - The linear damping value.
  187. * @param instanceIndex - If this body is instanced, the index of the instance to set the linear damping for.
  188. *
  189. * This method is useful for controlling the linear damping of the physics body,
  190. * which is the rate at which the body's velocity decreases over time. This is useful for simulating
  191. * the effects of air resistance or other forms of friction.
  192. */
  193. setLinearDamping(damping: number, instanceIndex?: number): void;
  194. /**
  195. * Gets the linear damping of the physics body.
  196. * @param instanceIndex - If this body is instanced, the index of the instance to get the linear damping for.
  197. * @returns The linear damping of the physics body.
  198. *
  199. * This method is useful for retrieving the linear damping of the physics body, which is the amount of
  200. * resistance the body has to linear motion. This is useful for simulating realistic physics behavior
  201. * in a game.
  202. */
  203. getLinearDamping(instanceIndex?: number): number;
  204. /**
  205. * Sets the angular damping of the physics body.
  206. * @param damping The angular damping of the body.
  207. * @param instanceIndex - If this body is instanced, the index of the instance to set the angular damping for.
  208. *
  209. * This method is useful for controlling the angular velocity of a physics body.
  210. * By setting the damping, the body's angular velocity will be reduced over time, simulating the effect of friction.
  211. * This can be used to create realistic physical behavior in a physics engine.
  212. */
  213. setAngularDamping(damping: number, instanceIndex?: number): void;
  214. /**
  215. * Gets the angular damping of the physics body.
  216. * @param instanceIndex - If this body is instanced, the index of the instance to get the angular damping for.
  217. *
  218. * @returns The angular damping of the physics body.
  219. *
  220. * This method is useful for getting the angular damping of the physics body,
  221. * which is the rate of reduction of the angular velocity over time.
  222. * This is important for simulating realistic physics behavior in a game.
  223. */
  224. getAngularDamping(instanceIndex?: number): number;
  225. /**
  226. * Sets the linear velocity of the physics object.
  227. * @param linVel - The linear velocity to set.
  228. * @param instanceIndex - If this body is instanced, the index of the instance to set the linear velocity for.
  229. *
  230. * This method is useful for setting the linear velocity of a physics object,
  231. * which is necessary for simulating realistic physics in a game engine.
  232. * By setting the linear velocity, the physics object will move in the direction and speed specified by the vector.
  233. * This allows for realistic physics simulations, such as simulating the motion of a ball rolling down a hill.
  234. */
  235. setLinearVelocity(linVel: Vector3, instanceIndex?: number): void;
  236. /**
  237. * Gets the linear velocity of the physics body and stores it in the given vector3.
  238. * @param linVel - The vector3 to store the linear velocity in.
  239. * @param instanceIndex - If this body is instanced, the index of the instance to get the linear velocity for.
  240. *
  241. * This method is useful for getting the linear velocity of a physics body in a physics engine.
  242. * This can be used to determine the speed and direction of the body, which can be used to calculate the motion of the body.
  243. */
  244. getLinearVelocityToRef(linVel: Vector3, instanceIndex?: number): void;
  245. /**
  246. * Gets the linear velocity of the physics body as a new vector3.
  247. * @param instanceIndex - If this body is instanced, the index of the instance to get the linear velocity for.
  248. * @returns The linear velocity of the physics body.
  249. *
  250. * This method is useful for getting the linear velocity of a physics body in a physics engine.
  251. * This can be used to determine the speed and direction of the body, which can be used to calculate the motion of the body.
  252. */
  253. getLinearVelocity(instanceIndex?: number): Vector3;
  254. /**
  255. * Sets the angular velocity of the physics object.
  256. * @param angVel - The angular velocity to set.
  257. * @param instanceIndex - If this body is instanced, the index of the instance to set the angular velocity for.
  258. *
  259. * This method is useful for setting the angular velocity of a physics object, which is necessary for
  260. * simulating realistic physics behavior. The angular velocity is used to determine the rate of rotation of the object,
  261. * which is important for simulating realistic motion.
  262. */
  263. setAngularVelocity(angVel: Vector3, instanceIndex?: number): void;
  264. /**
  265. * Gets the angular velocity of the physics body and stores it in the given vector3.
  266. * @param angVel - The vector3 to store the angular velocity in.
  267. * @param instanceIndex - If this body is instanced, the index of the instance to get the angular velocity for.
  268. *
  269. * This method is useful for getting the angular velocity of a physics body, which can be used to determine the body's
  270. * rotational speed. This information can be used to create realistic physics simulations.
  271. */
  272. getAngularVelocityToRef(angVel: Vector3, instanceIndex?: number): void;
  273. /**
  274. * Gets the angular velocity of the physics body as a new vector3.
  275. * @param instanceIndex - If this body is instanced, the index of the instance to get the angular velocity for.
  276. * @returns The angular velocity of the physics body.
  277. *
  278. * This method is useful for getting the angular velocity of a physics body, which can be used to determine the body's
  279. * rotational speed. This information can be used to create realistic physics simulations.
  280. */
  281. getAngularVelocity(instanceIndex?: number): Vector3;
  282. /**
  283. * Applies an impulse to the physics object.
  284. *
  285. * @param impulse The impulse vector.
  286. * @param location The location of the impulse.
  287. * @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.
  288. *
  289. * This method is useful for applying an impulse to a physics object, which can be used to simulate physical forces such as gravity,
  290. * collisions, and explosions. This can be used to create realistic physics simulations in a game or other application.
  291. */
  292. applyImpulse(impulse: Vector3, location: Vector3, instanceIndex?: number): void;
  293. /**
  294. * Add torque to a physics body
  295. * @param angularImpulse The angular impulse vector.
  296. * @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.
  297. */
  298. applyAngularImpulse(angularImpulse: Vector3, instanceIndex?: number): void;
  299. /**
  300. * Applies a force to the physics object.
  301. *
  302. * @param force The force vector.
  303. * @param location The location of the force.
  304. * @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.
  305. *
  306. * This method is useful for applying a force to a physics object, which can be used to simulate physical forces such as gravity,
  307. * collisions, and explosions. This can be used to create realistic physics simulations in a game or other application.
  308. */
  309. applyForce(force: Vector3, location: Vector3, instanceIndex?: number): void;
  310. /**
  311. * Retrieves the geometry of the body from the physics plugin.
  312. *
  313. * @returns The geometry of the body.
  314. *
  315. * This method is useful for retrieving the geometry of the body from the physics plugin, which can be used for various physics calculations.
  316. */
  317. getGeometry(): {};
  318. /**
  319. * Returns an observable that will be notified for when a collision starts or continues for this PhysicsBody
  320. * @returns Observable
  321. */
  322. getCollisionObservable(): Observable<IPhysicsCollisionEvent>;
  323. /**
  324. * Returns an observable that will be notified when the body has finished colliding with another body
  325. * @returns
  326. */
  327. getCollisionEndedObservable(): Observable<IBasePhysicsCollisionEvent>;
  328. /**
  329. * Enable or disable collision callback for this PhysicsBody.
  330. * @param enabled true if PhysicsBody's collision will rise a collision event and notifies the observable
  331. */
  332. setCollisionCallbackEnabled(enabled: boolean): void;
  333. /**
  334. * Enable or disable collision ended callback for this PhysicsBody.
  335. * @param enabled true if PhysicsBody's collision ended will rise a collision event and notifies the observable
  336. */
  337. setCollisionEndedCallbackEnabled(enabled: boolean): void;
  338. /**
  339. * Get the center of the object in world space.
  340. * @param instanceIndex - If this body is instanced, the index of the instance to get the center for.
  341. * @returns geometric center of the associated mesh
  342. */
  343. getObjectCenterWorld(instanceIndex?: number): Vector3;
  344. /**
  345. * Get the center of the object in world space.
  346. * @param ref - The vector3 to store the result in.
  347. * @param instanceIndex - If this body is instanced, the index of the instance to get the center for.
  348. * @returns geometric center of the associated mesh
  349. */
  350. getObjectCenterWorldToRef(ref: Vector3, instanceIndex?: number): Vector3;
  351. /**
  352. * Adds a constraint to the physics engine.
  353. *
  354. * @param childBody - The body to which the constraint will be applied.
  355. * @param constraint - The constraint to be applied.
  356. * @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.
  357. * @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.
  358. *
  359. */
  360. addConstraint(childBody: PhysicsBody, constraint: PhysicsConstraint, instanceIndex?: number, childInstanceIndex?: number): void;
  361. /**
  362. * Sync with a bone
  363. * @param bone The bone that the impostor will be synced to.
  364. * @param boneMesh The mesh that the bone is influencing.
  365. * @param jointPivot The pivot of the joint / bone in local space.
  366. * @param distToJoint Optional distance from the impostor to the joint.
  367. * @param adjustRotation Optional quaternion for adjusting the local rotation of the bone.
  368. * @param boneAxis Optional vector3 axis the bone is aligned with
  369. */
  370. syncWithBone(bone: Bone, boneMesh: AbstractMesh, jointPivot: Vector3, distToJoint?: number, adjustRotation?: Quaternion, boneAxis?: Vector3): void;
  371. /**
  372. * Executes a callback on the body or all of the instances of a body
  373. * @param callback the callback to execute
  374. */
  375. iterateOverAllInstances(callback: (body: PhysicsBody, instanceIndex?: number) => void): void;
  376. /**
  377. * Sets the gravity factor of the physics body
  378. * @param factor the gravity factor to set
  379. * @param instanceIndex the instance of the body to set, if undefined all instances will be set
  380. */
  381. setGravityFactor(factor: number, instanceIndex?: number): void;
  382. /**
  383. * Gets the gravity factor of the physics body
  384. * @param instanceIndex the instance of the body to get, if undefined the value of first instance will be returned
  385. * @returns the gravity factor
  386. */
  387. getGravityFactor(instanceIndex?: number): number;
  388. /**
  389. * Set the target transformation (position and rotation) of the body, such that the body will set its velocity to reach that target
  390. * @param position The target position
  391. * @param rotation The target rotation
  392. * @param instanceIndex The index of the instance in an instanced body
  393. */
  394. setTargetTransform(position: Vector3, rotation: Quaternion, instanceIndex?: number): void;
  395. /**
  396. * Returns if the body has been disposed.
  397. * @returns true if disposed, false otherwise.
  398. */
  399. get isDisposed(): boolean;
  400. /**
  401. * Disposes the body from the physics engine.
  402. *
  403. * 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.
  404. */
  405. dispose(): void;
  406. }