Permalink
Browse files

Cleanup

  • Loading branch information...
jzaefferer committed Dec 9, 2011
1 parent 494dc68 commit 8dae2bb4871dab3855ce8289aee7d4218a604ed9
Showing with 26 additions and 514 deletions.
  1. +4 −4 application.css
  2. +4 −3 application.js
  3. +17 −6 bouncing.js
  4. +0 −6 index.html
  5. +0 −488 jquery.tmpl.js
  6. +0 −6 vector-based.html
  7. +1 −1 vector-based.js
View
@@ -1,10 +1,10 @@
-.bird {
+body {
+ overflow: hidden;
+}
+.dot {
position: absolute;
border-radius: 5px;
background: black;
width: 10px;
height: 10px;
-}
-body {
- overflow: hidden;
}
View
@@ -2,7 +2,7 @@
var idCounter = 0;
var Bird = function() {
this.id = idCounter++;
- this.output = $("#bird").tmpl(this).appendTo("body");
+ this.output = $("<div>").addClass("dot").appendTo("body");
this.speed = 1 + Math.random();
this.maxSpeed = this.maxSpeed + Math.random() * 10;
this.left = Math.random() * 1000;
@@ -34,7 +34,8 @@
var diff = (newAngle - oldAngle) / 10;
this.angle = oldAngle + diff;
//*/
- this.angle = newAngle;
+ // make it more interesting until it works properly
+ this.angle = newAngle + Math.PI / 2;
// TODO make acceleration dependent on distance to target, reduce speed when close to target
this.speed *= this.acceleration;
this.speed = Math.min(this.maxSpeed, this.speed);
@@ -55,7 +56,7 @@
$(function() {
var birds = [];
- for (var i = 0; i < 1; i++) {
+ for (var i = 0; i < 10; i++) {
birds.push(new Bird());
}
View
@@ -12,16 +12,18 @@ $(function() {
function Ball() {
this.position = new Point(200, 200);
- this.output = $("<div>").addClass("bird").appendTo("body");
+ this.output = $("<div>").addClass("dot").appendTo("body");
this.velocity = new Vector(-5, 0);
}
Ball.prototype = {
remove: function() {
this.output.remove();
},
move: function() {
-
+ // apply gravity
this.velocity = this.velocity.add(GRAVITY.scale(0.1));
+
+ // collision detection against world
if (this.position.y > world.y2) {
this.velocity.x2 = -this.velocity.x2 * FRICTION;
this.position.y = world.y2;
@@ -38,8 +40,12 @@ $(function() {
this.position.x = world.x2;
}
}
+
+ // update position
this.position.x += this.velocity.x1;
this.position.y += this.velocity.x2;
+
+ // render
this.output.css({
left: this.position.x,
top: this.position.y
@@ -49,29 +55,34 @@ $(function() {
var balls = [];
balls.push(new Ball());
+
+ // animation loop
setInterval(function() {
balls.forEach(function(ball) {
ball.move();
});
- }, 50);
+ }, 25);
+ // create new balls with velocity
var start;
$(document).mousedown(function(event) {
start = new Point(event.pageX, event.pageY);
}).mouseup(function(event) {
var end = new Point(event.pageX, event.pageY);
var ball = new Ball();
ball.position = end;
- ball.velocity = start.relative(end).scale(0.5);
+ ball.velocity = start.relative(end).scale(0.2);
+ ball.move();
balls.push(ball);
});
+
+ // clear on escape
$(document).keyup(function(event) {
- // clear on escape
if (event.keyCode === 27) {
balls.forEach(function(ball) {
ball.remove();
});
balls.splice(0, balls.length);
}
- })
+ });
});
View
@@ -2,17 +2,11 @@
<html>
<head>
<script src="jquery-1.4.4.js"></script>
- <script src="jquery.tmpl.js"></script>
<script src="application.js"></script>
<link rel="stylesheet" href="application.css"/>
<meta charset="utf-8" />
<title>Flocking Simulation</title>
</head>
<body>
- <script id="bird" type="text/x-jquery-tmpl">
- <div id="bird${id}" class="bird">
-
- </div>
- </script>
</body>
</html> ​
Oops, something went wrong.

0 comments on commit 8dae2bb

Please sign in to comment.