HELP with filling rects!
in
Programming Questions
•
6 months ago
I need the rects on the left of the screen to move on the X coordinate in accordance to the spring over them. Anyone know a simple way to do that? I'm very new so as simplistic answer would be great. I'm thinking of making 3 more rects under the 3 I already have that match the background (making them invis) then having the white rects move using a PVector that changes the length of them when the spings are over the invis rects.
int num = 6;
Spring[] springs = new Spring[num];
int pop[];
void setup()
{
size(700, 500);
noStroke();
springs[0] = new Spring(460, 410, 30, 0.98, 8.0, 0.08, springs, 0);
springs[1] = new Spring(460, 340, 30, 0.95, 9.0, 0.1, springs, 1);
springs[2] = new Spring(460, 270, 30, 0.90, 9.9, 0.1, springs, 2);
springs[3] = new Spring(460, 200, 30, 0.98, 8.0, 0.08, springs, 3);
springs[4] = new Spring(460, 120, 30, 0.98, 8.0, 0.08, springs, 4);
springs[5] = new Spring(460, 40, 30, 0.98, 8.0, 0.08, springs, 5);
}
void draw()
{
background(51);
for (int i = 0; i < num; i++) {
springs[i].update();
springs[i].display();
}
stroke(20);
fill(255,255,255);
rect(0,0,400,160);
rect(0,160,400,150);
rect(0,310,400,140);
}
void mousePressed()
{
for (int i = 0; i < num; i++) {
springs[i].pressed();
}
}
void mouseReleased()
{
for (int i=0; i<num; i++) {
springs[i].released();
}
}
class Spring
{
float xpos, ypos;
float tempxpos, tempypos;
int size = 20;
boolean over = false;
boolean move = false;
float mass;
float k = 0.2;
float damp;
float rest_posx;
float rest_posy;
float velx = 0.0;
float vely = 0.0;
float accel = 0;
float force = 0;
Spring[] friends;
int me;
Spring(float x, float y, int s, float d, float m,
float k_in, Spring[] others, int id)
{
xpos = tempxpos = x;
ypos = tempypos = y;
rest_posx = x;
rest_posy = y;
size = s;
damp = d;
mass = m;
k = k_in;
friends = others;
me = id;
}
void update()
{
if (move) {
rest_posy = mouseY;
rest_posx = mouseX;
}
force = -k * (tempypos - rest_posy);
accel = force / mass;
vely = damp * (vely + accel);
tempypos = tempypos + vely;
force = -k * (tempxpos - rest_posx);
accel = force / mass;
velx = damp * (velx + accel);
tempxpos = tempxpos + velx;
if ((overEvent() || move) && !otherOver() ) {
over = true;
} else {
over = false;
}
}
boolean overEvent() {
float disX = tempxpos - mouseX;
float disY = tempypos - mouseY;
if (sqrt(sq(disX) + sq(disY)) < size/2 ) {
return true;
} else {
return false;
}
}
boolean otherOver() {
for (int i=0; i<num; i++) {
if (i != me) {
if (friends[i].over == true) {
return true;
}
}
}
return false;
}
void display()
{
if (over) {
fill(153);
} else {
fill(255);
}
ellipse(tempxpos, tempypos, size, size);
}
void pressed()
{
if (over) {
move = true;
} else {
move = false;
}
}
void released()
{
move = false;
rest_posx = xpos;
rest_posy = ypos;
}
}
int num = 6;
Spring[] springs = new Spring[num];
int pop[];
void setup()
{
size(700, 500);
noStroke();
springs[0] = new Spring(460, 410, 30, 0.98, 8.0, 0.08, springs, 0);
springs[1] = new Spring(460, 340, 30, 0.95, 9.0, 0.1, springs, 1);
springs[2] = new Spring(460, 270, 30, 0.90, 9.9, 0.1, springs, 2);
springs[3] = new Spring(460, 200, 30, 0.98, 8.0, 0.08, springs, 3);
springs[4] = new Spring(460, 120, 30, 0.98, 8.0, 0.08, springs, 4);
springs[5] = new Spring(460, 40, 30, 0.98, 8.0, 0.08, springs, 5);
}
void draw()
{
background(51);
for (int i = 0; i < num; i++) {
springs[i].update();
springs[i].display();
}
stroke(20);
fill(255,255,255);
rect(0,0,400,160);
rect(0,160,400,150);
rect(0,310,400,140);
}
void mousePressed()
{
for (int i = 0; i < num; i++) {
springs[i].pressed();
}
}
void mouseReleased()
{
for (int i=0; i<num; i++) {
springs[i].released();
}
}
class Spring
{
float xpos, ypos;
float tempxpos, tempypos;
int size = 20;
boolean over = false;
boolean move = false;
float mass;
float k = 0.2;
float damp;
float rest_posx;
float rest_posy;
float velx = 0.0;
float vely = 0.0;
float accel = 0;
float force = 0;
Spring[] friends;
int me;
Spring(float x, float y, int s, float d, float m,
float k_in, Spring[] others, int id)
{
xpos = tempxpos = x;
ypos = tempypos = y;
rest_posx = x;
rest_posy = y;
size = s;
damp = d;
mass = m;
k = k_in;
friends = others;
me = id;
}
void update()
{
if (move) {
rest_posy = mouseY;
rest_posx = mouseX;
}
force = -k * (tempypos - rest_posy);
accel = force / mass;
vely = damp * (vely + accel);
tempypos = tempypos + vely;
force = -k * (tempxpos - rest_posx);
accel = force / mass;
velx = damp * (velx + accel);
tempxpos = tempxpos + velx;
if ((overEvent() || move) && !otherOver() ) {
over = true;
} else {
over = false;
}
}
boolean overEvent() {
float disX = tempxpos - mouseX;
float disY = tempypos - mouseY;
if (sqrt(sq(disX) + sq(disY)) < size/2 ) {
return true;
} else {
return false;
}
}
boolean otherOver() {
for (int i=0; i<num; i++) {
if (i != me) {
if (friends[i].over == true) {
return true;
}
}
}
return false;
}
void display()
{
if (over) {
fill(153);
} else {
fill(255);
}
ellipse(tempxpos, tempypos, size, size);
}
void pressed()
{
if (over) {
move = true;
} else {
move = false;
}
}
void released()
{
move = false;
rest_posx = xpos;
rest_posy = ypos;
}
}
1