A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Ball vs Ball Collision Issues

  1. #1
    Senior Member
    Join Date
    Apr 2007
    Posts
    269

    Question Ball vs Ball Collision Issues

    Hi I'm currently messing about with Ball vs Ball Collision and came across a nasty bug in my demo. Use the arrow keys to move the red ball if you hold the left key down you can recreate the bug the balls will penetrate each other. Why does this happen and how can I prevent in.

    Collision Code:
    Actionscript Code:
    if ((Math.sqrt((((ballA.xPos - ballB.xPos)*(ballA.xPos - ballB.xPos))+((ballA.yPos - ballB.yPos)*(ballA.yPos - ballB.yPos))))-((ballA.Mass/2)+(ballB.Mass/2)))<=0) {                           
        var dx = ballA.xPos - ballB.xPos;
        var dy = ballA.yPos - ballB.yPos;
        var collisionision_angle = Math.atan2(dy,dx);
        var magnitude_1 = Math.sqrt(ballA.xVel * ballA.xVel + ballA.yVel * ballA.yVel);
        var magnitude_2 = Math.sqrt(ballB.xVel * ballB.xVel + ballB.yVel * ballB.yVel);
        var direction_1 = Math.atan2(ballA.yVel,ballA.xVel);
        var direction_2 = Math.atan2(ballB.yVel,ballB.xVel);
        var new_xspeed_1 = magnitude_1 * Math.cos(direction_1 - collisionision_angle);
        var new_yspeed_1 = magnitude_1 * Math.sin(direction_1 - collisionision_angle);
        var new_xspeed_2 = magnitude_2 * Math.cos(direction_2 - collisionision_angle);
        var new_yspeed_2 = magnitude_2 * Math.sin(direction_2 - collisionision_angle);
        var final_xspeed_1 = ((ballA.Mass-ballB.Mass)*new_xspeed_1+(ballB.Mass+ballB.Mass)*new_xspeed_2)/(ballA.Mass+ballB.Mass);
        var final_xspeed_2 = ((ballA.Mass+ballA.Mass)*new_xspeed_1+(ballB.Mass-ballA.Mass)*new_xspeed_2)/(ballA.Mass+ballB.Mass);
        var final_yspeed_1 = new_yspeed_1;
        var final_yspeed_2 = new_yspeed_2;
        ballA.xVel = Math.cos(collisionision_angle) * final_xspeed_1 + Math.cos(collisionision_angle + Math.PI / 2) * final_yspeed_1;
        ballA.yVel = Math.sin(collisionision_angle) * final_xspeed_1 + Math.sin(collisionision_angle + Math.PI / 2) * final_yspeed_1;
        ballB.xVel = Math.cos(collisionision_angle) * final_xspeed_2 + Math.cos(collisionision_angle + Math.PI / 2) * final_yspeed_2;
        ballB.yVel = Math.sin(collisionision_angle) * final_xspeed_2 + Math.sin(collisionision_angle + Math.PI / 2) * final_yspeed_2;
    }

    Thanks in advance.

  2. #2
    Please, Call Me Bob trogdor458's Avatar
    Join Date
    Aug 2006
    Location
    Pensacola, FL
    Posts
    915
    I recognize this method of doing things, and I know many problems associated with it
    I'll provide you an example of an easy way I know to do ball to ball collisions, though it isn't the best

    Variables have been abbreviated (and I included gravity):
    Code:
    dx=Ax+Axv-Bx-Bxv;
    dy=Ay+Ayv+gravity-By-Byv;
    dis=(Amass+Bmass)/2;
    if(dx*dx+dy*dy<dis*dis){
    	angle=Math.atan2(dy,dx);
    	tx=dis*Math.cos(angle)-dx;
    	ty=dis*Math.sin(angle)-dy;
    	Axv+=tx*Amass/(dis*2);
    	Ayv+=ty*Amass/(dis*2);
    	Bxv-=tx*Bmass/(dis*2);
    	Byv-=ty*Bmass/(dis*2);
    }
    Unless I typoed somewhere...

  3. #3
    Senior Member
    Join Date
    Apr 2007
    Posts
    269
    Thanks man I'll implement that code and post the results ;]

  4. #4
    Senior Member
    Join Date
    Apr 2007
    Posts
    269
    This is the new code implemented it solves the problem of the penetrating balls. The only problem now is it's taken away the transfer of momentum like in the old one when two collided they bounce off in opposite directions whereas in this one they just push each other. Please help me with this last bit.

    Actionscript Code:
    function checkBallCollisions() {
            for (var i:int = 0; i<Props.length; i++) {
                var ballA = Props[i];
                for (var j:int = i+1; j<Props.length; j++) {
                    var ballB = Props[j];
                    var dx = ballA.xPos+ballA.xVel-ballB.xPos-ballB.xVel;
                    var dy = ballA.yPos+ballA.yVel-ballB.yPos-ballB.yVel;
                    var dis = (ballA.Mass+ballB.Mass)/2;
                    if(dx*dx+dy*dy<dis*dis) {
                    var angle = Math.atan2(dy,dx);
                    var tx = dis*Math.cos(angle)-dx;
                    var ty = dis*Math.sin(angle)-dy;
                    ballA.xVel += tx*ballA.Mass/(dis*2);
                    ballA.yVel += ty*ballA.Mass/(dis*2);
                    ballB.xVel -= tx*ballB.Mass/(dis*2);
                    ballB.yVel -= ty*ballB.Mass/(dis*2);
                }
            }
        }
    }

    Thanks in advance.

  5. #5
    Senior Member
    Join Date
    Apr 2007
    Posts
    269
    Bump!

  6. #6
    Please, Call Me Bob trogdor458's Avatar
    Join Date
    Aug 2006
    Location
    Pensacola, FL
    Posts
    915
    This is really just a guess but try replacing the last four (dis*2) with just dis
    If it still isn't reactive enough, you can still scale the forces anyway you like on those last 4 lines of code
    Last edited by trogdor458; 08-01-2010 at 05:09 PM.

  7. #7
    Senior Member
    Join Date
    Apr 2007
    Posts
    269
    That causes some nasty bugs when they collided in close proximity like as if the force is being multiplied for every ball it hits. I think the problem here is instead of setting it up as:

    ballA has xVel, yVel, xPos, yPos, and Mass.
    it collides with
    ballB which has xVel, yVel, xPos, yPos, and Mass.
    After the collision what is ballA and ballB's xVel and yVel.

    In the equation we're using now instead of the velocity just being set its being slowly increased. Hence the += and -= which is good because it solves the problem of the balls penetrating each other as it's ideal for when a ball is pushing another.

    But when they just collided and I want that billiard ball physics with all that ricochet.

  8. #8
    Senior Member
    Join Date
    Apr 2007
    Posts
    269
    I got my desired effect in the end using this page as reference. http://www.kirupa.com/developer/as3/...isions_pg2.htm

    To avoid the problem where the balls get stuck together by constantly rounding the variables using this as help http://kb2.adobe.com/cps/155/tn_15542.html

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center