Print

Print


I am confused how the following code from Tutorial3 accomplishes the task of
instantiating an assortment of particle agents:

public void start()
        {
        super.start();
        trails = new DoubleGrid2D(gridWidth, gridHeight);
        particles = new SparseGrid2D(gridWidth, gridHeight);

        Particle p;

        for(int i=0 ; i<numParticles ; i++)
            {
            p = new Particle(random.nextInt(3) - 1, random.nextInt(3) - 1);
schedule.scheduleRepeating(p);
            particles.setObjectLocation(p,
                                        new
Int2D(random.nextInt(gridWidth),random.nextInt(gridHeight)));

            }

It would appear that we want to a create a collection of particles, but in
fact we keep creating a single instance of the particle class (or at least
multiple objects with the same reference "p." If we wanted to have the
particles interact with each other (bounce off of each other), how could we
differentiate between the particle objects, since they all have the same
reference?

Thanks

mike

Michael Makowsky
Graduate Student
Department of Economics, 3G4
George Mason University
Fairfax, VA 22030
703.608.1366
[log in to unmask]
homepage http://mason.gmu.edu/~mmakowsk