Home > Physics_Simulation > Collision – Circle/ Circle

Collision – Circle/ Circle

November 17, 2011 Leave a comment Go to comments

This tutorial features collision detection between two circles using distance. First, lets aquaint ourselves with the variables. Ar and Br are radius of circle A and B. Iv included an image below to clarify my point.

collision1

Next, you will need to form the condition of collision. When colliding, the two balls will touch or even penetrate each other. This inherently means that distance between the centers of circle A and B will be equal or shorter than the sum of their radius. Observe the image below.

collision2

Iv included required source code here for you to work along. First, there’s this Ball.as class so that you can easily call circular sprites. Check below:

package  
{
	import flash.display.Sprite;
	/**
	 * Ball sprite
	 * @author Shiu
	 */
	public class Ball extends Sprite
	{
		private var _radius:Number;
		private var _col:Number;
		
		public function Ball(radius:Number = 10, col:Number = 0xFF6633) {
			this._radius = radius;
			this._col = col;
			this.draw();
		}
		
		//function to draw the sprite according to specifications
		private function draw():void {
			graphics.beginFill(_col);
			graphics.drawCircle(0, 0, _radius);
			graphics.endFill();
		}
		
		public function get radius():Number {
			return _radius;
		}
		
		public function set radius(value:Number):void {
			_radius = value;
			this.draw();
		}
		
		public function get col():Number {
			return _col;
		}
		
		public function set col(value:Number):void {
			_col = value;
			this.draw();
		}
	}
}

Then here comes the bunch of code for your collision in Main.as

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * Purpose: Demo on circle/ circle collision detection
	 * @author Shiu
	 */
	public class Main extends Sprite 
	{
		private var b:Ball;
		private var c:Ball;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			c = new Ball; addChild(c); c.x = 300; c.y = 500; c.radius = 30; c.col = 0x339911;
			b = new Ball; addChild(b); b.x = 200; b.y = 200;	//placing in the sprites
			
			b.addEventListener(Event.ENTER_FRAME, move); // adding in animation
		}
		
		//b will always move toward c
		private function move(e:Event):void 
		{
			var vec:Vector2D = new Vector2D(c.x - b.x, c.y - b.y);
			vec.setMagnitude(10);
			if (collide(b,c)) b.removeEventListener(Event.ENTER_FRAME, move);
			else {b.x += vec.x; b.y += vec.y;}
		}
		
		//collision detection
		private function collide (b1:Ball, b2:Ball) :Boolean {
			var isCollide:Boolean = false;
			var dist:Vector2D = new Vector2D(b1.x - b2.x, b1.y - b2.y);
			if (dist.getMagnitude() <= b1.radius +b2.radius) isCollide = true;
			return isCollide;
		}
	}
	
}

You may also get your code here.

Advertisements
Categories: Physics_Simulation
  1. November 17, 2011 at 1:26 AM

    Fellow readers, I just found WordPress cannot accept .as files. So for the moment, I shall use the code snippet feature to dump Actionscript in 😦

  2. November 17, 2011 at 1:29 AM

    Ok, you may download the .as files from here –> http://gist.github.com/1372751

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: