java中的二维弹性碰撞问题

java中的二维弹性碰撞问题,java,collision,Java,Collision,我在计算两个球碰撞后的速度时遇到了问题。 我有一个函数,它可以得到java代码中两个球碰撞后的速度 { Ball a,b; double theta=Math.atan2(b.getY()-a.getY(),b.getX()-a.getX());//angle of rotation of axis //m1 and m2 are the mass of the two balls float m1=a.getMass();float m2=b.getMass(

我在计算两个球碰撞后的速度时遇到了问题。 我有一个函数,它可以得到java代码中两个球碰撞后的速度

   { Ball a,b;

    double theta=Math.atan2(b.getY()-a.getY(),b.getX()-a.getX());//angle of rotation of axis

    //m1 and m2 are the mass of the two balls
    float m1=a.getMass();float m2=b.getMass();




    ////////////////////////////////////////////////////////////////////////
    double V1angle=Math.atan2(a.getVelocityy(),a.getVelocityx());
    double V1=Math.abs(a.getVelocityx())*Math.abs(Math.cos(V1angle))+Math.abs(a.getVelocityy())*Math.abs(Math.sin(V1angle));//V1 is the velocity of ball a before collision
    double V2angle=Math.atan2(b.getVelocityy(),b.getVelocityx());
    double V2=Math.abs(b.getVelocityx())*Math.abs(Math.cos(V2angle))+Math.abs(b.getVelocityy())*Math.abs(Math.sin(V2angle));//V2 is the velocity of ball b before collision

    ////////////////////////////////////////////////////////////////////////

   //calculating the new x and y velocity in the new system of ball a and b

   double V1x=Math.cos(V1angle-theta)*V1;//V1x is the x velocity of ball a 
   double V2x=Math.cos(V2angle-theta)*V2;//V2x is the x velocity of ball b 




   double V1y=Math.sin(V1angle-theta)*V1;//V1y is the y velocity of ball a
   double V2y=Math.sin(V2angle-theta)*V2;//V2y is the y velocity of ball b




    ////////////////////////////////////////////////////////////////////////



     //calculating the final x and y velocity of the two balls after collision in the new system

    double U1x=((m1-m2)*V1x)/(m1+m2)+((2*m2))*V2x/(m1+m2);


    double U2x=((m2-m1)/(m1+m2))*V2x+((2*m1)/(m1+m2))*V1x;


    double U1y=((m1-m2)/(m1+m2))*V1y+((2*m2)/(float)  (m1+m2))*V2y;
    double U2y=((m2-m1)/(m1+m2))*V2y+(   (2*m1)/(m1+m2))*V1y;

     //////////////////////////////////////////////////////////////



    double U11x=U1x,U11y=U1y,U22x=U2x,U22y=U2y;


    //converting x and y velocity in the new rotated system back to the main system
    U1x=U11x*Math.cos(theta)+U11y*Math.cos(theta+Math.PI/2);
    U1y=U11x*Math.sin(theta)+U11y*Math.sin(theta+Math.PI/2);


    U2x=U22x*Math.cos(theta)+U22y*Math.cos(theta+Math.PI/2);
    U2y=U22x*Math.sin(theta)+U22y*Math.sin(theta+Math.PI/2);

   a.setVelocityy((float)U1y);
   b.setVelocityy((float)U2y);

    a.setVelocityx((float)U1x);
    b.setVelocityx((float)U2x);

  }
虽然代码是有效的,但碰撞后我得到的速度似乎是一维的,根据我在二维弹性碰撞中的理解,碰撞后球的运动应该是相互垂直的,我没有得到

例如,如果球1以speedx 10和speedy 0移动 用speedx-10和speedy-5进行两次移动 碰撞后我得到的速度是:一号球变成了speedx-10和speedy-5

假设两个球的质量相同,那么球2的速度为10,速度为零

任何有用的源代码的帮助或链接都会很好 线等回答
编辑:在代码中,U1x、U1y、U2x、U2y是碰撞后的x和y速度,我需要计算两个球的速度

plz告诉我哪些代码行你不理解,例如,这条代码行Math.cosV1angle theta*Math.absV1;,为什么在V1上调用Mathabs?我不认为V1是负的。另一个问题是,您有很多未注释的公式。理解它是可能的,但这需要一些时间,这可能会减少志愿者帮助你编写代码的意愿。如果你能检查一下,我只是编辑它。因为碰撞后球的运动应该相互垂直,我不认为这是真的。如果两个球完全面对面碰撞,它们将产生完全相同的方向。你是对的:我刚刚检查了弹性碰撞的教程,发现我的代码实际上可以工作: