Ania88
YaBB Newbies
Offline
Posts: 1
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, AnnaThe 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); } }