A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: collision detection with circles

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

    collision detection with circles

    here's a little info on collision detection


    You can go to Sampe movie and drag the balls to see example

    you can get the movie source for that example at
    source

    Basically what I've done is to calculate the distance from ball 1 and ball 2 and if it is less than or equal to 24 then they have collided.
    PHP Code:
    a=abs (element ("ball1").element ("ball2").x);
    b=element ("ball1").element ("ball2").y;

    distsqrt (a*a+b*b); 

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

    more on this

    Playing around further I came up with the following functions:
    PHP Code:
    function distance(element1,element2){
        
    a=abs (element1.element2.x);
        
    b=element1.element2.y;
        return 
    sqrt (a*a+b*b);
    }

    function 
    collide(distance,width){
        return 
    distance<=width
        

    put those in your starting script then when you want to check for a hit do this
    PHP Code:
    dist=root.distance(element ("ball1"),element ("ball2"));
    hit=root.collide(dist,24); 
    Or better yet let the function do all the work:
    PHP Code:
    function C_collide(element1,element2)
        {
        
    //get width of both elements
        
    width1=(element1.getBounds().xMax-element1.getBounds().xMin)-5
        width2
    =(element2.getBounds().xMax-element2.getBounds().xMin)-5
        
    //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
        

    not sure why I had to subtract 5 from the widths but I found I did.

    then call this function like this:
    PHP Code:
    hit=root.C_collide(element ("ball1"),element ("ball2")); 
    Hope you find this useful!

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