ammoJSPlugin.ts 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. import { Quaternion, Vector3 } 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 { Nullable } from "../../types";
  8. import { AbstractMesh } from "../../Meshes/abstractMesh";
  9. declare var Ammo: any;
  10. /**
  11. * AmmoJS Physics plugin
  12. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  13. * @see https://github.com/kripken/ammo.js/
  14. */
  15. export class AmmoJSPlugin implements IPhysicsEnginePlugin {
  16. /**
  17. * Reference to the Ammo library
  18. */
  19. public bjsAMMO: any;
  20. /**
  21. * Created ammoJS world which physics bodies are added to
  22. */
  23. public world: any;
  24. /**
  25. * Name of the plugin
  26. */
  27. public name: string = "AmmoJSPlugin";
  28. private _timeStep: number = 1 / 60;
  29. private _fixedTimeStep: number = 1 / 60;
  30. private _maxSteps = 5;
  31. private _tmpQuaternion = new Quaternion();
  32. private _tmpAmmoTransform: any;
  33. private _tmpAmmoQuaternion: any;
  34. private _tmpAmmoConcreteContactResultCallback: any;
  35. private _collisionConfiguration: any;
  36. private _dispatcher: any;
  37. private _overlappingPairCache: any;
  38. private _solver: any;
  39. private _tmpAmmoVectorA: any;
  40. private _tmpAmmoVectorB: any;
  41. private _tmpAmmoVectorC: any;
  42. private _tmpContactCallbackResult = false;
  43. private static readonly DISABLE_COLLISION_FLAG = 4;
  44. private static readonly KINEMATIC_FLAG = 2;
  45. private static readonly DISABLE_DEACTIVATION_FLAG = 4;
  46. /**
  47. * Initializes the ammoJS plugin
  48. * @param _useDeltaForWorldStep if the time between frames should be used when calculating physics steps (Default: true)
  49. * @param ammoInjection can be used to inject your own ammo reference
  50. */
  51. public constructor(private _useDeltaForWorldStep: boolean = true, ammoInjection: any = Ammo) {
  52. if (typeof ammoInjection === "function") {
  53. ammoInjection();
  54. }
  55. this.bjsAMMO = ammoInjection;
  56. if (!this.isSupported()) {
  57. Logger.Error("AmmoJS is not available. Please make sure you included the js file.");
  58. return;
  59. }
  60. // Initialize the physics world
  61. this._collisionConfiguration = new this.bjsAMMO.btDefaultCollisionConfiguration();
  62. this._dispatcher = new this.bjsAMMO.btCollisionDispatcher(this._collisionConfiguration);
  63. this._overlappingPairCache = new this.bjsAMMO.btDbvtBroadphase();
  64. this._solver = new this.bjsAMMO.btSequentialImpulseConstraintSolver();
  65. this.world = new this.bjsAMMO.btDiscreteDynamicsWorld(this._dispatcher, this._overlappingPairCache, this._solver, this._collisionConfiguration);
  66. this._tmpAmmoConcreteContactResultCallback = new this.bjsAMMO.ConcreteContactResultCallback();
  67. this._tmpAmmoConcreteContactResultCallback.addSingleResult = () => { this._tmpContactCallbackResult = true; };
  68. // Create temp ammo variables
  69. this._tmpAmmoTransform = new this.bjsAMMO.btTransform();
  70. this._tmpAmmoTransform.setIdentity();
  71. this._tmpAmmoQuaternion = new this.bjsAMMO.btQuaternion(0, 0, 0, 1);
  72. this._tmpAmmoVectorA = new this.bjsAMMO.btVector3(0, 0, 0);
  73. this._tmpAmmoVectorB = new this.bjsAMMO.btVector3(0, 0, 0);
  74. this._tmpAmmoVectorC = new this.bjsAMMO.btVector3(0, 0, 0);
  75. }
  76. /**
  77. * Sets the gravity of the physics world (m/(s^2))
  78. * @param gravity Gravity to set
  79. */
  80. public setGravity(gravity: Vector3): void {
  81. this._tmpAmmoVectorA.setValue(gravity.x, gravity.y, gravity.z);
  82. this.world.setGravity(this._tmpAmmoVectorA);
  83. }
  84. /**
  85. * Amount of time to step forward on each frame (only used if useDeltaForWorldStep is false in the constructor)
  86. * @param timeStep timestep to use in seconds
  87. */
  88. public setTimeStep(timeStep: number) {
  89. this._timeStep = timeStep;
  90. }
  91. /**
  92. * 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)
  93. * @param fixedTimeStep fixedTimeStep to use in seconds
  94. */
  95. public setFixedTimeStep(fixedTimeStep: number) {
  96. this._fixedTimeStep = fixedTimeStep;
  97. }
  98. /**
  99. * Sets the maximum number of steps by the physics engine per frame (Default: 5)
  100. * @param maxSteps the maximum number of steps by the physics engine per frame
  101. */
  102. public setMaxSteps(maxSteps: number) {
  103. this._maxSteps = maxSteps;
  104. }
  105. /**
  106. * Gets the current timestep (only used if useDeltaForWorldStep is false in the constructor)
  107. * @returns the current timestep in seconds
  108. */
  109. public getTimeStep(): number {
  110. return this._timeStep;
  111. }
  112. // Ammo's contactTest and contactPairTest take a callback that runs synchronously, wrap them so that they are easier to consume
  113. private _isImpostorInContact(impostor: PhysicsImpostor) {
  114. this._tmpContactCallbackResult = false;
  115. this.world.contactTest(impostor.physicsBody, this._tmpAmmoConcreteContactResultCallback);
  116. return this._tmpContactCallbackResult;
  117. }
  118. // Ammo's collision events have some weird quirks
  119. // contactPairTest fires too many events as it fires events even when objects are close together but contactTest does not
  120. // so only fire event if both contactTest and contactPairTest have a hit
  121. private _isImpostorPairInContact(impostorA: PhysicsImpostor, impostorB: PhysicsImpostor) {
  122. this._tmpContactCallbackResult = false;
  123. this.world.contactPairTest(impostorA.physicsBody, impostorB.physicsBody, this._tmpAmmoConcreteContactResultCallback);
  124. return this._tmpContactCallbackResult;
  125. }
  126. // Ammo's behavior when maxSteps > 0 does not behave as described in docs
  127. // @see http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Stepping_The_World
  128. //
  129. // When maxSteps is 0 do the entire simulation in one step
  130. // 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)
  131. // Note: To get deterministic physics, timeStep would always need to be divisible by fixedTimeStep
  132. private _stepSimulation(timeStep: number = 1 / 60, maxSteps: number = 10, fixedTimeStep: number = 1 / 60) {
  133. if (maxSteps == 0) {
  134. this.world.stepSimulation(timeStep, 0);
  135. }else {
  136. while (maxSteps > 0 && timeStep > 0) {
  137. if (timeStep - fixedTimeStep < fixedTimeStep) {
  138. this.world.stepSimulation(timeStep, 0);
  139. timeStep = 0;
  140. }else {
  141. timeStep -= fixedTimeStep;
  142. this.world.stepSimulation(fixedTimeStep, 0);
  143. }
  144. maxSteps--;
  145. }
  146. }
  147. }
  148. /**
  149. * Moves the physics simulation forward delta seconds and updates the given physics imposters
  150. * Prior to the step the imposters physics location is set to the position of the babylon meshes
  151. * After the step the babylon meshes are set to the position of the physics imposters
  152. * @param delta amount of time to step forward
  153. * @param impostors array of imposters to update before/after the step
  154. */
  155. public executeStep(delta: number, impostors: Array<PhysicsImpostor>): void {
  156. for (var impostor of impostors) {
  157. // Update physics world objects to match babylon world
  158. impostor.beforeStep();
  159. }
  160. this._stepSimulation(this._useDeltaForWorldStep ? delta : this._timeStep, this._maxSteps, this._fixedTimeStep);
  161. for (var mainImpostor of impostors) {
  162. // After physics update make babylon world objects match physics world objects
  163. mainImpostor.afterStep();
  164. // Handle collision event
  165. if (mainImpostor._onPhysicsCollideCallbacks.length > 0) {
  166. if (this._isImpostorInContact(mainImpostor)) {
  167. for (var collideCallback of mainImpostor._onPhysicsCollideCallbacks) {
  168. for (var otherImpostor of collideCallback.otherImpostors) {
  169. if (mainImpostor.physicsBody.isActive() || otherImpostor.physicsBody.isActive()) {
  170. if (this._isImpostorPairInContact(mainImpostor, otherImpostor)) {
  171. mainImpostor.onCollide({ body: otherImpostor.physicsBody });
  172. otherImpostor.onCollide({ body: mainImpostor.physicsBody });
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * Applies an implulse on the imposter
  183. * @param impostor imposter to apply impulse
  184. * @param force amount of force to be applied to the imposter
  185. * @param contactPoint the location to apply the impulse on the imposter
  186. */
  187. public applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3) {
  188. var worldPoint = this._tmpAmmoVectorA;
  189. var impulse = this._tmpAmmoVectorB;
  190. worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
  191. impulse.setValue(force.x, force.y, force.z);
  192. impostor.physicsBody.applyImpulse(impulse, worldPoint);
  193. }
  194. /**
  195. * Applies a force on the imposter
  196. * @param impostor imposter to apply force
  197. * @param force amount of force to be applied to the imposter
  198. * @param contactPoint the location to apply the force on the imposter
  199. */
  200. public applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3) {
  201. var worldPoint = this._tmpAmmoVectorA;
  202. var impulse = this._tmpAmmoVectorB;
  203. worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
  204. impulse.setValue(force.x, force.y, force.z);
  205. impostor.physicsBody.applyForce(impulse, worldPoint);
  206. }
  207. /**
  208. * Creates a physics body using the plugin
  209. * @param impostor the imposter to create the physics body on
  210. */
  211. public generatePhysicsBody(impostor: PhysicsImpostor) {
  212. impostor._pluginData = {toDispose: []};
  213. //parent-child relationship
  214. if (impostor.parent) {
  215. if (impostor.physicsBody) {
  216. this.removePhysicsBody(impostor);
  217. impostor.forceUpdate();
  218. }
  219. return;
  220. }
  221. if (impostor.isBodyInitRequired()) {
  222. var colShape = this._createShape(impostor);
  223. var mass = impostor.getParam("mass");
  224. impostor._pluginData.mass = mass;
  225. var localInertia = new Ammo.btVector3(0, 0, 0);
  226. var startTransform = new Ammo.btTransform();
  227. startTransform.setIdentity();
  228. if (mass !== 0) {
  229. colShape.calculateLocalInertia(mass, localInertia);
  230. }
  231. this._tmpAmmoVectorA.setValue(impostor.object.position.x, impostor.object.position.y, impostor.object.position.z);
  232. this._tmpAmmoQuaternion.setValue(impostor.object.rotationQuaternion!.x, impostor.object.rotationQuaternion!.y, impostor.object.rotationQuaternion!.z, impostor.object.rotationQuaternion!.w);
  233. startTransform.setOrigin(this._tmpAmmoVectorA);
  234. startTransform.setRotation(this._tmpAmmoQuaternion);
  235. var myMotionState = new Ammo.btDefaultMotionState(startTransform);
  236. var rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia);
  237. var body = new Ammo.btRigidBody(rbInfo);
  238. // Make objects kinematic if it's mass is 0
  239. if (mass === 0) {
  240. body.setCollisionFlags(body.getCollisionFlags() | AmmoJSPlugin.KINEMATIC_FLAG);
  241. body.setActivationState(AmmoJSPlugin.DISABLE_DEACTIVATION_FLAG);
  242. }
  243. // Disable collision if NoImpostor, but keep collision if shape is btCompoundShape
  244. if (impostor.type == PhysicsImpostor.NoImpostor && !colShape.getChildShape) {
  245. body.setCollisionFlags(body.getCollisionFlags() | AmmoJSPlugin.DISABLE_COLLISION_FLAG);
  246. }
  247. body.setRestitution(impostor.getParam("restitution"));
  248. this.world.addRigidBody(body);
  249. impostor.physicsBody = body;
  250. impostor._pluginData.toDispose.concat([body, rbInfo, myMotionState, startTransform, localInertia, colShape]);
  251. }
  252. }
  253. /**
  254. * Removes the physics body from the imposter and disposes of the body's memory
  255. * @param impostor imposter to remove the physics body from
  256. */
  257. public removePhysicsBody(impostor: PhysicsImpostor) {
  258. if (this.world) {
  259. this.world.removeRigidBody(impostor.physicsBody);
  260. impostor._pluginData.toDispose.forEach((d: any) => {
  261. this.bjsAMMO.destroy(d);
  262. });
  263. }
  264. }
  265. /**
  266. * Generates a joint
  267. * @param impostorJoint the imposter joint to create the joint with
  268. */
  269. public generateJoint(impostorJoint: PhysicsImpostorJoint) {
  270. var mainBody = impostorJoint.mainImpostor.physicsBody;
  271. var connectedBody = impostorJoint.connectedImpostor.physicsBody;
  272. if (!mainBody || !connectedBody) {
  273. return;
  274. }
  275. var jointData = impostorJoint.joint.jointData;
  276. if (!jointData.mainPivot) {
  277. jointData.mainPivot = new Vector3(0, 0, 0);
  278. }
  279. if (!jointData.connectedPivot) {
  280. jointData.connectedPivot = new Vector3(0, 0, 0);
  281. }
  282. var joint: any;
  283. switch (impostorJoint.joint.type) {
  284. case PhysicsJoint.DistanceJoint:
  285. var distance = (<DistanceJointData>jointData).maxDistance;
  286. if (distance) {
  287. jointData.mainPivot = new Vector3(0, -distance / 2, 0);
  288. jointData.connectedPivot = new Vector3(0, distance / 2, 0);
  289. }
  290. 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));
  291. break;
  292. case PhysicsJoint.HingeJoint:
  293. if (!jointData.mainAxis) {
  294. jointData.mainAxis = new Vector3(0, 0, 0);
  295. }
  296. if (!jointData.connectedAxis) {
  297. jointData.connectedAxis = new Vector3(0, 0, 0);
  298. }
  299. var mainAxis = new Ammo.btVector3(jointData.mainAxis.x, jointData.mainAxis.y, jointData.mainAxis.z);
  300. var connectedAxis = new Ammo.btVector3(jointData.connectedAxis.x, jointData.connectedAxis.y, jointData.connectedAxis.z);
  301. 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);
  302. break;
  303. case PhysicsJoint.BallAndSocketJoint:
  304. 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));
  305. break;
  306. default:
  307. Logger.Warn("JointType not currently supported by the Ammo plugin, falling back to PhysicsJoint.BallAndSocketJoint");
  308. 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));
  309. break;
  310. }
  311. this.world.addConstraint(joint, true);
  312. impostorJoint.joint.physicsJoint = joint;
  313. }
  314. /**
  315. * Removes a joint
  316. * @param impostorJoint the imposter joint to remove the joint from
  317. */
  318. public removeJoint(impostorJoint: PhysicsImpostorJoint) {
  319. if (this.world) {
  320. this.world.removeConstraint(impostorJoint.joint.physicsJoint);
  321. }
  322. }
  323. // adds all verticies (including child verticies) to the triangle mesh
  324. private _addMeshVerts(btTriangleMesh: any, topLevelObject: IPhysicsEnabledObject, object: IPhysicsEnabledObject) {
  325. var triangleCount = 0;
  326. if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
  327. var indices = object.getIndices();
  328. if (!indices) {
  329. indices = [];
  330. }
  331. var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
  332. if (!vertexPositions) {
  333. vertexPositions = [];
  334. }
  335. object.computeWorldMatrix(false);
  336. var faceCount = indices.length / 3;
  337. for (var i = 0; i < faceCount; i++) {
  338. var triPoints = [];
  339. for (var point = 0; point < 3; point++) {
  340. 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]);
  341. v = Vector3.TransformCoordinates(v, object.getWorldMatrix());
  342. v.subtractInPlace(topLevelObject.position);
  343. var vec: any;
  344. if (point == 0) {
  345. vec = this._tmpAmmoVectorA;
  346. }else if (point == 1) {
  347. vec = this._tmpAmmoVectorB;
  348. }else {
  349. vec = this._tmpAmmoVectorC;
  350. }
  351. vec.setValue(v.x, v.y, v.z);
  352. triPoints.push(vec);
  353. }
  354. btTriangleMesh.addTriangle(triPoints[0], triPoints[1], triPoints[2]);
  355. triangleCount++;
  356. }
  357. object.getChildMeshes().forEach((m) => {
  358. triangleCount += this._addMeshVerts(btTriangleMesh, topLevelObject, m);
  359. });
  360. }
  361. return triangleCount;
  362. }
  363. private _createShape(impostor: PhysicsImpostor, ignoreChildren= false) {
  364. var object = impostor.object;
  365. var returnValue: any;
  366. var extendSize = impostor.getObjectExtendSize();
  367. if (!ignoreChildren) {
  368. var meshChildren = impostor.object.getChildMeshes ? impostor.object.getChildMeshes(true) : [];
  369. returnValue = new Ammo.btCompoundShape();
  370. // Add shape of all children to the compound shape
  371. var childrenAdded = 0;
  372. meshChildren.forEach((childMesh) => {
  373. var childImpostor = childMesh.getPhysicsImpostor();
  374. if (childImpostor) {
  375. var shape = this._createShape(childImpostor);
  376. // Position needs to be scaled based on parent's scaling
  377. var parentMat = childMesh.parent!.getWorldMatrix().clone();
  378. var s = new Vector3();
  379. parentMat.decompose(s);
  380. this._tmpAmmoTransform.getOrigin().setValue(childMesh.position.x * s.x, childMesh.position.y * s.y, childMesh.position.z * s.z);
  381. this._tmpAmmoQuaternion.setValue(childMesh.rotationQuaternion!.x, childMesh.rotationQuaternion!.y, childMesh.rotationQuaternion!.z, childMesh.rotationQuaternion!.w);
  382. this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
  383. returnValue.addChildShape(this._tmpAmmoTransform, shape);
  384. childImpostor.dispose();
  385. childrenAdded++;
  386. }
  387. });
  388. if (childrenAdded > 0) {
  389. // Add parents shape as a child if present
  390. if (impostor.type != PhysicsImpostor.NoImpostor) {
  391. var shape = this._createShape(impostor, true);
  392. if (shape) {
  393. this._tmpAmmoTransform.getOrigin().setValue(0, 0, 0);
  394. this._tmpAmmoQuaternion.setValue(0, 0, 0, 1);
  395. this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
  396. returnValue.addChildShape(this._tmpAmmoTransform, shape);
  397. }
  398. }
  399. return returnValue;
  400. }else {
  401. // If no children with impostors create the actual shape below instead
  402. Ammo.destroy(returnValue);
  403. returnValue = null;
  404. }
  405. }
  406. switch (impostor.type) {
  407. case PhysicsImpostor.SphereImpostor:
  408. returnValue = new Ammo.btSphereShape(extendSize.x / 2);
  409. break;
  410. case PhysicsImpostor.CylinderImpostor:
  411. this._tmpAmmoVectorA.setValue(extendSize.x / 2, extendSize.y / 2, extendSize.z / 2);
  412. returnValue = new Ammo.btCylinderShape(this._tmpAmmoVectorA);
  413. break;
  414. case PhysicsImpostor.PlaneImpostor:
  415. case PhysicsImpostor.BoxImpostor:
  416. this._tmpAmmoVectorA.setValue(extendSize.x / 2, extendSize.y / 2, extendSize.z / 2);
  417. returnValue = new Ammo.btBoxShape(this._tmpAmmoVectorA);
  418. break;
  419. case PhysicsImpostor.MeshImpostor:
  420. var tetraMesh = new Ammo.btTriangleMesh();
  421. impostor._pluginData.toDispose.concat([tetraMesh]);
  422. var triangeCount = this._addMeshVerts(tetraMesh, object, object);
  423. if (triangeCount == 0) {
  424. returnValue = new Ammo.btCompoundShape();
  425. }else {
  426. returnValue = new Ammo.btBvhTriangleMeshShape(tetraMesh);
  427. }
  428. break;
  429. case PhysicsImpostor.NoImpostor:
  430. // Fill with sphere but collision is disabled on the rigid body in generatePhysicsBody, using an empty shape caused unexpected movement with joints
  431. returnValue = new Ammo.btSphereShape(extendSize.x / 2);
  432. break;
  433. default:
  434. Logger.Warn("The impostor type is not currently supported by the ammo plugin.");
  435. break;
  436. }
  437. return returnValue;
  438. }
  439. /**
  440. * Sets the physics body position/rotation from the babylon mesh's position/rotation
  441. * @param impostor imposter containing the physics body and babylon object
  442. */
  443. public setTransformationFromPhysicsBody(impostor: PhysicsImpostor) {
  444. impostor.physicsBody.getMotionState().getWorldTransform(this._tmpAmmoTransform);
  445. impostor.object.position.set(this._tmpAmmoTransform.getOrigin().x(), this._tmpAmmoTransform.getOrigin().y(), this._tmpAmmoTransform.getOrigin().z());
  446. if (!impostor.object.rotationQuaternion) {
  447. if (impostor.object.rotation) {
  448. this._tmpQuaternion.set(this._tmpAmmoTransform.getRotation().x(), this._tmpAmmoTransform.getRotation().y(), this._tmpAmmoTransform.getRotation().z(), this._tmpAmmoTransform.getRotation().w());
  449. this._tmpQuaternion.toEulerAnglesToRef(impostor.object.rotation);
  450. }
  451. }else {
  452. impostor.object.rotationQuaternion.set(this._tmpAmmoTransform.getRotation().x(), this._tmpAmmoTransform.getRotation().y(), this._tmpAmmoTransform.getRotation().z(), this._tmpAmmoTransform.getRotation().w());
  453. }
  454. }
  455. /**
  456. * Sets the babylon object's position/rotation from the physics body's position/rotation
  457. * @param impostor imposter containing the physics body and babylon object
  458. * @param newPosition new position
  459. * @param newRotation new rotation
  460. */
  461. public setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion) {
  462. var trans = impostor.physicsBody.getWorldTransform();
  463. // If rotation/position has changed update and activate riged body
  464. if (
  465. trans.getOrigin().x() != newPosition.x ||
  466. trans.getOrigin().y() != newPosition.y ||
  467. trans.getOrigin().z() != newPosition.z ||
  468. trans.getRotation().x() != newRotation.x ||
  469. trans.getRotation().y() != newRotation.y ||
  470. trans.getRotation().z() != newRotation.z ||
  471. trans.getRotation().w() != newRotation.w
  472. ) {
  473. this._tmpAmmoVectorA.setValue(newPosition.x, newPosition.y, newPosition.z);
  474. trans.setOrigin(this._tmpAmmoVectorA);
  475. this._tmpAmmoQuaternion.setValue(newRotation.x, newRotation.y, newRotation.z, newRotation.w);
  476. trans.setRotation(this._tmpAmmoQuaternion);
  477. impostor.physicsBody.setWorldTransform(trans);
  478. if (impostor.mass == 0) {
  479. // Kinematic objects must be updated using motion state
  480. var motionState = impostor.physicsBody.getMotionState();
  481. if (motionState) {
  482. motionState.setWorldTransform(trans);
  483. }
  484. }else {
  485. impostor.physicsBody.activate();
  486. }
  487. }
  488. }
  489. /**
  490. * If this plugin is supported
  491. * @returns true if its supported
  492. */
  493. public isSupported(): boolean {
  494. return this.bjsAMMO !== undefined;
  495. }
  496. /**
  497. * Sets the linear velocity of the physics body
  498. * @param impostor imposter to set the velocity on
  499. * @param velocity velocity to set
  500. */
  501. public setLinearVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
  502. this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
  503. impostor.physicsBody.setLinearVelocity(this._tmpAmmoVectorA);
  504. }
  505. /**
  506. * Sets the angular velocity of the physics body
  507. * @param impostor imposter to set the velocity on
  508. * @param velocity velocity to set
  509. */
  510. public setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
  511. this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
  512. impostor.physicsBody.setAngularVelocity(this._tmpAmmoVectorA);
  513. }
  514. /**
  515. * gets the linear velocity
  516. * @param impostor imposter to get linear velocity from
  517. * @returns linear velocity
  518. */
  519. public getLinearVelocity(impostor: PhysicsImpostor): Nullable<Vector3> {
  520. var v = impostor.physicsBody.getLinearVelocity();
  521. if (!v) {
  522. return null;
  523. }
  524. return new Vector3(v.x(), v.y(), v.z());
  525. }
  526. /**
  527. * gets the angular velocity
  528. * @param impostor imposter to get angular velocity from
  529. * @returns angular velocity
  530. */
  531. public getAngularVelocity(impostor: PhysicsImpostor): Nullable<Vector3> {
  532. var v = impostor.physicsBody.getAngularVelocity();
  533. if (!v) {
  534. return null;
  535. }
  536. return new Vector3(v.x(), v.y(), v.z());
  537. }
  538. /**
  539. * Sets the mass of physics body
  540. * @param impostor imposter to set the mass on
  541. * @param mass mass to set
  542. */
  543. public setBodyMass(impostor: PhysicsImpostor, mass: number) {
  544. impostor.physicsBody.setMassProps(mass);
  545. impostor._pluginData.mass = mass;
  546. }
  547. /**
  548. * Gets the mass of the physics body
  549. * @param impostor imposter to get the mass from
  550. * @returns mass
  551. */
  552. public getBodyMass(impostor: PhysicsImpostor): number {
  553. return impostor._pluginData.mass;
  554. }
  555. /**
  556. * Gets friction of the impostor
  557. * @param impostor impostor to get friction from
  558. * @returns friction value
  559. */
  560. public getBodyFriction(impostor: PhysicsImpostor): number {
  561. return impostor.physicsBody.getFriction();
  562. }
  563. /**
  564. * Sets friction of the impostor
  565. * @param impostor impostor to set friction on
  566. * @param friction friction value
  567. */
  568. public setBodyFriction(impostor: PhysicsImpostor, friction: number) {
  569. impostor.physicsBody.setFriction(friction);
  570. }
  571. /**
  572. * Gets restitution of the impostor
  573. * @param impostor impostor to get restitution from
  574. * @returns restitution value
  575. */
  576. public getBodyRestitution(impostor: PhysicsImpostor): number {
  577. return impostor.physicsBody.getRestitution();
  578. }
  579. /**
  580. * Sets resitution of the impostor
  581. * @param impostor impostor to set resitution on
  582. * @param restitution resitution value
  583. */
  584. public setBodyRestitution(impostor: PhysicsImpostor, restitution: number) {
  585. impostor.physicsBody.setRestitution(restitution);
  586. }
  587. /**
  588. * Sleeps the physics body and stops it from being active
  589. * @param impostor impostor to sleep
  590. */
  591. public sleepBody(impostor: PhysicsImpostor) {
  592. Logger.Warn("sleepBody is not currently supported by the Ammo physics plugin");
  593. }
  594. /**
  595. * Activates the physics body
  596. * @param impostor impostor to activate
  597. */
  598. public wakeUpBody(impostor: PhysicsImpostor) {
  599. impostor.physicsBody.activate();
  600. }
  601. /**
  602. * Updates the distance parameters of the joint
  603. * @param joint joint to update
  604. * @param maxDistance maximum distance of the joint
  605. * @param minDistance minimum distance of the joint
  606. */
  607. public updateDistanceJoint(joint: PhysicsJoint, maxDistance: number, minDistance?: number) {
  608. Logger.Warn("updateDistanceJoint is not currently supported by the Ammo physics plugin");
  609. }
  610. /**
  611. * Sets a motor on the joint
  612. * @param joint joint to set motor on
  613. * @param speed speed of the motor
  614. * @param maxForce maximum force of the motor
  615. * @param motorIndex index of the motor
  616. */
  617. public setMotor(joint: IMotorEnabledJoint, speed?: number, maxForce?: number, motorIndex?: number) {
  618. joint.physicsJoint.enableAngularMotor(true, speed, maxForce);
  619. }
  620. /**
  621. * Sets the motors limit
  622. * @param joint joint to set limit on
  623. * @param upperLimit upper limit
  624. * @param lowerLimit lower limit
  625. */
  626. public setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number) {
  627. Logger.Warn("setLimit is not currently supported by the Ammo physics plugin");
  628. }
  629. /**
  630. * Syncs the position and rotation of a mesh with the impostor
  631. * @param mesh mesh to sync
  632. * @param impostor impostor to update the mesh with
  633. */
  634. public syncMeshWithImpostor(mesh: AbstractMesh, impostor: PhysicsImpostor) {
  635. var body = impostor.physicsBody;
  636. body.getMotionState().getWorldTransform(this._tmpAmmoTransform);
  637. mesh.position.x = this._tmpAmmoTransform.getOrigin().x();
  638. mesh.position.y = this._tmpAmmoTransform.getOrigin().y();
  639. mesh.position.z = this._tmpAmmoTransform.getOrigin().z();
  640. if (mesh.rotationQuaternion) {
  641. mesh.rotationQuaternion.x = this._tmpAmmoTransform.getRotation().x();
  642. mesh.rotationQuaternion.y = this._tmpAmmoTransform.getRotation().y();
  643. mesh.rotationQuaternion.z = this._tmpAmmoTransform.getRotation().z();
  644. mesh.rotationQuaternion.w = this._tmpAmmoTransform.getRotation().w();
  645. }
  646. }
  647. /**
  648. * Gets the radius of the impostor
  649. * @param impostor impostor to get radius from
  650. * @returns the radius
  651. */
  652. public getRadius(impostor: PhysicsImpostor): number {
  653. var exntend = impostor.getObjectExtendSize();
  654. return exntend.x / 2;
  655. }
  656. /**
  657. * Gets the box size of the impostor
  658. * @param impostor impostor to get box size from
  659. * @param result the resulting box size
  660. */
  661. public getBoxSizeToRef(impostor: PhysicsImpostor, result: Vector3): void {
  662. var exntend = impostor.getObjectExtendSize();
  663. result.x = exntend.x;
  664. result.y = exntend.y;
  665. result.z = exntend.z;
  666. }
  667. /**
  668. * Disposes of the impostor
  669. */
  670. public dispose() {
  671. // Dispose of world
  672. Ammo.destroy(this.world);
  673. Ammo.destroy(this._solver);
  674. Ammo.destroy(this._overlappingPairCache);
  675. Ammo.destroy(this._dispatcher);
  676. Ammo.destroy(this._collisionConfiguration);
  677. // Dispose of tmp variables
  678. Ammo.destroy(this._tmpAmmoVectorA);
  679. Ammo.destroy(this._tmpAmmoVectorB);
  680. Ammo.destroy(this._tmpAmmoVectorC);
  681. Ammo.destroy(this._tmpAmmoTransform);
  682. Ammo.destroy(this._tmpAmmoQuaternion);
  683. Ammo.destroy(this._tmpAmmoConcreteContactResultCallback);
  684. this.world = null;
  685. }
  686. }