About basic toxiclibes spring system

edited December 2016 in Library Questions

Hi,

Here is a code i have written by learning Nature of code. bugs turn up as i try to modify a Spring system to be interactive. I try to use distance function to detect if the mouse click knots( any one in them) then drag it away. But the fact is once we run the code there are few of knots can be dragged away and if change is too dynamic the knots will overlap with each other causing some wired situation.In the image, huge bobbles are for the ease of checking if the mouse pressed inside one of them.(few of them are working)

is there any one can help to to fix it ? Thanks.

https://github.com/Zivcheung/processing questions/blob/master/question%20about%20basic%20toxiclibes%20spring%20system

or

https://github.com/Zivcheung/processing-questions2016-12-08 17_45_21-spring_cloth

Tagged:

Answers

  • Answer ✓

    @Tian===

    something like that???- if i understand well what you want....

            import toxi.physics2d.*;
            import toxi.physics2d.behaviors.*;
            import toxi.geom.*;
    
            VerletPhysics2D physics;
            Fabric cloth;
    
            void setup(){
              size(1000,800);
              physics=new VerletPhysics2D();
              physics.addBehavior(new GravityBehavior(new Vec2D(0,0.1)));
              cloth=new Fabric(500,50,10,10);
              //frameRate(120);
            }
    
            void draw(){
              background(255);
              physics.update();
              cloth.display();
              cloth.updateDragged(mouseX,mouseY);//problem area
    
    
              //text((int)frameRate,12,50);
            }
            //////////////problem area
            void mousePressed(){
              cloth.constrain(mouseX,mouseY); 
            }
            void mouseReleased(){
              cloth.release();
            }
            //////////////////////////
    
            class Particle extends VerletParticle2D{
              float r;
              boolean dragged=false;
              Vec2D offsite;
    
              Particle(Vec2D pos){
    
                super(pos);
                r=50;
                offsite=new Vec2D(0,0);
                //println("jexecute la classe");
              }
            ////////////////////////////////////////////////  problem area 
              void constrain(int x_,int y_){
    
               // float d=dist(x,x_,y,y_);
    
               float d=dist(x,y,x_,y_);//here my change
    
                if(d<r){
    
                  offsite.x=x-x_;
                  offsite.y=y-y_;
                  lock();
                  dragged=true;
                }
    
              }
    
              void updateDragged(int x_,int y_){
                if(dragged){
                 set(x_+offsite.x,y_+offsite.y);
                }
              }
    
              void release(){
                unlock();
                dragged=false;
              }
             ////////////////////////////////////////////////// 
    
              void display(){
                fill(100);
                stroke(0);
                ellipse(x,y,r,r);
              }
            }
    
            class Fabric{
    
              ArrayList<Particle> particles;
              int rows, coloms;
              float x,y;
    
    
              Fabric(float x_, float y_,int rows_,int coloms_){
                particles=new ArrayList<Particle>();
                rows=rows_;
                coloms=coloms_;
                x=x_;
                y=y_;
                float len=50f;
                float strength=0.4;
                for(int j=0; j<rows;j++){
                  for(int i=0;i<coloms;i++){
                    Particle newP=new Particle(new Vec2D(x+i*len-coloms*len/2,y+j*len));
                    particles.add(newP);
                    physics.addParticle(newP);
    
                    if(i>0){
                      Particle preX=particles.get((i-1)+coloms*j);
                      VerletSpring2D s1=new VerletSpring2D(newP,preX,len,strength);
                      physics.addSpring(s1);
                    }
                    if(j>0){
                      Particle preY=particles.get(i+coloms*(j-1));
                      VerletSpring2D s2=new VerletSpring2D(newP,preY,len,strength);
                      physics.addSpring(s2);
                    }
    
                  }
                }
    
                fixCornor();
              }
            ///////////// problem area 
              void constrain(int x_,int y_){
                for(Particle p:particles){
                  //println(p.constrain(x_,y_));
                  p.constrain(x_,y_); 
    
                }
              }
    
              void updateDragged(int x_,int y_){
                for(Particle p:particles){
                  if(p.dragged){
                    p.updateDragged(x_,y_);
                  }
                }
              }
    
              void release(){
                for(Particle p:particles){
                  if(p.dragged){
                    p.release();
                  }
                }
              }
            ///////////////////////      
    
    
              void display(){
                for(int j=0;j<rows;j++){
                  for(int i=0;i<coloms;i++){
                    Particle newP=particles.get(i+coloms*j);
                    newP.display();
                    stroke(0,130,230);
                    if(i !=0){
                      Particle preX=particles.get((i-1)+coloms*j);
                      line(newP.x,newP.y,preX.x,preX.y);
                    }
                    if(j!=0){
                      Particle preY=particles.get(i+coloms*(j-1));
                      line(newP.x,newP.y,preY.x,preY.y);
                    }
    
                  }
                }
              }
    
              void fixCornor(){//fonctionne
                Particle p0=particles.get(0);
                p0.lock();
                Particle p1=particles.get(coloms-1);
                p1.lock();  
              }
    
    
    
    
            }
    
  • @akenaton I spent an afternoon.... Thanks bro, i just nearly go insane... lol. btw, how did u post code on here ?

  • You copy you code, and then paste it here. Select you code, ctrl + o to indent, and leave a line above and below the code. It will get formatted like in akenaton's example.

Sign In or Register to comment.