Home arrow Articles arrow Flash arrow Collision detection & bouncing part 2: actionscript examples of collision detection
Collision detection & bouncing part 2: actionscript examples of collision detection Print E-mail
Tuesday, 06 June 2006
Article Index
Collision detection & bouncing part 2: actionscript examples of collision detection
demo 1: A ball hitting a wall
demo 2: collision detection before the ball hits the wall
demo 3: the ball approaching the wall at a sharp angle
demo 4: the ultimate ball-hits-wall algorithm
demo 5: a polygon hitting a wall

Demo 5: a polygon hitting a wall

Download all demos' source code as zip | view this demo's source code in a new window


Determining the intersection of  a polygon inside a (convex) polygon is easy. A polygon will always hit a wall with one of it's corners first. So we just draw a speed vector in each corner of the polygon and check it's intersection with any of the walls.

In each corner of the polygon MovieClip, I've put an instance of MovieClip "corner_mc". The first loop goes trough the walls array. The second loop goes through the corners. We look for an intersection of the line segment defined by the corner's position and the speed vector on one hand and the wall's line segment on the other hand.


var bCollision = false;
for (var i=0; i<this.walls.length; i++){
    for (var j=1; j<=4; j++) {
        if ( checkCollision(
                 {x: this.ball_mc._x + this.ball_mc['corner'+j+'_mc']._x,
                 y:this.ball_mc._y + this.ball_mc['corner'+j+'_mc']._y},
                 this.v,
                 {x:this.walls[i].x, y:this.walls[i].y},
                 this.walls[i].v) ) {
             bCollision = true;
             break;
        }
    }
}


That was it for this article. Please leave comments if you know alternative algorithms, find mistakes, have questions or found this article helpful.

 



Comments
thank you so much for this!!
i have been searching for weeks to find this

  Posted by Roman Vaughan, Whose homepage is http://smartie.awardspace.com/ on Thursday, 03 August 2006 at 7:21

This is the best collision detection explanation and example I have seen for flash yet! Much better than using a simple hitTest or whatever, thanks so much!
  Posted by Vince, Whose homepage is http://kamazar.com on Sunday, 07 January 2007 at 12:56

There are some good flash tutorials dealing with collisions on www.tonypa.pri.ee. The website should provide some extra reference material for those interested.
  Posted by Mike Hamman, on Friday, 11 April 2008 at 4:06

Thanks, this is the most on to it explanation I've had. Will allow for some truly dynamic realistic collisions. Are you still building on this?
  Posted by Alexis, Whose homepage is http://www.joomlabear.com on Sunday, 29 June 2008 at 5:22


 1 
Page 1 of 1 ( 4 comments )
©2005 MosCom

Add your comments to this article Collision detection & bouncing part... ...

Name (required)

E-Mail (required)
Your email will not be displayed on the site - only to our administrator
Homepage

Comment