xxxxxxxxxx
var num = 0;
var springs = [];
var outline;
function setup() {
createCanvas(250, 500);
noStroke();
var o0 = createVector(width / 2, 50);
var o1 = createVector(width - 30, 75);
var o2 = createVector(width - 20, 150);
var o3 = createVector(width - 80, height - 100);
var o4 = createVector(width - 80, height - 50);
var o5 = createVector(width / 2, height - 30);
var o6 = createVector(80, height - 50);
var o7 = createVector(80, height - 100);
var o8 = createVector(20, 150);
var o9 = createVector(30, 75);
outline = [o7, o8, o9, o0, o1, o2, o3, o4, o5, o6];
springs[0] = new Spring(width / 2, outline[1].y, 20, 0.98, 8.0, 0.1, springs, 0);
springs[0].velx = 0.8;
springs[0].vely = 0.7;
}
function draw() {
background(255);
var ax = accelerationX;
var ay = accelerationY;
text(ax, 20, 20);
// lines
stroke(50);
strokeWeight(3);
for (var i = 0; i < outline.length - 1; i++) {
line(outline[i].x, outline[i].y, outline[i + 1].x, outline[i + 1].y);
}
line(outline[outline.length - 1].x, outline[outline.length - 1].y, outline[0].x, outline[0].y);
line(outline[6].x, outline[6].y, outline[0].x, outline[0].y);
line(outline[6].x, outline[6].y, outline[9].x, outline[9].y);
// dots
fill(50);
for (i = 0; i < outline.length; i++) {
ellipse(outline[i].x, outline[i].y, 10, 10);
}
for (i = 0; i < 7; i++) {
line(springs[0].x_pos, springs[0].y_pos, outline[i].x, outline[i].y);
}
noStroke();
fill(255, 0, 0);
if (springs.length > 0) {
for (var i = 0; i < springs.length; i++) {
//springs[i].velx = ax* 10;
// springs[i].vely = ay;
springs[i].update();
springs[i].display();
}
}
}
function mousePressed() {
for (var i = 0; i < springs.length; i++) {
springs[i].pressed();
}
}
function mouseReleased() {
for (var i = 0; i < springs.length; i++) {
springs[i].released();
}
}
// Spring class
function Spring(_x, _y, _s, _d, _m, _k_in, _others, _id) {
// Screen values
// this.xpos = _x;
// this.ypos = _y;
this.x_pos = _x;
this.y_pos = _y;
this.x_org = _x;
this.y_org = _y;
this.size = 20;
this.size = _s;
this.over = false;
this.move = false;
// Spring simulation constants
this.mass = _m; // Mass
this.k = 0.2; // Spring constant
this.k = _k_in;
this.damp = _d; // Damping
this.rest_posx = _x; // Rest position X
this.rest_posy = _y; // Rest position Y
// Spring simulation variables
//float pos = 20.0; // Position
this.velx = 0.0; // X Velocity
this.vely = 0.0; // Y Velocity
this.accel = 0; // Acceleration
this.force = 0; // Force
this.friends = _others;
this.id = _id;
this.update = function() {
num = this.friends.length;
if (this.move) {
this.rest_posy = mouseY;
this.rest_posx = mouseX;
}
this.force = -this.k * (this.y_pos - this.rest_posy); // f=-ky
this.accel = this.force / this.mass; // Set the acceleration, f=ma == a=f/m
this.vely = this.damp * (this.vely + this.accel); // Set the velocity
this.y_pos = this.y_pos + this.vely; // Updated position
this.force = -this.k * (this.x_pos - this.rest_posx); // f=-ky
this.accel = this.force / this.mass; // Set the acceleration, f=ma == a=f/m
this.velx = this.damp * (this.velx + this.accel); // Set the velocity
this.x_pos = this.x_pos + this.velx; // Updated position
if ((this.overEvent() || this.move) && !(this.otherOver())) {
this.over = true;
} else {
this.over = false;
}
}
// Test to see if mouse is over this spring
this.overEvent = function() {
var disX = this.x_pos - mouseX;
var disY = this.y_pos - mouseY;
var dis = createVector(disX, disY);
if (dis.mag() < this.size / 2) {
return true;
} else {
return false;
}
}
// Make sure no other springs are active
this.otherOver = function() {
for (var i = 0; i < num; i++) {
if (i != this.id) {
if (this.friends[i].over == true) {
return true;
}
}
}
return false;
}
this.display = function() {
if (this.over) {
fill(153);
} else {
//fill(255);
}
ellipse(this.x_pos, this.y_pos, this.size, this.size);
}
this.pressed = function() {
if (this.over) {
this.move = true;
} else {
this.move = false;
}
}
this.released = function() {
this.move = false;
this.rest_posx = this.x_org;
this.rest_posy = this.y_org;
}
};