JSFiddle - React, Tailwind, and code Playground
by Darby Rathbone
JavaScript
var Vector = (function() {
function Vector(x, y) {
this.x = x;
this.y = y;
this.prevmag = 1
};
return Vector
})();
Vector.prototype = {
toString: function() {
return "[" + this.x + "," + this.y + "]"
},
mag: function() {
return Math.sqrt((this.x * this.x) + (this.y * this.y))
},
mult: function(k) {
this.x = this.x * k;
this.y = this.y * k;
return this.toString()
},
div: function(k) {
this.x = (this.x) / k;
this.y = (this.y) / k;
return this.toString()
},
norm: function() {
this.prevmag = this.mag();
this.div((this.mag()));
return this.toString()
},
add: function(vk) {
this.x = this.x + vk.x;
this.y = this.y + vk.y;
return this.toString()
},
sub: function(vk) {
this.x = this.x - vk.x;
this.y = this.y - vk.y;
return this.toString()
},
direction: function() {
return Math.atan2(this.y, this.x)
},
dot: function(v1) {
return (v1.x * this.x) + (v1.y * this.y)
}
};
var Position = (function() {
function Position(x, y) {
this.x = x;
this.y = y;
this.velocity = new Vector(0, 0);
this.xMax = 100;
this.yMax = 100;
this.radius = 10;
this.accel = new Vector(0, 0)
};
return Position
})(),
Spring = (function() {
function Spring(k, d, len) {
this.end1 = null;
this.end2 = null;
this.k = k;
this.d = d;
this.len = len
};
return Spring
})();
Spring.prototype = {
force: function() {
return new Vector(this.k * (this.end1.x - this.end2.x - this.len), this.k * (this.end1.y - this.end2.y - this.len))
},
damper: function() {
return (new...