Convert to cleaner OOP style.

This commit is contained in:
Jürg Lehni 2014-03-12 10:46:45 +01:00
parent 76c0f7dd12
commit cd985d8fb3

View file

@ -8,7 +8,7 @@
<script type="text/paperscript" canvas="canvas"> <script type="text/paperscript" canvas="canvas">
// kynd.info 2014 // kynd.info 2014
var Ball = function(r, p, v) { function Ball(r, p, v) {
this.radius = r; this.radius = r;
this.point = p; this.point = p;
this.vector = v; this.vector = v;
@ -37,86 +37,85 @@
} }
} }
Ball.prototype = {
iterate: function() {
this.checkWallCollision();
if (this.vector.length > this.maxVec)
this.vector.length = this.maxVec;
this.point += this.vector;
this.updateShape();
},
Ball.prototype.iterate = function() { checkWallCollision: function() {
this.checkWallCollision(); if (this.point.x < -this.radius) { this.point.x = view.size.width + this.radius;}
if (this.vector.length > this.maxVec) { if (this.point.x > view.size.width + this.radius) { this.point.x = -this.radius;}
this.vector.length = this.maxVec; if (this.point.y < -this.radius) { this.point.y = view.size.height + this.radius;}
} if (this.point.y > view.size.height + this.radius) { this.point.y = -this.radius;}
this.point += this.vector; },
this.updateShape();
}
Ball.prototype.checkWallCollision = function() { updateShape: function() {
if (this.point.x < -this.radius) { this.point.x = view.size.width + this.radius;} var segments = this.path.segments;
if (this.point.x > view.size.width + this.radius) { this.point.x = -this.radius;} for (var i = 0; i < this.numSegment; i ++) {
if (this.point.y < -this.radius) { this.point.y = view.size.height + this.radius;} segments[i].point = this.point + {
if (this.point.y > view.size.height + this.radius) { this.point.y = -this.radius;} angle: 360 / this.numSegment * i,
} length: this.boundOffset[i]
};
}
Ball.prototype.updateShape = function() { this.path.smooth();
var segments = this.path.segments; for (var i = 0; i < this.numSegment; i ++) {
for (var i = 0; i < this.numSegment; i ++) { if (this.boundOffset[i] < this.radius / 4) {
segments[i].point = this.point + { this.boundOffset[i] = this.radius / 4;
angle: 360 / this.numSegment * i, };
length: this.boundOffset[i] var next = (i + 1) % this.numSegment;
}; var prev = (i > 0) ? i - 1 : this.numSegment - 1;
} this.boundOffsetBuff[i] = this.boundOffset[i] += (this.radius - this.boundOffset[i]) / 15;
this.boundOffsetBuff[i] = this.boundOffset[i] += ((this.boundOffset[next] + this.boundOffset[prev]) / 2 - this.boundOffset[i]) / 3;
}
},
this.path.smooth(); react: function(b) {
for (var i = 0; i < this.numSegment; i ++) { var dist = this.point.getDistance(b.point);
if (this.boundOffset[i] < this.radius / 4) { if (dist < this.radius + b.radius && dist != 0) {
this.boundOffset[i] = this.radius / 4; var overlap = this.radius + b.radius - dist;
}; var direc = (this.point - b.point).normalize(overlap * 0.015);
var next = (i + 1) % this.numSegment; this.vector += direc;
var prev = (i > 0) ? i - 1 : this.numSegment - 1; b.vector -= direc;
this.boundOffsetBuff[i] = this.boundOffset[i] += (this.radius - this.boundOffset[i]) / 15;
this.boundOffsetBuff[i] = this.boundOffset[i] += ((this.boundOffset[next] + this.boundOffset[prev]) / 2 - this.boundOffset[i]) / 3;
}
}
Ball.prototype.react = function(b) { this.calcBounds(b);
var dist = this.point.getDistance(b.point); b.calcBounds(this);
if (dist < this.radius + b.radius && dist != 0) { this.updateBounds();
var overlap = this.radius + b.radius - dist; b.updateBounds();
var direc = (this.point - b.point).normalize(overlap * 0.015); }
this.vector += direc; },
b.vector -= direc;
this.calcBounds(b); getboundOffset: function(b) {
b.calcBounds(this); var diff = this.point - b;
this.updateBounds(); var angle = (diff.angle + 180) % 360;
b.updateBounds(); return this.boundOffset[Math.floor(angle / 360 * this.boundOffset.length)];
} },
}
Ball.prototype.getboundOffset = function(b) { calcBounds: function(b) {
var diff = this.point - b; for (var i = 0; i < this.numSegment; i ++) {
var angle = (diff.angle + 180) % 360; var tp = this.getSidePoint(i);
return this.boundOffset[Math.floor(angle / 360 * this.boundOffset.length)]; var bLen = b.getboundOffset(tp);
} var td = tp.getDistance(b.point);
if (td < bLen ) {
this.boundOffsetBuff[i] -= (bLen - td) / 2;
}
}
},
Ball.prototype.calcBounds = function(b) { getSidePoint: function(index) {
for (var i = 0; i < this.numSegment; i ++) { return this.point + this.sidePoints[index] * this.boundOffset[index];
var tp = this.getSidePoint(i); },
var bLen = b.getboundOffset(tp);
var td = tp.getDistance(b.point); updateBounds: function() {
if (td < bLen ) { for (var i = 0; i < this.numSegment; i ++) {
this.boundOffsetBuff[i] -= (bLen - td) / 2; this.boundOffset[i] = this.boundOffsetBuff[i];
} }
} }
} };
Ball.prototype.getSidePoint = function(index) {
return this.point + this.sidePoints[index] * this.boundOffset[index];
}
Ball.prototype.updateBounds = function() {
for (var i = 0; i < this.numSegment; i ++) {
this.boundOffset[i] = this.boundOffsetBuff[i];
}
}
//--------------------- main --------------------- //--------------------- main ---------------------