java中2D弹性碰撞的问题

时间:2015-01-17 18:10:30

标签: 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();




    ////////////////////////////////////////////////////////////////////////
    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移动 我碰撞后得到的速度是:第一球速度为-10,速度为-5

并且球2变速速度为10且速度为零(假设两个球具有相同的质量)

任何有用的源代码的帮助或链接都会很棒 thx提前

编辑:在代码中,U1x,U1y,U2x,U2y是碰撞后的x和y速度,我需要为两个球计算

0 个答案:

没有答案
相关问题