dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: collisiom

  1. #1
    Member
    Join Date
    Jan 2001
    Posts
    54

    collisiom

    Its been quite sometime since I posted here; BUT!

    Need to know how (if possible) when 2 objects collide, for one object to bounce off the other? Such as in when to pool balls collide or like a bowling pin when hit by the ball. Is it even possible in 3dfa?

  2. #2
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Sure but only if they are moving using velocity. Simplest is to reverse the velocity..

    To really do it right you want to calculate the direction of the collision and change velocity based on that. (beyond my meager math skills but look in the flash tutorials it's there)

    oh and you might want to use my circleCollide() function if the objects are round other wise than collide before they look like they do because the "bounding rectangle" is bigger in the corners.

    My function library is available:
    HERE
    Last edited by blanius; 03-17-2004 at 11:10 AM.

  3. #3
    Senior Member zoranvedek's Avatar
    Join Date
    Aug 2001
    Location
    Wagoner OK
    Posts
    892
    Somewhere at flashkit here, is an entire thread about the physics of a pool table, with links to enough resources to keep you busy for a long long time. Try searching the general forums for pool balls, or something similiar.

    It's well worth hunting down if want to get it straight............


    -J

  4. #4
    Member
    Join Date
    Jan 2001
    Posts
    54
    thanks guys...and sorry for the typo in the subject...should have said collision LOL.

    Anyway I did search the flashkit boards and did find some really interesting and oh so complicated math for the collisions which was for flash 4 and flash 5, which at this time are way over my head.

    Any suggestions to getting me started simple....one stationary object and one moving object collide, then move according to the angle and velocity of the moving object? Which I would assume is no simple task at all.

    Any suggestion from Bret or Ed? I personally consider you guys as the gurus of 3dfa LOL is why i turn to you.

  5. #5
    Member
    Join Date
    Jan 2001
    Posts
    54
    Oh and guru zoranvedek as well!

  6. #6
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    :)

    Ah shucks!

    Any start here: the following code from my function script
    PHP Code:
    //*************************************
    //Circle Collide uses distance test function
    //*************************************
        
    function circleCollide(element1,element2)
        {
        
    //get width of both elements
        
    width1=(element1.getBounds().xMax-element1.getBounds().xMin)-6
        width2
    =(element2.getBounds().xMax-element2.getBounds().xMin)-6
        
    //get distance 
        
    a=element1.element2.x;
        
    b=element1.element2.y;
        
    distsqrt (a*a+b*b);
        
    //account for different sizes
        
    width=(width1/2)+(width2/2);
        
    //check distance and size
        
        //return t/f
        
    return dist<=width
        
    }
        

    //*************************************    
    //Get the Angle for rotation between two objects
    //
    //*************************************
    function angleTo(myelement,myelement2)
        {
        
    angle=atan2((myelement.x-myelement2.x),(myelement.y-myelement2.y))
        return 
    angle-90
        

    From this you can figure out the distance and the angle. The circleCollide works for any symetrical element, it basically calculates the distance between two objects and if it's less than halve the combine sizes of the two elements they must be colliding.

    You could then get the angle from A to B using the angleTo function...

    The trick then is to figure out what to do with the velocities.

    Have to play with it a bit, but why don't you start there and for starters just invert both the x and y velocity.

    I don't think I have the math for the rest. I'll look into it though

  7. #7
    Member
    Join Date
    Jan 2001
    Posts
    54
    Thanks Bret...I got the boundry of the circle, but using the help files and reading the threads I cant figure out how to make objects bounce of each other without directly adding object.velocity.x or y for each object
    I am trying to figure out how to do something similar to the example at

    Circle Collision

    But cant releate the code used there to the scripts used in 3dfa

  8. #8
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Seen that one before, it might be where I got the collision of circles code myself.

    Yeah that's tough code, It's pretty old for version 4 of Flash so much of it is not usfull for even Flash MX

    Looks like what he's done here is to SWAP the velocities which might work.

    try doing that by Making a function like

    PHP Code:
    function swapVelocity(element1,element2){
       
    holdx=element1.velocity.x;
       
    holdy=element1.velocity.y;
       
    element1.velocity.x=element2.velocity.x;
       
    element1.velocity.y=element2.velocity.y;
       
    element2.velocity.x=holdx;
       
    element2.velocity.y=holdy;

    Yep this works reasonably well
    LINKS:
    http://bretlanius.com/flash/bounce.html
    http://bretlanius.com/flash/bounce.movie
    Last edited by blanius; 03-19-2004 at 11:40 AM.

  9. #9
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    One thing I noticed here with the collision detection I used is that you have to be careful about the fps of your flash movie. If you use a high low fps the elements can overlap a bit before detected, and sometimes it misses a slight collision.

    So either use higher fps than about 15+ or tweek the collision detection or at least call it more times per frame in your script.

  10. #10
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    Ok Got it

    After tearing out a bit of the sparse hair on my head I finally figured it out.

    There are a few problems still. Sometimes balls get stuck overlapping I tweeked the circle collision routine to try to fix it but they still can get stuck.

    PHP Code:
    for (x=0;x<maxballs;x++){

        for (
    loop=x+1;loop<maxballs;loop++){
    //this is the trick! so as not to repeate collisions    
            
    if (root.circleCollide(balls[x],balls[loop])&&loop!=x){
                
    trace (balls[x].name+" hit "+balls[loop].name)
                
                
    root.swapVelocity(balls[x],balls[loop]);
                }
        
        }


    Bounce balls
    The Source
    Last edited by blanius; 04-01-2004 at 10:53 AM.

  11. #11
    Senior Member lopez1_de's Avatar
    Join Date
    Oct 2001
    Location
    Germany
    Posts
    299
    good job bret
    Last edited by lopez1_de; 04-01-2004 at 01:52 PM.

  12. #12
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590
    It may be a bit late to post this, and a little off topic, but the following thread has a few collision detection type routines in.

    Other Thread In particular, there is some stuff in here about how to avoid your balls getting stuck to the walls. I noticed that this happens in the above example.

    Also, I attach a movie that uses some more complex stuff to attempt to do a Newton's Cradle type model, with lots of collision detection, between multiple objects. It would be better if I'd used a detection method like Brett's in this case, rather than standard 3DFA collision detection, but maybe this would be a useful example. It looks best when exported for some reason.

    Morgan.
    Attached Files Attached Files
    Last edited by ForumNewbie; 04-06-2004 at 07:44 PM.
    Please note that my domain has changed to http://www.morganmultinational.com

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