A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [AS2] Ball Collision & Momentum. Yay >_<'

  1. #1
    Senior Member Shnitzel's Avatar
    Join Date
    May 2007
    Location
    IL
    Posts
    130

    [AS2] Ball Collision & Momentum. Yay >_<'

    Oh yeah, ball collision. Ain't that a fun subject?

    So I'm trying to make two balls collide and by the law of momentum conservation (Or however it's called in English :S ), have them moving at the correct different directions and speeds.
    It's sort of like a pool game



    I got the balls moving, got a way to find out when they collide and then started trying to make the hitting ball (let's call it cueball xD) to move back.

    Since I've made my balls movement directions angle based (as in x+=cos(angle)*r) I figured all I got to do is change the angle by 180 degrees.
    Which sounded logically correct.
    But what I forgot is that it'll also flip the y movement. So I made "xangle" and "yangle" and then only changed the "xangle" by 180. That seemed to work, only with no realistic physics.

    If the cueball hits exactly at the center of the other ball by moving only on the X line, then obviously, it'll move back on the X line (Though since they both have the "same mass" it should stop, but I don't want that). In that case all I need to do is add 180 degrees to my xangle.



    But what if it hits 30 degrees below the center of the other ball?
    I've been working on this project and a few more for almost 15 hours now. And have been typing all sorts of complicated and useless lines to try and get the collisions to work. I've read pool table equations and gone through dozens of websites. I searched for pool tutorials (and only found on messed up source code and another badly explained tut).
    I also found a very old thread on this forum regarding almost the same subject but beside of theoretical info, it didn't help me at all.



    At this point I don't even know if this subject is very simple and all I need is a good sleep or it's pretty complicated and I won't get any help here :/

    Either way I'm going to keep on trying right now. Any sort of help, any, is welcome.

  2. #2
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    This is a pretty good tutorial explaining it:

    http://www.emanueleferonato.com/2007...on-with-flash/

  3. #3
    Senior Member Shnitzel's Avatar
    Join Date
    May 2007
    Location
    IL
    Posts
    130
    Thanks a lot 55

    Only problem is this won't really work for me. My "movement" system is quite different than his and so does the speed changing.
    It did however give me some ideas to try based on his code and explanations. But none really worked :\


    On the bright side, after 4 hours of sleeping and dreaming of highschool physics, I woke up and started drawing and calculating stuff in a notebook xD
    The solution is actually very simple and basic, and sort of appears in the tutorial.

    If anyone else got stuck at something like this, here it is:
    x: mv1cosa = mv1`cosb + mv2cosc
    y: mv1sina = mv1`sinb + mv2sinc
    Same mass that they don't really have so:
    x: v1cosa = v1`cosb + v2cosc
    y: v1sina = v1`sinb + v2sinc
    =
    x: v1`cosb = v2cosc - v1cosa
    y: v1`sinb = v2sinc - v1sina

    c is the angle the other ball moves in and since it'll move at the impulse direction it'll move at the direction of the hit between the two balls.
    b is the new angle of the "cue"-ball, which you don't need to calculate at all.
    a is the angle the "cue"-ball moved before the collision.


    ------------------------------------

    So now that I've got that covered, I need help at another thing.
    I noticed the moment of collision between the balls isn't right.
    Code:
    	for (i=0; i<=1 ; i++) {
    		bDist = Math.sqrt(((_root["Ball"+i]._x-Player._x)*(_root["Ball"+i]._x-Player._x))+((_root["Ball"+i]._y-Player._y)*(_root["Ball"+i]._y-Player._y)))
    		if (bDist <= Player._width) {
    			collision blah
    		}
    	}
    This code seems like it should work. When the distance between the centers of the two balls is smaller or equal to their radius together (which is the diameter of one of them since they're all the same size) they collide.

    But when I test the movie, it seems they're colliding at a distance which is almost twice the diameter. And sometimes they overlap.

    So if anyone knows how to prevent them from overlapping and have a correct moment of collision, that'll be really great



    And again, thanks for the reply 55 ^^

  4. #4
    Senior Member Shnitzel's Avatar
    Join Date
    May 2007
    Location
    IL
    Posts
    130
    Bumptidy bump

  5. #5
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Hey, can you upload an example swf (or an fla would be even better) so I can see this actually happening?
    Thanks.

  6. #6
    Senior Member Shnitzel's Avatar
    Join Date
    May 2007
    Location
    IL
    Posts
    130
    I managed to fix it a little by limiting the speeds, but I don't think that's the right fix. It can't be, I've seen lots of pool games where the balls move at fast speeds and the collisions work perfectly. Anyway, they still collide too soon.

    http://www.swfupload.com/view/113513.htm

    Click the ball on the right and drag, then release.

    Thanks for the help.

  7. #7
    Senior Member Shnitzel's Avatar
    Join Date
    May 2007
    Location
    IL
    Posts
    130
    Aw, come on, this is getting buried :\

  8. #8
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Sorry, I've just been really busy lately.

    From the code and swf you've posted, I can't immediately see anything wrong.
    I suspect it has something to do with the "collision blah" part you omitted.
    I don't think there's anything wrong with the actual hit-testing, so it must be what you're doing once it's detected.

    Try removing all the balls except two, and see if you can get that to work first.
    Then, try to get the collision working with just those before you attempt 16 at once.

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