Permalink
Browse files

Gravitational mouse following. No actual flocking involved, yet.

  • Loading branch information...
1 parent d14a45d commit b1e2242de3caccd3e44a4cd5df5b5322b803f4a2 @jzaefferer committed Jan 4, 2011
Showing with 7,217 additions and 4 deletions.
  1. +37 −3 application.js
  2. +1 −1 index.html
  3. +7,179 −0 jquery-1.4.4.js
View
40 application.js
@@ -3,14 +3,32 @@
var Bird = function() {
this.id = idCounter++;
this.output = $("#bird").tmpl(this).appendTo("body");
+ this.speed = 1 + Math.random();
+ this.maxSpeed = 3 + Math.random() * 2;
+ this.left = Math.random() * 1000;
+ this.top = Math.random() * 1000;
};
Bird.prototype = {
left: 0,
top: 0,
+
+ targetLeft: 0,
+ targetTop: 0,
+
+ speed: 0.1,
+ acceleration: 1.05,
+ angle: 0,
+ maxSpeed: 3,
+
move: function() {
- this.left += 1;
- this.top += 1;
+ //console.log(this.targetLeft, this.targetTop)
+ this.angle = Math.atan2(this.targetTop - this.top, this.targetLeft - this.left) + (Math.PI - Math.PI / 2);
+ this.speed *= this.acceleration;
+ this.speed = Math.min(this.maxSpeed, this.speed);
+ this.left += this.speed * Math.cos(this.angle);
+ this.top += this.speed * Math.sin(this.angle);
},
+
render: function() {
this.output.css({
left: this.left,
@@ -24,13 +42,29 @@
$(function() {
var birds = [];
- for (var i = 0; i < 10; i++) {
+ for (var i = 0; i < 50; i++) {
birds.push(new Bird());
}
+
+ var mousePosition = {
+ left: 0,
+ top: 0
+ };
+
+ $(document).mousemove(function(event) {
+ mousePosition = {
+ left: event.pageX,
+ top: event.pageY
+ }
+ });
+
setInterval(function() {
birds.forEach(function(bird) {
+ bird.targetLeft = mousePosition.left;
+ bird.targetTop = mousePosition.top;
bird.move();
bird.render();
});
}, 50);
+
});
View
2 index.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
+ <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"/>
View
7,179 jquery-1.4.4.js
7,179 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit b1e2242

Please sign in to comment.