babylon.physicsHelper.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. module BABYLON {
  2. /**
  3. * The strenght of the force in correspondence to the distance of the affected object
  4. */
  5. export enum PhysicsRadialImpulseFallof {
  6. Constant, // impulse is constant in strength across it's whole radius
  7. Linear // impulse gets weaker if it's further from the origin
  8. }
  9. export class PhysicsHelper {
  10. private _scene: Scene;
  11. private _physicsEngine: Nullable<PhysicsEngine>;
  12. constructor(scene: Scene) {
  13. this._scene = scene;
  14. this._physicsEngine = this._scene.getPhysicsEngine();
  15. if (!this._physicsEngine) {
  16. Tools.Warn('Physics engine not enabled. Please enable the physics before you can use the methods.');
  17. }
  18. }
  19. /**
  20. * @param {Vector3} origin the origin of the explosion
  21. * @param {number} radius the explosion radius
  22. * @param {number} strength the explosion strength
  23. * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
  24. */
  25. public applyRadialExplosionImpulse(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant) {
  26. if (!this._physicsEngine) {
  27. Tools.Warn('Physics engine not enabled. Please enable the physics before you call this method.');
  28. return null;
  29. }
  30. var impostors = this._physicsEngine.getImpostors();
  31. if (impostors.length === 0) {
  32. return null;
  33. }
  34. var event = new PhysicsRadialExplosionEvent(this._scene);
  35. for (var i = 0; i < impostors.length; ++i) {
  36. var impostor = impostors[i];
  37. var impostorForceAndContactPoint = event.getImpostorForceAndContactPoint(
  38. impostor,
  39. origin,
  40. radius,
  41. strength,
  42. falloff
  43. );
  44. if (impostorForceAndContactPoint === null) {
  45. continue;
  46. }
  47. impostor.applyImpulse(
  48. impostorForceAndContactPoint.force,
  49. impostorForceAndContactPoint.contactPoint
  50. );
  51. }
  52. event.cleanup(false);
  53. return event;
  54. }
  55. /**
  56. * @param {Vector3} origin the origin of the explosion
  57. * @param {number} radius the explosion radius
  58. * @param {number} strength the explosion strength
  59. * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
  60. */
  61. public applyRadialExplosionForce(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant) {
  62. if (!this._physicsEngine) {
  63. Tools.Warn('Physics engine not enabled. Please enable the physics before you call the PhysicsHelper.');
  64. return null;
  65. }
  66. var impostors = this._physicsEngine.getImpostors();
  67. if (impostors.length === 0) {
  68. return null;
  69. }
  70. var event = new PhysicsRadialExplosionEvent(this._scene);
  71. for (var i = 0; i < impostors.length; ++i) {
  72. var impostor = impostors[i];
  73. var impostorForceAndContactPoint = event.getImpostorForceAndContactPoint(
  74. impostor,
  75. origin,
  76. radius,
  77. strength,
  78. falloff
  79. );
  80. if (impostorForceAndContactPoint === null) {
  81. continue;
  82. }
  83. impostor.applyForce(
  84. impostorForceAndContactPoint.force,
  85. impostorForceAndContactPoint.contactPoint
  86. );
  87. }
  88. event.cleanup(false);
  89. return event;
  90. }
  91. /**
  92. * @param {Vector3} origin the origin of the explosion
  93. * @param {number} radius the explosion radius
  94. * @param {number} strength the explosion strength
  95. * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
  96. */
  97. public gravitationalField(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant) {
  98. if (!this._physicsEngine) {
  99. Tools.Warn('Physics engine not enabled. Please enable the physics before you call the PhysicsHelper.');
  100. return null;
  101. }
  102. var impostors = this._physicsEngine.getImpostors();
  103. if (impostors.length === 0) {
  104. return null;
  105. }
  106. var event = new PhysicsGravitationalFieldEvent(
  107. this,
  108. this._scene,
  109. origin,
  110. radius,
  111. strength,
  112. falloff
  113. );
  114. event.cleanup(false);
  115. return event;
  116. }
  117. }
  118. /***** Radial explosion *****/
  119. export class PhysicsRadialExplosionEvent {
  120. private _scene: Scene;
  121. private _radialSphere: Mesh; // create a sphere, so we can get the intersecting meshes inside
  122. private _rays: Array<Ray> = [];
  123. private _dataFetched: boolean = false; // check if the data has been fetched. If not, do cleanup
  124. constructor(scene: Scene) {
  125. this._scene = scene;
  126. }
  127. /**
  128. * Returns the data related to the radial explosion event (radialSphere & rays).
  129. * @returns {PhysicsRadialExplosionEventData}
  130. */
  131. public getData(): PhysicsRadialExplosionEventData {
  132. this._dataFetched = true;
  133. return {
  134. radialSphere: this._radialSphere,
  135. rays: this._rays,
  136. };
  137. }
  138. /**
  139. * Returns the force and contact point of the impostor or false, if the impostor is not affected by the force/impulse.
  140. * @param impostor
  141. * @param {Vector3} origin the origin of the explosion
  142. * @param {number} radius the explosion radius
  143. * @param {number} strength the explosion strength
  144. * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear
  145. * @returns {Nullable<PhysicsForceAndContactPoint>}
  146. */
  147. public getImpostorForceAndContactPoint(impostor: PhysicsImpostor, origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof): Nullable<PhysicsForceAndContactPoint> {
  148. if (impostor.mass === 0) {
  149. return null;
  150. }
  151. if (!this._intersectsWithRadialSphere(impostor, origin, radius)) {
  152. return null;
  153. }
  154. var impostorObject = (<Mesh>impostor.object);
  155. var impostorObjectCenter = impostor.getObjectCenter();
  156. var direction = impostorObjectCenter.subtract(origin);
  157. var ray = new Ray(origin, direction, radius);
  158. this._rays.push(ray);
  159. var hit = ray.intersectsMesh(impostorObject);
  160. var contactPoint = hit.pickedPoint;
  161. if (!contactPoint) {
  162. return null;
  163. }
  164. var distanceFromOrigin = BABYLON.Vector3.Distance(origin, contactPoint);
  165. if (distanceFromOrigin > radius) {
  166. return null;
  167. }
  168. var multiplier = falloff === PhysicsRadialImpulseFallof.Constant
  169. ? strength
  170. : strength * (1 - (distanceFromOrigin / radius));
  171. var force = direction.multiplyByFloats(multiplier, multiplier, multiplier);
  172. return { force: force, contactPoint: contactPoint };
  173. }
  174. /**
  175. * Disposes the radialSphere.
  176. * @param {bolean} force
  177. */
  178. public cleanup(force: boolean = true) {
  179. if (force) {
  180. this._radialSphere.dispose();
  181. } else {
  182. setTimeout(() => {
  183. if (!this._dataFetched) {
  184. this._radialSphere.dispose();
  185. }
  186. }, 0);
  187. }
  188. }
  189. /*** Helpers ***/
  190. private _prepareRadialSphere() {
  191. if (!this._radialSphere) {
  192. this._radialSphere = BABYLON.Mesh.CreateSphere(
  193. "radialSphere",
  194. 32,
  195. 1,
  196. this._scene
  197. );
  198. this._radialSphere.isVisible = false;
  199. }
  200. }
  201. private _intersectsWithRadialSphere(impostor: PhysicsImpostor, origin: Vector3, radius: number): boolean {
  202. var impostorObject = <Mesh>impostor.object;
  203. this._prepareRadialSphere();
  204. this._radialSphere.position = origin;
  205. this._radialSphere.scaling = new Vector3(radius * 2, radius * 2, radius * 2);
  206. this._radialSphere._updateBoundingInfo();
  207. this._radialSphere.computeWorldMatrix(true);
  208. return this._radialSphere.intersectsMesh(
  209. impostorObject,
  210. true
  211. );
  212. }
  213. }
  214. export interface PhysicsRadialExplosionEventData {
  215. radialSphere: Mesh;
  216. rays: Array<Ray>;
  217. }
  218. export interface PhysicsForceAndContactPoint {
  219. force: Vector3;
  220. contactPoint: Vector3;
  221. }
  222. /***** Gravitational Field *****/
  223. export class PhysicsGravitationalFieldEvent {
  224. private _physicsHelper: PhysicsHelper;
  225. private _scene: Scene;
  226. private _origin: Vector3;
  227. private _radius: number;
  228. private _strength: number;
  229. private _falloff: PhysicsRadialImpulseFallof;
  230. private _tickCallback: any;
  231. private _radialSphere: Mesh;
  232. private _dataFetched: boolean = false; // check if the has been fetched the data. If not, do cleanup
  233. constructor(
  234. physicsHelper: PhysicsHelper,
  235. scene: Scene,
  236. origin: Vector3,
  237. radius: number,
  238. strength: number,
  239. falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant
  240. ) {
  241. this._physicsHelper = physicsHelper;
  242. this._scene = scene;
  243. this._origin = origin;
  244. this._radius = radius;
  245. this._strength = strength;
  246. this._falloff = falloff;
  247. this._tickCallback = this._tick.bind(this);
  248. }
  249. /**
  250. * Returns the data related to the gravitational field event (radialSphere).
  251. * @returns {PhysicsGravitationalFieldEventData}
  252. */
  253. public getData(): PhysicsGravitationalFieldEventData {
  254. this._dataFetched = true;
  255. return {
  256. radialSphere: this._radialSphere,
  257. };
  258. }
  259. /**
  260. * Enables the gravitational field.
  261. */
  262. public enable() {
  263. this._tickCallback.call(this);
  264. this._scene.registerBeforeRender(this._tickCallback);
  265. }
  266. /**
  267. * Disables the gravitational field.
  268. */
  269. public disable() {
  270. this._scene.unregisterBeforeRender(this._tickCallback);
  271. }
  272. /**
  273. * Disposes the radialSphere.
  274. * @param {bolean} force
  275. */
  276. public cleanup(force: boolean = true) {
  277. if (force) {
  278. this._radialSphere.dispose();
  279. } else {
  280. setTimeout(() => {
  281. if (!this._dataFetched) {
  282. this._radialSphere.dispose();
  283. }
  284. }, 0);
  285. }
  286. }
  287. private _tick() {
  288. // Since the params won't change, we fetch the event only once
  289. if (this._radialSphere) {
  290. this._physicsHelper.applyRadialExplosionForce(
  291. this._origin,
  292. this._radius,
  293. this._strength * -1,
  294. this._falloff
  295. );
  296. } else {
  297. var radialExplosionEvent = <PhysicsRadialExplosionEvent>this._physicsHelper.applyRadialExplosionForce(
  298. this._origin,
  299. this._radius,
  300. this._strength * -1,
  301. this._falloff
  302. );
  303. this._radialSphere = <Mesh>radialExplosionEvent.getData().radialSphere.clone('radialSphereClone');
  304. }
  305. }
  306. }
  307. export interface PhysicsGravitationalFieldEventData {
  308. radialSphere: Mesh;
  309. }
  310. }