Basic breathing... needs more refinement

This commit is contained in:
Amadeus Demarzi 2014-07-26 13:52:03 -07:00
parent 26d45774dc
commit 33be6008d2
5 changed files with 95 additions and 24 deletions

View File

@ -7,16 +7,13 @@ Engine.Point.Puller = function(id, x, y, shapeSize){
this.id = id; this.id = id;
this.shapeSize = shapeSize; this.shapeSize = shapeSize;
this.ref = new Vector(x, y);
this.ref = { this.pos = new Vector(
x: x, x * shapeSize.x,
y: y y * shapeSize.y
}; );
this.pos.x = x * shapeSize.x;
this.pos.y = y * shapeSize.y;
this.pos = Vector.coerce(this.pos);
this.home = this.pos.clone(); this.home = this.pos.clone();
this.accel = Vector.coerce(this.accel); this.accel = Vector.coerce(this.accel);
this.vel = Vector.coerce(this.vel); this.vel = Vector.coerce(this.vel);

View File

@ -3,14 +3,22 @@
Vector Vector
){ 'use strict'; ){ 'use strict';
Engine.Point = function(id, x, y, width, height){ Engine.Point = function(id, x, y, shapeSize){
this.id = id; this.id = id;
this.pos = new Vector(x, y);
this.shapeSize = shapeSize;
this.ref = new Vector(x, y);
this.pos = new Vector(
x * shapeSize.x,
y * shapeSize.y
);
this.target = this.pos.clone(); this.target = this.pos.clone();
this.pos.x = width / 2; this.pos.x = shapeSize.x / 2;
this.pos.y = height / 2; this.pos.y = shapeSize.y / 2;
this.accel = Vector.coerce(this.accel); this.accel = Vector.coerce(this.accel);
this.vel = Vector.coerce(this.vel); this.vel = Vector.coerce(this.vel);
this.stiffness = Engine.getRandomFloat(3, 6); this.stiffness = Engine.getRandomFloat(3, 6);
this.friction = Engine.getRandomFloat(0.15, 0.3); this.friction = Engine.getRandomFloat(0.15, 0.3);
@ -45,6 +53,27 @@ Engine.Point.prototype = {
y: 0 y: 0
}, },
updateBreathingPhysics: function(){
this.stiffness = 0.1;
this.friction = 0.05;
},
updateTarget: function(newSize){
var diff;
this.target.x = this.ref.x * newSize.x;
this.target.y = this.ref.y * newSize.y;
diff = Vector.sub(newSize, this.shapeSize).div(2);
this.target.sub(diff);
this.target.add({
x: Engine.getRandomFloat(-8, 8),
y: Engine.getRandomFloat(-8, 8)
});
},
update: function(engine){ update: function(engine){
var newAccel; var newAccel;

View File

@ -5,11 +5,12 @@
Vector Vector
){ ){
Engine.Shape = function(x, y, width, height, points, polygons, simple){ Engine.Shape = function(x, y, width, height, points, polygons){
var i, ref, point, poly; var i, ref, point, poly;
this.pos = new Vector(x, y); this.pos = new Vector(x, y);
this.size = new Vector(width, height); this.size = new Vector(width, height);
this.sizeRef = this.size.clone();
ref = {}; ref = {};
this.points = []; this.points = [];
@ -18,10 +19,9 @@ Engine.Shape = function(x, y, width, height, points, polygons, simple){
for (i = 0; i < points.length; i++) { for (i = 0; i < points.length; i++) {
point = new Point( point = new Point(
points[i].id, points[i].id,
points[i].x * this.size.x, points[i].x,
points[i].y * this.size.y, points[i].y,
this.size.x, this.size
this.size.y
); );
ref[point.id] = point; ref[point.id] = point;
this.points.push(point); this.points.push(point);
@ -33,17 +33,63 @@ Engine.Shape = function(x, y, width, height, points, polygons, simple){
ref[poly.points[0]], ref[poly.points[0]],
ref[poly.points[1]], ref[poly.points[1]],
ref[poly.points[2]], ref[poly.points[2]],
poly.color, poly.color
simple
)); ));
} }
}; };
Engine.Shape.prototype = { Engine.Shape.prototype = {
breathing: false,
breath: 0,
breathLength: 1,
breatheIn: false,
startBreathing: function(){
var p;
this.breathing = true;
this.breath = this.breathLength;
for (p = 0; p < this.points.length; p++) {
this.points[p].updateBreathingPhysics();
}
},
breathe: function(tick){
var p, scale, newSize;
this.breath += tick;
if (this.breath < this.breathLength) {
return;
}
if (this.breatheIn) {
scale = 1;
} else {
scale = 1.05;
}
this.breatheIn = !this.breatheIn;
newSize = Vector.mult(this.sizeRef, scale);
for (p = 0; p < this.points.length; p++) {
this.points[p].updateTarget(newSize);
}
this.breath = 0;
},
update: function(engine){ update: function(engine){
var p; var p;
if (this.breathing === true) {
this.breathe(engine.tick);
}
for (p = 0; p < this.points.length; p++) { for (p = 0; p < this.points.length; p++) {
this.points[p].update(engine); this.points[p].update(engine);
} }

View File

@ -6,8 +6,6 @@ Engine.Typewriter = function(element){
this.element = element; this.element = element;
this.content = this.element.textContent.split(''); this.content = this.element.textContent.split('');
this.element.innerHTML = ''; this.element.innerHTML = '';
console.dir(this);
}; };
Engine.Typewriter.prototype = { Engine.Typewriter.prototype = {

View File

@ -97,6 +97,7 @@ Engine = Base.extend({
.wait(1000) .wait(1000)
.then(function(){ .then(function(){
this.showGrid = true; this.showGrid = true;
this.logo.startBreathing();
}, this) }, this)
.wait(1000) .wait(1000)
.then(function(){ .then(function(){