physicsHelper.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. import type { Nullable } from "../types";
  2. import { Vector3 } from "../Maths/math.vector";
  3. import type { Mesh } from "../Meshes/mesh";
  4. import type { Scene } from "../scene";
  5. import type { PhysicsImpostor } from "./v1/physicsImpostor";
  6. import type { PhysicsBody } from "./v2/physicsBody";
  7. /**
  8. * A helper for physics simulations
  9. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  10. */
  11. export declare class PhysicsHelper {
  12. private _scene;
  13. private _physicsEngine;
  14. private _hitData;
  15. /**
  16. * Initializes the Physics helper
  17. * @param scene Babylon.js scene
  18. */
  19. constructor(scene: Scene);
  20. /**
  21. * Applies a radial explosion impulse
  22. * @param origin the origin of the explosion
  23. * @param radiusOrEventOptions the radius or the options of radial explosion
  24. * @param strength the explosion strength
  25. * @param falloff possible options: Constant & Linear. Defaults to Constant
  26. * @returns A physics radial explosion event, or null
  27. */
  28. applyRadialExplosionImpulse(origin: Vector3, radiusOrEventOptions: number | PhysicsRadialExplosionEventOptions, strength?: number, falloff?: PhysicsRadialImpulseFalloff): Nullable<PhysicsRadialExplosionEvent>;
  29. /**
  30. * Applies a radial explosion force
  31. * @param origin the origin of the explosion
  32. * @param radiusOrEventOptions the radius or the options of radial explosion
  33. * @param strength the explosion strength
  34. * @param falloff possible options: Constant & Linear. Defaults to Constant
  35. * @returns A physics radial explosion event, or null
  36. */
  37. applyRadialExplosionForce(origin: Vector3, radiusOrEventOptions: number | PhysicsRadialExplosionEventOptions, strength?: number, falloff?: PhysicsRadialImpulseFalloff): Nullable<PhysicsRadialExplosionEvent>;
  38. private _applicationForBodies;
  39. /**
  40. * Creates a gravitational field
  41. * @param origin the origin of the gravitational field
  42. * @param radiusOrEventOptions the radius or the options of radial gravitational field
  43. * @param strength the gravitational field strength
  44. * @param falloff possible options: Constant & Linear. Defaults to Constant
  45. * @returns A physics gravitational field event, or null
  46. */
  47. gravitationalField(origin: Vector3, radiusOrEventOptions: number | PhysicsRadialExplosionEventOptions, strength?: number, falloff?: PhysicsRadialImpulseFalloff): Nullable<PhysicsGravitationalFieldEvent>;
  48. /**
  49. * Creates a physics updraft event
  50. * @param origin the origin of the updraft
  51. * @param radiusOrEventOptions the radius or the options of the updraft
  52. * @param strength the strength of the updraft
  53. * @param height the height of the updraft
  54. * @param updraftMode possible options: Center & Perpendicular. Defaults to Center
  55. * @returns A physics updraft event, or null
  56. */
  57. updraft(origin: Vector3, radiusOrEventOptions: number | PhysicsUpdraftEventOptions, strength?: number, height?: number, updraftMode?: PhysicsUpdraftMode): Nullable<PhysicsUpdraftEvent>;
  58. /**
  59. * Creates a physics vortex event
  60. * @param origin the of the vortex
  61. * @param radiusOrEventOptions the radius or the options of the vortex
  62. * @param strength the strength of the vortex
  63. * @param height the height of the vortex
  64. * @returns a Physics vortex event, or null
  65. * A physics vortex event or null
  66. */
  67. vortex(origin: Vector3, radiusOrEventOptions: number | PhysicsVortexEventOptions, strength?: number, height?: number): Nullable<PhysicsVortexEvent>;
  68. private _copyPhysicsHitData;
  69. }
  70. /**
  71. * Represents a physics radial explosion event
  72. */
  73. declare class PhysicsRadialExplosionEvent {
  74. private _scene;
  75. private _options;
  76. private _sphere;
  77. private _dataFetched;
  78. /**
  79. * Initializes a radial explosion event
  80. * @param _scene BabylonJS scene
  81. * @param _options The options for the vortex event
  82. */
  83. constructor(_scene: Scene, _options: PhysicsRadialExplosionEventOptions);
  84. /**
  85. * Returns the data related to the radial explosion event (sphere).
  86. * @returns The radial explosion event data
  87. */
  88. getData(): PhysicsRadialExplosionEventData;
  89. private _getHitData;
  90. /**
  91. * Returns the force and contact point of the body or false, if the body is not affected by the force/impulse.
  92. * @param body A physics body where the transform node is an AbstractMesh
  93. * @param origin the origin of the explosion
  94. * @param data the data of the hit
  95. * @param instanceIndex the instance index of the body
  96. * @returns if there was a hit
  97. */
  98. getBodyHitData(body: PhysicsBody, origin: Vector3, data: PhysicsHitData, instanceIndex?: number): boolean;
  99. /**
  100. * Returns the force and contact point of the impostor or false, if the impostor is not affected by the force/impulse.
  101. * @param impostor A physics imposter
  102. * @param origin the origin of the explosion
  103. * @param data the data of the hit
  104. * @returns A physics force and contact point, or null
  105. */
  106. getImpostorHitData(impostor: PhysicsImpostor, origin: Vector3, data: PhysicsHitData): boolean;
  107. /**
  108. * Triggers affected impostors callbacks
  109. * @param affectedImpostorsWithData defines the list of affected impostors (including associated data)
  110. */
  111. triggerAffectedImpostorsCallback(affectedImpostorsWithData: Array<PhysicsAffectedImpostorWithData>): void;
  112. /**
  113. * Triggers affected bodies callbacks
  114. * @param affectedBodiesWithData defines the list of affected bodies (including associated data)
  115. */
  116. triggerAffectedBodiesCallback(affectedBodiesWithData: Array<PhysicsAffectedBodyWithData>): void;
  117. /**
  118. * Disposes the sphere.
  119. * @param force Specifies if the sphere should be disposed by force
  120. */
  121. dispose(force?: boolean): void;
  122. /*** Helpers ***/
  123. private _prepareSphere;
  124. private _intersectsWithSphere;
  125. }
  126. /**
  127. * Represents a gravitational field event
  128. */
  129. declare class PhysicsGravitationalFieldEvent {
  130. private _physicsHelper;
  131. private _scene;
  132. private _origin;
  133. private _options;
  134. private _tickCallback;
  135. private _sphere;
  136. private _dataFetched;
  137. /**
  138. * Initializes the physics gravitational field event
  139. * @param _physicsHelper A physics helper
  140. * @param _scene BabylonJS scene
  141. * @param _origin The origin position of the gravitational field event
  142. * @param _options The options for the vortex event
  143. */
  144. constructor(_physicsHelper: PhysicsHelper, _scene: Scene, _origin: Vector3, _options: PhysicsRadialExplosionEventOptions);
  145. /**
  146. * Returns the data related to the gravitational field event (sphere).
  147. * @returns A gravitational field event
  148. */
  149. getData(): PhysicsGravitationalFieldEventData;
  150. /**
  151. * Enables the gravitational field.
  152. */
  153. enable(): void;
  154. /**
  155. * Disables the gravitational field.
  156. */
  157. disable(): void;
  158. /**
  159. * Disposes the sphere.
  160. * @param force The force to dispose from the gravitational field event
  161. */
  162. dispose(force?: boolean): void;
  163. private _tick;
  164. }
  165. /**
  166. * Represents a physics updraft event
  167. */
  168. declare class PhysicsUpdraftEvent {
  169. private _scene;
  170. private _origin;
  171. private _options;
  172. private _physicsEngine;
  173. private _originTop;
  174. private _originDirection;
  175. private _tickCallback;
  176. private _cylinder;
  177. private _cylinderPosition;
  178. private _dataFetched;
  179. private static _HitData;
  180. /**
  181. * Initializes the physics updraft event
  182. * @param _scene BabylonJS scene
  183. * @param _origin The origin position of the updraft
  184. * @param _options The options for the updraft event
  185. */
  186. constructor(_scene: Scene, _origin: Vector3, _options: PhysicsUpdraftEventOptions);
  187. /**
  188. * Returns the data related to the updraft event (cylinder).
  189. * @returns A physics updraft event
  190. */
  191. getData(): PhysicsUpdraftEventData;
  192. /**
  193. * Enables the updraft.
  194. */
  195. enable(): void;
  196. /**
  197. * Disables the updraft.
  198. */
  199. disable(): void;
  200. /**
  201. * Disposes the cylinder.
  202. * @param force Specifies if the updraft should be disposed by force
  203. */
  204. dispose(force?: boolean): void;
  205. private _getHitData;
  206. private _getBodyHitData;
  207. private _getImpostorHitData;
  208. private _tick;
  209. /*** Helpers ***/
  210. private _prepareCylinder;
  211. private _intersectsWithCylinder;
  212. }
  213. /**
  214. * Represents a physics vortex event
  215. */
  216. declare class PhysicsVortexEvent {
  217. private _scene;
  218. private _origin;
  219. private _options;
  220. private _physicsEngine;
  221. private _originTop;
  222. private _tickCallback;
  223. private _cylinder;
  224. private _cylinderPosition;
  225. private _dataFetched;
  226. private static _OriginOnPlane;
  227. private static _HitData;
  228. /**
  229. * Initializes the physics vortex event
  230. * @param _scene The BabylonJS scene
  231. * @param _origin The origin position of the vortex
  232. * @param _options The options for the vortex event
  233. */
  234. constructor(_scene: Scene, _origin: Vector3, _options: PhysicsVortexEventOptions);
  235. /**
  236. * Returns the data related to the vortex event (cylinder).
  237. * @returns The physics vortex event data
  238. */
  239. getData(): PhysicsVortexEventData;
  240. /**
  241. * Enables the vortex.
  242. */
  243. enable(): void;
  244. /**
  245. * Disables the cortex.
  246. */
  247. disable(): void;
  248. /**
  249. * Disposes the sphere.
  250. * @param force
  251. */
  252. dispose(force?: boolean): void;
  253. private _getHitData;
  254. private _getBodyHitData;
  255. private _getImpostorHitData;
  256. private _tick;
  257. /*** Helpers ***/
  258. private _prepareCylinder;
  259. private _intersectsWithCylinder;
  260. }
  261. /**
  262. * Options fot the radial explosion event
  263. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  264. */
  265. export declare class PhysicsRadialExplosionEventOptions {
  266. /**
  267. * The radius of the sphere for the radial explosion.
  268. */
  269. radius: number;
  270. /**
  271. * The strength of the explosion.
  272. */
  273. strength: number;
  274. /**
  275. * The strength of the force in correspondence to the distance of the affected object
  276. */
  277. falloff: PhysicsRadialImpulseFalloff;
  278. /**
  279. * Sphere options for the radial explosion.
  280. */
  281. sphere: {
  282. segments: number;
  283. diameter: number;
  284. };
  285. /**
  286. * Sphere options for the radial explosion.
  287. */
  288. affectedImpostorsCallback: (affectedImpostorsWithData: Array<PhysicsAffectedImpostorWithData>) => void;
  289. /**
  290. * Sphere options for the radial explosion.
  291. */
  292. affectedBodiesCallback: (affectedBodiesWithData: Array<PhysicsAffectedBodyWithData>) => void;
  293. }
  294. /**
  295. * Options fot the updraft event
  296. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  297. */
  298. export declare class PhysicsUpdraftEventOptions {
  299. /**
  300. * The radius of the cylinder for the vortex
  301. */
  302. radius: number;
  303. /**
  304. * The strength of the updraft.
  305. */
  306. strength: number;
  307. /**
  308. * The height of the cylinder for the updraft.
  309. */
  310. height: number;
  311. /**
  312. * The mode for the updraft.
  313. */
  314. updraftMode: PhysicsUpdraftMode;
  315. }
  316. /**
  317. * Options fot the vortex event
  318. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  319. */
  320. export declare class PhysicsVortexEventOptions {
  321. /**
  322. * The radius of the cylinder for the vortex
  323. */
  324. radius: number;
  325. /**
  326. * The strength of the vortex.
  327. */
  328. strength: number;
  329. /**
  330. * The height of the cylinder for the vortex.
  331. */
  332. height: number;
  333. /**
  334. * At which distance, relative to the radius the centripetal forces should kick in? Range: 0-1
  335. */
  336. centripetalForceThreshold: number;
  337. /**
  338. * This multiplier determines with how much force the objects will be pushed sideways/around the vortex, when below the threshold.
  339. */
  340. centripetalForceMultiplier: number;
  341. /**
  342. * This multiplier determines with how much force the objects will be pushed sideways/around the vortex, when above the threshold.
  343. */
  344. centrifugalForceMultiplier: number;
  345. /**
  346. * This multiplier determines with how much force the objects will be pushed upwards, when in the vortex.
  347. */
  348. updraftForceMultiplier: number;
  349. }
  350. /**
  351. * The strength of the force in correspondence to the distance of the affected object
  352. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  353. */
  354. export declare enum PhysicsRadialImpulseFalloff {
  355. /** Defines that impulse is constant in strength across it's whole radius */
  356. Constant = 0,
  357. /** Defines that impulse gets weaker if it's further from the origin */
  358. Linear = 1
  359. }
  360. /**
  361. * The strength of the force in correspondence to the distance of the affected object
  362. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  363. */
  364. export declare enum PhysicsUpdraftMode {
  365. /** Defines that the upstream forces will pull towards the top center of the cylinder */
  366. Center = 0,
  367. /** Defines that once a impostor is inside the cylinder, it will shoot out perpendicular from the ground of the cylinder */
  368. Perpendicular = 1
  369. }
  370. /**
  371. * Interface for a physics hit data
  372. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  373. */
  374. export interface PhysicsHitData {
  375. /**
  376. * The force applied at the contact point
  377. */
  378. force: Vector3;
  379. /**
  380. * The contact point
  381. */
  382. contactPoint: Vector3;
  383. /**
  384. * The distance from the origin to the contact point
  385. */
  386. distanceFromOrigin: number;
  387. /**
  388. * For an instanced physics body (mesh with thin instances), the index of the thin instance the hit applies to
  389. */
  390. instanceIndex?: number;
  391. }
  392. /**
  393. * Interface for radial explosion event data
  394. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  395. */
  396. export interface PhysicsRadialExplosionEventData {
  397. /**
  398. * A sphere used for the radial explosion event
  399. */
  400. sphere: Mesh;
  401. }
  402. /**
  403. * Interface for gravitational field event data
  404. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  405. */
  406. export interface PhysicsGravitationalFieldEventData {
  407. /**
  408. * A sphere mesh used for the gravitational field event
  409. */
  410. sphere: Mesh;
  411. }
  412. /**
  413. * Interface for updraft event data
  414. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  415. */
  416. export interface PhysicsUpdraftEventData {
  417. /**
  418. * A cylinder used for the updraft event
  419. */
  420. cylinder?: Mesh;
  421. }
  422. /**
  423. * Interface for vortex event data
  424. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  425. */
  426. export interface PhysicsVortexEventData {
  427. /**
  428. * A cylinder used for the vortex event
  429. */
  430. cylinder: Mesh;
  431. }
  432. /**
  433. * Interface for an affected physics impostor
  434. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine#further-functionality-of-the-impostor-class
  435. */
  436. export interface PhysicsAffectedImpostorWithData {
  437. /**
  438. * The impostor affected by the effect
  439. */
  440. impostor: PhysicsImpostor;
  441. /**
  442. * The data about the hit/force from the explosion
  443. */
  444. hitData: PhysicsHitData;
  445. }
  446. /**
  447. * Interface for an affected physics body
  448. * @see
  449. */
  450. export interface PhysicsAffectedBodyWithData {
  451. /**
  452. * The impostor affected by the effect
  453. */
  454. body: PhysicsBody;
  455. /**
  456. * The data about the hit/force from the explosion
  457. */
  458. hitData: PhysicsHitData;
  459. }
  460. export {};