ammoJSPlugin.ts 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. import { Quaternion, Vector3, Matrix } from "../../Maths/math.vector";
  2. import { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../../Physics/IPhysicsEngine";
  3. import { Logger } from "../../Misc/logger";
  4. import { PhysicsImpostor, IPhysicsEnabledObject } from "../../Physics/physicsImpostor";
  5. import { PhysicsJoint, IMotorEnabledJoint, DistanceJointData } from "../../Physics/physicsJoint";
  6. import { VertexBuffer } from "../../Meshes/buffer";
  7. import { VertexData } from "../../Meshes/mesh.vertexData";
  8. import { Nullable } from "../../types";
  9. import { AbstractMesh } from "../../Meshes/abstractMesh";
  10. import { Mesh } from "../../Meshes/mesh";
  11. import { ShapeBuilder } from "../../Meshes/Builders/shapeBuilder";
  12. import { LinesBuilder } from "../../Meshes/Builders/linesBuilder";
  13. import { LinesMesh } from '../../Meshes/linesMesh';
  14. import { PhysicsRaycastResult } from "../physicsRaycastResult";
  15. import { Scalar } from "../../Maths/math.scalar";
  16. import { Epsilon } from '../../Maths/math.constants';
  17. declare var Ammo: any;
  18. /**
  19. * AmmoJS Physics plugin
  20. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  21. * @see https://github.com/kripken/ammo.js/
  22. */
  23. export class AmmoJSPlugin implements IPhysicsEnginePlugin {
  24. /**
  25. * Reference to the Ammo library
  26. */
  27. public bjsAMMO: any = {};
  28. /**
  29. * Created ammoJS world which physics bodies are added to
  30. */
  31. public world: any;
  32. /**
  33. * Name of the plugin
  34. */
  35. public name: string = "AmmoJSPlugin";
  36. private _timeStep: number = 1 / 60;
  37. private _fixedTimeStep: number = 1 / 60;
  38. private _maxSteps = 5;
  39. private _tmpQuaternion = new Quaternion();
  40. private _tmpAmmoTransform: any;
  41. private _tmpAmmoQuaternion: any;
  42. private _tmpAmmoConcreteContactResultCallback: any;
  43. private _collisionConfiguration: any;
  44. private _dispatcher: any;
  45. private _overlappingPairCache: any;
  46. private _solver: any;
  47. private _softBodySolver: any;
  48. private _tmpAmmoVectorA: any;
  49. private _tmpAmmoVectorB: any;
  50. private _tmpAmmoVectorC: any;
  51. private _tmpAmmoVectorD: any;
  52. private _tmpContactCallbackResult = false;
  53. private _tmpAmmoVectorRCA: any;
  54. private _tmpAmmoVectorRCB: any;
  55. private _raycastResult: PhysicsRaycastResult;
  56. private _tmpContactPoint = new Vector3();
  57. private static readonly DISABLE_COLLISION_FLAG = 4;
  58. private static readonly KINEMATIC_FLAG = 2;
  59. private static readonly DISABLE_DEACTIVATION_FLAG = 4;
  60. /**
  61. * Initializes the ammoJS plugin
  62. * @param _useDeltaForWorldStep if the time between frames should be used when calculating physics steps (Default: true)
  63. * @param ammoInjection can be used to inject your own ammo reference
  64. * @param overlappingPairCache can be used to specify your own overlapping pair cache
  65. */
  66. public constructor(private _useDeltaForWorldStep: boolean = true, ammoInjection: any = Ammo, overlappingPairCache: any = null) {
  67. if (typeof ammoInjection === "function") {
  68. ammoInjection(this.bjsAMMO);
  69. } else {
  70. this.bjsAMMO = ammoInjection;
  71. }
  72. if (!this.isSupported()) {
  73. Logger.Error("AmmoJS is not available. Please make sure you included the js file.");
  74. return;
  75. }
  76. // Initialize the physics world
  77. this._collisionConfiguration = new this.bjsAMMO.btSoftBodyRigidBodyCollisionConfiguration();
  78. this._dispatcher = new this.bjsAMMO.btCollisionDispatcher(this._collisionConfiguration);
  79. this._overlappingPairCache = overlappingPairCache || new this.bjsAMMO.btDbvtBroadphase();
  80. this._solver = new this.bjsAMMO.btSequentialImpulseConstraintSolver();
  81. this._softBodySolver = new this.bjsAMMO.btDefaultSoftBodySolver();
  82. this.world = new this.bjsAMMO.btSoftRigidDynamicsWorld(this._dispatcher, this._overlappingPairCache, this._solver, this._collisionConfiguration, this._softBodySolver);
  83. this._tmpAmmoConcreteContactResultCallback = new this.bjsAMMO.ConcreteContactResultCallback();
  84. this._tmpAmmoConcreteContactResultCallback.addSingleResult = (contactPoint: any, colObj0Wrap: any, partId0: any, index0: any) => {
  85. contactPoint = this.bjsAMMO.wrapPointer(contactPoint, Ammo.btManifoldPoint);
  86. const worldPoint = contactPoint.getPositionWorldOnA();
  87. this._tmpContactPoint.x = worldPoint.x();
  88. this._tmpContactPoint.y = worldPoint.y();
  89. this._tmpContactPoint.z = worldPoint.z();
  90. this._tmpContactCallbackResult = true;
  91. };
  92. this._raycastResult = new PhysicsRaycastResult();
  93. // Create temp ammo variables
  94. this._tmpAmmoTransform = new this.bjsAMMO.btTransform();
  95. this._tmpAmmoTransform.setIdentity();
  96. this._tmpAmmoQuaternion = new this.bjsAMMO.btQuaternion(0, 0, 0, 1);
  97. this._tmpAmmoVectorA = new this.bjsAMMO.btVector3(0, 0, 0);
  98. this._tmpAmmoVectorB = new this.bjsAMMO.btVector3(0, 0, 0);
  99. this._tmpAmmoVectorC = new this.bjsAMMO.btVector3(0, 0, 0);
  100. this._tmpAmmoVectorD = new this.bjsAMMO.btVector3(0, 0, 0);
  101. }
  102. /**
  103. * Sets the gravity of the physics world (m/(s^2))
  104. * @param gravity Gravity to set
  105. */
  106. public setGravity(gravity: Vector3): void {
  107. this._tmpAmmoVectorA.setValue(gravity.x, gravity.y, gravity.z);
  108. this.world.setGravity(this._tmpAmmoVectorA);
  109. this.world.getWorldInfo().set_m_gravity(this._tmpAmmoVectorA);
  110. }
  111. /**
  112. * Amount of time to step forward on each frame (only used if useDeltaForWorldStep is false in the constructor)
  113. * @param timeStep timestep to use in seconds
  114. */
  115. public setTimeStep(timeStep: number) {
  116. this._timeStep = timeStep;
  117. }
  118. /**
  119. * Increment to step forward in the physics engine (If timeStep is set to 1/60 and fixedTimeStep is set to 1/120 the physics engine should run 2 steps per frame) (Default: 1/60)
  120. * @param fixedTimeStep fixedTimeStep to use in seconds
  121. */
  122. public setFixedTimeStep(fixedTimeStep: number) {
  123. this._fixedTimeStep = fixedTimeStep;
  124. }
  125. /**
  126. * Sets the maximum number of steps by the physics engine per frame (Default: 5)
  127. * @param maxSteps the maximum number of steps by the physics engine per frame
  128. */
  129. public setMaxSteps(maxSteps: number) {
  130. this._maxSteps = maxSteps;
  131. }
  132. /**
  133. * Gets the current timestep (only used if useDeltaForWorldStep is false in the constructor)
  134. * @returns the current timestep in seconds
  135. */
  136. public getTimeStep(): number {
  137. return this._timeStep;
  138. }
  139. /**
  140. * The create custom shape handler function to be called when using BABYLON.PhysicsImposter.CustomImpostor
  141. */
  142. public onCreateCustomShape: (impostor: PhysicsImpostor) => any;
  143. // Ammo's contactTest and contactPairTest take a callback that runs synchronously, wrap them so that they are easier to consume
  144. private _isImpostorInContact(impostor: PhysicsImpostor) {
  145. this._tmpContactCallbackResult = false;
  146. this.world.contactTest(impostor.physicsBody, this._tmpAmmoConcreteContactResultCallback);
  147. return this._tmpContactCallbackResult;
  148. }
  149. // Ammo's collision events have some weird quirks
  150. // contactPairTest fires too many events as it fires events even when objects are close together but contactTest does not
  151. // so only fire event if both contactTest and contactPairTest have a hit
  152. private _isImpostorPairInContact(impostorA: PhysicsImpostor, impostorB: PhysicsImpostor) {
  153. this._tmpContactCallbackResult = false;
  154. this.world.contactPairTest(impostorA.physicsBody, impostorB.physicsBody, this._tmpAmmoConcreteContactResultCallback);
  155. return this._tmpContactCallbackResult;
  156. }
  157. // Ammo's behavior when maxSteps > 0 does not behave as described in docs
  158. // @see http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Stepping_The_World
  159. //
  160. // When maxSteps is 0 do the entire simulation in one step
  161. // When maxSteps is > 0, run up to maxStep times, if on the last step the (remaining step - fixedTimeStep) is < fixedTimeStep, the remainder will be used for the step. (eg. if remainder is 1.001 and fixedTimeStep is 1 the last step will be 1.001, if instead it did 2 steps (1, 0.001) issues occuered when having a tiny step in ammo)
  162. // Note: To get deterministic physics, timeStep would always need to be divisible by fixedTimeStep
  163. private _stepSimulation(timeStep: number = 1 / 60, maxSteps: number = 10, fixedTimeStep: number = 1 / 60) {
  164. if (maxSteps == 0) {
  165. this.world.stepSimulation(timeStep, 0);
  166. } else {
  167. while (maxSteps > 0 && timeStep > 0) {
  168. if (timeStep - fixedTimeStep < fixedTimeStep) {
  169. this.world.stepSimulation(timeStep, 0);
  170. timeStep = 0;
  171. } else {
  172. timeStep -= fixedTimeStep;
  173. this.world.stepSimulation(fixedTimeStep, 0);
  174. }
  175. maxSteps--;
  176. }
  177. }
  178. }
  179. /**
  180. * Moves the physics simulation forward delta seconds and updates the given physics imposters
  181. * Prior to the step the imposters physics location is set to the position of the babylon meshes
  182. * After the step the babylon meshes are set to the position of the physics imposters
  183. * @param delta amount of time to step forward
  184. * @param impostors array of imposters to update before/after the step
  185. */
  186. public executeStep(delta: number, impostors: Array<PhysicsImpostor>): void {
  187. for (var impostor of impostors) {
  188. // Update physics world objects to match babylon world
  189. if (!impostor.soft) {
  190. impostor.beforeStep();
  191. }
  192. }
  193. this._stepSimulation(this._useDeltaForWorldStep ? delta : this._timeStep, this._maxSteps, this._fixedTimeStep);
  194. for (var mainImpostor of impostors) {
  195. // After physics update make babylon world objects match physics world objects
  196. if (mainImpostor.soft) {
  197. this._afterSoftStep(mainImpostor);
  198. }
  199. else {
  200. mainImpostor.afterStep();
  201. }
  202. // Handle collision event
  203. if (mainImpostor._onPhysicsCollideCallbacks.length > 0) {
  204. if (this._isImpostorInContact(mainImpostor)) {
  205. for (var collideCallback of mainImpostor._onPhysicsCollideCallbacks) {
  206. for (var otherImpostor of collideCallback.otherImpostors) {
  207. if (mainImpostor.physicsBody.isActive() || otherImpostor.physicsBody.isActive()) {
  208. if (this._isImpostorPairInContact(mainImpostor, otherImpostor)) {
  209. mainImpostor.onCollide({ body: otherImpostor.physicsBody, point: this._tmpContactPoint });
  210. otherImpostor.onCollide({ body: mainImpostor.physicsBody, point: this._tmpContactPoint });
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. * Update babylon mesh to match physics world object
  221. * @param impostor imposter to match
  222. */
  223. private _afterSoftStep(impostor: PhysicsImpostor): void {
  224. if (impostor.type === PhysicsImpostor.RopeImpostor) {
  225. this._ropeStep(impostor);
  226. }
  227. else {
  228. this._softbodyOrClothStep(impostor);
  229. }
  230. }
  231. /**
  232. * Update babylon mesh vertices vertices to match physics world softbody or cloth
  233. * @param impostor imposter to match
  234. */
  235. private _ropeStep(impostor: PhysicsImpostor): void {
  236. var bodyVertices = impostor.physicsBody.get_m_nodes();
  237. var nbVertices = bodyVertices.size();
  238. var node: any;
  239. var nodePositions: any;
  240. var x, y, z: number;
  241. var path: Array<Vector3> = new Array();
  242. for (var n = 0; n < nbVertices; n++) {
  243. node = bodyVertices.at(n);
  244. nodePositions = node.get_m_x();
  245. x = nodePositions.x();
  246. y = nodePositions.y();
  247. z = nodePositions.z();
  248. path.push(new Vector3(x, y, z));
  249. }
  250. var object = impostor.object;
  251. var shape = impostor.getParam("shape");
  252. if (impostor._isFromLine) {
  253. impostor.object = LinesBuilder.CreateLines("lines", { points: path, instance: <LinesMesh>object });
  254. }
  255. else {
  256. impostor.object = ShapeBuilder.ExtrudeShape("ext", { shape: shape, path: path, instance: <Mesh>object });
  257. }
  258. }
  259. /**
  260. * Update babylon mesh vertices vertices to match physics world softbody or cloth
  261. * @param impostor imposter to match
  262. */
  263. private _softbodyOrClothStep(impostor: PhysicsImpostor): void {
  264. var normalDirection = (impostor.type === PhysicsImpostor.ClothImpostor) ? 1 : -1;
  265. var object = impostor.object;
  266. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  267. if (!vertexPositions) {
  268. vertexPositions = [];
  269. }
  270. var vertexNormals = object.getVerticesData(VertexBuffer.NormalKind);
  271. if (!vertexNormals) {
  272. vertexNormals = [];
  273. }
  274. var nbVertices = vertexPositions.length / 3;
  275. var bodyVertices = impostor.physicsBody.get_m_nodes();
  276. var node: any;
  277. var nodePositions: any;
  278. var nodeNormals: any;
  279. var x, y, z: number;
  280. var nx, ny, nz: number;
  281. for (var n = 0; n < nbVertices; n++) {
  282. node = bodyVertices.at(n);
  283. nodePositions = node.get_m_x();
  284. x = nodePositions.x();
  285. y = nodePositions.y();
  286. z = nodePositions.z() * normalDirection;
  287. var nodeNormals = node.get_m_n();
  288. nx = nodeNormals.x();
  289. ny = nodeNormals.y();
  290. nz = nodeNormals.z() * normalDirection;
  291. vertexPositions[3 * n] = x;
  292. vertexPositions[3 * n + 1] = y;
  293. vertexPositions[3 * n + 2] = z;
  294. vertexNormals[3 * n] = nx;
  295. vertexNormals[3 * n + 1] = ny;
  296. vertexNormals[3 * n + 2] = nz;
  297. }
  298. var vertex_data = new VertexData();
  299. vertex_data.positions = vertexPositions;
  300. vertex_data.normals = vertexNormals;
  301. vertex_data.uvs = object.getVerticesData(VertexBuffer.UVKind);
  302. vertex_data.colors = object.getVerticesData(VertexBuffer.ColorKind);
  303. if (object && object.getIndices) {
  304. vertex_data.indices = object.getIndices();
  305. }
  306. vertex_data.applyToMesh(<Mesh>object);
  307. }
  308. private _tmpMatrix = new Matrix();
  309. /**
  310. * Applies an impulse on the imposter
  311. * @param impostor imposter to apply impulse to
  312. * @param force amount of force to be applied to the imposter
  313. * @param contactPoint the location to apply the impulse on the imposter
  314. */
  315. public applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3) {
  316. if (!impostor.soft) {
  317. impostor.physicsBody.activate();
  318. var worldPoint = this._tmpAmmoVectorA;
  319. var impulse = this._tmpAmmoVectorB;
  320. // Convert contactPoint relative to center of mass
  321. if (impostor.object && impostor.object.getWorldMatrix) {
  322. contactPoint.subtractInPlace(impostor.object.getWorldMatrix().getTranslation());
  323. }
  324. worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
  325. impulse.setValue(force.x, force.y, force.z);
  326. impostor.physicsBody.applyImpulse(impulse, worldPoint);
  327. }
  328. else {
  329. Logger.Warn("Cannot be applied to a soft body");
  330. }
  331. }
  332. /**
  333. * Applies a force on the imposter
  334. * @param impostor imposter to apply force
  335. * @param force amount of force to be applied to the imposter
  336. * @param contactPoint the location to apply the force on the imposter
  337. */
  338. public applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3) {
  339. if (!impostor.soft) {
  340. impostor.physicsBody.activate();
  341. var worldPoint = this._tmpAmmoVectorA;
  342. var impulse = this._tmpAmmoVectorB;
  343. // Convert contactPoint relative to center of mass
  344. if (impostor.object && impostor.object.getWorldMatrix) {
  345. contactPoint.subtractInPlace(impostor.object.getWorldMatrix().getTranslation());
  346. }
  347. worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
  348. impulse.setValue(force.x, force.y, force.z);
  349. impostor.physicsBody.applyForce(impulse, worldPoint);
  350. }
  351. else {
  352. Logger.Warn("Cannot be applied to a soft body");
  353. }
  354. }
  355. /**
  356. * Creates a physics body using the plugin
  357. * @param impostor the imposter to create the physics body on
  358. */
  359. public generatePhysicsBody(impostor: PhysicsImpostor) {
  360. // Note: this method will not be called on child imposotrs for compound impostors
  361. impostor._pluginData.toDispose = [];
  362. //parent-child relationship
  363. if (impostor.parent) {
  364. if (impostor.physicsBody) {
  365. this.removePhysicsBody(impostor);
  366. impostor.forceUpdate();
  367. }
  368. return;
  369. }
  370. if (impostor.isBodyInitRequired()) {
  371. var colShape = this._createShape(impostor);
  372. var mass = impostor.getParam("mass");
  373. impostor._pluginData.mass = mass;
  374. if (impostor.soft) {
  375. colShape.get_m_cfg().set_collisions(0x11);
  376. colShape.get_m_cfg().set_kDP(impostor.getParam("damping"));
  377. this.bjsAMMO.castObject(colShape, this.bjsAMMO.btCollisionObject).getCollisionShape().setMargin(impostor.getParam("margin"));
  378. colShape.setActivationState(AmmoJSPlugin.DISABLE_DEACTIVATION_FLAG);
  379. this.world.addSoftBody(colShape, 1, -1);
  380. impostor.physicsBody = colShape;
  381. impostor._pluginData.toDispose.push(colShape);
  382. this.setBodyPressure(impostor, 0);
  383. if (impostor.type === PhysicsImpostor.SoftbodyImpostor) {
  384. this.setBodyPressure(impostor, impostor.getParam("pressure"));
  385. }
  386. this.setBodyStiffness(impostor, impostor.getParam("stiffness"));
  387. this.setBodyVelocityIterations(impostor, impostor.getParam("velocityIterations"));
  388. this.setBodyPositionIterations(impostor, impostor.getParam("positionIterations"));
  389. }
  390. else {
  391. var localInertia = new this.bjsAMMO.btVector3(0, 0, 0);
  392. var startTransform = new this.bjsAMMO.btTransform();
  393. startTransform.setIdentity();
  394. if (mass !== 0) {
  395. colShape.calculateLocalInertia(mass, localInertia);
  396. }
  397. this._tmpAmmoVectorA.setValue(impostor.object.position.x, impostor.object.position.y, impostor.object.position.z);
  398. this._tmpAmmoQuaternion.setValue(impostor.object.rotationQuaternion!.x, impostor.object.rotationQuaternion!.y, impostor.object.rotationQuaternion!.z, impostor.object.rotationQuaternion!.w);
  399. startTransform.setOrigin(this._tmpAmmoVectorA);
  400. startTransform.setRotation(this._tmpAmmoQuaternion);
  401. var myMotionState = new this.bjsAMMO.btDefaultMotionState(startTransform);
  402. var rbInfo = new this.bjsAMMO.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia);
  403. var body = new this.bjsAMMO.btRigidBody(rbInfo);
  404. // Make objects kinematic if it's mass is 0
  405. if (mass === 0) {
  406. body.setCollisionFlags(body.getCollisionFlags() | AmmoJSPlugin.KINEMATIC_FLAG);
  407. body.setActivationState(AmmoJSPlugin.DISABLE_DEACTIVATION_FLAG);
  408. }
  409. // Disable collision if NoImpostor, but keep collision if shape is btCompoundShape
  410. if (impostor.type == PhysicsImpostor.NoImpostor && !colShape.getChildShape) {
  411. body.setCollisionFlags(body.getCollisionFlags() | AmmoJSPlugin.DISABLE_COLLISION_FLAG);
  412. }
  413. let group = impostor.getParam("group");
  414. let mask = impostor.getParam("mask");
  415. if (group && mask) {
  416. this.world.addRigidBody(body, group, mask);
  417. } else {
  418. this.world.addRigidBody(body);
  419. }
  420. impostor.physicsBody = body;
  421. impostor._pluginData.toDispose = impostor._pluginData.toDispose.concat([body, rbInfo, myMotionState, startTransform, localInertia, colShape]);
  422. }
  423. this.setBodyRestitution(impostor, impostor.getParam("restitution"));
  424. this.setBodyFriction(impostor, impostor.getParam("friction"));
  425. }
  426. }
  427. /**
  428. * Removes the physics body from the imposter and disposes of the body's memory
  429. * @param impostor imposter to remove the physics body from
  430. */
  431. public removePhysicsBody(impostor: PhysicsImpostor) {
  432. if (this.world) {
  433. if (impostor.soft) {
  434. this.world.removeSoftBody(impostor.physicsBody);
  435. } else {
  436. this.world.removeRigidBody(impostor.physicsBody);
  437. }
  438. if (impostor._pluginData) {
  439. impostor._pluginData.toDispose.forEach((d: any) => {
  440. this.bjsAMMO.destroy(d);
  441. });
  442. impostor._pluginData.toDispose = [];
  443. }
  444. }
  445. }
  446. /**
  447. * Generates a joint
  448. * @param impostorJoint the imposter joint to create the joint with
  449. */
  450. public generateJoint(impostorJoint: PhysicsImpostorJoint) {
  451. var mainBody = impostorJoint.mainImpostor.physicsBody;
  452. var connectedBody = impostorJoint.connectedImpostor.physicsBody;
  453. if (!mainBody || !connectedBody) {
  454. return;
  455. }
  456. var jointData = impostorJoint.joint.jointData;
  457. if (!jointData.mainPivot) {
  458. jointData.mainPivot = new Vector3(0, 0, 0);
  459. }
  460. if (!jointData.connectedPivot) {
  461. jointData.connectedPivot = new Vector3(0, 0, 0);
  462. }
  463. var joint: any;
  464. switch (impostorJoint.joint.type) {
  465. case PhysicsJoint.DistanceJoint:
  466. var distance = (<DistanceJointData>jointData).maxDistance;
  467. if (distance) {
  468. jointData.mainPivot = new Vector3(0, -distance / 2, 0);
  469. jointData.connectedPivot = new Vector3(0, distance / 2, 0);
  470. }
  471. joint = new this.bjsAMMO.btPoint2PointConstraint(mainBody, connectedBody, new this.bjsAMMO.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new this.bjsAMMO.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
  472. break;
  473. case PhysicsJoint.HingeJoint:
  474. if (!jointData.mainAxis) {
  475. jointData.mainAxis = new Vector3(0, 0, 0);
  476. }
  477. if (!jointData.connectedAxis) {
  478. jointData.connectedAxis = new Vector3(0, 0, 0);
  479. }
  480. var mainAxis = new this.bjsAMMO.btVector3(jointData.mainAxis.x, jointData.mainAxis.y, jointData.mainAxis.z);
  481. var connectedAxis = new this.bjsAMMO.btVector3(jointData.connectedAxis.x, jointData.connectedAxis.y, jointData.connectedAxis.z);
  482. joint = new this.bjsAMMO.btHingeConstraint(mainBody, connectedBody, new this.bjsAMMO.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new this.bjsAMMO.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z), mainAxis, connectedAxis);
  483. break;
  484. case PhysicsJoint.BallAndSocketJoint:
  485. joint = new this.bjsAMMO.btPoint2PointConstraint(mainBody, connectedBody, new this.bjsAMMO.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new this.bjsAMMO.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
  486. break;
  487. default:
  488. Logger.Warn("JointType not currently supported by the Ammo plugin, falling back to PhysicsJoint.BallAndSocketJoint");
  489. joint = new this.bjsAMMO.btPoint2PointConstraint(mainBody, connectedBody, new this.bjsAMMO.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new this.bjsAMMO.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
  490. break;
  491. }
  492. this.world.addConstraint(joint, !impostorJoint.joint.jointData.collision);
  493. impostorJoint.joint.physicsJoint = joint;
  494. }
  495. /**
  496. * Removes a joint
  497. * @param impostorJoint the imposter joint to remove the joint from
  498. */
  499. public removeJoint(impostorJoint: PhysicsImpostorJoint) {
  500. if (this.world) {
  501. this.world.removeConstraint(impostorJoint.joint.physicsJoint);
  502. }
  503. }
  504. // adds all verticies (including child verticies) to the triangle mesh
  505. private _addMeshVerts(btTriangleMesh: any, topLevelObject: IPhysicsEnabledObject, object: IPhysicsEnabledObject) {
  506. var triangleCount = 0;
  507. if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
  508. var indices = object.getIndices();
  509. if (!indices) {
  510. indices = [];
  511. }
  512. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  513. if (!vertexPositions) {
  514. vertexPositions = [];
  515. }
  516. object.computeWorldMatrix(false);
  517. var faceCount = indices.length / 3;
  518. for (var i = 0; i < faceCount; i++) {
  519. var triPoints = [];
  520. for (var point = 0; point < 3; point++) {
  521. var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
  522. // Adjust for initial scaling
  523. Matrix.ScalingToRef(object.scaling.x, object.scaling.y, object.scaling.z, this._tmpMatrix);
  524. v = Vector3.TransformCoordinates(v, this._tmpMatrix);
  525. var vec: any;
  526. if (point == 0) {
  527. vec = this._tmpAmmoVectorA;
  528. } else if (point == 1) {
  529. vec = this._tmpAmmoVectorB;
  530. } else {
  531. vec = this._tmpAmmoVectorC;
  532. }
  533. vec.setValue(v.x, v.y, v.z);
  534. triPoints.push(vec);
  535. }
  536. btTriangleMesh.addTriangle(triPoints[0], triPoints[1], triPoints[2]);
  537. triangleCount++;
  538. }
  539. object.getChildMeshes().forEach((m) => {
  540. triangleCount += this._addMeshVerts(btTriangleMesh, topLevelObject, m);
  541. });
  542. }
  543. return triangleCount;
  544. }
  545. /**
  546. * Initialise the soft body vertices to match its object's (mesh) vertices
  547. * Softbody vertices (nodes) are in world space and to match this
  548. * The object's position and rotation is set to zero and so its vertices are also then set in world space
  549. * @param impostor to create the softbody for
  550. */
  551. private _softVertexData(impostor: PhysicsImpostor): VertexData {
  552. var object = impostor.object;
  553. if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
  554. var indices = object.getIndices();
  555. if (!indices) {
  556. indices = [];
  557. }
  558. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  559. if (!vertexPositions) {
  560. vertexPositions = [];
  561. }
  562. var vertexNormals = object.getVerticesData(VertexBuffer.NormalKind);
  563. if (!vertexNormals) {
  564. vertexNormals = [];
  565. }
  566. object.computeWorldMatrix(false);
  567. var newPoints = [];
  568. var newNorms = [];
  569. for (var i = 0; i < vertexPositions.length; i += 3) {
  570. var v = new Vector3(vertexPositions[i], vertexPositions[i + 1], vertexPositions[i + 2]);
  571. var n = new Vector3(vertexNormals[i], vertexNormals[i + 1], vertexNormals[i + 2]);
  572. v = Vector3.TransformCoordinates(v, object.getWorldMatrix());
  573. n = Vector3.TransformNormal(n, object.getWorldMatrix());
  574. newPoints.push(v.x, v.y, v.z);
  575. newNorms.push(n.x, n.y, n.z);
  576. }
  577. var vertex_data = new VertexData();
  578. vertex_data.positions = newPoints;
  579. vertex_data.normals = newNorms;
  580. vertex_data.uvs = object.getVerticesData(VertexBuffer.UVKind);
  581. vertex_data.colors = object.getVerticesData(VertexBuffer.ColorKind);
  582. if (object && object.getIndices) {
  583. vertex_data.indices = object.getIndices();
  584. }
  585. vertex_data.applyToMesh(<Mesh>object);
  586. object.position = Vector3.Zero();
  587. object.rotationQuaternion = null;
  588. object.rotation = Vector3.Zero();
  589. object.computeWorldMatrix(true);
  590. return vertex_data;
  591. }
  592. return VertexData.ExtractFromMesh(<Mesh>object);
  593. }
  594. /**
  595. * Create an impostor's soft body
  596. * @param impostor to create the softbody for
  597. */
  598. private _createSoftbody(impostor: PhysicsImpostor) {
  599. var object = impostor.object;
  600. if (object && object.getIndices) {
  601. var indices = object.getIndices();
  602. if (!indices) {
  603. indices = [];
  604. }
  605. var vertex_data = this._softVertexData(impostor);
  606. var vertexPositions = vertex_data.positions;
  607. var vertexNormals = vertex_data.normals;
  608. if (vertexPositions === null || vertexNormals === null) {
  609. return new this.bjsAMMO.btCompoundShape();
  610. }
  611. else {
  612. var triPoints = [];
  613. var triNorms = [];
  614. for (var i = 0; i < vertexPositions.length; i += 3) {
  615. var v = new Vector3(vertexPositions[i], vertexPositions[i + 1], vertexPositions[i + 2]);
  616. var n = new Vector3(vertexNormals[i], vertexNormals[i + 1], vertexNormals[i + 2]);
  617. triPoints.push(v.x, v.y, -v.z);
  618. triNorms.push(n.x, n.y, -n.z);
  619. }
  620. var softBody = new this.bjsAMMO.btSoftBodyHelpers().CreateFromTriMesh(
  621. this.world.getWorldInfo(),
  622. triPoints,
  623. object.getIndices(),
  624. indices.length / 3,
  625. true
  626. );
  627. var nbVertices = vertexPositions.length / 3;
  628. var bodyVertices = softBody.get_m_nodes();
  629. var node: any;
  630. var nodeNormals: any;
  631. for (var i = 0; i < nbVertices; i++) {
  632. node = bodyVertices.at(i);
  633. var nodeNormals = node.get_m_n();
  634. nodeNormals.setX(triNorms[3 * i]);
  635. nodeNormals.setY(triNorms[3 * i + 1]);
  636. nodeNormals.setZ(triNorms[3 * i + 2]);
  637. }
  638. return softBody;
  639. }
  640. }
  641. }
  642. /**
  643. * Create cloth for an impostor
  644. * @param impostor to create the softbody for
  645. */
  646. private _createCloth(impostor: PhysicsImpostor) {
  647. var object = impostor.object;
  648. if (object && object.getIndices) {
  649. var indices = object.getIndices();
  650. if (!indices) {
  651. indices = [];
  652. }
  653. var vertex_data = this._softVertexData(impostor);
  654. var vertexPositions = vertex_data.positions;
  655. var vertexNormals = vertex_data.normals;
  656. if (vertexPositions === null || vertexNormals === null) {
  657. return new this.bjsAMMO.btCompoundShape();
  658. }
  659. else {
  660. var len = vertexPositions.length;
  661. var segments = Math.sqrt(len / 3);
  662. impostor.segments = segments;
  663. var segs = segments - 1;
  664. this._tmpAmmoVectorA.setValue(vertexPositions[0], vertexPositions[1], vertexPositions[2]);
  665. this._tmpAmmoVectorB.setValue(vertexPositions[3 * segs], vertexPositions[3 * segs + 1], vertexPositions[3 * segs + 2]);
  666. this._tmpAmmoVectorD.setValue(vertexPositions[len - 3], vertexPositions[len - 2], vertexPositions[len - 1]);
  667. this._tmpAmmoVectorC.setValue(vertexPositions[len - 3 - 3 * segs], vertexPositions[len - 2 - 3 * segs], vertexPositions[len - 1 - 3 * segs]);
  668. var clothBody = new this.bjsAMMO.btSoftBodyHelpers().CreatePatch(
  669. this.world.getWorldInfo(),
  670. this._tmpAmmoVectorA,
  671. this._tmpAmmoVectorB,
  672. this._tmpAmmoVectorC,
  673. this._tmpAmmoVectorD,
  674. segments,
  675. segments,
  676. impostor.getParam("fixedPoints"),
  677. true
  678. );
  679. return clothBody;
  680. }
  681. }
  682. }
  683. /**
  684. * Create rope for an impostor
  685. * @param impostor to create the softbody for
  686. */
  687. private _createRope(impostor: PhysicsImpostor) {
  688. var len: number;
  689. var segments: number;
  690. var vertex_data = this._softVertexData(impostor);
  691. var vertexPositions = vertex_data.positions;
  692. var vertexNormals = vertex_data.normals;
  693. if (vertexPositions === null || vertexNormals === null) {
  694. return new this.bjsAMMO.btCompoundShape();
  695. }
  696. //force the mesh to be updatable
  697. vertex_data.applyToMesh(<Mesh>impostor.object, true);
  698. impostor._isFromLine = true;
  699. // If in lines mesh all normals will be zero
  700. var vertexSquared: Array<number> = <Array<number>>vertexNormals.map((x: number) => x * x);
  701. var reducer = (accumulator: number, currentValue: number): number => accumulator + currentValue;
  702. var reduced: number = vertexSquared.reduce(reducer);
  703. if (reduced === 0) { // line mesh
  704. len = vertexPositions.length;
  705. segments = len / 3 - 1;
  706. this._tmpAmmoVectorA.setValue(vertexPositions[0], vertexPositions[1], vertexPositions[2]);
  707. this._tmpAmmoVectorB.setValue(vertexPositions[len - 3], vertexPositions[len - 2], vertexPositions[len - 1]);
  708. }
  709. else { //extruded mesh
  710. impostor._isFromLine = false;
  711. var pathVectors = impostor.getParam("path");
  712. var shape = impostor.getParam("shape");
  713. if (shape === null) {
  714. Logger.Warn("No shape available for extruded mesh");
  715. return new this.bjsAMMO.btCompoundShape();
  716. }
  717. if ((vertexPositions!.length % (3 * pathVectors.length)) !== 0) {
  718. Logger.Warn("Path does not match extrusion");
  719. return new this.bjsAMMO.btCompoundShape();
  720. }
  721. len = pathVectors.length;
  722. segments = len - 1;
  723. this._tmpAmmoVectorA.setValue(pathVectors[0].x, pathVectors[0].y, pathVectors[0].z);
  724. this._tmpAmmoVectorB.setValue(pathVectors[len - 1].x, pathVectors[len - 1].y, pathVectors[len - 1].z);
  725. }
  726. impostor.segments = segments;
  727. var fixedPoints = impostor.getParam("fixedPoints");
  728. fixedPoints = (fixedPoints > 3) ? 3 : fixedPoints;
  729. var ropeBody = new this.bjsAMMO.btSoftBodyHelpers().CreateRope(
  730. this.world.getWorldInfo(),
  731. this._tmpAmmoVectorA,
  732. this._tmpAmmoVectorB,
  733. segments - 1,
  734. fixedPoints
  735. );
  736. ropeBody.get_m_cfg().set_collisions(0x11);
  737. return ropeBody;
  738. }
  739. /**
  740. * Create a custom physics impostor shape using the plugin's onCreateCustomShape handler
  741. * @param impostor to create the custom physics shape for
  742. */
  743. private _createCustom(impostor: PhysicsImpostor): any {
  744. let returnValue: any = null;
  745. if (this.onCreateCustomShape) {
  746. returnValue = this.onCreateCustomShape(impostor);
  747. }
  748. if (returnValue == null) {
  749. returnValue = new this.bjsAMMO.btCompoundShape();
  750. }
  751. return returnValue;
  752. }
  753. // adds all verticies (including child verticies) to the convex hull shape
  754. private _addHullVerts(btConvexHullShape: any, topLevelObject: IPhysicsEnabledObject, object: IPhysicsEnabledObject) {
  755. var triangleCount = 0;
  756. if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
  757. var indices = object.getIndices();
  758. if (!indices) {
  759. indices = [];
  760. }
  761. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  762. if (!vertexPositions) {
  763. vertexPositions = [];
  764. }
  765. object.computeWorldMatrix(false);
  766. var faceCount = indices.length / 3;
  767. for (var i = 0; i < faceCount; i++) {
  768. var triPoints = [];
  769. for (var point = 0; point < 3; point++) {
  770. var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
  771. // Adjust for initial scaling
  772. Matrix.ScalingToRef(object.scaling.x, object.scaling.y, object.scaling.z, this._tmpMatrix);
  773. v = Vector3.TransformCoordinates(v, this._tmpMatrix);
  774. var vec: any;
  775. if (point == 0) {
  776. vec = this._tmpAmmoVectorA;
  777. } else if (point == 1) {
  778. vec = this._tmpAmmoVectorB;
  779. } else {
  780. vec = this._tmpAmmoVectorC;
  781. }
  782. vec.setValue(v.x, v.y, v.z);
  783. triPoints.push(vec);
  784. }
  785. btConvexHullShape.addPoint(triPoints[0], true);
  786. btConvexHullShape.addPoint(triPoints[1], true);
  787. btConvexHullShape.addPoint(triPoints[2], true);
  788. triangleCount++;
  789. }
  790. object.getChildMeshes().forEach((m) => {
  791. triangleCount += this._addHullVerts(btConvexHullShape, topLevelObject, m);
  792. });
  793. }
  794. return triangleCount;
  795. }
  796. private _createShape(impostor: PhysicsImpostor, ignoreChildren = false) {
  797. var object = impostor.object;
  798. var returnValue: any;
  799. var extendSize = impostor.getObjectExtendSize();
  800. if (!ignoreChildren) {
  801. var meshChildren = impostor.object.getChildMeshes ? impostor.object.getChildMeshes(true) : [];
  802. returnValue = new this.bjsAMMO.btCompoundShape();
  803. // Add shape of all children to the compound shape
  804. var childrenAdded = 0;
  805. meshChildren.forEach((childMesh) => {
  806. var childImpostor = childMesh.getPhysicsImpostor();
  807. if (childImpostor) {
  808. if (childImpostor.type == PhysicsImpostor.MeshImpostor) {
  809. throw "A child MeshImpostor is not supported. Only primitive impostors are supported as children (eg. box or sphere)";
  810. }
  811. var shape = this._createShape(childImpostor);
  812. // Position needs to be scaled based on parent's scaling
  813. var parentMat = childMesh.parent!.getWorldMatrix().clone();
  814. var s = new Vector3();
  815. parentMat.decompose(s);
  816. this._tmpAmmoTransform.getOrigin().setValue(childMesh.position.x * s.x, childMesh.position.y * s.y, childMesh.position.z * s.z);
  817. this._tmpAmmoQuaternion.setValue(childMesh.rotationQuaternion!.x, childMesh.rotationQuaternion!.y, childMesh.rotationQuaternion!.z, childMesh.rotationQuaternion!.w);
  818. this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
  819. returnValue.addChildShape(this._tmpAmmoTransform, shape);
  820. childImpostor.dispose();
  821. childrenAdded++;
  822. }
  823. });
  824. if (childrenAdded > 0) {
  825. // Add parents shape as a child if present
  826. if (impostor.type != PhysicsImpostor.NoImpostor) {
  827. var shape = this._createShape(impostor, true);
  828. if (shape) {
  829. this._tmpAmmoTransform.getOrigin().setValue(0, 0, 0);
  830. this._tmpAmmoQuaternion.setValue(0, 0, 0, 1);
  831. this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
  832. returnValue.addChildShape(this._tmpAmmoTransform, shape);
  833. }
  834. }
  835. return returnValue;
  836. } else {
  837. // If no children with impostors create the actual shape below instead
  838. this.bjsAMMO.destroy(returnValue);
  839. returnValue = null;
  840. }
  841. }
  842. switch (impostor.type) {
  843. case PhysicsImpostor.SphereImpostor:
  844. // Is there a better way to compare floats number? With an epsylon or with a Math function
  845. if (Scalar.WithinEpsilon(extendSize.x, extendSize.y, 0.0001) && Scalar.WithinEpsilon(extendSize.x, extendSize.z, 0.0001)) {
  846. returnValue = new this.bjsAMMO.btSphereShape(extendSize.x / 2);
  847. } else {
  848. // create a btMultiSphereShape because it's not possible to set a local scaling on a btSphereShape
  849. var positions = [new this.bjsAMMO.btVector3(0, 0, 0)];
  850. var radii = [1];
  851. returnValue = new this.bjsAMMO.btMultiSphereShape(positions, radii, 1);
  852. returnValue.setLocalScaling(new this.bjsAMMO.btVector3(extendSize.x / 2, extendSize.y / 2, extendSize.z / 2));
  853. }
  854. break;
  855. case PhysicsImpostor.CapsuleImpostor:
  856. returnValue = new this.bjsAMMO.btCapsuleShape(extendSize.x / 2, extendSize.y / 2);
  857. break;
  858. case PhysicsImpostor.CylinderImpostor:
  859. this._tmpAmmoVectorA.setValue(extendSize.x / 2, extendSize.y / 2, extendSize.z / 2);
  860. returnValue = new this.bjsAMMO.btCylinderShape(this._tmpAmmoVectorA);
  861. break;
  862. case PhysicsImpostor.PlaneImpostor:
  863. case PhysicsImpostor.BoxImpostor:
  864. this._tmpAmmoVectorA.setValue(extendSize.x / 2, extendSize.y / 2, extendSize.z / 2);
  865. returnValue = new this.bjsAMMO.btBoxShape(this._tmpAmmoVectorA);
  866. break;
  867. case PhysicsImpostor.MeshImpostor:
  868. if (impostor.getParam("mass") == 0) {
  869. // Only create btBvhTriangleMeshShape impostor is static
  870. // See https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=7283
  871. var tetraMesh = new this.bjsAMMO.btTriangleMesh();
  872. impostor._pluginData.toDispose.push(tetraMesh);
  873. var triangeCount = this._addMeshVerts(tetraMesh, object, object);
  874. if (triangeCount == 0) {
  875. returnValue = new this.bjsAMMO.btCompoundShape();
  876. } else {
  877. returnValue = new this.bjsAMMO.btBvhTriangleMeshShape(tetraMesh);
  878. }
  879. break;
  880. }
  881. // Otherwise create convexHullImpostor
  882. case PhysicsImpostor.ConvexHullImpostor:
  883. var convexMesh = new this.bjsAMMO.btConvexHullShape();
  884. var triangeCount = this._addHullVerts(convexMesh, object, object);
  885. if (triangeCount == 0) {
  886. // Cleanup Unused Convex Hull Shape
  887. impostor._pluginData.toDispose.push(convexMesh);
  888. returnValue = new this.bjsAMMO.btCompoundShape();
  889. } else {
  890. returnValue = convexMesh;
  891. }
  892. break;
  893. case PhysicsImpostor.NoImpostor:
  894. // Fill with sphere but collision is disabled on the rigid body in generatePhysicsBody, using an empty shape caused unexpected movement with joints
  895. returnValue = new this.bjsAMMO.btSphereShape(extendSize.x / 2);
  896. break;
  897. case PhysicsImpostor.CustomImpostor:
  898. // Only usable when the plugin's onCreateCustomShape is set
  899. returnValue = this._createCustom(impostor);
  900. break;
  901. case PhysicsImpostor.SoftbodyImpostor:
  902. // Only usable with a mesh that has sufficient and shared vertices
  903. returnValue = this._createSoftbody(impostor);
  904. break;
  905. case PhysicsImpostor.ClothImpostor:
  906. // Only usable with a ground mesh that has sufficient and shared vertices
  907. returnValue = this._createCloth(impostor);
  908. break;
  909. case PhysicsImpostor.RopeImpostor:
  910. // Only usable with a line mesh or an extruded mesh that is updatable
  911. returnValue = this._createRope(impostor);
  912. break;
  913. default:
  914. Logger.Warn("The impostor type is not currently supported by the ammo plugin.");
  915. break;
  916. }
  917. return returnValue;
  918. }
  919. /**
  920. * Sets the physics body position/rotation from the babylon mesh's position/rotation
  921. * @param impostor imposter containing the physics body and babylon object
  922. */
  923. public setTransformationFromPhysicsBody(impostor: PhysicsImpostor) {
  924. impostor.physicsBody.getMotionState().getWorldTransform(this._tmpAmmoTransform);
  925. impostor.object.position.set(this._tmpAmmoTransform.getOrigin().x(), this._tmpAmmoTransform.getOrigin().y(), this._tmpAmmoTransform.getOrigin().z());
  926. if (!impostor.object.rotationQuaternion) {
  927. if (impostor.object.rotation) {
  928. this._tmpQuaternion.set(this._tmpAmmoTransform.getRotation().x(), this._tmpAmmoTransform.getRotation().y(), this._tmpAmmoTransform.getRotation().z(), this._tmpAmmoTransform.getRotation().w());
  929. this._tmpQuaternion.toEulerAnglesToRef(impostor.object.rotation);
  930. }
  931. } else {
  932. impostor.object.rotationQuaternion.set(this._tmpAmmoTransform.getRotation().x(), this._tmpAmmoTransform.getRotation().y(), this._tmpAmmoTransform.getRotation().z(), this._tmpAmmoTransform.getRotation().w());
  933. }
  934. }
  935. /**
  936. * Sets the babylon object's position/rotation from the physics body's position/rotation
  937. * @param impostor imposter containing the physics body and babylon object
  938. * @param newPosition new position
  939. * @param newRotation new rotation
  940. */
  941. public setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion) {
  942. var trans = impostor.physicsBody.getWorldTransform();
  943. // If rotation/position has changed update and activate rigged body
  944. if (
  945. Math.abs(trans.getOrigin().x() - newPosition.x) > Epsilon ||
  946. Math.abs(trans.getOrigin().y() - newPosition.y) > Epsilon ||
  947. Math.abs(trans.getOrigin().z() - newPosition.z) > Epsilon ||
  948. Math.abs(trans.getRotation().x() - newRotation.x) > Epsilon ||
  949. Math.abs(trans.getRotation().y() - newRotation.y) > Epsilon ||
  950. Math.abs(trans.getRotation().z() - newRotation.z) > Epsilon ||
  951. Math.abs(trans.getRotation().w() - newRotation.w) > Epsilon
  952. ) {
  953. this._tmpAmmoVectorA.setValue(newPosition.x, newPosition.y, newPosition.z);
  954. trans.setOrigin(this._tmpAmmoVectorA);
  955. this._tmpAmmoQuaternion.setValue(newRotation.x, newRotation.y, newRotation.z, newRotation.w);
  956. trans.setRotation(this._tmpAmmoQuaternion);
  957. impostor.physicsBody.setWorldTransform(trans);
  958. if (impostor.mass == 0) {
  959. // Kinematic objects must be updated using motion state
  960. var motionState = impostor.physicsBody.getMotionState();
  961. if (motionState) {
  962. motionState.setWorldTransform(trans);
  963. }
  964. } else {
  965. impostor.physicsBody.activate();
  966. }
  967. }
  968. }
  969. /**
  970. * If this plugin is supported
  971. * @returns true if its supported
  972. */
  973. public isSupported(): boolean {
  974. return this.bjsAMMO !== undefined;
  975. }
  976. /**
  977. * Sets the linear velocity of the physics body
  978. * @param impostor imposter to set the velocity on
  979. * @param velocity velocity to set
  980. */
  981. public setLinearVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
  982. this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
  983. if (impostor.soft) {
  984. impostor.physicsBody.linearVelocity(this._tmpAmmoVectorA);
  985. }
  986. else {
  987. impostor.physicsBody.setLinearVelocity(this._tmpAmmoVectorA);
  988. }
  989. }
  990. /**
  991. * Sets the angular velocity of the physics body
  992. * @param impostor imposter to set the velocity on
  993. * @param velocity velocity to set
  994. */
  995. public setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
  996. this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
  997. if (impostor.soft) {
  998. impostor.physicsBody.angularVelocity(this._tmpAmmoVectorA);
  999. }
  1000. else {
  1001. impostor.physicsBody.setAngularVelocity(this._tmpAmmoVectorA);
  1002. }
  1003. }
  1004. /**
  1005. * gets the linear velocity
  1006. * @param impostor imposter to get linear velocity from
  1007. * @returns linear velocity
  1008. */
  1009. public getLinearVelocity(impostor: PhysicsImpostor): Nullable<Vector3> {
  1010. if (impostor.soft) {
  1011. var v = impostor.physicsBody.linearVelocity();
  1012. }
  1013. else {
  1014. var v = impostor.physicsBody.getLinearVelocity();
  1015. }
  1016. if (!v) {
  1017. return null;
  1018. }
  1019. var result = new Vector3(v.x(), v.y(), v.z());
  1020. this.bjsAMMO.destroy(v);
  1021. return result;
  1022. }
  1023. /**
  1024. * gets the angular velocity
  1025. * @param impostor imposter to get angular velocity from
  1026. * @returns angular velocity
  1027. */
  1028. public getAngularVelocity(impostor: PhysicsImpostor): Nullable<Vector3> {
  1029. if (impostor.soft) {
  1030. var v = impostor.physicsBody.angularVelocity();
  1031. }
  1032. else {
  1033. var v = impostor.physicsBody.getAngularVelocity();
  1034. }
  1035. if (!v) {
  1036. return null;
  1037. }
  1038. var result = new Vector3(v.x(), v.y(), v.z());
  1039. this.bjsAMMO.destroy(v);
  1040. return result;
  1041. }
  1042. /**
  1043. * Sets the mass of physics body
  1044. * @param impostor imposter to set the mass on
  1045. * @param mass mass to set
  1046. */
  1047. public setBodyMass(impostor: PhysicsImpostor, mass: number) {
  1048. if (impostor.soft) {
  1049. impostor.physicsBody.setTotalMass(mass, false);
  1050. }
  1051. else {
  1052. impostor.physicsBody.setMassProps(mass);
  1053. }
  1054. impostor._pluginData.mass = mass;
  1055. }
  1056. /**
  1057. * Gets the mass of the physics body
  1058. * @param impostor imposter to get the mass from
  1059. * @returns mass
  1060. */
  1061. public getBodyMass(impostor: PhysicsImpostor): number {
  1062. return impostor._pluginData.mass || 0;
  1063. }
  1064. /**
  1065. * Gets friction of the impostor
  1066. * @param impostor impostor to get friction from
  1067. * @returns friction value
  1068. */
  1069. public getBodyFriction(impostor: PhysicsImpostor): number {
  1070. return impostor._pluginData.friction || 0;
  1071. }
  1072. /**
  1073. * Sets friction of the impostor
  1074. * @param impostor impostor to set friction on
  1075. * @param friction friction value
  1076. */
  1077. public setBodyFriction(impostor: PhysicsImpostor, friction: number) {
  1078. if (impostor.soft) {
  1079. impostor.physicsBody.get_m_cfg().set_kDF(friction);
  1080. }
  1081. else {
  1082. impostor.physicsBody.setFriction(friction);
  1083. }
  1084. impostor._pluginData.friction = friction;
  1085. }
  1086. /**
  1087. * Gets restitution of the impostor
  1088. * @param impostor impostor to get restitution from
  1089. * @returns restitution value
  1090. */
  1091. public getBodyRestitution(impostor: PhysicsImpostor): number {
  1092. return impostor._pluginData.restitution || 0;
  1093. }
  1094. /**
  1095. * Sets resitution of the impostor
  1096. * @param impostor impostor to set resitution on
  1097. * @param restitution resitution value
  1098. */
  1099. public setBodyRestitution(impostor: PhysicsImpostor, restitution: number) {
  1100. impostor.physicsBody.setRestitution(restitution);
  1101. impostor._pluginData.restitution = restitution;
  1102. }
  1103. /**
  1104. * Gets pressure inside the impostor
  1105. * @param impostor impostor to get pressure from
  1106. * @returns pressure value
  1107. */
  1108. public getBodyPressure(impostor: PhysicsImpostor): number {
  1109. if (!impostor.soft) {
  1110. Logger.Warn("Pressure is not a property of a rigid body");
  1111. return 0;
  1112. }
  1113. return impostor._pluginData.pressure || 0;
  1114. }
  1115. /**
  1116. * Sets pressure inside a soft body impostor
  1117. * Cloth and rope must remain 0 pressure
  1118. * @param impostor impostor to set pressure on
  1119. * @param pressure pressure value
  1120. */
  1121. public setBodyPressure(impostor: PhysicsImpostor, pressure: number) {
  1122. if (impostor.soft) {
  1123. if (impostor.type === PhysicsImpostor.SoftbodyImpostor) {
  1124. impostor.physicsBody.get_m_cfg().set_kPR(pressure);
  1125. impostor._pluginData.pressure = pressure;
  1126. }
  1127. else {
  1128. impostor.physicsBody.get_m_cfg().set_kPR(0);
  1129. impostor._pluginData.pressure = 0;
  1130. }
  1131. }
  1132. else {
  1133. Logger.Warn("Pressure can only be applied to a softbody");
  1134. }
  1135. }
  1136. /**
  1137. * Gets stiffness of the impostor
  1138. * @param impostor impostor to get stiffness from
  1139. * @returns pressure value
  1140. */
  1141. public getBodyStiffness(impostor: PhysicsImpostor): number {
  1142. if (!impostor.soft) {
  1143. Logger.Warn("Stiffness is not a property of a rigid body");
  1144. return 0;
  1145. }
  1146. return impostor._pluginData.stiffness || 0;
  1147. }
  1148. /**
  1149. * Sets stiffness of the impostor
  1150. * @param impostor impostor to set stiffness on
  1151. * @param stiffness stiffness value from 0 to 1
  1152. */
  1153. public setBodyStiffness(impostor: PhysicsImpostor, stiffness: number) {
  1154. if (impostor.soft) {
  1155. stiffness = stiffness < 0 ? 0 : stiffness;
  1156. stiffness = stiffness > 1 ? 1 : stiffness;
  1157. impostor.physicsBody.get_m_materials().at(0).set_m_kLST(stiffness);
  1158. impostor._pluginData.stiffness = stiffness;
  1159. }
  1160. else {
  1161. Logger.Warn("Stiffness cannot be applied to a rigid body");
  1162. }
  1163. }
  1164. /**
  1165. * Gets velocityIterations of the impostor
  1166. * @param impostor impostor to get velocity iterations from
  1167. * @returns velocityIterations value
  1168. */
  1169. public getBodyVelocityIterations(impostor: PhysicsImpostor): number {
  1170. if (!impostor.soft) {
  1171. Logger.Warn("Velocity iterations is not a property of a rigid body");
  1172. return 0;
  1173. }
  1174. return impostor._pluginData.velocityIterations || 0;
  1175. }
  1176. /**
  1177. * Sets velocityIterations of the impostor
  1178. * @param impostor impostor to set velocity iterations on
  1179. * @param velocityIterations velocityIterations value
  1180. */
  1181. public setBodyVelocityIterations(impostor: PhysicsImpostor, velocityIterations: number) {
  1182. if (impostor.soft) {
  1183. velocityIterations = velocityIterations < 0 ? 0 : velocityIterations;
  1184. impostor.physicsBody.get_m_cfg().set_viterations(velocityIterations);
  1185. impostor._pluginData.velocityIterations = velocityIterations;
  1186. }
  1187. else {
  1188. Logger.Warn("Velocity iterations cannot be applied to a rigid body");
  1189. }
  1190. }
  1191. /**
  1192. * Gets positionIterations of the impostor
  1193. * @param impostor impostor to get position iterations from
  1194. * @returns positionIterations value
  1195. */
  1196. public getBodyPositionIterations(impostor: PhysicsImpostor): number {
  1197. if (!impostor.soft) {
  1198. Logger.Warn("Position iterations is not a property of a rigid body");
  1199. return 0;
  1200. }
  1201. return impostor._pluginData.positionIterations || 0;
  1202. }
  1203. /**
  1204. * Sets positionIterations of the impostor
  1205. * @param impostor impostor to set position on
  1206. * @param positionIterations positionIterations value
  1207. */
  1208. public setBodyPositionIterations(impostor: PhysicsImpostor, positionIterations: number) {
  1209. if (impostor.soft) {
  1210. positionIterations = positionIterations < 0 ? 0 : positionIterations;
  1211. impostor.physicsBody.get_m_cfg().set_piterations(positionIterations);
  1212. impostor._pluginData.positionIterations = positionIterations;
  1213. }
  1214. else {
  1215. Logger.Warn("Position iterations cannot be applied to a rigid body");
  1216. }
  1217. }
  1218. /**
  1219. * Append an anchor to a cloth object
  1220. * @param impostor is the cloth impostor to add anchor to
  1221. * @param otherImpostor is the rigid impostor to anchor to
  1222. * @param width ratio across width from 0 to 1
  1223. * @param height ratio up height from 0 to 1
  1224. * @param influence the elasticity between cloth impostor and anchor from 0, very stretchy to 1, little strech
  1225. * @param noCollisionBetweenLinkedBodies when true collisions between soft impostor and anchor are ignored; default false
  1226. */
  1227. public appendAnchor(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, width: number, height: number, influence: number = 1, noCollisionBetweenLinkedBodies: boolean = false) {
  1228. var segs = impostor.segments;
  1229. var nbAcross = Math.round((segs - 1) * width);
  1230. var nbUp = Math.round((segs - 1) * height);
  1231. var nbDown = segs - 1 - nbUp;
  1232. var node = nbAcross + segs * nbDown;
  1233. impostor.physicsBody.appendAnchor(node, otherImpostor.physicsBody, noCollisionBetweenLinkedBodies, influence);
  1234. }
  1235. /**
  1236. * Append an hook to a rope object
  1237. * @param impostor is the rope impostor to add hook to
  1238. * @param otherImpostor is the rigid impostor to hook to
  1239. * @param length ratio along the rope from 0 to 1
  1240. * @param influence the elasticity between soft impostor and anchor from 0, very stretchy to 1, little strech
  1241. * @param noCollisionBetweenLinkedBodies when true collisions between soft impostor and anchor are ignored; default false
  1242. */
  1243. public appendHook(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, length: number, influence: number = 1, noCollisionBetweenLinkedBodies: boolean = false) {
  1244. var node = Math.round(impostor.segments * length);
  1245. impostor.physicsBody.appendAnchor(node, otherImpostor.physicsBody, noCollisionBetweenLinkedBodies, influence);
  1246. }
  1247. /**
  1248. * Sleeps the physics body and stops it from being active
  1249. * @param impostor impostor to sleep
  1250. */
  1251. public sleepBody(impostor: PhysicsImpostor) {
  1252. Logger.Warn("sleepBody is not currently supported by the Ammo physics plugin");
  1253. }
  1254. /**
  1255. * Activates the physics body
  1256. * @param impostor impostor to activate
  1257. */
  1258. public wakeUpBody(impostor: PhysicsImpostor) {
  1259. impostor.physicsBody.activate();
  1260. }
  1261. /**
  1262. * Updates the distance parameters of the joint
  1263. * @param joint joint to update
  1264. * @param maxDistance maximum distance of the joint
  1265. * @param minDistance minimum distance of the joint
  1266. */
  1267. public updateDistanceJoint(joint: PhysicsJoint, maxDistance: number, minDistance?: number) {
  1268. Logger.Warn("updateDistanceJoint is not currently supported by the Ammo physics plugin");
  1269. }
  1270. /**
  1271. * Sets a motor on the joint
  1272. * @param joint joint to set motor on
  1273. * @param speed speed of the motor
  1274. * @param maxForce maximum force of the motor
  1275. * @param motorIndex index of the motor
  1276. */
  1277. public setMotor(joint: IMotorEnabledJoint, speed?: number, maxForce?: number, motorIndex?: number) {
  1278. joint.physicsJoint.enableAngularMotor(true, speed, maxForce);
  1279. }
  1280. /**
  1281. * Sets the motors limit
  1282. * @param joint joint to set limit on
  1283. * @param upperLimit upper limit
  1284. * @param lowerLimit lower limit
  1285. */
  1286. public setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number) {
  1287. Logger.Warn("setLimit is not currently supported by the Ammo physics plugin");
  1288. }
  1289. /**
  1290. * Syncs the position and rotation of a mesh with the impostor
  1291. * @param mesh mesh to sync
  1292. * @param impostor impostor to update the mesh with
  1293. */
  1294. public syncMeshWithImpostor(mesh: AbstractMesh, impostor: PhysicsImpostor) {
  1295. var body = impostor.physicsBody;
  1296. body.getMotionState().getWorldTransform(this._tmpAmmoTransform);
  1297. mesh.position.x = this._tmpAmmoTransform.getOrigin().x();
  1298. mesh.position.y = this._tmpAmmoTransform.getOrigin().y();
  1299. mesh.position.z = this._tmpAmmoTransform.getOrigin().z();
  1300. if (mesh.rotationQuaternion) {
  1301. mesh.rotationQuaternion.x = this._tmpAmmoTransform.getRotation().x();
  1302. mesh.rotationQuaternion.y = this._tmpAmmoTransform.getRotation().y();
  1303. mesh.rotationQuaternion.z = this._tmpAmmoTransform.getRotation().z();
  1304. mesh.rotationQuaternion.w = this._tmpAmmoTransform.getRotation().w();
  1305. }
  1306. }
  1307. /**
  1308. * Gets the radius of the impostor
  1309. * @param impostor impostor to get radius from
  1310. * @returns the radius
  1311. */
  1312. public getRadius(impostor: PhysicsImpostor): number {
  1313. var exntend = impostor.getObjectExtendSize();
  1314. return exntend.x / 2;
  1315. }
  1316. /**
  1317. * Gets the box size of the impostor
  1318. * @param impostor impostor to get box size from
  1319. * @param result the resulting box size
  1320. */
  1321. public getBoxSizeToRef(impostor: PhysicsImpostor, result: Vector3): void {
  1322. var exntend = impostor.getObjectExtendSize();
  1323. result.x = exntend.x;
  1324. result.y = exntend.y;
  1325. result.z = exntend.z;
  1326. }
  1327. /**
  1328. * Disposes of the impostor
  1329. */
  1330. public dispose() {
  1331. // Dispose of world
  1332. this.bjsAMMO.destroy(this.world);
  1333. this.bjsAMMO.destroy(this._solver);
  1334. this.bjsAMMO.destroy(this._overlappingPairCache);
  1335. this.bjsAMMO.destroy(this._dispatcher);
  1336. this.bjsAMMO.destroy(this._collisionConfiguration);
  1337. // Dispose of tmp variables
  1338. this.bjsAMMO.destroy(this._tmpAmmoVectorA);
  1339. this.bjsAMMO.destroy(this._tmpAmmoVectorB);
  1340. this.bjsAMMO.destroy(this._tmpAmmoVectorC);
  1341. this.bjsAMMO.destroy(this._tmpAmmoTransform);
  1342. this.bjsAMMO.destroy(this._tmpAmmoQuaternion);
  1343. this.bjsAMMO.destroy(this._tmpAmmoConcreteContactResultCallback);
  1344. this.world = null;
  1345. }
  1346. /**
  1347. * Does a raycast in the physics world
  1348. * @param from when should the ray start?
  1349. * @param to when should the ray end?
  1350. * @returns PhysicsRaycastResult
  1351. */
  1352. public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult {
  1353. this._tmpAmmoVectorRCA = new this.bjsAMMO.btVector3(from.x, from.y, from.z);
  1354. this._tmpAmmoVectorRCB = new this.bjsAMMO.btVector3(to.x, to.y, to.z);
  1355. var rayCallback = new this.bjsAMMO.ClosestRayResultCallback(this._tmpAmmoVectorRCA, this._tmpAmmoVectorRCB);
  1356. this.world.rayTest(this._tmpAmmoVectorRCA, this._tmpAmmoVectorRCB, rayCallback);
  1357. this._raycastResult.reset(from, to);
  1358. if (rayCallback.hasHit()) {
  1359. // TODO: do we want/need the body? If so, set all the data
  1360. /*
  1361. var rigidBody = this.bjsAMMO.btRigidBody.prototype.upcast(
  1362. rayCallback.get_m_collisionObject()
  1363. );
  1364. var body = {};
  1365. */
  1366. this._raycastResult.setHitData(
  1367. {
  1368. x: rayCallback.get_m_hitNormalWorld().x(),
  1369. y: rayCallback.get_m_hitNormalWorld().y(),
  1370. z: rayCallback.get_m_hitNormalWorld().z(),
  1371. },
  1372. {
  1373. x: rayCallback.get_m_hitPointWorld().x(),
  1374. y: rayCallback.get_m_hitPointWorld().y(),
  1375. z: rayCallback.get_m_hitPointWorld().z(),
  1376. }
  1377. );
  1378. this._raycastResult.calculateHitDistance();
  1379. }
  1380. this.bjsAMMO.destroy(rayCallback);
  1381. this.bjsAMMO.destroy(this._tmpAmmoVectorRCA);
  1382. this.bjsAMMO.destroy(this._tmpAmmoVectorRCB);
  1383. return this._raycastResult;
  1384. }
  1385. }