babylon.ammoJSPlugin.ts 32 KB

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