Created
July 26, 2011 21:21
-
-
Save Olical/1108086 to your computer and use it in GitHub Desktop.
A pre release of my JavaScript physics engine, I will release it when I am happy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Physics MooTools plugin v1.0.0 pre release | |
| Licenced under GPL v3 (see README.md) | |
| Copyright 2011 - Oliver Caldwell (olivercaldwell.co.uk) | |
| */ | |
| var Physics={World:new Class({Implements:[Events,Options],options:{fps:16,width:500,height:500},initialize:function(e){e&&this.setOptions(e);this.interval=!1;this.clear=function(){var a=null,b=null;this.particles=[];this.positions=[];for(a=0;a<this.options.width;a+=1)for(b=0;b<this.options.height;b+=1)this.positions[a]=[],this.positions[a][b]=!1;return this};this.start=function(){this.interval===!1?(this.interval=setInterval(this.step,1E3/this.options.fps),this.fireEvent("start")):this.fireEvent("alreadyRunning"); | |
| return this}.bind(this);this.stop=function(){this.interval!==!1?(clearInterval(this.interval),this.interval=!1,this.fireEvent("stop")):this.fireEvent("notRunning");return this}.bind(this);this.moveParticle=function(a,b){this.positions[a.options.position.x][a.options.position.y]=!1;this.positions[b.x][b.y]=a;a.options.position=b;return this}.bind(this);this.applyFriction=function(a,b){a.options.velocity[b]!==0&&(a.options.velocity[b]-=a.options.velocity[b]/a.options.weight);a.options.velocity[b]>-0.01&& | |
| a.options.velocity[b]<0.01&&(a.options.velocity[b]=0);return this}.bind(this);this.slideParticle=function(a,b){var e=a.options.position,g={x:b.x-e.x,y:b.y-e.y},j=g.x.abs()>=g.y.abs()?g.x.abs():g.y.abs(),g={x:g.x/j,y:g.y/j},c={},h={},d=null,f=null;for(i=1;i<=j;i+=1)if(c.x=(e.x+g.x*i).floor(),c.y=(e.y+g.y*i).floor(),c.x>=0&&c.y>=0&&c.x<this.options.width&&c.y<this.options.height)if(this.positions[c.x][c.y]){h.particle=!0;break}else d={x:c.x,y:c.y};else{h.wall=!0;break}if(d){this.moveParticle(a,d);if(h.particle){f= | |
| this.positions[c.x][c.y];if(f.options.position.x<d.x||f.options.position.x>d.x)f.options.locked||(f.options.velocity.x+=a.options.velocity.x),a.options.velocity.x=0;if(f.options.position.y<d.y||f.options.position.y>d.y)f.options.locked||(f.options.velocity.y+=a.options.velocity.y),a.options.velocity.y=0;this.fireEvent("collision",[a,f])}if(h.wall){if(d.x===0||d.x===this.options.width-1)a.options.velocity.x=0;if(d.y===0||d.y===this.options.height-1)a.options.velocity.y=0;this.fireEvent("wallCollision", | |
| [a])}}return this}.bind(this);this.step=function(){this.particles.each(function(a){a.options.locked||(a.options.gas?a.options.velocity.y-=a.options.weight:a.options.velocity.y+=a.options.weight,this.applyFriction(a,"x"),this.applyFriction(a,"y"),this.slideParticle(a,{x:(a.options.position.x+a.options.velocity.x).floor(),y:(a.options.position.y+a.options.velocity.y).floor()}))}.bind(this));this.fireEvent("step",[this.particles]);return this}.bind(this);this.addParticle=function(a,b){!this.positions[a.options.position.x][a.options.position.y]|| | |
| b?(this.particles.push(a),this.positions[a.options.position.x][a.options.position.y]=a,this.fireEvent("addParticle",[a])):this.fireEvent("inUse",[a,this.positions[a.options.position.x][a.options.position.y]]);return this}.bind(this);this.removeParticle=function(a){var b=this.particles.indexOf(a);b!==-1?(this.particles.splice(b,1),this.positions[a.options.posision.x][a.options.posision.y]=!1,this.fireEvent("removeParticle",[a])):this.fireEvent("noParticle",[a]);return this}.bind(this);this.clear(); | |
| this.start()}}),Particle:new Class({Implements:[Options],options:{position:{x:0,y:0},velocity:{x:0,y:0},weight:3,gas:!1,locked:!1},initialize:function(e){e&&this.setOptions(e)}})}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment