Cover
 This is the archive Discourse for the Processing (ALPHA) software. Please visit the new Processing forum for current information.
 Processing 1.0 _ALPHA_    Programming Questions & Help    Programs (Moderators: fry, REAS)    beginnger: Creating Objects « Previous topic | Next topic »

 Pages: 1
 Author Topic: beginnger: Creating Objects  (Read 622 times)
roland_g

 beginnger: Creating Objects « on: Feb 15th, 2005, 10:15pm »

I am still pretty bad at programming and want to modify this simple bouncing ball programm so it automatically creates a new bouncing ball whenever a ball hits the wall.

Unfortunatly I don't quite know how to do this. How to I get the exact coordinates whenever the ball hits the wall and how do I assign a different speed to the dynamically created ball so all the balls don't overlap and move along the same course?

Like I said I am still learning, so please bear with me

Code:
 //--------------------------------------------------------- float size; float [] xspeed = new float[3]; float [] yspeed = new float[3]; float xpos, ypos; float xdirection = 1; float ydirection = 1;   //--------------------------------------------------------- void setup (){   size (500,500);   xpos = width/2;   ypos = height/2;   xspeed[0] = 0.5;   xspeed[1] = 2.8;   xspeed[2] = 1.9;   yspeed[0] = 3.1;   yspeed[1] = 9.3;   yspeed[2] = 10.4;     framerate(200); }   //--------------------------------------------------------- void loop (){   background(#280915);   noStroke();   fill(#940B35);   smooth();     //----- SPEED CONFIG   xpos = xpos +(xspeed[1] * xdirection);   ypos = ypos +(yspeed[0] * ydirection);     //----- CHANGE HORIZONTAL DIRECTION   if (xpos > width-size/2 || xpos < 1+size/2)   {     xdirection *=-1;   } //----- CHANGE VERTICAL DIRECTION   if (ypos > height-size/2 || ypos <1+size/2)   {     ydirection *=-1;   }   //----- SIZE CHANGE /*  if (xdirection < 1){   size = 70;  }   else{   size = 100;}*/   //----- DRAW ELLIPSE   ellipseMode(CENTER_DIAMETER);   ellipse(xpos, ypos,size,size); }
sspboyd

 Re: beginnger: Creating Objects « Reply #1 on: Feb 16th, 2005, 9:49pm »

Hi Roland,
Have you seen this tutorial?
http://p5.chronotext.org/tutorials/motion_oop/

You might find it helpful. It will introduce you to object oriented programming, and, coincidentally it uses an example program of moving several balls around.

gmail.com w/ sspboyd username
roland_g

 Re: beginnger: Creating Objects « Reply #2 on: Feb 23rd, 2005, 10:51am »

thanks!

I tried another object oriented programming tutorial earlier, but that didn't go over too well.

Hope I'll make it work this time

Code:
 Ball ball001; Ball ball002;     void setup (){   size (500,500);   framerate(30);   colorMode(RGB);   color tempcolor = color (10,90,255);   ball001 = new Ball(tempcolor,width/2,height/2,5,2.4);   ball001 = new Ball(tempcolor,width/2,height/2,2.9,3.6); }     void loop (){   background(#280915);   ball001.draw();   ball002.draw();   ball001.move();   ball002.move();   }       class Ball {   int size = 50; float xpos; float ypos; float xspeed; float yspeed; color c; int xdirection = 1; int ydirection = 1;   Ball (color c_, float xp, float yp,float xs, float ys){  c = c_;  xpos = xp;  ypos = yp;  xspeed = xs;  yspeed = ys;   }   void draw(){  ellipseMode(CENTER_DIAMETER);  noStroke();  fill (c);  smooth();  ellipse(xpos,ypos,size,size); }   void move(){ xpos = xpos +(xspeed * xdirection); ypos = ypos +(yspeed * ydirection); if (xpos > width-size/2 || xpos < 1+size/2){  xdirection *=-1;} if (ypos > height-size/2 || ypos <1+size/2){  ydirection *=-1;} } }
 « Last Edit: Feb 23rd, 2005, 10:52am by roland_g »
roland_g

 Re: beginnger: Creating Objects « Reply #3 on: Feb 23rd, 2005, 2:25pm »

it worked

Now this is my first time using classes, does anybody have some tips on how to optimize this code?.

thank you

Code:
 Ball[] ball; int objectnumber = 250;   void setup (){ background(#280915);   size (500,500);   framerate(30);   ball = new Ball[objectnumber];     for (int i= 0; i < objectnumber; i++){     ball[i] = new Ball(#940B35,random(50,500),random(10,400),random(1,8),random(1,7));   }   }   void loop (){  background(#280915);     for (int i = 0; i < objectnumber; i++){     ball[i].draw ();     ball[i].move ();   } }   class Ball {       float size= 5;   float xpos;   float ypos;   float xspeed;   float yspeed;   color c;   int xdirection = 1;   int ydirection = 1;     Ball (color c_,float xp, float yp,float xs, float ys){     c = c_;     xpos = xp;     ypos = yp;     xspeed = xs;     yspeed = ys;     }     void draw(){     ellipseMode(CENTER_DIAMETER);           colorMode(RGB);     smooth();     stroke(255,10);       //bezier(mouseX,mouseY,0,xpos,ypos,height/2,xpos,ypos);     //rect(mouseX,mouseY,10,10);     noStroke();           fill (255,40);     //push();     //translate(xpos,ypos);     //rotate(20);     ellipse(xpos,ypos,size+5,size+5);     //pop();     fill (c);           ellipse(xpos,ypos,size,size);                   }     void move(){     xpos = xpos +(xspeed * xdirection);     ypos = ypos +(yspeed * ydirection);     if (xpos > width-size/2 || xpos < 1+size/2){  xdirection *=-1;     }     if (ypos > height-size/2 || ypos <1+size/2){  ydirection *=-1;     }   } }
 « Last Edit: Feb 23rd, 2005, 6:37pm by roland_g »
 Pages: 1

 Forum Jump: ----------------------------- Discussion -----------------------------  - Community, Collaboration, Status   - Events, Publications, Propaganda   - General Processing Discussion ----------------------------- Programming Questions & Help -----------------------------  - Syntax => Programs   - Integration ----------------------------- Topics & Contributions -----------------------------  - Tools   - Responsive Form, Games   - Information Visualization   - Simulation, Artificial Life   - Tangible Computing   - Automated Systems   - Sound   - Video, Camera   - Beyond Categories ----------------------------- Suggestions -----------------------------  - Software Suggestions   - Website, Reference, Example Suggestions ----------------------------- Bugs -----------------------------  - Software Bugs   - Website, Reference, Example Bugs   - Bug Fixes, Implemented Suggestions ----------------------------- Teaching -----------------------------  - Course Blueprints   - Theory and Practice « Previous topic | Next topic »