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 & HelpOther Libraries › Adding Responsive Objects
Page Index Toggle Pages: 1
Adding Responsive Objects (Read 822 times)
Adding Responsive Objects
Apr 7th, 2010, 11:05pm
 
Hi,

I have only recently started learning processing. I am in the process of writing a code where particles react to the presence of objects.

Currently my entire code works if i use rectangles - the moment i attempt to add a triangle or a quad with the same properties as the rectangle it produces an error.

I have attached both my original rectangle and my 'triangle' script - i'm not too sure whether i am perhaps missing something?

Thanks,
Anna


The Problem Script:
//Declaring a global variable of type ArrayList
ArrayList particles;

//A "rectangle" will bounce particles
Rectangle blackhole1;
Rectangle blackhole3;
//A "triangle" will bounce particles
Triangle blackhole2;

int x1;
int y1;
int x2;
int y2;
int x3;
int y3;

void setup()
{
 size(800,800);
 x1 = 100;
 y1 = 20;
 x2 = 30;
 y2 = 400;
 x3 = 300;
 y3 = 200;
 
 blackhole1 = new Rectangle(50,50,100,25);
 //Specify triangle
 blackhole2 = new Triangle(100,20,30,400,300,200);
 blackhole3 = new Rectangle(400,500,100,50);
 particles = new ArrayList ();
 smooth();
}

void draw()
{
 background(255);

 //Displaying the rectangle
 stroke(0);
 fill(175);
 rect(blackhole1.x, blackhole1.y, blackhole1.width, blackhole1.height);
 //Add triangle variables
 triangle(blackhole2.x1, blackhole2.y1,blackhole2.x2, blackhole2.y2,blackhole2.x3, blackhole2.y3);
 rect(blackhole3.x, blackhole3.y, blackhole3.width, blackhole3.height);
 //MAKE THIS TRIANGLE
 //triangle(blackhole2.x1,blackhole2.y1, blackhole2.x2,blackhole2.y2,blackhole2.x3,blackhole2.y3);

 
 //Add a new particle at mouse location
 particles.add(new Particle(mouseX, mouseY));
 
 //Loop through all particles
 for (int i = particles.size()-1; i >= 0; i--)
 {
   Particle p = (Particle) particles.get(i);
   p.run();
   p.direction();
   p.display();
   
   if (blackhole1.contains(p.x,p.y))
   {
     p.bounce();
   }
   
   //Add triangle properties to bounce off
   if (blackhole2.contains(p.x1,p.y1,p.x2,p.y2,p.x3,p.y3))
   {
     p.bounce();
   }

   
   if (blackhole3.contains(p.x,p.y))
   {
     p.bounce();
   }

 }
}

//A simple Particle Class
class Particle
{
 float x;
 float y;
 float xspeed;
 float yspeed;
 float life;
 
 //Make the Particle
 Particle (float tempX, float tempY)
 {
   x = tempX;
   y = tempY;
   //Change angle of particle movement
   xspeed = random(-2,1);
   yspeed = random(-2,0);
   life = 255;
 }
 
 //Move
 void run()
 {
   x = x - xspeed;
   //Y at + when particles move down screen; and at - when they move up
   y = y + yspeed;
 }
 
 //Fall down
 void direction()
 {
   //yspeed for horizontal movement
   //yspeed = - 0.01;
   //xspeed for vertical movement
   xspeed = - 0.4;
 }
 
 //Make balls bounce of walls
 void bounce()
 {
   xspeed = (-xspeed/1.01);
   yspeed = (-yspeed/1.01);
 }
 
 //Show
 void display()
 {
   stroke(0);
   fill(255,0, life);
   ellipse(x,y,8,8);
 }
}





Original Script:

//Declaring a global variable of type ArrayList
ArrayList particles;

//A "rectangle" will bounce particles
Rectangle blackhole1;
Rectangle blackhole3;


void setup()
{
 size(800,800);
 blackhole1 = new Rectangle(50,50,100,25);
 blackhole3 = new Rectangle(400,500,100,50);
 particles = new ArrayList ();
 smooth();
}

void draw()
{
 background(255);

 //Displaying the rectangle
 stroke(0);
 fill(175);
 rect(blackhole1.x, blackhole1.y, blackhole1.width, blackhole1.height);
 rect(blackhole3.x, blackhole3.y, blackhole3.width, blackhole3.height);
 //MAKE THIS TRIANGLE
 //triangle(blackhole2.x1,blackhole2.y1, blackhole2.x2,blackhole2.y2,blackhole2.x3,blackhole2.y3);

 
 //Add a new particle at mouse location
 particles.add(new Particle(mouseX, mouseY));
 
 //Loop through all particles
 for (int i = particles.size()-1; i >= 0; i--)
 {
   Particle p = (Particle) particles.get(i);
   p.run();
   p.direction();
   p.display();
   
   if (blackhole1.contains(p.x,p.y))
   {
     p.bounce();
   }

   
   if (blackhole3.contains(p.x,p.y))
   {
     p.bounce();
   }

 }
}

//A simple Particle Class
class Particle
{
 float x;
 float y;
 float xspeed;
 float yspeed;
 float life;
 
 //Make the Particle
 Particle (float tempX, float tempY)
 {
   x = tempX;
   y = tempY;
   //Change angle of particle movement
   xspeed = random(-2,1);
   yspeed = random(-2,0);
   life = 255;
 }
 
 //Move
 void run()
 {
   x = x - xspeed;
   //Y at + when particles move down screen; and at - when they move up
   y = y + yspeed;
 }
 
 //Fall down
 void direction()
 {
   //yspeed for horizontal movement
   //yspeed = - 0.01;
   //xspeed for vertical movement
   xspeed = - 0.4;
 }
 
 //Make balls bounce of walls
 void bounce()
 {
   xspeed = (-xspeed/1.01);
   yspeed = (-yspeed/1.01);
 }
 
 //Show
 void display()
 {
   stroke(0);
   fill(255,0, life);
   ellipse(x,y,8,8);
 }
}
Re: Adding Responsive Objects
Reply #1 - Apr 8th, 2010, 1:31am
 
Ania88 wrote on Apr 7th, 2010, 11:05pm:
the moment i attempt to add a triangle or a quad with the same properties as the rectangle it produces an error.

You should copy/paste (or reproduce) the errors you get when you ask for help... We cannot always run shown programs.
Here, we don't even have Rectangle or Triangle classes.
Page Index Toggle Pages: 1