MMDPhysics.js 19 KB

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