We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › bouncing spheres
Page Index Toggle Pages: 1
bouncing spheres (Read 976 times)
bouncing spheres
Apr 15th, 2010, 12:39am
 
Hello, i need to use sphere instead of ellipse in this program. I tryed to change the drawing function but i've got some problem cause the translate. Does anybody can help me to do it? thanks

/*Physic bouncing balls*/

int numSpot = 10;
boolean oneTouched = false;
Spot[] sp;

void setup(){
 size(400,400);
 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].w/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 w,h;
 color fillColor;
 float speedX, speedY;
 // acceleration force
 float gravity;
 // stop motion
 float damping, friction;
 boolean touched = false;


 // --> constructor
 Spot(){
   x = width/2;
   y = 30;
   w = 30;
   h = 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);
   ellipse(x,y,w,h);
 }

 void move(){
   x+=speedX;
   speedY+=gravity;
   y+=speedY;
   
   // Check display window edge collisions
 if (x > width-w/2){
   x = width-w/2;
   speedX*=-1;
 }
 else if (x < w/2){
   x = w/2;
   speedX*=-1;
 }
 else if (y > height-h/2){
   y = height-h/2;
   speedY*=-1;
   speedY*=damping;
   speedX*=friction;
 }
 else if (y < h/2){
   y  = 0;
   speedY *=-1;
 }
 }
}
Re: bouncing spheres
Reply #1 - Apr 15th, 2010, 1:13am
 
ok, i did it. I forgot to use pushMatrix and pop Matrix Roll Eyes
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!  Smiley
(hey, sorry if my english is not really good, but i hope you can understand what i said Roll Eyes )

/* 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;
 }
 }
}
Re: bouncing spheres
Reply #2 - Apr 15th, 2010, 2:51am
 
Lupiae wrote on Apr 15th, 2010, 1:13am:
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.

I am not a specialist of 3D, but I think that it is a all-or-nothing thing: if you have 3D objects, light acts on all of them, there is no "flat/2D" lines and background.
Although, I am not sure with OpenGL, even less in Processing, but with raytracing (eg. POV-Ray) you can play on the reflection factor of the texture. You can also have spotlights, illuminating less the surroundings.
And for simple pseudo-3D effect, you can just move an image of sphere instead of computing it...

Quote:
And also it could be interesting to make sphere's collision (now the sphere enter inside the other).

This one is simple and very common, just check the distance of two spheres.
Re: bouncing spheres
Reply #3 - Apr 15th, 2010, 4:01am
 
sorry, me too i'm not good with 3D (by the way, i'm not good at all  Undecided). Can you give me a little suggest how to check the distance?
Re: bouncing spheres
Reply #4 - Apr 15th, 2010, 4:46am
 
Actually, your sketch seems more 2D than 3D...
But well, circle collision just check that the distance between the center of the circles (computed with dist()) is equal or below the sum of the radii of the circles.

You should explore a bit the Learning section (or the examples provided with Processing), you will find stuff like Circle Collision (a bit advanced with energy handling), or Bouncy Bubbles.
Re: bouncing spheres
Reply #5 - Apr 16th, 2010, 6:24am
 

yeah, I know it's a forced 3D Smiley. But it's an experiment, I will improve better in future.
Thanks for your suggest, i'm understanding it's more complicated than I was thinking  Sad.
By the way the two links about learning are very useful! I will read and I will try to solve the problem. I will post the result!
Page Index Toggle Pages: 1