MMDPhysics.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /**
  2. * @author takahiro / https://github.com/takahirox
  3. *
  4. * Dependencies
  5. * - Ammo.js https://github.com/kripken/ammo.js
  6. *
  7. *
  8. * MMD specific Physics class.
  9. *
  10. * See THREE.MMDLoader for the passed parameter list of RigidBody/Constraint.
  11. *
  12. *
  13. * TODO
  14. * - use Physijs http://chandlerprall.github.io/Physijs/
  15. * and improve the performance by making use of Web worker.
  16. * - if possible, make this class being non-MMD specific.
  17. */
  18. THREE.MMDPhysics = function ( mesh, params ) {
  19. this.mesh = mesh;
  20. this.helper = new THREE.MMDPhysics.PhysicsHelper();
  21. /*
  22. * Note: Default 1 / 65 unit step is a workaround that
  23. * some bones go wrong at near 60fps if it's 1 / 60.
  24. * Be careful that small unitStep could cause heavy
  25. * physics calculation.
  26. */
  27. this.unitStep = ( params && params.unitStep ) ? params.unitStep : 1 / 65;
  28. this.maxStepNum = ( params && params.maxStepNum ) ? params.maxStepNum : 3;
  29. this.world = null;
  30. this.bodies = [];
  31. this.constraints = [];
  32. this.init();
  33. };
  34. THREE.MMDPhysics.prototype = {
  35. constructor: THREE.MMDPhysics,
  36. init: function () {
  37. this.initWorld();
  38. this.initRigidBodies();
  39. this.initConstraints();
  40. this.reset();
  41. },
  42. initWorld: function () {
  43. var config = new Ammo.btDefaultCollisionConfiguration();
  44. var dispatcher = new Ammo.btCollisionDispatcher( config );
  45. var cache = new Ammo.btDbvtBroadphase();
  46. var solver = new Ammo.btSequentialImpulseConstraintSolver();
  47. var world = new Ammo.btDiscreteDynamicsWorld( dispatcher, cache, solver, config );
  48. world.setGravity( new Ammo.btVector3( 0, -10 * 10, 0 ) );
  49. this.world = world;
  50. },
  51. initRigidBodies: function () {
  52. var bodies = this.mesh.geometry.rigidBodies;
  53. for ( var i = 0; i < bodies.length; i++ ) {
  54. var b = new THREE.MMDPhysics.RigidBody( this.mesh, this.world, bodies[ i ], this.helper );
  55. this.bodies.push( b );
  56. }
  57. },
  58. initConstraints: function () {
  59. var constraints = this.mesh.geometry.constraints;
  60. for ( var i = 0; i < constraints.length; i++ ) {
  61. var params = constraints[ i ];
  62. var bodyA = this.bodies[ params.rigidBodyIndex1 ];
  63. var bodyB = this.bodies[ params.rigidBodyIndex2 ];
  64. var c = new THREE.MMDPhysics.Constraint( this.mesh, this.world, bodyA, bodyB, params, this.helper );
  65. this.constraints.push( c );
  66. }
  67. },
  68. update: function ( delta ) {
  69. var unitStep = this.unitStep;
  70. var stepTime = delta;
  71. var maxStepNum = ( ( delta / unitStep ) | 0 ) + 1;
  72. if ( maxStepNum > this.maxStepNum ) {
  73. maxStepNum = this.maxStepNum;
  74. }
  75. this.updateRigidBodies();
  76. this.world.stepSimulation( stepTime, maxStepNum, unitStep );
  77. this.updateBones();
  78. },
  79. updateRigidBodies: function () {
  80. for ( var i = 0; i < this.bodies.length; i++ ) {
  81. this.bodies[ i ].updateFromBone();
  82. }
  83. },
  84. updateBones: function () {
  85. for ( var i = 0; i < this.bodies.length; i++ ) {
  86. this.bodies[ i ].updateBone();
  87. }
  88. },
  89. reset: function () {
  90. for ( var i = 0; i < this.bodies.length; i++ ) {
  91. this.bodies[ i ].reset();
  92. }
  93. },
  94. warmup: function ( cycles ) {
  95. for ( var i = 0; i < cycles; i++ ) {
  96. this.update( 1 );
  97. }
  98. }
  99. };
  100. /**
  101. * This helper class responsibilies are
  102. *
  103. * 1. manage Ammo.js and Three.js object resources and
  104. * improve the performance and the memory consumption by
  105. * reusing objects.
  106. *
  107. * 2. provide simple Ammo object operations.
  108. */
  109. THREE.MMDPhysics.PhysicsHelper = function () {
  110. // for Three.js
  111. this.threeVector3s = [];
  112. this.threeMatrix4s = [];
  113. this.threeQuaternions = [];
  114. // for Ammo.js
  115. this.transforms = [];
  116. this.quaternions = [];
  117. this.vector3s = [];
  118. };
  119. THREE.MMDPhysics.PhysicsHelper.prototype = {
  120. allocThreeVector3: function () {
  121. return ( this.threeVector3s.length > 0 ) ? this.threeVector3s.pop() : new THREE.Vector3();
  122. },
  123. freeThreeVector3: function ( v ) {
  124. this.threeVector3s.push( v );
  125. },
  126. allocThreeMatrix4: function () {
  127. return ( this.threeMatrix4s.length > 0 ) ? this.threeMatrix4s.pop() : new THREE.Matrix4();
  128. },
  129. freeThreeMatrix4: function ( m ) {
  130. this.threeMatrix4s.push( m );
  131. },
  132. allocThreeQuaternion: function () {
  133. return ( this.threeQuaternions.length > 0 ) ? this.threeQuaternions.pop() : new THREE.Quaternion();
  134. },
  135. freeThreeQuaternion: function ( q ) {
  136. this.threeQuaternions.push( q );
  137. },
  138. allocTransform: function () {
  139. return ( this.transforms.length > 0 ) ? this.transforms.pop() : new Ammo.btTransform();
  140. },
  141. freeTransform: function ( t ) {
  142. this.transforms.push( t );
  143. },
  144. allocQuaternion: function () {
  145. return ( this.quaternions.length > 0 ) ? this.quaternions.pop() : new Ammo.btQuaternion();
  146. },
  147. freeQuaternion: function ( q ) {
  148. this.quaternions.push( q );
  149. },
  150. allocVector3: function () {
  151. return ( this.vector3s.length > 0 ) ? this.vector3s.pop() : new Ammo.btVector3();
  152. },
  153. freeVector3: function ( v ) {
  154. this.vector3s.push( v );
  155. },
  156. setIdentity: function ( t ) {
  157. t.setIdentity();
  158. },
  159. getBasis: function ( t ) {
  160. var q = this.allocQuaternion();
  161. t.getBasis().getRotation( q );
  162. return q;
  163. },
  164. getBasisAsMatrix3: function ( t ) {
  165. var q = this.getBasis( t );
  166. var m = this.quaternionToMatrix3( q );
  167. this.freeQuaternion( q );
  168. return m;
  169. },
  170. getOrigin: function( t ) {
  171. return t.getOrigin();
  172. },
  173. setOrigin: function( t, v ) {
  174. t.getOrigin().setValue( v.x(), v.y(), v.z() );
  175. },
  176. copyOrigin: function( t1, t2 ) {
  177. var o = t2.getOrigin();
  178. this.setOrigin( t1, o );
  179. },
  180. setBasis: function( t, q ) {
  181. t.setRotation( q );
  182. },
  183. setBasisFromMatrix3: function( t, m ) {
  184. var q = this.matrix3ToQuaternion( m );
  185. this.setBasis( t, q );
  186. this.freeQuaternion( q );
  187. },
  188. setOriginFromArray3: function ( t, a ) {
  189. t.getOrigin().setValue( a[ 0 ], a[ 1 ], a[ 2 ] );
  190. },
  191. setBasisFromArray3: function ( t, a ) {
  192. t.getBasis().setEulerZYX( a[ 0 ], a[ 1 ], a[ 2 ] );
  193. },
  194. setBasisFromArray4: function ( t, a ) {
  195. var q = this.array4ToQuaternion( a );
  196. this.setBasis( t, q );
  197. this.freeQuaternion( q );
  198. },
  199. array4ToQuaternion: function( a ) {
  200. var q = this.allocQuaternion();
  201. q.setX( a[ 0 ] );
  202. q.setY( a[ 1 ] );
  203. q.setZ( a[ 2 ] );
  204. q.setW( a[ 3 ] );
  205. return q;
  206. },
  207. multiplyTransforms: function ( t1, t2 ) {
  208. var t = this.allocTransform();
  209. this.setIdentity( t );
  210. var m1 = this.getBasisAsMatrix3( t1 );
  211. var m2 = this.getBasisAsMatrix3( t2 );
  212. var o1 = this.getOrigin( t1 );
  213. var o2 = this.getOrigin( t2 );
  214. var v1 = this.multiplyMatrix3ByVector3( m1, o2 );
  215. var v2 = this.addVector3( v1, o1 );
  216. this.setOrigin( t, v2 );
  217. var m3 = this.multiplyMatrices3( m1, m2 );
  218. this.setBasisFromMatrix3( t, m3 );
  219. this.freeVector3( v1 );
  220. this.freeVector3( v2 );
  221. return t;
  222. },
  223. inverseTransform: function ( t ) {
  224. var t2 = this.allocTransform();
  225. var m1 = this.getBasisAsMatrix3( t );
  226. var o = this.getOrigin( t );
  227. var m2 = this.transposeMatrix3( m1 );
  228. var v1 = this.negativeVector3( o );
  229. var v2 = this.multiplyMatrix3ByVector3( m2, v1 );
  230. this.setOrigin( t2, v2 );
  231. this.setBasisFromMatrix3( t2, m2 );
  232. this.freeVector3( v1 );
  233. this.freeVector3( v2 );
  234. return t2;
  235. },
  236. multiplyMatrices3: function ( m1, m2 ) {
  237. var m3 = [];
  238. var v10 = this.rowOfMatrix3( m1, 0 );
  239. var v11 = this.rowOfMatrix3( m1, 1 );
  240. var v12 = this.rowOfMatrix3( m1, 2 );
  241. var v20 = this.columnOfMatrix3( m2, 0 );
  242. var v21 = this.columnOfMatrix3( m2, 1 );
  243. var v22 = this.columnOfMatrix3( m2, 2 );
  244. m3[ 0 ] = this.dotVectors3( v10, v20 );
  245. m3[ 1 ] = this.dotVectors3( v10, v21 );
  246. m3[ 2 ] = this.dotVectors3( v10, v22 );
  247. m3[ 3 ] = this.dotVectors3( v11, v20 );
  248. m3[ 4 ] = this.dotVectors3( v11, v21 );
  249. m3[ 5 ] = this.dotVectors3( v11, v22 );
  250. m3[ 6 ] = this.dotVectors3( v12, v20 );
  251. m3[ 7 ] = this.dotVectors3( v12, v21 );
  252. m3[ 8 ] = this.dotVectors3( v12, v22 );
  253. this.freeVector3( v10 );
  254. this.freeVector3( v11 );
  255. this.freeVector3( v12 );
  256. this.freeVector3( v20 );
  257. this.freeVector3( v21 );
  258. this.freeVector3( v22 );
  259. return m3;
  260. },
  261. addVector3: function( v1, v2 ) {
  262. var v = this.allocVector3();
  263. v.setValue( v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z() );
  264. return v;
  265. },
  266. dotVectors3: function( v1, v2 ) {
  267. return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
  268. },
  269. rowOfMatrix3: function( m, i ) {
  270. var v = this.allocVector3();
  271. v.setValue( m[ i * 3 + 0 ], m[ i * 3 + 1 ], m[ i * 3 + 2 ] );
  272. return v;
  273. },
  274. columnOfMatrix3: function( m, i ) {
  275. var v = this.allocVector3();
  276. v.setValue( m[ i + 0 ], m[ i + 3 ], m[ i + 6 ] );
  277. return v;
  278. },
  279. negativeVector3: function( v ) {
  280. var v2 = this.allocVector3();
  281. v2.setValue( -v.x(), -v.y(), -v.z() );
  282. return v2;
  283. },
  284. multiplyMatrix3ByVector3: function ( m, v ) {
  285. var v4 = this.allocVector3();
  286. var v0 = this.rowOfMatrix3( m, 0 );
  287. var v1 = this.rowOfMatrix3( m, 1 );
  288. var v2 = this.rowOfMatrix3( m, 2 );
  289. var x = this.dotVectors3( v0, v );
  290. var y = this.dotVectors3( v1, v );
  291. var z = this.dotVectors3( v2, v );
  292. v4.setValue( x, y, z );
  293. this.freeVector3( v0 );
  294. this.freeVector3( v1 );
  295. this.freeVector3( v2 );
  296. return v4;
  297. },
  298. transposeMatrix3: function( m ) {
  299. var m2 = [];
  300. m2[ 0 ] = m[ 0 ];
  301. m2[ 1 ] = m[ 3 ];
  302. m2[ 2 ] = m[ 6 ];
  303. m2[ 3 ] = m[ 1 ];
  304. m2[ 4 ] = m[ 4 ];
  305. m2[ 5 ] = m[ 7 ];
  306. m2[ 6 ] = m[ 2 ];
  307. m2[ 7 ] = m[ 5 ];
  308. m2[ 8 ] = m[ 8 ];
  309. return m2;
  310. },
  311. quaternionToMatrix3: function ( q ) {
  312. var m = [];
  313. var x = q.x();
  314. var y = q.y();
  315. var z = q.z();
  316. var w = q.w();
  317. var xx = x * x;
  318. var yy = y * y;
  319. var zz = z * z;
  320. var xy = x * y;
  321. var yz = y * z;
  322. var zx = z * x;
  323. var xw = x * w;
  324. var yw = y * w;
  325. var zw = z * w;
  326. m[ 0 ] = 1 - 2 * ( yy + zz );
  327. m[ 1 ] = 2 * ( xy - zw );
  328. m[ 2 ] = 2 * ( zx + yw );
  329. m[ 3 ] = 2 * ( xy + zw );
  330. m[ 4 ] = 1 - 2 * ( zz + xx );
  331. m[ 5 ] = 2 * ( yz - xw );
  332. m[ 6 ] = 2 * ( zx - yw );
  333. m[ 7 ] = 2 * ( yz + xw );
  334. m[ 8 ] = 1 - 2 * ( xx + yy );
  335. return m;
  336. },
  337. matrix3ToQuaternion: function( m ) {
  338. var t = m[ 0 ] + m[ 4 ] + m[ 8 ];
  339. var s, x, y, z, w;
  340. if( t > 0 ) {
  341. s = Math.sqrt( t + 1.0 ) * 2;
  342. w = 0.25 * s;
  343. x = ( m[ 7 ] - m[ 5 ] ) / s;
  344. y = ( m[ 2 ] - m[ 6 ] ) / s;
  345. z = ( m[ 3 ] - m[ 1 ] ) / s;
  346. } else if( ( m[ 0 ] > m[ 4 ] ) && ( m[ 0 ] > m[ 8 ] ) ) {
  347. s = Math.sqrt( 1.0 + m[ 0 ] - m[ 4 ] - m[ 8 ] ) * 2;
  348. w = ( m[ 7 ] - m[ 5 ] ) / s;
  349. x = 0.25 * s;
  350. y = ( m[ 1 ] + m[ 3 ] ) / s;
  351. z = ( m[ 2 ] + m[ 6 ] ) / s;
  352. } else if( m[ 4 ] > m[ 8 ] ) {
  353. s = Math.sqrt( 1.0 + m[ 4 ] - m[ 0 ] - m[ 8 ] ) * 2;
  354. w = ( m[ 2 ] - m[ 6 ] ) / s;
  355. x = ( m[ 1 ] + m[ 3 ] ) / s;
  356. y = 0.25 * s;
  357. z = ( m[ 5 ] + m[ 7 ] ) / s;
  358. } else {
  359. s = Math.sqrt( 1.0 + m[ 8 ] - m[ 0 ] - m[ 4 ] ) * 2;
  360. w = ( m[ 3 ] - m[ 1 ] ) / s;
  361. x = ( m[ 2 ] + m[ 6 ] ) / s;
  362. y = ( m[ 5 ] + m[ 7 ] ) / s;
  363. z = 0.25 * s;
  364. }
  365. var q = this.allocQuaternion();
  366. q.setX( x );
  367. q.setY( y );
  368. q.setZ( z );
  369. q.setW( w );
  370. return q;
  371. },
  372. };
  373. THREE.MMDPhysics.RigidBody = function ( mesh, world, params, helper ) {
  374. this.mesh = mesh;
  375. this.world = world;
  376. this.params = params;
  377. this.helper = helper;
  378. this.body = null;
  379. this.bone = null;
  380. this.boneOffsetForm = null;
  381. this.boneOffsetFormInverse = null;
  382. this.init();
  383. };
  384. THREE.MMDPhysics.RigidBody.prototype = {
  385. constructor: THREE.MMDPhysics.RigidBody,
  386. init: function () {
  387. function generateShape ( p ) {
  388. switch( p.shapeType ) {
  389. case 0:
  390. return new Ammo.btSphereShape( p.width );
  391. case 1:
  392. return new Ammo.btBoxShape( new Ammo.btVector3( p.width, p.height, p.depth ) );
  393. case 2:
  394. return new Ammo.btCapsuleShape( p.width, p.height );
  395. default:
  396. throw 'unknown shape type ' + p.shapeType;
  397. }
  398. };
  399. var helper = this.helper;
  400. var params = this.params;
  401. var bones = this.mesh.skeleton.bones;
  402. var bone = ( params.boneIndex === -1 ) ? new THREE.Bone() : bones[ params.boneIndex ];
  403. var shape = generateShape( params );
  404. var weight = ( params.type === 0 ) ? 0 : params.weight;
  405. var localInertia = helper.allocVector3();
  406. localInertia.setValue( 0, 0, 0 );
  407. if( weight !== 0 ) {
  408. shape.calculateLocalInertia( weight, localInertia );
  409. }
  410. var boneOffsetForm = helper.allocTransform();
  411. helper.setIdentity( boneOffsetForm );
  412. helper.setOriginFromArray3( boneOffsetForm, params.position );
  413. helper.setBasisFromArray3( boneOffsetForm, params.rotation );
  414. var boneForm = helper.allocTransform();
  415. helper.setIdentity( boneForm );
  416. helper.setOriginFromArray3( boneForm, bone.getWorldPosition().toArray() );
  417. var form = helper.multiplyTransforms( boneForm, boneOffsetForm );
  418. var state = new Ammo.btDefaultMotionState( form );
  419. var info = new Ammo.btRigidBodyConstructionInfo( weight, state, shape, localInertia );
  420. info.set_m_friction( params.friction );
  421. info.set_m_restitution( params.restriction );
  422. var body = new Ammo.btRigidBody( info );
  423. if ( params.type === 0 ) {
  424. body.setCollisionFlags( body.getCollisionFlags() | 2 );
  425. body.setActivationState( 4 );
  426. }
  427. body.setDamping( params.positionDamping, params.rotationDamping );
  428. body.setSleepingThresholds( 0, 0 );
  429. this.world.addRigidBody( body, 1 << params.groupIndex, params.groupTarget );
  430. this.body = body;
  431. this.bone = bone;
  432. this.boneOffsetForm = boneOffsetForm;
  433. this.boneOffsetFormInverse = helper.inverseTransform( boneOffsetForm );
  434. helper.freeVector3( localInertia );
  435. helper.freeTransform( form );
  436. helper.freeTransform( boneForm );
  437. },
  438. reset: function () {
  439. this.setTransformFromBone();
  440. },
  441. updateFromBone: function () {
  442. if ( this.params.boneIndex === -1 ) {
  443. return;
  444. }
  445. if ( this.params.type === 0 ) {
  446. this.setTransformFromBone();
  447. }
  448. if ( this.params.type === 2 ) {
  449. this.setPositionFromBone();
  450. }
  451. },
  452. updateBone: function () {
  453. if ( this.params.type === 0 || this.params.boneIndex === -1 ) {
  454. return;
  455. }
  456. this.updateBoneRotation();
  457. if ( this.params.type === 1 ) {
  458. this.updateBonePosition();
  459. }
  460. this.bone.updateMatrixWorld( true );
  461. },
  462. getBoneTransform: function () {
  463. var helper = this.helper;
  464. var p = this.bone.getWorldPosition();
  465. var q = this.bone.getWorldQuaternion();
  466. var tr = helper.allocTransform();
  467. helper.setOriginFromArray3( tr, p.toArray() );
  468. helper.setBasisFromArray4( tr, q.toArray() );
  469. var form = helper.multiplyTransforms( tr, this.boneOffsetForm );
  470. helper.freeTransform( tr );
  471. return form;
  472. },
  473. getWorldTransformForBone: function () {
  474. var helper = this.helper;
  475. var tr = helper.allocTransform();
  476. this.body.getMotionState().getWorldTransform( tr );
  477. var tr2 = helper.multiplyTransforms( tr, this.boneOffsetFormInverse );
  478. helper.freeTransform( tr );
  479. return tr2;
  480. },
  481. setTransformFromBone: function () {
  482. var helper = this.helper;
  483. var form = this.getBoneTransform();
  484. // TODO: check the most appropriate way to set
  485. //this.body.setWorldTransform( form );
  486. this.body.setCenterOfMassTransform( form );
  487. this.body.getMotionState().setWorldTransform( form );
  488. helper.freeTransform( form );
  489. },
  490. setPositionFromBone: function () {
  491. var helper = this.helper;
  492. var form = this.getBoneTransform();
  493. var tr = helper.allocTransform();
  494. this.body.getMotionState().getWorldTransform( tr );
  495. helper.copyOrigin( tr, form );
  496. // TODO: check the most appropriate way to set
  497. //this.body.setWorldTransform( tr );
  498. this.body.setCenterOfMassTransform( tr );
  499. this.body.getMotionState().setWorldTransform( tr );
  500. helper.freeTransform( tr );
  501. helper.freeTransform( form );
  502. },
  503. updateBoneRotation: function () {
  504. this.bone.updateMatrixWorld( true );
  505. var helper = this.helper;
  506. var tr = this.getWorldTransformForBone();
  507. var q = helper.getBasis( tr );
  508. var thQ = helper.allocThreeQuaternion();
  509. var thQ2 = helper.allocThreeQuaternion();
  510. var thQ3 = helper.allocThreeQuaternion();
  511. thQ.set( q.x(), q.y(), q.z(), q.w() );
  512. thQ2.setFromRotationMatrix( this.bone.matrixWorld );
  513. thQ2.conjugate()
  514. thQ2.multiply( thQ );
  515. //this.bone.quaternion.multiply( thQ2 );
  516. thQ3.setFromRotationMatrix( this.bone.matrix );
  517. this.bone.quaternion.copy( thQ2.multiply( thQ3 ) );
  518. helper.freeThreeQuaternion( thQ );
  519. helper.freeThreeQuaternion( thQ2 );
  520. helper.freeThreeQuaternion( thQ3 );
  521. helper.freeQuaternion( q );
  522. helper.freeTransform( tr );
  523. },
  524. updateBonePosition: function () {
  525. var helper = this.helper;
  526. var tr = this.getWorldTransformForBone();
  527. var thV = helper.allocThreeVector3();
  528. var o = helper.getOrigin( tr );
  529. thV.set( o.x(), o.y(), o.z() );
  530. var v = this.bone.worldToLocal( thV );
  531. this.bone.position.add( v );
  532. helper.freeThreeVector3( thV );
  533. helper.freeTransform( tr );
  534. }
  535. };
  536. THREE.MMDPhysics.Constraint = function ( mesh, world, bodyA, bodyB, params, helper ) {
  537. this.mesh = mesh;
  538. this.world = world;
  539. this.bodyA = bodyA;
  540. this.bodyB = bodyB;
  541. this.params = params;
  542. this.helper = helper;
  543. this.constraint = null;
  544. this.init();
  545. };
  546. THREE.MMDPhysics.Constraint.prototype = {
  547. constructor: THREE.MMDPhysics.Constraint,
  548. init: function () {
  549. var helper = this.helper;
  550. var params = this.params;
  551. var bodyA = this.bodyA;
  552. var bodyB = this.bodyB;
  553. var form = helper.allocTransform();
  554. helper.setIdentity( form );
  555. helper.setOriginFromArray3( form, params.position );
  556. helper.setBasisFromArray3( form, params.rotation );
  557. var formA = helper.allocTransform();
  558. var formB = helper.allocTransform();
  559. bodyA.body.getMotionState().getWorldTransform( formA );
  560. bodyB.body.getMotionState().getWorldTransform( formB );
  561. var formInverseA = helper.inverseTransform( formA );
  562. var formInverseB = helper.inverseTransform( formB );
  563. var formA2 = helper.multiplyTransforms( formInverseA, form );
  564. var formB2 = helper.multiplyTransforms( formInverseB, form );
  565. var constraint = new Ammo.btGeneric6DofSpringConstraint( bodyA.body, bodyB.body, formA2, formB2, true );
  566. var lll = helper.allocVector3();
  567. var lul = helper.allocVector3();
  568. var all = helper.allocVector3();
  569. var aul = helper.allocVector3();
  570. lll.setValue( params.translationLimitation1[ 0 ],
  571. params.translationLimitation1[ 1 ],
  572. params.translationLimitation1[ 2 ] );
  573. lul.setValue( params.translationLimitation2[ 0 ],
  574. params.translationLimitation2[ 1 ],
  575. params.translationLimitation2[ 2 ] );
  576. all.setValue( params.rotationLimitation1[ 0 ],
  577. params.rotationLimitation1[ 1 ],
  578. params.rotationLimitation1[ 2 ] );
  579. aul.setValue( params.rotationLimitation2[ 0 ],
  580. params.rotationLimitation2[ 1 ],
  581. params.rotationLimitation2[ 2 ] );
  582. constraint.setLinearLowerLimit( lll );
  583. constraint.setLinearUpperLimit( lul );
  584. constraint.setAngularLowerLimit( all );
  585. constraint.setAngularUpperLimit( aul );
  586. for ( var i = 0; i < 3; i++ ) {
  587. if( params.springPosition[ i ] !== 0 ) {
  588. constraint.enableSpring( i, true );
  589. constraint.setStiffness( i, params.springPosition[ i ] );
  590. }
  591. }
  592. for ( var i = 0; i < 3; i++ ) {
  593. if( params.springRotation[ i ] !== 0 ) {
  594. constraint.enableSpring( i + 3, true );
  595. constraint.setStiffness( i + 3, params.springRotation[ i ] );
  596. }
  597. }
  598. this.world.addConstraint( constraint, true );
  599. this.constraint = constraint;
  600. helper.freeTransform( form );
  601. helper.freeTransform( formA );
  602. helper.freeTransform( formB );
  603. helper.freeTransform( formInverseA );
  604. helper.freeTransform( formInverseB );
  605. helper.freeTransform( formA2 );
  606. helper.freeTransform( formB2 );
  607. helper.freeVector3( lll );
  608. helper.freeVector3( lul );
  609. helper.freeVector3( all );
  610. helper.freeVector3( aul );
  611. }
  612. };