Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Util.js...
- Object.type = function (_obj) {
- return Object.prototype.toString.call(_obj).slice(8, -1);
- };
- Object.set = function (_obj, _data, _keys) {
- var key, index, max, item, type;
- for (index = 0, max = _keys.length; index < max; ++index) {
- key = _keys[index];
- item = _data[key];
- type = Object.type(item);
- if (type === 'Object') {
- _obj[key] = Object.set({}, item);
- } else if (type === 'Array') {
- _obj[key] = item.slice();
- } else {
- _obj[key] = item;
- }
- }
- return _obj;
- };
- // State.js...
- var State = function (_data) {
- if (_data) { this.set(_data); }
- };
- State.prototype.set = function (_data) {
- if (Object.type(_data) === 'Object') {
- Object.set(this, _data, [
- 'position',
- 'momentum',
- 'orientation',
- 'angularMomentum',
- 'velocity',
- 'spin',
- 'angularVelocity',
- 'bodyToWorld',
- 'worldToBody',
- 'size',
- 'mass',
- 'inverseMass',
- 'inertiaTensor',
- 'inverseInertiaTensor'
- ]);
- }
- };
- State.prototype.recalculate = function () {
- // velocity
- vec3.multiply(this.momentum, this.inverseMass, this.velocity);
- // angularVelocity
- vec3.scale(this.angularMomentum, this.inverseInertiaTensor, this.angularVelocity);
- quat4.normalize(this.orientation);
- // spin
- quat4.multiply(quat4.create([this.angularVelocity[0], this.angularVelocity[1], this.angularVelocity[2], 0]),
- this.orientation,
- this.spin);
- var translation = mat4.create();
- mat4.translate(translation, this.position);
- mat4.multiply(translation, quat4.toMat4(this.orientation), this.bodyToWorld);
- mat4.inverse(this.bodyToWorld, this.worldToBody);
- };
Advertisement
Add Comment
Please, Sign In to add comment