jabauer
YaBB Newbies
Offline
Posts: 18
Re: Avoid overlap in VerletParticles in toxiclibs
Reply #2 - Mar 14th , 2010, 8:48pm
@toxi Many thanks for the quick reply. I had seen Shiffman's code and was adapting it for my project. I tried the MinDistance spring as you suggested, but it didn't keep the nodes from overlapping -- maybe I'm doing something wrong. I've attached the code (I switched back to the VerletSpring2D because with MinDistance the nodes settle too fast to see the physics behind the springs). If you have a moment, could you run the program? On most runs the graph settles with one of the nodes obscured. I've commented out my partial attempt to surround each node with a CircularConstraint. I wasn't sure if the constraint would move with the node. Many, many thanks for your time. import toxi.geom.*; import toxi.physics2d.*; import toxi.physics2d.constraints.*; //reference to the physics world VerletPhysics2D physics; ArrayList particles; ArrayList springs; HashMap particleTable = new HashMap(); Particle rolloverItem; PFont f; void setup( ) { size(400, 400); smooth( ); frameRate(30); f = createFont("GilSans",10, true); particles = new ArrayList(); springs = new ArrayList(); //Initialize the physics physics = new VerletPhysics2D( ); physics.setGravity(new Vec2D(0, 0.5)); //This is the center of the world Vec2D center = new Vec2D(width/2, height/2); //these are the world's dimensions (33%, a vector point out from the center in both directions) Vec2D extent = new Vec2D(width/3, height/3); //Set the world's bounding box physics.setWorldBounds(Rect.fromCenterExtent(center, extent)); loadData(); } void loadData( ) { createParticle("John", "husband", "2"); createParticle("Abigail", "wife", "1"); createParticle("John Quincy", "husband", "3"); createParticle("Louisa Catherine", "wife", "4"); createSpring("1","2"); createSpring("1","3"); createSpring("2","3"); createSpring("3","4"); } void draw( ) { //update the physics world physics.update( ); background(255); stroke(0); rolloverItem = null; //draw a line between each particle that is connected by a spring for (int i = 0; i < springs.size(); i++) { VerletSpring2D s = (VerletSpring2D) springs.get(i); line(s.a.x, s.a.y, s.b.x, s.b.y); } //display all particles for (int i = 0; i < particles.size(); i++) { Particle p = (Particle) particles.get(i); p.display(); } /* if (rolloverItem !=null) { rolloverItem.drawName( ); } if (mousePressed) { p1.drawName( ); } */ } Particle createParticle(String name, String type, String id) { Particle p = new Particle(Vec2D.randomVector(), 1, name, type, id); particles.add(p); particleTable.put(id, p); physics.addParticle(p); return p; } Particle findParticle(String id) { Particle p = (Particle) particleTable.get(id); if (p == null) { return createParticle("bad id", "dummy", id); } return p; } /* CircularConstraint createConstraint(String id) { Particle p = findParticle(id); CircularConstraint c = new CircularConstraint(p.pos, 30); return c; } */ VerletSpring2D createSpring(String id1, String id2) { Particle p1 = findParticle(id1); Particle p2 = findParticle(id2); VerletSpring2D s = new VerletSpring2D(p1, p2, 100, 0.01); springs.add(s); physics.addSpring(s); return s; } class Particle extends VerletParticle2D { String name; String type; String id; float w, h; Particle(Vec2D pos, float g, String name, String type, String id) { super(pos, g); //g for gravity this.name = name; this.type = type; this.id = id; } void display( ) { if (type == "husband") { fill(#009430); //green } else { fill(#e86a10); //orange } //fill(175); noStroke(); textFont(f); float w = textWidth(name) + 15; float h = textAscent( ) + textDescent( ) + 10; /* if (w > h) { r = w; } else { r = h; } */ ellipse(x, y, w, h); fill(0); textAlign(CENTER, CENTER); textFont(f); text(name, x, y); if (mouseInside()) { rolloverItem = this; } } void drawName( ) { fill(0); textAlign(CENTER, CENTER); textFont(f); text(name, x, y); } boolean mouseInside( ) { return (mouseX > x - w && mouseX < x + w && mouseY > y - h && mouseY < y + h); } }