ok, i did it. I forgot to use pushMatrix and pop Matrix
But i need help to make it smarter, for example i want to give some light effects (because i want to put noStroke to make it less "heavy"). i tried but i don't understand how to not influence also the background and the connecting green lines. And also it could be interesting to make sphere's collision (now the sphere enter inside the other).
Any help it will be very appreciated!
(hey, sorry if my english is not really good, but i hope you can understand what i said
)
/* Physic Bouncing Sphere */
import processing.opengl.*;
int numSpot = 4;
boolean oneTouched = false;
Spot[] sp;
void setup(){
size(400,400,OPENGL);
//smooth();
sp = new Spot[numSpot];
for(int i=0;i< numSpot;i++){
sp[i] = new Spot();
}
}
void draw(){
background(14,36,48);
stroke(100,200,134);
strokeWeight(2);
for(int i=0;i< numSpot;i++){
for(int j=0;j< numSpot;j++){
line(sp[i].x,sp[i].y,sp[j].x,sp[j].y);
}
}
for(int i=0;i< numSpot;i++){
sp[i].display();
sp[i].move();
}
if(mousePressed==true){
for(int i=0;i< numSpot;i++){
if(sp[i].touched){
sp[i].x = mouseX;
sp[i].y = mouseY;
}
}
}
}
void mousePressed(){
for(int i=0;i< numSpot;i++){
float d = dist(mouseX,mouseY,sp[i].x,sp[i].y);
if(d < sp[i].r/2 && oneTouched==false){
sp[i].touched = true;
oneTouched = true;
}
}
if(oneTouched == false){
for(int i=0;i< numSpot;i++){
sp[i].x = mouseX;
sp[i].y = mouseY;
sp[i].speedY = random(-1,1);
sp[i].speedX = random(-2,2);
}
}
}
void mouseReleased(){
for(int i=0;i< numSpot;i++){
if(sp[i].touched){
sp[i].speedY = random(-1,1);
sp[i].speedX = random(-2,2);
}
sp[i].touched = false;
}
oneTouched = false;
}
/***************************************************************
CLASS
***************************************************************/
class Spot{
// --> VARS
float x,y;
float r;
color fillColor;
float speedX, speedY;
// acceleration force
float gravity;
// stop motion
float damping, friction;
boolean touched = false;
// --> constructor
Spot(){
x = width/2;
y = height/2;
r = 30;
speedY = random(-1,1);
speedX = random(-2,2);
gravity = 0.5;
damping = 0.8;
friction = 0.9;
fillColor = color(252,58,81);
}
// --> METHODS
void display(){
fill(fillColor);
//noStroke();
pushMatrix ();
translate (x,y);
sphere (r);
popMatrix();
}
void move(){
x+=speedX;
speedY+=gravity;
y+=speedY;
// Check display window edge collisions
if (x > width-r/2){
x = width-r/2;
speedX*=-1;
}
else if (x < r/2){
x = r/2;
speedX*=-1;
}
else if (y > height-r/2){
y = height-r/2;
speedY*=-1;
speedY*=damping;
speedX*=friction;
}
else if (y < r/2){
y = 0;
speedY *=-1;
}
}
}