Jump to content
  • Advertisement
Sign in to follow this  
pepsi coder

simple ball physics not working properly

This topic is 4014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi can you please help me, my balls get pushed away abrubtly when colliding sometimes,im keeping my velocities less than my ball radiuses so it can not be that, i have done loads of experimentation but with no joy. (cr=0.4; mass=1;) please av a gander at my following code written in flash cs3 thankyou in advance. for (var i1:uint=0;i1<CARTOTAL-1;i1++) { for (var i2:uint=i1+1;i2<CARTOTAL;i2++) { var dx:Number=cararray[i1].x-cararray[i2].x, dy:Number=cararray[i1].y-cararray[i2].y, d:Number=Math.sqrt(dx*dx+dy*dy), r1:Number=19, r2:Number=r1; if (d<=(r1+r2)) { // component of velocity in the direction (dx,dy) var vp1:Number=cararray[i1].velx*dx/d+cararray[i1].vely*dy/d, vp2:Number=cararray[i2].velx*dx/d+cararray[i2].vely*dy/d, dt:Number=(r1+r2-d)/(vp1+vp2); // move back cararray[i1].x-=cararray[i1].velx*dt; cararray[i1].y-=cararray[i1].vely*dt; cararray[i2].x-=cararray[i2].velx*dt; cararray[i2].y-=cararray[i2].vely*dt; dx=cararray[i1].x-cararray[i2].x; dy=cararray[i1].y-cararray[i2].y; d=Math.sqrt(dx*dx+dy*dy); // unit vector in the direction of the collision var ax:Number=dx/d , ay:Number=dy/d; // projection of the velocities var va1:Number=( cararray[i1].velx*ax+cararray[i1].vely*ay), vb1:Number=(-cararray[i1].velx*ay+cararray[i1].vely*ax), va2:Number=( cararray[i2].velx*ax+cararray[i2].vely*ay), vb2:Number=(-cararray[i2].velx*ay+cararray[i2].vely*ax); // new velocities after collisions var vap1:Number=va1+(1+cr)*(va2-va1)/(1+cararray[i1].mass/cararray[i2].mass), vap2:Number=va2+(1+cr)*(va1-va2)/(1+cararray[i2].mass/cararray[i1].mass); // undo the projections (new velocities after collision) cararray[i1].velx=vap1*ax-vb1*ay; cararray[i1].vely=vap1*ay+vb1*ax; cararray[i2].velx=vap2*ax-vb2*ay; cararray[i2].vely=vap2*ay+vb2*ax; // move forward cararray[i1].x+=cararray[i1].velx*dt; cararray[i1].y+=cararray[i1].vely*dt; cararray[i2].x+=cararray[i2].velx*dt; cararray[i2].y+=cararray[i2].vely*dt; } } }

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!