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 & HelpSyntax Questions › flocking excample to XML
Page Index Toggle Pages: 1
flocking excample to XML (Read 402 times)
flocking excample to XML
Nov 16th, 2007, 11:41am
 
i'd like to work with the possisions of the boids in the flocking excample in an XML file! is this possible???

here's the file again:

Flock flock;

void setup() {
 size(500,500);
 colorMode(RGB,255,255,255,100);
 flock = new Flock();
 // Add an initial set of boids into the system
 for (int i = 0; i < 50; i++) {
   flock.addBoid(new Boid(new Vector3D(width/2,height/2),2.0f,0.05f));
 }
 smooth();
}

void draw() {

 background(100);
 flock.run();
}

// Add a new boid into the System
void mousePressed() {
 flock.addBoid(new Boid(new Vector3D(mouseX,mouseY),2.0f,0.05f));
}

class Flock {
 ArrayList boids; // An arraylist for all the boids

 Flock() {
   boids = new ArrayList(); // Initialize the arraylist
 }

 void run() {
   for (int i = 0; i < boids.size(); i++) {
     Boid b = (Boid) boids.get(i);  
     b.run(boids);  // Passing the entire list of boids to each boid individually
   }
 }

 void addBoid(Boid b) {
   boids.add(b);
 }

}


class Boid {

 Vector3D loc;
 Vector3D vel;
 Vector3D acc;
 float r;
 float maxforce;    // Maximum steering force
 float maxspeed;    // Maximum speed

 Boid(Vector3D l, float ms, float mf) {
   acc = new Vector3D(0,0);
   vel = new Vector3D(random(-1,1),random(-1,1));
   loc = l.copy();
   r = 2.0f;
   maxspeed = ms;
   maxforce = mf;
 }
 
 void run(ArrayList boids) {
   flock(boids);
   update();
   borders();
   render();
 }

 // We accumulate a new acceleration each time based on three rules
 void flock(ArrayList boids) {
   Vector3D sep = separate(boids);   // Separation
   Vector3D ali = align(boids);      // Alignment
   Vector3D coh = cohesion(boids);   // Cohesion
   // Arbitrarily weight these forces
   sep.mult(2.0f);
   ali.mult(1.0f);
   coh.mult(1.0f);
   // Add the force vectors to acceleration
   acc.add(sep);
   acc.add(ali);
   acc.add(coh);
 }
 
 // Method to update location
 void update() {
   // Update velocity
   vel.add(acc);
   // Limit speed
   vel.limit(maxspeed);
   loc.add(vel);
   // Reset accelertion to 0 each cycle
   acc.setXYZ(0,0,0);
 }

 void seek(Vector3D target) {
   acc.add(steer(target,false));
 }

 void arrive(Vector3D target) {
   acc.add(steer(target,true));
 }



...
...
...
...
...



how do i write the possision and ID of a boid in an XML-file???

thanks everybody! mias
Re: flocking excample to XML
Reply #1 - Nov 16th, 2007, 8:56pm
 
Hi. Each boid's location is defined by a Vector3D ('loc') : loc.x ; loc.y.

Iterate the boids list and print their location into an xml file :

Code:
PrintWriter xmlFile = createWrite("positions.xml");
xmlFile.println("<?xml version=\"1.0\"?>");
xmlFile.println("<boids>");
for (int i = 0; i < flock.boids.size(); i++) {
Boid b = (Boid)flock.boids.get(i);
xmlFile.println(" <boid id=\"" + i + "\" x=\"" + b.loc.x + "\" y=\"" + b.loc.y + "\" />");
}
xmlFile.println("</boids>");
xmlFile.flush();
xmlFile.close();
Page Index Toggle Pages: 1