• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            C++ Program Code for 2D Elastic Collision of 2 Balls

            from:http://www.plasmaphysics.org.uk/programs/coll2d_cpp.htm

            //******************************************************************************
            //   This program is a 'remote' 2D-collision detector for two balls on linear
            //   trajectories and returns, if applicable, the location of the collision for 
            //   both balls as well as the new velocity vectors (assuming a fully elastic
            //   collision).
            //   In  'f' (free) mode no positions but only the initial velocities
            //   and an impact angle are required.
            //   All variables apart from 'mode' and 'error' are of Double Precision
            //   Floating Point type.
            //
            //   The Parameters are:
            //
            //    mode  (char) (if='f' alpha must be supplied; otherwise arbitrary)
            //    alpha (impact angle) only required in mode='f'; 
            //                     should be between -PI/2 and PI/2 (0 = head-on collision))
            //    m1   (mass of ball 1)
            //    m2   (mass of ball 2)
            //    r1   (radius of ball 1)        not needed for 'f' mode
            //    r2   (radius of ball 2)                "
            //  & x1   (x-coordinate of ball 1)          "
            //  & y1   (y-coordinate of ball 1)          "
            //  & x2   (x-coordinate of ball 2)          "
            //  & y2   (y-coordinate of ball 2)          "
            //  & vx1  (velocity x-component of ball 1) 
            //  & vy1  (velocity y-component of ball 1)         
            //  & vx2  (velocity x-component of ball 2)         
            //  & vy2  (velocity y-component of ball 2)
            //  & error (int)  (0: no error
            //                  1: balls do not collide
            //                  2: initial positions impossible (balls overlap))
            //
            //   Note that the parameters with an ampersand (&) are passed by reference,
            //   i.e. the corresponding arguments in the calling program will be updated;
            //   however, the coordinates and velocities will only be updated if 'error'=0.
            //
            //   All variables should have the same data types in the calling program
            //   and all should be initialized before calling the function even if
            //   not required in the particular mode.
            //
            //   This program is free to use for everybody. However, you use it at your own
            //   risk and I do not accept any liability resulting from incorrect behaviour.
            //   I have tested the program for numerous cases and I could not see anything 
            //   wrong with it but I can not guarantee that it is bug-free under any 
            //   circumstances.
            //
            //   I would appreciate if you could report any problems to me
            //   (for contact details see  http://www.plasmaphysics.org.uk/feedback.htm ).
            //
            //   Thomas Smid, January  2004
            //                December 2005 (corrected faulty collision detection; 
            //                               a few minor changes to improve speed;
            //                               added simplified code without collision detection)
            //*********************************************************************************

                   
            void collision2D(char mode,double alpha,
                             
            double m1, double m2, double r1, double r2,
                             
            double& x1, double& y1, double& x2, double& y2,
                             
            double& vx1, double& vy1, double& vx2, double& vy2,
                             
            int& error )     {

                   
            double  r12,m21,d,gammav,gammaxy,dgamma,dr,dc,sqs,t,
                           dvx2,a,x21,y21,vx21,vy21,pi2;

            //     ***initialize some variables ****
                   pi2=2*acos(-1.0E0);
                   error
            =0;
                   r12
            =r1+r2;
                   m21
            =m2/m1;
                   x21
            =x2-x1;
                   y21
            =y2-y1;
                   vx21
            =vx2-vx1;
                   vy21
            =vy2-vy1;



            //     ****  return old positions and velocities if relative velocity =0 ****
                   if ( vx21==0 && vy21==0 ) {error=1return;}


            //     *** calculate relative velocity angle             
                   gammav=atan2(-vy21,-vx21);




            //******** this block only if initial positions are given *********

                   
            if (mode != 'f'{

                   
                   d
            =sqrt(x21*x21 +y21*y21);
                   
            //     **** return if distance between balls smaller than sum of radii ***
                   if (d<r12) {error=2return;}

            //     *** calculate relative position angle and normalized impact parameter ***
                   gammaxy=atan2(y21,x21);
                   dgamma
            =gammaxy-gammav;
                      
            if (dgamma>pi2) {dgamma=dgamma-pi2;}
                       
            else if (dgamma<-pi2) {dgamma=dgamma+pi2;}
                   dr
            =d*sin(dgamma)/r12;
                   
            //     **** return old positions and velocities if balls do not collide ***
                   if (  (fabs(dgamma)>pi2/4 && fabs(dgamma)<0.75*pi2) || fabs(dr)>1 )   
                       
            {error=1return;}


            //     **** calculate impact angle if balls do collide ***
                   alpha=asin(dr);

                   
            //     **** calculate time to collision ***
                   dc=d*cos(dgamma);
                   
            if (dc>0{sqs=1.0;} else {sqs=-1.0;}
                   t
            =(dc-sqs*r12*sqrt(1-dr*dr))/sqrt(vx21*vx21+ vy21*vy21);
            //    **** update positions ***
                   x1=x1+vx1*t;
                   y1
            =y1+vy1*t;
                   x2
            =x2+vx2*t;
                   y2
            =y2+vy2*t;

                   
               }


            //******** END 'this block only if initial positions are given' *********
                  
                   
                   
            //     ***  update velocities ***

                   a
            =tan( gammav +alpha);

                   dvx2
            =-2*(vx21 +a*vy21) /((1+a*a)*(1+m21));
                   
                   vx2
            =vx2+dvx2;
                   vy2
            =vy2+a*dvx2;
                   vx1
            =vx1-m21*dvx2;
                   vy1
            =vy1-a*m21*dvx2;


                   
            return;
            }




            //******************************************************************************
            //  Simplified Version
            //  The advantage of the 'remote' collision detection in the program above is 
            //  that one does not have to continuously track the balls to detect a collision. 
            //  The program needs only to be called once for any two balls unless their 
            //  velocity changes. However, if somebody wants to use a separate collision 
            //  detection routine for whatever reason, below is a simplified version of the 
            //  code which just calculates the new velocities, assuming that the balls are 
            //  already touching and approaching each other (these two conditions are 
            //  important as otherwise the results will be incorrect)
            //****************************************************************************


                   
            void collision2Ds(double m1, double m2,
                             
            double x1, double y1, double x2, double y2,
                             
            double& vx1, double& vy1, double& vx2, double& vy2)     {

                   
            double  m21,dvx2,a,x21,y21,vx21,vy21,fy21,sign;


                   m21
            =m2/m1;
                   x21
            =x2-x1;
                   y21
            =y2-y1;
                   vx21
            =vx2-vx1;
                   vy21
            =vy2-vy1;



            //     *** I have inserted the following statements to avoid a zero divide; 
            //         (for single precision calculations, 
            //          1.0E-12 should be replaced by a larger value). **************  
              
                   fy21
            =1.0E-12*fabs(y21);                            
                   
            if ( fabs(x21)<fy21 ) {  
                               
            if (x21<0{ sign=-1; } else { sign=1;}  
                               x21
            =fy21*sign; 
                    }
             

            //     ***  update velocities ***
                   a=y21/x21;
                   dvx2
            = -2*(vx21 +a*vy21)/((1+a*a)*(1+m21)) ;
                   vx2
            =vx2+dvx2;
                   vy2
            =vy2+a*dvx2;
                   vx1
            =vx1-m21*dvx2;
                   vy1
            =vy1-a*m21*dvx2;


                   
            return;
            }

            posted on 2007-12-25 16:12 楊粼波 閱讀(409) 評論(0)  編輯 收藏 引用

            久久天天躁夜夜躁狠狠| 国产精品99久久久久久人| 精品久久久久香蕉网| 亚洲国产视频久久| 热综合一本伊人久久精品| 99久久国语露脸精品国产| 色综合久久综合中文综合网| 久久天天躁狠狠躁夜夜2020 | 久久久久久精品成人免费图片| 久久99精品久久久久久秒播| 国产午夜电影久久| 久久久久国产一区二区| 青青热久久国产久精品| 2021国产精品午夜久久| 亚洲人成伊人成综合网久久久| 国产亚洲精品久久久久秋霞| 亚洲国产另类久久久精品黑人 | 国产精品99久久不卡| 日韩精品国产自在久久现线拍 | 久久精品中文字幕一区| 色欲综合久久躁天天躁| 久久久SS麻豆欧美国产日韩| 色婷婷综合久久久中文字幕| 精品久久久久久久久中文字幕| 51久久夜色精品国产| 亚洲欧美国产精品专区久久| 亚洲中文精品久久久久久不卡| 国产精品一区二区久久国产| 亚洲午夜久久久精品影院| 亚洲精品无码久久毛片| 国产午夜福利精品久久2021 | 久久精品中文无码资源站| 国产2021久久精品| 久久亚洲日韩看片无码| 国产精品久久久福利| 亚洲欧美国产日韩综合久久| 久久精品国产亚洲av麻豆小说| 中文精品久久久久国产网址| 久久天天躁狠狠躁夜夜2020一| www.久久热.com| 亚洲精品无码久久久久去q |