avoid overlapping particles
in
Contributed Library Questions
•
6 months ago
I want to avoid overlapping particles like this:
Right now when adding particles (left mouse) i add 2 particles instead of 1. One is fixed at it's position (which i call anchor) and the other is not (which i call node). Then i make spring between the anchor and the node.
I make a repulsion for all the nodes.
This work's quite well (better then the image above).
However, i tried to get it working with zoom. So they only get pushed away by zooming (so most likely by zooming out).
It works a litle but not as good as i want to.
Also sometimes when you zoom very far and then zoom back the particles stay on top of each other no matter what.
Could someone help me?
- import traer.physics.*;
- ParticleSystem physics;
- float particleMass = 1.0;
- float zoom = 1;
- float d_strength = 0;
- void setup() {
- size(600, 600);
- smooth();
- frameRate(30);
- physics = new ParticleSystem();
- physics.setGravity(0.0);
- physics.setDrag(1.0);
- }
- void draw() {
- background(255);
- physics.tick();
- for(int i = 0; i < physics.numberOfSprings(); i++) {
- Spring s = physics.getSpring(i);
- Particle anchor = s.getOneEnd();
- Particle node = s.getTheOtherEnd();
- noStroke();
- fill(255, 0, 0, 100);
- ellipse(zoomX(node.position().x()), zoomY(node.position().y()), 15, 15);
- stroke(0);
- line(zoomX(node.position().x()), zoomY(node.position().y()), zoomX(anchor.position().x()), zoomY(anchor.position().y()));
- }
- fill(0);
- text("zoom: "+zoom, 20, 20);
- text("d_strength: "+d_strength, 20, 50);
- }
- float zoomX(float x) {
- return (x-width/2)*zoom+width/2;
- }
- float zoomY(float y) {
- return (y-height/2)*zoom+height/2;
- }
- float unzoomX(float x) {
- return (x - width/2) / zoom + width/2;
- }
- float unzoomY(float y) {
- return (y - height/2) / zoom + height/2;
- }
- void keyPressed() {
- if(key == '-') {
- zoom -= 0.1;
- setAttractionStrengthBasedOnZoom();
- }
- if (key == '=') {
- zoom += 0.1;
- setAttractionStrengthBasedOnZoom();
- }
- }
- void setAttractionStrengthBasedOnZoom() {
- for(int i = 0; i < physics.numberOfAttractions(); i++) {
- Attraction a = physics.getAttraction(i);
- float stength = map(zoom, 0, 1, -2000, -1000);
- stength = min(0, stength);
- a.setStrength(stength);
- d_strength = stength;
- }
- }
- void mousePressed() {
- Particle anchor = physics.makeParticle(particleMass, unzoomX(mouseX), unzoomY(mouseY), 0);
- anchor.makeFixed();
- Particle node = physics.makeParticle(particleMass, unzoomX(mouseX), unzoomY(mouseY), 0);
- // If they are strong they act like a stick. If they are weak they take a long time to return to their rest length
- float strength = 1;
- // If springs have high damping they don't overshoot and they settle down quickly, with low damping springs oscillate.
- float damping = 1;
- // the spring wants to be at this length and acts on the particles to push or pull them exactly this far apart at all times.
- float restLength = 0;
- physics.makeSpring(anchor, node, strength, damping, restLength);
- // make repulsion for new one with every node
- for(int i = 0; i < physics.numberOfSprings(); i++) {
- Spring s = physics.getSpring(i);
- Particle a = s.getTheOtherEnd();
- strength = -1000;
- // wtf does this do?
- // 0 and i dont see shit
- // 500 and they will overlap when i click on them
- float minimumDistance = 10;
- physics.makeAttraction(a, node, strength, minimumDistance);
- }
- // sloppy fast way to make sure new added one has same strength as others
- setAttractionStrengthBasedOnZoom();
- }
1