Last active
July 17, 2016 14:21
-
-
Save nanu146/ca0533f6245b00ae6e6853461eb7f27c to your computer and use it in GitHub Desktop.
particle explosion with gravity
This file contains 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
function degreeToRadians(value){ | |
return (value/360)*2*Math.PI; | |
} |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title> | |
Test Demo | |
</title> | |
<style type="text/css"> | |
html, body { | |
margin: 0px; | |
} | |
canvas { | |
display: block; | |
} | |
</style> | |
<script type="text/javascript" src="geom_conversions.js"></script> | |
<script type="text/javascript" src="vector.js"></script> | |
<script type="text/javascript" src="particles.js"></script> | |
<script type="text/javascript"> | |
window.onload=function(){ | |
canvas=document.getElementById("canvas"); | |
context=canvas.getContext("2d"); | |
width=canvas.width=window.innerWidth; | |
height=canvas.height=window.innerHeight; | |
/*position=vector.create(100,500); | |
velocity=vector.create(0,0); | |
velocity.setLength(10); | |
velocity.setAngle(degreeToRadians(135)) | |
console.log(position)*/ | |
particles=[]; | |
numparticles=600; | |
for(i=0;i<numparticles;i++){ | |
particles.push(particle.create(width/2,height/2,(Math.random()*10)+1,Math.random()*Math.PI*2,vector.create(0,.3))) | |
} | |
update(); | |
function update(){ | |
context.clearRect(0,0,width,height); | |
/*velocity.setLength(velocity.getLength()); | |
position.addTo(velocity); | |
context.beginPath(); | |
context.arc(position.getX(),position.getY(),10,0,2*Math.PI,false); | |
context.fill();*/ | |
for (var i = 0; i < numparticles; i++) { | |
particles[i].update(); | |
/*acceleration=vector.create(0,0); | |
acceleration.setLength(1); | |
particles[i].accelerate(acceleration);*/ | |
context.beginPath(); | |
context.arc(particles[i].position.getX(),particles[i].position.getY(),3,0,2*Math.PI,false); | |
context.fill(); | |
} | |
requestAnimationFrame(update); | |
} | |
} | |
</script> | |
</head> | |
<body> | |
<canvas id="canvas"> | |
</canvas> | |
</body> | |
</html> |
This file contains 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
particle= | |
{ | |
velocity :null, | |
position : null, | |
gravity: null, | |
/// dummy constructor | |
create : function(x,y,speed,angle,gravity) | |
{ | |
var obj=Object.create(this); | |
obj.velocity=vector.create(0,0); | |
obj.velocity.setLength(speed); | |
obj.velocity.setAngle(angle); | |
obj.position=vector.create(x,y); | |
obj.gravity= !!gravity?gravity:vector.create(0,0); | |
return obj; | |
}, | |
accelerate: function(accel) | |
{ | |
this.velocity.setLength(this.velocity.getLength()+2) | |
}, | |
update: function(){ | |
this.velocity.addTo(this.gravity); | |
this.position.addTo(this.velocity); | |
} | |
} |
This file contains 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
vector= | |
{ | |
_x:0, | |
_y:0, | |
// dummy constructor | |
create : function(x,y){var obj= Object.create(this);obj._y=y; obj._x=x; return obj;}, | |
// member functions | |
getX : function(){ return this._x}, | |
getY : function(){ return this._y}, | |
setX : function(value){ this._x=value;}, | |
setY : function(value){ this._y=value;}, | |
getLength : function(){ return Math.sqrt(this._x*this._x + this._y*this._y)}, | |
getAngle : function(){ return Math.atan2(this._y,this._x) }, | |
setAngle : function(angle){ length=this.getLength(); this._y =Math.cos(angle)*length; this._x= Math.sin(angle)*length; }, | |
setLength: function(length){ angle=this.getAngle(); this._y=Math.cos(angle)*length; this._x=Math.sin(angle)*length; }, | |
add : function(v2){ vect = this.create(this._x+v2._x, this._y+v2._y); return vect; }, | |
subtract : function(v2){ vect = this.create(this._x-v2._x, this._y-v2._y); return vect; }, | |
multiply: function(value){ return vector.create(this._x*value,this._y*value)}, | |
divide: function(value){ return vector.create(this._x/value,this._y/value)}, | |
scale: function(value){ this._x=this._x*value; this._y=this._y*value;}, | |
addTo: function(v2){ this._x=this._x+v2._x; this._y=this._y+v2._y }, | |
subtractFrom: function(v2){ this._x=this._x-v2._x; this._y=this._y-v2._y } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment