Acceleration and gravity etc
by jonnyc
HTML
<html>
<head>
<title>Canvas</title>
<script type="text/javascript">
// When the window has loaded, DOM is ready. Run the draw() function.
</script>
</head>
<body>
<canvas id="myCanvas" width="400" height="400"></canvas>
</body>
</html>
CSS
#myCanvas{
background:black;
}
body{
overflow:hidden;
}
JavaScript
// Create an array to store our particles
var particles = [];
// The amount of particles to render
var particleCount = 99;
// The maximum velocity in each direction
var maxVelocity = 2;
// The target frames per second (how often do we want to update / redraw the scene)
var targetFPS = 33;
var collisions = true;
var gravity = false;
// Set the dimensions of the canvas as variables so they can be used.
var canvasWidth = 400;
var canvasHeight = 400;
function updateBounds() {
canvasHeight = $(window).height();
canvasWidth = $(window).width();
$('#myCanvas').attr("height", $(window).height());
$('#myCanvas').attr("width", $(window).width());
}
$(window).resize(updateBounds);
updateBounds();
var particleRadius = 20;
var constDistance = Math.pow((particleRadius * 2), 2);
var noCollisionDistance = particleRadius*2;
function Vector() {
this.setValues = function(x, y) {
this.x = x;
this.y = y;
};
this.x = 0;
this.y = 0;
this.z = 0;
this.reverse = function() {
this.x = -this.x;
this.y = -this.y;
};
this.sumComponenetParts = function() {
return Math.pow(this.x, 2) + Math.pow(this.y, 2);
};
this.magnitude = function() {
return Math.sqrt(this.sumComponenetParts());
};
}
var vectorStatic = {
add: function(v1, v2) {
var result = new Vector();
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
return result;
},
subtract: function(v1, v2) {
var result = new Vector();
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
return result;
},
divide: function(v, d) {
var result = new Vector();
result.x = v.x / d;
result.y = v.y / d;
return result;
},
multiply: function(v, d) {
var result = new Vector();
result.x = v.x * d;
result.y = v.y * d;
return result;
},
crossProduct: function(v1, v2){
var result =...