Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Pythagorean theorem - collision detection

This is a discussion on Pythagorean theorem - collision detection within the Tutorials forums, part of the English Forums category; Here we are at our second appointment with the Pythagorean theorem. I advise you to read the first part of ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 27-09-07, 09:10
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Pythagorean theorem - collision detection

Here we are at our second appointment with the Pythagorean theorem.
I advise you to read the first part of this article so to better understand what will come next.
In the first part, we saw how to calculate the distance in between two objects.
Now, we will discover how to detect the collision of two objects using the Pythagorean theorem.
Many of you would surely think 'what's the use of Pythagora to detect the collision when we have the method hitTestObject available''
To detect simple collision, hitTestObject remains a valid method. For more complex collisions and specially when developing flash games, Pythagora is the best way to go.

Let's see the example'

I create a FLA and save it as 'collisione_pitagora.fla' into which I create two MovieClips with the instance names 'ball_0_mc' and 'ball_1_mc'.
I create a Document Class, an AS file saved as 'Collisione.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Collisione extends MovieClip
	{
		public function Collisione()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			ball_0_mc.x=0;
			ball_1_mc.x=stage.stageWidth;
			
			this.addEventListener(Event.ENTER_FRAME,go);
		}
		
		private function go(e:Event):void
		{
			var x:Number=ball_0_mc.x-ball_1_mc.x;
			var y:Number=ball_0_mc.y-ball_1_mc.y;
			var distance:Number=Math.sqrt(x*x+y*y);
			if(distance<=ball_0_mc.width)
			{
				this.removeEventListener(Event.ENTER_FRAME,go);
				trace('stop');
			}
			else
			{
				ball_0_mc.x++;
				ball_1_mc.x--;
			}
		}
	}
}
The result:







Let's analyse the code.

Methods

init();
I impost the frame rate
stage.frameRate=31;
I position the MovieClips
ball_0_mc.x=0;
ball_1_mc.x=stage.stageWidth;
I add an interval ENTER_FRAME
this.addEventListener(Event.ENTER_FRAME,go);

go();
I apply the Pythagorean theorem as seen in this artiche
var x:Number=ball_0_mc.x-ball_1_mc.x;
var y:Number=ball_0_mc.y-ball_1_mc.y;
var distance:Number=Math.sqrt(x*x+y*y);
If the distance is smaller then 'ball_0_mc''s width (as they have a centred registration point)
if(distance<=ball_0_mc.width)
{
I remove the interval and do a trace to advise me of the event
this.removeEventListener(Event.ENTER_FRAME,go);
trace('stop');
}
Otherwise, I move the two MovieClips towards each other
else
{
ball_0_mc.x++;
ball_1_mc.x--;
}

See you next!
__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 28-08-08 at 07:03..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Multiple collision on angled surface Flep Tutorials 0 03-04-08 06:47
Rotation of coordinates and collision with an angled surface with Flash CS3 Flep Tutorials 0 21-03-08 09:30
Collision between MCs niubbo HELP free utilities 0 25-02-08 13:08
Pythagorean theorem with Actionscript 3.0 Flep Tutorials 0 27-09-07 21:33
Flash Player 9 detection kit Affidabile Davide78 Flash CS3 generale 3 10-08-07 14:52


All times are GMT. The time now is 23:25.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406