babylon.physicsHelper.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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): Nullable<PhysicsRadialExplosionEvent> {
  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. impostors.forEach(impostor => {
  36. var impostorForceAndContactPoint = event.getImpostorForceAndContactPoint(impostor, origin, radius, strength, falloff);
  37. if (!impostorForceAndContactPoint) {
  38. return;
  39. }
  40. impostor.applyImpulse(impostorForceAndContactPoint.force, impostorForceAndContactPoint.contactPoint);
  41. });
  42. event.dispose(false);
  43. return event;
  44. }
  45. /**
  46. * @param {Vector3} origin the origin of the explosion
  47. * @param {number} radius the explosion radius
  48. * @param {number} strength the explosion strength
  49. * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
  50. */
  51. public applyRadialExplosionForce(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant): Nullable<PhysicsRadialExplosionEvent> {
  52. if (!this._physicsEngine) {
  53. Tools.Warn('Physics engine not enabled. Please enable the physics before you call the PhysicsHelper.');
  54. return null;
  55. }
  56. var impostors = this._physicsEngine.getImpostors();
  57. if (impostors.length === 0) {
  58. return null;
  59. }
  60. var event = new PhysicsRadialExplosionEvent(this._scene);
  61. impostors.forEach(impostor => {
  62. var impostorForceAndContactPoint = event.getImpostorForceAndContactPoint(impostor, origin, radius, strength, falloff);
  63. if (!impostorForceAndContactPoint) {
  64. return;
  65. }
  66. impostor.applyForce(impostorForceAndContactPoint.force, impostorForceAndContactPoint.contactPoint);
  67. })
  68. event.dispose(false);
  69. return event;
  70. }
  71. /**
  72. * @param {Vector3} origin the origin of the explosion
  73. * @param {number} radius the explosion radius
  74. * @param {number} strength the explosion strength
  75. * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear. Defaults to Constant
  76. */
  77. public gravitationalField(origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant): Nullable<PhysicsGravitationalFieldEvent> {
  78. if (!this._physicsEngine) {
  79. Tools.Warn('Physics engine not enabled. Please enable the physics before you call the PhysicsHelper.');
  80. return null;
  81. }
  82. var impostors = this._physicsEngine.getImpostors();
  83. if (impostors.length === 0) {
  84. return null;
  85. }
  86. var event = new PhysicsGravitationalFieldEvent(this, this._scene, origin, radius, strength, falloff);
  87. event.dispose(false);
  88. return event;
  89. }
  90. }
  91. /***** Radial explosion *****/
  92. export class PhysicsRadialExplosionEvent {
  93. private _scene: Scene;
  94. private _radialSphere: Mesh; // create a sphere, so we can get the intersecting meshes inside
  95. private _rays: Array<Ray> = [];
  96. private _dataFetched: boolean = false; // check if the data has been fetched. If not, do cleanup
  97. constructor(scene: Scene) {
  98. this._scene = scene;
  99. }
  100. /**
  101. * Returns the data related to the radial explosion event (radialSphere & rays).
  102. * @returns {PhysicsRadialExplosionEventData}
  103. */
  104. public getData(): PhysicsRadialExplosionEventData {
  105. this._dataFetched = true;
  106. return {
  107. radialSphere: this._radialSphere,
  108. rays: this._rays,
  109. };
  110. }
  111. /**
  112. * Returns the force and contact point of the impostor or false, if the impostor is not affected by the force/impulse.
  113. * @param impostor
  114. * @param {Vector3} origin the origin of the explosion
  115. * @param {number} radius the explosion radius
  116. * @param {number} strength the explosion strength
  117. * @param {PhysicsRadialImpulseFallof} falloff possible options: Constant & Linear
  118. * @returns {Nullable<PhysicsForceAndContactPoint>}
  119. */
  120. public getImpostorForceAndContactPoint(impostor: PhysicsImpostor, origin: Vector3, radius: number, strength: number, falloff: PhysicsRadialImpulseFallof): Nullable<PhysicsForceAndContactPoint> {
  121. if (impostor.mass === 0) {
  122. return null;
  123. }
  124. if (!this._intersectsWithRadialSphere(impostor, origin, radius)) {
  125. return null;
  126. }
  127. var impostorObject = (<Mesh>impostor.object);
  128. var impostorObjectCenter = impostor.getObjectCenter();
  129. var direction = impostorObjectCenter.subtract(origin);
  130. var ray = new Ray(origin, direction, radius);
  131. this._rays.push(ray);
  132. var hit = ray.intersectsMesh(impostorObject);
  133. var contactPoint = hit.pickedPoint;
  134. if (!contactPoint) {
  135. return null;
  136. }
  137. var distanceFromOrigin = Vector3.Distance(origin, contactPoint);
  138. if (distanceFromOrigin > radius) {
  139. return null;
  140. }
  141. var multiplier = falloff === PhysicsRadialImpulseFallof.Constant
  142. ? strength
  143. : strength * (1 - (distanceFromOrigin / radius));
  144. var force = direction.multiplyByFloats(multiplier, multiplier, multiplier);
  145. return { force: force, contactPoint: contactPoint };
  146. }
  147. /**
  148. * Disposes the radialSphere.
  149. * @param {bolean} force
  150. */
  151. public dispose(force: boolean = true) {
  152. if (force) {
  153. this._radialSphere.dispose();
  154. } else {
  155. setTimeout(() => {
  156. if (!this._dataFetched) {
  157. this._radialSphere.dispose();
  158. }
  159. }, 0);
  160. }
  161. }
  162. /*** Helpers ***/
  163. private _prepareRadialSphere(): void {
  164. if (!this._radialSphere) {
  165. this._radialSphere = MeshBuilder.CreateSphere("radialSphere", { segments: 32, diameter: 1 }, this._scene);
  166. this._radialSphere.isVisible = false;
  167. }
  168. }
  169. private _intersectsWithRadialSphere(impostor: PhysicsImpostor, origin: Vector3, radius: number): boolean {
  170. var impostorObject = <Mesh>impostor.object;
  171. this._prepareRadialSphere();
  172. this._radialSphere.position = origin;
  173. this._radialSphere.scaling = new Vector3(radius * 2, radius * 2, radius * 2);
  174. this._radialSphere._updateBoundingInfo();
  175. this._radialSphere.computeWorldMatrix(true);
  176. return this._radialSphere.intersectsMesh(impostorObject, true);
  177. }
  178. }
  179. export interface PhysicsRadialExplosionEventData {
  180. radialSphere: Mesh;
  181. rays: Array<Ray>;
  182. }
  183. export interface PhysicsForceAndContactPoint {
  184. force: Vector3;
  185. contactPoint: Vector3;
  186. }
  187. /***** Gravitational Field *****/
  188. export class PhysicsGravitationalFieldEvent {
  189. private _physicsHelper: PhysicsHelper;
  190. private _scene: Scene;
  191. private _origin: Vector3;
  192. private _radius: number;
  193. private _strength: number;
  194. private _falloff: PhysicsRadialImpulseFallof;
  195. private _tickCallback: any;
  196. private _radialSphere: Mesh;
  197. private _dataFetched: boolean = false; // check if the has been fetched the data. If not, do cleanup
  198. constructor(
  199. physicsHelper: PhysicsHelper,
  200. scene: Scene,
  201. origin: Vector3,
  202. radius: number,
  203. strength: number,
  204. falloff: PhysicsRadialImpulseFallof = PhysicsRadialImpulseFallof.Constant
  205. ) {
  206. this._physicsHelper = physicsHelper;
  207. this._scene = scene;
  208. this._origin = origin;
  209. this._radius = radius;
  210. this._strength = strength;
  211. this._falloff = falloff;
  212. this._tickCallback = this._tick.bind(this);
  213. }
  214. /**
  215. * Returns the data related to the gravitational field event (radialSphere).
  216. * @returns {PhysicsGravitationalFieldEventData}
  217. */
  218. public getData(): PhysicsGravitationalFieldEventData {
  219. this._dataFetched = true;
  220. return {
  221. radialSphere: this._radialSphere,
  222. };
  223. }
  224. /**
  225. * Enables the gravitational field.
  226. */
  227. public enable() {
  228. this._tickCallback.call(this);
  229. this._scene.registerBeforeRender(this._tickCallback);
  230. }
  231. /**
  232. * Disables the gravitational field.
  233. */
  234. public disable() {
  235. this._scene.unregisterBeforeRender(this._tickCallback);
  236. }
  237. /**
  238. * Disposes the radialSphere.
  239. * @param {bolean} force
  240. */
  241. public dispose(force: boolean = true) {
  242. if (force) {
  243. this._radialSphere.dispose();
  244. } else {
  245. setTimeout(() => {
  246. if (!this._dataFetched) {
  247. this._radialSphere.dispose();
  248. }
  249. }, 0);
  250. }
  251. }
  252. private _tick() {
  253. // Since the params won't change, we fetch the event only once
  254. if (this._radialSphere) {
  255. this._physicsHelper.applyRadialExplosionForce(this._origin, this._radius, this._strength * -1, this._falloff);
  256. } else {
  257. var radialExplosionEvent = this._physicsHelper.applyRadialExplosionForce(this._origin, this._radius, this._strength * -1, this._falloff);
  258. if (radialExplosionEvent) {
  259. this._radialSphere = <Mesh>radialExplosionEvent.getData().radialSphere.clone('radialSphereClone');
  260. }
  261. }
  262. }
  263. }
  264. export interface PhysicsGravitationalFieldEventData {
  265. radialSphere: Mesh;
  266. }
  267. }