ammoJSPlugin.ts 62 KB

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