LiamGoodacre

Phil State

Feb 4th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  Util.js...
  2. Object.type = function (_obj) {
  3.   return Object.prototype.toString.call(_obj).slice(8, -1);
  4. };
  5. Object.set = function (_obj, _data, _keys) {
  6.   var key, index, max, item, type;
  7.   for (index = 0, max = _keys.length; index < max; ++index) {
  8.     key = _keys[index];
  9.     item = _data[key];
  10.     type = Object.type(item);
  11.     if (type === 'Object') {
  12.       _obj[key] = Object.set({}, item);
  13.     } else if (type === 'Array') {
  14.       _obj[key] = item.slice();
  15.     } else {
  16.       _obj[key] = item;
  17.     }
  18.   }
  19.   return _obj;
  20. };
  21.  
  22. //  State.js...
  23. var State = function (_data) {
  24.   if (_data) { this.set(_data); }
  25. };
  26.  
  27. State.prototype.set = function (_data) {
  28.   if (Object.type(_data) === 'Object') {
  29.     Object.set(this, _data, [
  30.       'position',
  31.       'momentum',
  32.       'orientation',
  33.       'angularMomentum',
  34.       'velocity',
  35.       'spin',
  36.       'angularVelocity',
  37.       'bodyToWorld',
  38.       'worldToBody',
  39.       'size',
  40.       'mass',
  41.       'inverseMass',
  42.       'inertiaTensor',
  43.       'inverseInertiaTensor'
  44.     ]);
  45.   }
  46. };
  47.  
  48. State.prototype.recalculate = function () {
  49.   // velocity
  50.   vec3.multiply(this.momentum, this.inverseMass, this.velocity);
  51.   // angularVelocity
  52.   vec3.scale(this.angularMomentum, this.inverseInertiaTensor, this.angularVelocity);
  53.  
  54.   quat4.normalize(this.orientation);
  55.  
  56.   // spin
  57.   quat4.multiply(quat4.create([this.angularVelocity[0], this.angularVelocity[1], this.angularVelocity[2], 0]),
  58.   this.orientation,
  59.   this.spin);
  60.   var translation = mat4.create();
  61.  
  62.   mat4.translate(translation, this.position);
  63.   mat4.multiply(translation, quat4.toMat4(this.orientation), this.bodyToWorld);
  64.   mat4.inverse(this.bodyToWorld, this.worldToBody);
  65. };
Advertisement
Add Comment
Please, Sign In to add comment