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 1 : A ball hitting a wall


In this demo we have a ball enclosed by four walls. Click on the ball to shoot it at a random direction. If it hits a wall, it will stop. When the ball moves, a (semi transparent) ghost ball is drawn at the new position. So whenever the ball is stopped by a collision, you can see what the next step would have been, if the ball had not been stopped.


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

Overview of the application

Setting up an application has many ways too. So I'll quickly explain how this demo application is build up. All demos are build up the same way, only the step() function and sometimes the shoot() function changes between demos.
The demo application is an actionscript 2 class called "collisionApp1". Here's the source code of the class. The source code is documented. In the first frame of the flash movie we create an instance of this class:
app = new collisionApp1(this);
stop();

The parameter this passes a reference to the _root MovieClip to the collisionApp1 class, so it has a movieClip to get events from and to draw on. In the constructor we set up stuff.
We tell the ball's MovieClip to call the shoot() function when clicked and we tell the parent MovieClip (= _root) to call the step() function onEnterframe.
The walls are defined by a starting point and a Vector. The walls are put in an array called walls.
An important variable is bPlaying. When set to true, the ball will be moving.
The shoot() function is called when the user clicks on the ball. The ball is reset to it's original position and given a random direction. The variable bPlaying is set to true.
The step() function is called onEnterFrame. If bPlaying is true, it moves the ball until a collision is detected by the checkCollision() function. Then bPlaying is set to false.
The function drawCross() is called by checkCollision(). It draws a cross at the intersection point.

That was an overview of the functions and their relations. Check the documentation in the source code for details.

Collision detection

ImageThe actual collision detection is done in the step() function. We loop through the walls array and try to find an intersection of the line segment representing the wall and the line segment defined by the ball's current position and the ball's new position.
The function checkCollision() returns true if the two line segments intersect. It uses the algorithm described in article 1.
Note how easy the implementation looks, as opposed to the theory in article 1. The checkCollision() function takes 4 paramaters: the starting points of both line segments and the vectors describing the length and direction of both line segments.



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

private function checkCollision(p1:Object, S1:Vector, p2:Object, S2:Vector):Boolean {
   var div = -S2.x * S1.y + S1.x * S2.y;
   var s = (-S1.y * (p1.x - p2.x) + S1.x * (p1.y - p2.y)) / div;
   var t = (S2.x * (p1.y - p2.y) - S2.y * (p1.x - p2.x)) / div;
   if (t >= 0 && t <= 1 && s >= 0 && s <= 1) {
      // draw mark at intersection
      var ix = p1.x + t * S1.x;
      var iy = p1.y + t * S1.y;
      this.drawCross(debug_mc, ix, iy, 0xCC0000);
      return true;
   } else {
      return false;
   }
}

The collision detection goes well. There's one problem though: the collision is detected after the ball has gone through the wall. Once we start bouncing, this will look ackward.



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