SEA3DAmmo.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /** _ _ _ _____ __ _______ ______
  2. * | |___| |_| |__ /__ | | | | _ | * *
  3. * | / _ \ _| | __\ | | \ | _ | U _
  4. * |_\___/\__|_||_| _ |____/____ |__ \_|_ |_|_____|
  5. *
  6. * @author LoTh / http://3dflashlo.wordpress.com/
  7. * @author SUNAG / http://www.sunag.com.br/
  8. * @author Ammo.lab / https://github.com/lo-th/Ammo.lab/
  9. */
  10. 'use strict';
  11. SEA3D.AMMO = {
  12. world: null,
  13. rigidBodies: [],
  14. rigidBodiesTarget: [],
  15. rigidBodiesEnabled: [],
  16. constraints: [],
  17. vehicles: [],
  18. vehiclesWheels: [],
  19. ACTIVE: 1,
  20. ISLAND_SLEEPING: 2,
  21. WANTS_DEACTIVATION: 3,
  22. DISABLE_DEACTIVATION: 4,
  23. DISABLE_SIMULATION: 5,
  24. VERSION: 0.8,
  25. init: function ( gravity, worldScale, broadphase ) {
  26. gravity = gravity !== undefined ? gravity : - 90.8;
  27. this.worldScale = worldScale == undefined ? 1 : worldScale;
  28. this.broadphase = broadphase == undefined ? 'bvt' : broadphase;
  29. this.solver = new Ammo.btSequentialImpulseConstraintSolver();
  30. this.collisionConfig = new Ammo.btDefaultCollisionConfiguration();
  31. this.dispatcher = new Ammo.btCollisionDispatcher( this.collisionConfig );
  32. switch ( this.broadphase ) {
  33. case 'bvt':
  34. this.broadphase = new Ammo.btDbvtBroadphase();
  35. break;
  36. case 'sap':
  37. this.broadphase = new Ammo.btAxisSweep3(
  38. new Ammo.btVector3( - this.worldScale, - this.worldScale, - this.worldScale ),
  39. new Ammo.btVector3( this.worldScale, this.worldScale, this.worldScale ),
  40. 4096
  41. );
  42. break;
  43. case 'simple':
  44. this.broadphase = new Ammo.btSimpleBroadphase();
  45. break;
  46. }
  47. this.world = new Ammo.btDiscreteDynamicsWorld( this.dispatcher, this.broadphase, this.solver, this.collisionConfig );
  48. this.setGravity( gravity );
  49. console.log( "THREE.AMMO " + this.VERSION );
  50. },
  51. setGravity: function ( gravity ) {
  52. this.gravity = gravity;
  53. this.world.setGravity( new Ammo.btVector3( 0, gravity, 0 ) );
  54. return this;
  55. },
  56. getGravity: function () {
  57. return this.gravity;
  58. },
  59. setEnabledRigidBody: function ( rb, enabled ) {
  60. var index = this.rigidBodies.indexOf( rb );
  61. if ( this.rigidBodiesEnabled[ index ] == enabled ) return;
  62. if ( enabled ) this.world.addRigidBody( rb );
  63. else this.world.removeRigidBody( rb );
  64. this.rigidBodiesEnabled[ index ] = true;
  65. return this;
  66. },
  67. getEnabledRigidBody: function ( rb ) {
  68. return this.rigidBodiesEnabled[ this.rigidBodies.indexOf( rb ) ];
  69. },
  70. addRigidBody: function ( rb, target, enabled ) {
  71. enabled = enabled !== undefined ? enabled : true;
  72. this.rigidBodies.push( rb );
  73. this.rigidBodiesTarget.push( target );
  74. this.rigidBodiesEnabled.push( false );
  75. this.setEnabledRigidBody( rb, enabled );
  76. return this;
  77. },
  78. removeRigidBody: function ( rb, destroy ) {
  79. var index = this.rigidBodies.indexOf( rb );
  80. this.setEnabledRigidBody( rb, false );
  81. this.rigidBodies.splice( index, 1 );
  82. this.rigidBodiesTarget.splice( index, 1 );
  83. this.rigidBodiesEnabled.splice( index, 1 );
  84. if ( destroy ) Ammo.destroy( rb );
  85. return this;
  86. },
  87. containsRigidBody: function ( rb ) {
  88. return this.rigidBodies.indexOf( rb ) > - 1;
  89. },
  90. addConstraint: function ( ctrt, disableCollisionsBetweenBodies ) {
  91. disableCollisionsBetweenBodies = disableCollisionsBetweenBodies == undefined ? true : disableCollisionsBetweenBodies;
  92. this.constraints.push( ctrt );
  93. this.world.addConstraint( ctrt, disableCollisionsBetweenBodies );
  94. return this;
  95. },
  96. removeConstraint: function ( ctrt, destroy ) {
  97. this.constraints.splice( this.constraints.indexOf( ctrt ), 1 );
  98. this.world.removeConstraint( ctrt );
  99. if ( destroy ) Ammo.destroy( ctrt );
  100. return this;
  101. },
  102. containsConstraint: function ( ctrt ) {
  103. return this.constraints.indexOf( rb ) > - 1;
  104. },
  105. addVehicle: function ( vehicle, wheels ) {
  106. this.vehicles.push( vehicle );
  107. this.vehiclesWheels.push( wheels != undefined ? wheels : [] );
  108. this.world.addAction( vehicle );
  109. return this;
  110. },
  111. removeVehicle: function ( vehicle, destroy ) {
  112. var index = this.vehicles.indexOf( vehicle );
  113. this.vehicles.splice( index, 1 );
  114. this.vehiclesWheels.splice( index, 1 );
  115. this.world.removeAction( vehicle );
  116. if ( destroy ) Ammo.destroy( vehicle );
  117. return this;
  118. },
  119. containsVehicle: function ( vehicle ) {
  120. return this.vehicles.indexOf( vehicle ) > - 1;
  121. },
  122. createTriangleMesh: function ( geometry, index, removeDuplicateVertices ) {
  123. index = index == undefined ? - 1 : index;
  124. removeDuplicateVertices = removeDuplicateVertices == undefined ? false : removeDuplicateVertices;
  125. var mTriMesh = new Ammo.btTriangleMesh();
  126. var v0 = new Ammo.btVector3( 0, 0, 0 );
  127. var v1 = new Ammo.btVector3( 0, 0, 0 );
  128. var v2 = new Ammo.btVector3( 0, 0, 0 );
  129. var vertex = geometry.getAttribute( 'position' ).array;
  130. var indexes = geometry.getIndex().array;
  131. var group = index >= 0 ? geometry.groups[ index ] : undefined,
  132. start = group ? group.start : 0,
  133. count = group ? group.count : indexes.length;
  134. var scale = 1 / this.worldScale;
  135. for ( var idx = start; idx < count; idx += 3 ) {
  136. var vx1 = indexes[ idx ] * 3,
  137. vx2 = indexes[ idx + 1 ] * 3,
  138. vx3 = indexes[ idx + 2 ] * 3;
  139. v0.setValue( vertex[ vx1 ] * scale, vertex[ vx1 + 1 ] * scale, vertex[ vx1 + 2 ] * scale );
  140. v1.setValue( vertex[ vx2 ] * scale, vertex[ vx2 + 1 ] * scale, vertex[ vx2 + 2 ] * scale );
  141. v2.setValue( vertex[ vx3 ] * scale, vertex[ vx3 + 1 ] * scale, vertex[ vx3 + 2 ] * scale );
  142. mTriMesh.addTriangle( v0, v1, v2, removeDuplicateVertices );
  143. }
  144. return mTriMesh;
  145. },
  146. createConvexHull: function ( geometry, index ) {
  147. index = index == undefined ? - 1 : index;
  148. var mConvexHull = new Ammo.btConvexHullShape();
  149. var v0 = new Ammo.btVector3( 0, 0, 0 );
  150. var vertex = geometry.getAttribute( 'position' ).array;
  151. var indexes = geometry.getIndex().array;
  152. var group = index >= 0 ? geometry.groups[ index ] : undefined,
  153. start = group ? group.start : 0,
  154. count = group ? group.count : indexes.length;
  155. var scale = 1 / this.worldScale;
  156. for ( var idx = start; idx < count; idx += 3 ) {
  157. var vx1 = indexes[ idx ] * 3;
  158. var point = new Ammo.btVector3(
  159. vertex[ vx1 ] * scale, vertex[ vx1 + 1 ] * scale, vertex[ vx1 + 2 ] * scale
  160. );
  161. mConvexHull.addPoint( point );
  162. }
  163. return mConvexHull;
  164. },
  165. getTargetByRigidBody: function ( rb ) {
  166. return this.rigidBodiesTarget[ this.rigidBodies.indexOf( rb ) ];
  167. },
  168. getRigidBodyByTarget: function ( target ) {
  169. return this.rigidBodies[ this.rigidBodiesTarget.indexOf( target ) ];
  170. },
  171. getTransformFromMatrix: function ( mtx ) {
  172. var transform = new Ammo.btTransform();
  173. var pos = THREE.SEA3D.VECBUF.setFromMatrixPosition( mtx );
  174. transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
  175. var scl = THREE.SEA3D.VECBUF.setFromMatrixScale( mtx );
  176. mtx.scale( scl.set( 1 / scl.x, 1 / scl.y, 1 / scl.z ) );
  177. var quat = new THREE.Quaternion().setFromRotationMatrix( mtx );
  178. var q = new Ammo.btQuaternion();
  179. q.setValue( quat.x, quat.y, quat.z, quat.w );
  180. transform.setRotation( q );
  181. Ammo.destroy( q );
  182. return transform;
  183. },
  184. getMatrixFromTransform: function ( transform ) {
  185. var position = new THREE.Vector3();
  186. var quaternion = new THREE.Quaternion();
  187. var scale = new THREE.Vector3( 1, 1, 1 );
  188. return function ( transform, matrix ) {
  189. matrix = matrix || new THREE.Matrix4();
  190. var pos = transform.getOrigin(),
  191. quat = transform.getRotation();
  192. position.set( pos.x(), pos.y(), pos.z() );
  193. quaternion.set( quat.x(), quat.y(), quat.z(), quat.w() );
  194. matrix.compose( position, quaternion, scale );
  195. return matrix;
  196. };
  197. }(),
  198. updateTargetTransform: function () {
  199. var matrix = new THREE.Matrix4();
  200. var position = new THREE.Vector3();
  201. var quaternion = new THREE.Quaternion();
  202. var scale = new THREE.Vector3( 1, 1, 1 );
  203. return function ( obj3d, transform, offset ) {
  204. var pos = transform.getOrigin(),
  205. quat = transform.getRotation();
  206. if ( offset ) {
  207. position.set( pos.x(), pos.y(), pos.z() );
  208. quaternion.set( quat.x(), quat.y(), quat.z(), quat.w() );
  209. matrix.compose( position, quaternion, scale );
  210. matrix.multiplyMatrices( matrix, offset );
  211. obj3d.position.setFromMatrixPosition( matrix );
  212. obj3d.quaternion.setFromRotationMatrix( matrix );
  213. } else {
  214. obj3d.position.set( pos.x(), pos.y(), pos.z() );
  215. obj3d.quaternion.set( quat.x(), quat.y(), quat.z(), quat.w() );
  216. }
  217. return this;
  218. };
  219. }(),
  220. update: function ( delta, iteration, fixedDelta ) {
  221. this.world.stepSimulation( delta, iteration || 0, fixedDelta || ( 60 / 1000 ) );
  222. var i, j;
  223. for ( i = 0; i < this.vehicles.length; i ++ ) {
  224. var vehicle = this.vehicles[ i ],
  225. numWheels = vehicle.getNumWheels(),
  226. wheels = this.vehiclesWheels[ i ];
  227. for ( j = 0; j < numWheels; j ++ ) {
  228. vehicle.updateWheelTransform( j, true );
  229. var wheelsTransform = vehicle.getWheelTransformWS( j ),
  230. wheelTarget = wheels[ j ];
  231. if ( wheelTarget ) {
  232. this.updateTargetTransform( wheelTarget, wheelsTransform, wheelTarget.physics ? wheelTarget.physics.offset : null );
  233. }
  234. }
  235. }
  236. for ( i = 0; i < this.rigidBodies.length; i ++ ) {
  237. var rb = this.rigidBodies[ i ],
  238. target = this.rigidBodiesTarget[ i ];
  239. if ( target && rb.isActive() ) {
  240. this.updateTargetTransform( target, rb.getWorldTransform(), target.physics ? target.physics.offset : null );
  241. }
  242. }
  243. return this;
  244. }
  245. };