ammoJSPlugin.ts 36 KB

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