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 › Unknown Number of Particles
Page Index Toggle Pages: 1
Unknown Number of Particles (Read 1037 times)
Unknown Number of Particles
Dec 31st, 2005, 6:52pm
 
Hi,

I'm creating an example of rain. As such I have a number of 'taps' which drip a droplet of rain. The dripRate can be changed on the fly.

I've been looking at examples as much as possible, but everything seems to have a predefined number of drops, and that's confusing me.

I'm used to Actionscript, where I can just create an object and let it fall and delete itself. I think that mindset is causing me trouble though.

Can anyone point me to a good example?

Thanks,
Re: Unknown Number of Particles
Reply #1 - Jan 1st, 2006, 4:12pm
 
Just something i whipped up for you...
It can add as many particles as you like, but it runs through all the particles (and skips drawing the ones outside the screen)..

Press space to add a droplet..


Code:

// Dynamic Rain - Simple

float speed = 0.61;
RainSystem t;

void setup()
{
size(200,200);
smooth();
t = new RainSystem();
}

void draw()
{
background(0);
t.draw();
}

class Drop
{
float x, y;
boolean alive;
Drop(float X, float Y)
{
alive = true;
x = X;
y = Y;
}

void update()
{
y += speed;
if(x < 0 || x >= width || y < 0 || y >= height) alive = false;
}

void draw()
{
update();
noStroke();
fill(255);
ellipse(x,y,2,5);
}
}

class RainSystem
{
int n = 0;
Drop[] drops;
RainSystem()
{
drops = new Drop[n];
addDrop(random(width),0);
}

void addDrop(float X, float Y)
{
Drop[] tmp = new Drop[n+1];
arraycopy(drops,0,tmp,0,n);
drops = tmp;
drops[n++] = new Drop(X,Y);
}

void draw()
{
for(int i = 0; i < drops.length; i++)
if(drops[i].alive) drops[i].draw();
}
}

void keyPressed()
{
if(key==' ') t.addDrop(random(width-1),0);
}



-seltar
Re: Unknown Number of Particles
Reply #2 - Jan 3rd, 2006, 5:14am
 
You can also use Vector documented at
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html

For example...

Vector list;
list = Vector();

//list is now a dynamic array. Technically it is a linked list.

list.add(myObject);

//now list.get(0) will return myObject, but you need to cast it...

myObjectType temp = (myObjectType) list.get(0);

You can .add() as many as you want, or remove, etc.
Re: Unknown Number of Particles
Reply #3 - Jan 3rd, 2006, 11:36pm
 
Sometimes it's easier to convert the whole Vector to an array if you're going to be looping through it over and over (and it doesn't change)

You can do it like this:

Code:
myObjectType arr[] = new myObjectType[list.size()];
list.toArray(arr);
for(int i=arr.length; --i>=0;) // do something with arr[i]

hth
Re: Unknown Number of Particles
Reply #4 - Jan 4th, 2006, 6:34pm
 
arrays will also be much faster than vectors (usually 2x), especially when used heavily in draw() or that sort of thing.
Page Index Toggle Pages: 1