引力和碰撞

问题描述

我制作了一个简单的程序,用于模拟圆形物体在具有重力和碰撞的环境中与圆形物体表面的行为。

我的问题与重力的应用有关:每当一个物体非常靠近吸引子时,它就会开始重新获得高度,我认为导致问题的原因是,假设球以非常低的速度接触地面,我的程序应用重力,因为它会接触,反转力并再次将其发送到空中。

一旦球达到足够低的速度,我尝试停止球,但效果总体上令人不快(它永远不会慢到让它看起来不明显)

这是代码,你认为错误是什么?由于我只列出了部分代码并且相当复杂,我不希望得到具体的回应,但是您认为问题通常可能在哪里? 我的猜测是我不尊重动能/势能关系,但我也不知道如何使它正确:/

    void update(ArrayList<Attracter>a) {
    pos.add(acceleration);
    println(acceleration.mag());
    for (Attracter ar : a)
      if (PVector.dist(pos,ar.pos)<ar.size/2+size/2) { 
        
        //send the compenetrated body back

        float difference=((ar.size/2+size/2)-PVector.dist(pos,ar.pos)+1);
        pos.sub(acceleration.copy().normalize().mult(difference));  
        
        //calculate the new acceleration
        PVector perpendicular= PVector.sub(pos,ar.pos).normalize(); //perpendicolare
        float angle=perpendicular.rotate(-PI/2).heading();//anGolo dellatangente
        perpendicular.rotate(-angle); //normalizzo l'anGolo
        acceleration.rotate(-angle); //normalizzo l'accellerazione
        
        PVector newacceleration= PVector.fromAngle(perpendicular.heading()-acceleration.heading());
        acceleration=newacceleration.setMag(acceleration.mag());
        acceleration.rotate(angle); //denormalizzo l'accellerazione
          
        //push the body forward
        pos.add(acceleration.copy().normalize().mult(difference));
        acceleration.mult(0.9); 
        }
  }

解决方法

去掉+1中的difference calculation,即尝试替换

float difference=((ar.size/2+size/2)-PVector.dist(pos,ar.pos)+1);

float difference=((ar.size/2+size/2)-PVector.dist(pos,ar.pos));