段和箱子碰撞响应

时间:2011-04-09 13:28:23

标签: java 3d physics segment plane

我正在Processing中开发一个基于Verlet的模拟引擎(类似于java)。我有一些麻烦,2个粒子通过一个棒约束连接并用一个盒子进行整理。我有一些代码可以检测到崩溃,但我不知道如何做一些真实的反应...

这是我的代码:

public void colisionStickCamilla(){
    //Point of the box
      PVector vo = new PVector(width/2,height,0);

      for(int i = 0;i<constraints.size();i++){
       Stick st = (Stick) constraints.get(i);
       Sphere s1 = st.gets1();
       Sphere s2 = st.gets2();
       //Particle's psoitions
       PVector v1 = s1.getVector();
       PVector v2 = s2.getVector();

       //Intersection with the upper part of the box
       PVector inter = interseccionPlano(new PVector(width/2,height+50,0),new PVector(0,-1,0),v1,v2);

       //Restricting to the limited plane
       if(inter.x > width/2-50 && inter.x < width/2+50  && inter.dist(new PVector(-1,-1,-1))!= 0){
         //Do some response to colision
         println("colision");

        float dist1 = v1.dist(inter);
        float dist2 = v2.dist(inter);
        if(dist1<dist2){
        //Previous position
           PVector posAnt = s1.getPosicionAnterior();
           PVector mov = v1.get();
           mov.sub(posAnt);
           mov.normalize();
           v1.sub(mov);
           s1.setPosicionAnterior(v1);
        }
        else{
          PVector posAnt = s2.getPosicionAnterior();
          PVector mov = v2.get();
          mov.sub(posAnt);
          mov.normalize();
          v2.sub(mov);
          s2.setPosicionAnterior(v2);
        }
}

}  
}

0 个答案:

没有答案