|
|
- /**
- * PhysicsJS v0.7.0 - 2014-12-08
- * A modular, extendable, and easy-to-use physics engine for javascript
- * http://wellcaffeinated.net/PhysicsJS
- *
- * Copyright (c) 2014 Jasper Palfree <jasper@wellcaffeinated.net>
- * Licensed MIT
- */
- !function(a,b){"object"==typeof exports?module.exports=b.call(a):"function"==typeof define&&define.amd?define(function(){return b.call(a)}):a.Physics=b.call(a)}("undefined"!=typeof window?window:this,function(){"use strict";var a=this,b=a.document,c=function f(){return f.world.apply(f,arguments)};c.util={},function(){c.aabb=function(a,b,c,d){var e={x:0,y:0,hw:0,hh:0};return void 0===a?e:(a&&void 0!==a.x&&(c=b.x,d=b.y,b=a.y,a=a.x),void 0===d&&void 0!==a&&void 0!==b?(e.hw=.5*a,e.hh=.5*b,c&&void 0!==c.x&&(e.x=c.x,e.y=c.y),e):(e.hw=.5*Math.abs(c-a),e.hh=.5*Math.abs(d-b),e.x=.5*(c+a),e.y=.5*(d+b),e))},c.aabb.contains=function(a,b){return b.x>a.x-a.hw&&b.x<a.x+a.hw&&b.y>a.y-a.hh&&b.y<a.y+a.hh},c.aabb.clone=function(a){return{x:a.x,y:a.y,hw:a.hw,hh:a.hh}},c.aabb.union=function(a,b,c){var d=c===!0?a:{},e=Math.max(a.x+a.hw,b.x+b.hw),f=Math.max(a.y+a.hh,b.y+b.hh),g=Math.min(a.x-a.hw,b.x-b.hw),h=Math.min(a.y-a.hh,b.y-b.hh);return d.hw=.5*Math.abs(e-g),d.hh=.5*Math.abs(f-h),d.x=.5*(e+g),d.y=.5*(f+h),d},c.aabb.overlap=function(a,b){var c=a.x-a.hw,d=b.x-b.hw,e=a.x+a.hw,f=b.x+b.hw;return e>=d&&f>=e||f>=c&&e>=f?(c=a.y-a.hh,d=b.y-b.hh,e=a.y+a.hh,f=b.y+b.hh,e>=d&&f>=e||f>=c&&e>=f):!1}}(),function(){var a=1e-4,b=100,d=function(a,b,c){var d=b.normSq()-b.dot(a),e=b.dot(a)-a.normSq();return 0>d?c.clone(b).negate():e>0?c.clone(a).negate():(c.clone(b).vsub(a),c.perp(a.cross(c)>0))},e=function(a){var b,d,e=a.length,f=a[e-2],g=a[e-3],h=c.scratchpad(),i=h.vector().clone(f.pt),j=h.vector().clone(g.pt).vsub(i);return j.equals(c.vector.zero)?h.done({a:f.a,b:f.b}):(b=-j.dot(i)/j.normSq(),d=1-b,h.done(0>=d?{a:g.a,b:g.b}:0>=b?{a:f.a,b:f.b}:{a:i.clone(f.a).mult(d).vadd(j.clone(g.a).mult(b)).values(),b:i.clone(f.b).mult(d).vadd(j.clone(g.b).mult(b)).values()}))},f=function(f,g,h,i){var j,k,l,m,n=!1,o=!1,p=!1,q=[],r=1,s=c.scratchpad(),t=s.vector().clone(g||c.vector.axis[0]),u=s.vector(),v=s.vector(),w=s.vector(),x=s.vector(),y=0;for(m=f(t),r=q.push(m),u.clone(m.pt),t.negate();++y;){if(u.swap(v),m=f(t),r=q.push(m),u.clone(m.pt),i&&i(q),u.equals(c.vector.zero)){n=!0;break}if(!o&&u.dot(t)<=0){if(h)break;o=!0}if(2===r)t=d(u,v,t);else if(o){if(t.normalize(),m=v.dot(t),Math.abs(m-u.dot(t))<a){p=-m;break}v.normSq()<w.clone(q[0].pt).normSq()?q.shift():q.splice(1,1),t=d(w.clone(q[1].pt),x.clone(q[0].pt),t)}else if(j=j||s.vector(),k=k||s.vector(),j.clone(v).vsub(u),k.clone(q[0].pt).vsub(u),l=j.cross(k)>0,l^u.cross(j)>0)q.shift(),j.perp(!l),t.swap(j);else{if(!(l^k.cross(u)>0)){n=!0;break}q.splice(1,1),k.perp(l),t.swap(j)}if(y>b)return s.done(),{simplex:q,iterations:y,distance:0,maxIterationsReached:!0}}return s.done(),m={overlap:n,simplex:q,iterations:y},p!==!1&&(m.distance=p,m.closest=e(q)),m};c.gjk=f}(),function(){c.statistics={pushRunningAvg:function(a,b,c,d){var e=a-c;return c+=e/b,d+=e*(a-c),[c,d]},pushRunningVectorAvg:function(a,b,c,d){var e=1/b,f=a.get(0)-c.get(0),g=a.get(1)-c.get(1);c.add(f*e,g*e),d&&(f*=a.get(0)-c.get(0),g*=a.get(1)-c.get(1),d.add(f,g))}}}(),function(){var a=function b(a,d,e){return this instanceof b?(this.v=new c.vector,this.o=new c.vector,a instanceof b?void this.clone(a):(a&&this.setTranslation(a),void this.setRotation(d||0,e))):new b(a,d)};a.prototype.setTranslation=function(a){return this.v.clone(a),this},a.prototype.setRotation=function(a,b){return this.cosA=Math.cos(a),this.sinA=Math.sin(a),b?this.o.clone(b):this.o.zero(),this},a.prototype.clone=function(b){return b?(this.setTranslation(b.v),this.cosA=b.cosA,this.sinA=b.sinA,this.o.clone(b.o),this):new a(this)},c.transform=a}(),function(a){var b=Math.sqrt,d=Math.min,e=Math.max,f=(Math.acos,Math.atan2),g=2*Math.PI,h=!!a.Float64Array,i=function j(a,b){return this instanceof j?(this._=h?new Float64Array(5):[],void(a&&(void 0!==a.x||a._&&a._.length)?this.clone(a):(this.recalc=!0,this.set(a,b)))):new j(a,b)};Object.defineProperties(i.prototype,{x:{get:function(){return+this._[0]},set:function(a){a=+a||0,this.recalc=a===this._[0],this._[0]=a}},y:{get:function(){return+this._[1]},set:function(a){a=+a||0,this.recalc=a===this._[1],this._[1]=a}}}),i.prototype.set=function(a,b){return this.reca
- }}),c.world=d}(),c.integrator("verlet",function(a){return c.body.mixin({started:function(a){return void 0!==a&&(this._started=!0),!!this._started}}),{init:function(b){a.init.call(this,b)},integrateVelocities:function(a,b){for(var c,d=b*b,e=1-this.options.drag,f=null,g=this.prevDt||b,h=.5*(d+b*g),i=0,j=a.length;j>i;++i)f=a[i],c=f.state,"static"===f.treatment||f.sleep(b)?(c.vel.zero(),c.acc.zero(),c.angular.vel=0,c.angular.acc=0):(c.vel.equals(c.old.vel)&&f.started()?c.vel.clone(c.pos).vsub(c.old.pos):(c.old.pos.clone(c.pos).vsub(c.vel),c.vel.mult(b)),e&&c.vel.mult(e),c.vel.vadd(c.acc.mult(h)),c.vel.mult(1/b),c.old.vel.clone(c.vel),c.acc.zero(),c.angular.vel===c.old.angular.vel&&f.started()?c.angular.vel=c.angular.pos-c.old.angular.pos:(c.old.angular.pos=c.angular.pos-c.angular.vel,c.angular.vel*=b),c.angular.vel+=c.angular.acc*h,c.angular.vel/=b,c.old.angular.vel=c.angular.vel,c.angular.acc=0,f.started(!0))},integratePositions:function(a,b){for(var c,d=null,e=this.prevDt||b,f=b/e,g=0,h=a.length;h>g;++g)d=a[g],c=d.state,"static"===d.treatment||d.sleep()||(c.vel.mult(b*f),c.old.pos.clone(c.pos),c.pos.vadd(c.vel),c.vel.mult(1/(b*f)),c.old.vel.clone(c.vel),c.angular.vel*=b*f,c.old.angular.pos=c.angular.pos,c.angular.pos+=c.angular.vel,c.angular.vel/=b*f,c.old.angular.vel=c.angular.vel);this.prevDt=b}}}),c.geometry("point",function(){}),c.body("point",function(a){return{init:function(b){a.init.call(this,b),this.moi=0}}}),c.geometry("circle",function(a){var b={radius:1};return{init:function(d){a.init.call(this,d),this.options.defaults(b),this.options.onChange(function(a){this.radius=a.radius}),this.options(d),this._aabb=c.aabb(),this.radius=this.options.radius},aabb:function(){var a=this.radius;return this._aabb.hw!==a&&(this._aabb=c.aabb(-a,-a,a,a)),c.aabb.clone(this._aabb)},getFarthestHullPoint:function(a,b){return b=b||new c.vector,b.clone(a).normalize().mult(this.radius)},getFarthestCorePoint:function(a,b,d){return b=b||new c.vector,b.clone(a).normalize().mult(this.radius-d)}}}),c.geometry("compound",function(a){var b={};return{init:function(c){a.init.call(this,c),this.options.defaults(b),this.options(c),this.children=[]},addChild:function(a,b,d){return this._aabb=null,this.children.push({g:a,pos:new c.vector(b),angle:d}),this},clear:function(){return this._aabb=null,this.children=[],this},aabb:function(a){if(!a&&this._aabb)return c.aabb.clone(this._aabb);var b,d,e,f=c.scratchpad(),g=c.vector();a=a||0;for(var h=0,i=this.children.length;i>h;h++)d=this.children[h],b=d.g.aabb(a+d.angle),g.clone(d.pos),a&&g.rotate(a),b.x+=g._[0],b.y+=g._[1],e=e?c.aabb.union(e,b,!0):b;return a||(this._aabb=c.aabb.clone(e)),f.done(e)},getFarthestHullPoint:function(a,b){var d,e,f=this.children.length,g=c.scratchpad(),h=g.vector(),i=0,j=0;for(b=b||new c.vector,e=0;f>e;e++)d=this.children[e],d.g.getFarthestHullPoint(a.rotate(-d.angle),h),i=h.rotate(d.angle).vadd(d.pos).proj(a.rotate(d.angle)),i>j&&(j=i,b.swap(h));return g.done(b)},getFarthestCorePoint:function(a,b,d){var e,f,g=this.children.length,h=c.scratchpad(),i=h.vector(),j=0,k=0;for(b=b||new c.vector,f=0;g>f;f++)e=this.children[f],e.g.getFarthestCorePoint(a.rotate(-e.angle),i,d),j=i.rotate(e.angle).vadd(e.pos).proj(a.rotate(e.angle)),j>k&&(k=j,b.swap(i));return h.done(b)}}}),c.geometry("convex-polygon",function(a){var b="Error: The vertices specified do not match that of a _convex_ polygon.",d={};return{init:function(b){var c=this;a.init.call(this,b),this.options.defaults(d),this.options.onChange(function(a){c.setVertices(a.vertices||[])}),this.options(b),c.setVertices(this.options.vertices||[])},setVertices:function(a){var d=c.scratchpad(),e=d.transform(),f=this.vertices=[];if(!c.geometry.isPolygonConvex(a))throw b;e.setRotation(0),e.setTranslation(c.geometry.getPolygonCentroid(a).negate());for(var g=0,h=a.length;h>g;++g)f.push(new c.vector(a[g]).translate(e));return this._area=c.geometry.getPolygonArea(f),this._aabb=!1,d.done(this)},aabb:function(a){if(!a&&this._aabb)return c.aabb.clone(this._aabb);var b,d=c.scratchpad(),e=d.vector(),f=d.transform().setRotation(a||0),g=d.vector().set(1,0).r
- g.done()}}}),c.integrator("velocity-verlet-alt",function(a){return c.body.mixin({started:function(a){return void 0!==a&&(this._started=!0),!!this._started}}),{init:function(b){a.init.call(this,b)},integrateVelocities:function(a,b){for(var c,d=1-this.options.drag,e=null,f=0,g=a.length;g>f;++f)e=a[f],c=e.state,"static"!==e.treatment?(e.started()||(c.old.acc.clone(c.acc),c.old.acc.mult(b),c.old.vel.clone(c.vel).vsub(c.old.acc),c.old.acc.mult(1/b)),d&&c.vel.mult(d),c.vel.vadd(c.old.acc.vadd(c.acc).mult(.5*b)),e.started()||(c.old.angular.acc=c.angular.acc,c.old.angular.vel=c.angular.vel-c.old.angular.acc*b),c.angular.vel+=.5*(c.angular.acc+c.old.angular.acc)*b,c.angular.acc=0,e.started(!0)):(c.vel.zero(),c.acc.zero(),c.angular.vel=0,c.angular.acc=0)},integratePositions:function(a,b){for(var c,d=b*b,e=null,f=0,g=a.length;g>f;++f)e=a[f],c=e.state,"static"!==e.treatment&&(c.old.pos.clone(c.pos),c.old.vel.mult(b),c.old.acc.mult(.5*d),c.pos.vadd(c.old.vel).vadd(c.old.acc),c.old.vel.clone(c.vel),c.old.acc.clone(c.acc),c.acc.zero(),c.old.angular.pos=c.angular.pos,c.angular.pos+=c.angular.vel*b+.5*c.old.angular.acc*d,c.old.angular.vel=c.angular.vel,c.old.angular.acc=c.angular.acc,c.angular.acc=0)}}}),c.integrator("velocity-verlet",function(a){return c.body.mixin({started:function(a){return void 0!==a&&(this._started=!0),!!this._started}}),{init:function(b){a.init.call(this,b)},integrate:function(a,b){var c=this._world;return this.integratePositions(a,b),c&&c.emit("integrate:positions",{bodies:a,dt:b}),this.integrateVelocities(a,b),c&&c.emit("integrate:velocities",{bodies:a,dt:b}),this},integrateVelocities:function(a,b){for(var c,d=1-this.options.drag,e=null,f=0,g=a.length;g>f;++f)e=a[f],c=e.state,"static"===e.treatment||e.sleep()?(c.vel.zero(),c.acc.zero(),c.angular.vel=0,c.angular.acc=0):(d&&c.vel.mult(d),c.old.vel.clone(c.vel),c.vel.vadd(c.old.acc.vadd(c.acc).mult(.5*b)),c.old.acc.clone(c.acc),c.acc.zero(),c.old.angular.vel=c.angular.vel,c.old.angular.acc=c.angular.acc,c.angular.vel+=.5*(c.angular.acc+c.old.angular.acc)*b,c.angular.acc=0,e.started(!0))},integratePositions:function(a,b){for(var c,d=b*b,e=null,f=0,g=a.length;g>f;++f)e=a[f],c=e.state,"static"===e.treatment||e.sleep(b)||(e.started()||(c.old.acc.clone(c.acc),c.old.acc.mult(b),c.old.vel.clone(c.vel).vsub(c.old.acc),c.old.acc.mult(1/b)),c.old.pos.clone(c.pos),c.old.vel.mult(b),c.old.acc.mult(.5*d),c.pos.vadd(c.old.vel).vadd(c.old.acc),c.old.vel.mult(1/b),c.old.acc.mult(2/d),e.started()||(c.old.angular.acc=c.angular.acc,c.old.angular.vel=c.angular.vel-c.old.angular.acc*b),c.old.angular.pos=c.angular.pos,c.angular.pos+=c.angular.vel*b+.5*c.old.angular.acc*d)}}}),c.renderer("canvas",function(a){if(!b)return{};var d=2*Math.PI,e=function(a,c){var d=b.createElement(a||"div");return c&&(d.innerHTML=c),d},f={white:"#fff",violet:"#542437",blue:"#53777A"},g={metaEl:null,styles:{point:f.blue,circle:{strokeStyle:f.blue,lineWidth:1,fillStyle:f.blue,angleIndicator:f.white},rectangle:{strokeStyle:f.violet,lineWidth:1,fillStyle:f.violet,angleIndicator:f.white},"convex-polygon":{strokeStyle:f.violet,lineWidth:1,fillStyle:f.violet,angleIndicator:f.white}},offset:{x:0,y:0}};return{init:function(d){var f=this;if(a.init.call(this,d),this.options.defaults(g,!0),this.options.onChange(function(){f.options.offset=new c.vector(f.options.offset)}),this.options(d,!0),this.hiddenCanvas=b.createElement("canvas"),this.hiddenCanvas.width=this.hiddenCanvas.height=100,!this.hiddenCanvas.getContext)throw"Canvas not supported";this.hiddenCtx=this.hiddenCanvas.getContext("2d");var h=this.el;if("CANVAS"!==h.nodeName.toUpperCase()&&(h=b.createElement("canvas"),this.el.appendChild(h),"string"==typeof this.options.el&&this.el===b.body&&(h.id=this.options.el),this.el=h),this.container=this.el.parentNode,this.ctx=h.getContext("2d"),this.els={},this.options.meta){var i=this.options.metaEl||e();i.className="pjs-meta",this.els.fps=e("span"),this.els.ipf=e("span"),i.appendChild(e("span","fps: ")),i.appendChild(this.els.fps),i.appendChild(e("br")),i.appendChild(e("span","ipf: ")),i.appendChild(this.els.ipf),h.parentNo
|