<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Relative performance for collision detection techniques in ActionScript 3</title>
	<atom:link href="http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/</link>
	<description>code = joy</description>
	<lastBuildDate>Thu, 11 Mar 2010 21:22:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: mike chambers</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-17507</link>
		<dc:creator>mike chambers</dc:creator>
		<pubDate>Sat, 07 Nov 2009 05:56:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-17507</guid>
		<description>fyi, I have been playing around with this and if you optimize the circle test, you can get performance approaching the built in hitTest:
&lt;pre&gt;
private function i_hitTestCircle2(clip1:Sprite, clip2:Sprite):Boolean
{			
	var dx:Number = clip2.x - clip1.x;
	var dy:Number = clip2.y  - clip1.y ;

	var radii:Number = ((clip1.width * .5) + (clip2.width * .5));
	return ((dx * dx) + (dy * dy) &lt; radii * radii);
}
&lt;/pre &gt;

&lt;p&gt;&#160;&lt;/p&gt;
Here are the results:

&lt;pre&gt;
hitTest (50000 iterations)                                              
Player version: MAC 10,1,51,28 (regular)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
hitTest                                                      54     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
hitTestCircle (50000 iterations)                                        
Player version: MAC 10,1,51,28 (regular)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
hitTestCircle                                               133     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
hitTestCircle2 (50000 iterations)                                       
Player version: MAC 10,1,51,28 (regular)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
hitTestCircle2                                               56     0.00
–––––––––––––––––––––––––––––––––––––––––––––––
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>fyi, I have been playing around with this and if you optimize the circle test, you can get performance approaching the built in hitTest:</p>
<pre>
private function i_hitTestCircle2(clip1:Sprite, clip2:Sprite):Boolean
{
	var dx:Number = clip2.x - clip1.x;
	var dy:Number = clip2.y  - clip1.y ;

	var radii:Number = ((clip1.width * .5) + (clip2.width * .5));
	return ((dx * dx) + (dy * dy) < radii * radii);
}
</pre >

&nbsp;

Here are the results:
</pre>
<pre>
hitTest (50000 iterations)
Player version: MAC 10,1,51,28 (regular)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
hitTest                                                      54     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
hitTestCircle (50000 iterations)
Player version: MAC 10,1,51,28 (regular)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
hitTestCircle                                               133     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
hitTestCircle2 (50000 iterations)
Player version: MAC 10,1,51,28 (regular)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
hitTestCircle2                                               56     0.00
–––––––––––––––––––––––––––––––––––––––––––––––
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Garney</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16496</link>
		<dc:creator>Ben Garney</dc:creator>
		<pubDate>Mon, 06 Jul 2009 16:59:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16496</guid>
		<description>@Matt Bolt:

Sure, Box2D actually does the right thing w.r.t collision (and PushButton is integrated with it out of box). Even if you don&#039;t care about the physical response part, the algorithms that Box2D uses to _detect_ collisions are smart and would get you the performance improvement I described.

Before we go 1.0 we are going to do a major code style update to match Flex style (http://code.google.com/p/pushbuttonengine/issues/detail?id=56) as well as reorganize the codebase to make it easier to work with... Our bad. :)</description>
		<content:encoded><![CDATA[<p>@Matt Bolt:</p>
<p>Sure, Box2D actually does the right thing w.r.t collision (and PushButton is integrated with it out of box). Even if you don&#8217;t care about the physical response part, the algorithms that Box2D uses to _detect_ collisions are smart and would get you the performance improvement I described.</p>
<p>Before we go 1.0 we are going to do a major code style update to match Flex style (<a href="http://code.google.com/p/pushbuttonengine/issues/detail?id=56" rel="nofollow">http://code.google.com/p/pushbuttonengine/issues/detail?id=56</a>) as well as reorganize the codebase to make it easier to work with&#8230; Our bad. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mikechambers</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16495</link>
		<dc:creator>mikechambers</dc:creator>
		<pubDate>Mon, 06 Jul 2009 15:54:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16495</guid>
		<description>@tonypa

That is a good point. Here is the result with clips that are about 3 times larger:

&lt;pre&gt;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitInternal (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached.  
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 8087     0.81
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitDynamic (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached. M
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 1848     0.18
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––&lt;/pre&gt;

Note, that is with cacheAsBitmap set to true.

mike chambers

mesh@adobe.com</description>
		<content:encoded><![CDATA[<p>@tonypa</p>
<p>That is a good point. Here is the result with clips that are about 3 times larger:</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitInternal (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached.
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 8087     0.81
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitDynamic (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached. M
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 1848     0.18
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>Note, that is with cacheAsBitmap set to true.</p>
<p>mike chambers</p>
<p><a href="mailto:mesh@adobe.com">mesh@adobe.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mikechambers</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16494</link>
		<dc:creator>mikechambers</dc:creator>
		<pubDate>Mon, 06 Jul 2009 15:43:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16494</guid>
		<description>@Renaun

You are exactly correct. Setting cacheAsBitmap = true on the author time clips brings the performance to the same level as the dynamic clips.

Here is the relevant section:

&lt;pre&gt;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitDynamic (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached. M
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 1844     0.18
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitInternal - cacheAsBitmap (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached.  
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 1805     0.18
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––&lt;/pre&gt;

Ill update the post to reflect this. Good catch.

mike chambers

mesh@adobe.com</description>
		<content:encoded><![CDATA[<p>@Renaun</p>
<p>You are exactly correct. Setting cacheAsBitmap = true on the author time clips brings the performance to the same level as the dynamic clips.</p>
<p>Here is the relevant section:</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitDynamic (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached. M
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 1844     0.18
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitInternal - cacheAsBitmap (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached.
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 1805     0.18
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>Ill update the post to reflect this. Good catch.</p>
<p>mike chambers</p>
<p><a href="mailto:mesh@adobe.com">mesh@adobe.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Renaun Erickson</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16485</link>
		<dc:creator>Renaun Erickson</dc:creator>
		<pubDate>Thu, 02 Jul 2009 14:20:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16485</guid>
		<description>About your conclusion #4 (MovieClip at author time vs runtime).  I think the issue is the use of cacheAsBitmap = true on the runtime objects.  If you set the same property in author time (Properties -&gt; Display -&gt; check &quot;Cache as bitmap&quot;) then it has the similar performance.</description>
		<content:encoded><![CDATA[<p>About your conclusion #4 (MovieClip at author time vs runtime).  I think the issue is the use of cacheAsBitmap = true on the runtime objects.  If you set the same property in author time (Properties -&gt; Display -&gt; check &#8220;Cache as bitmap&#8221;) then it has the similar performance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tonypa</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16470</link>
		<dc:creator>tonypa</dc:creator>
		<pubDate>Mon, 29 Jun 2009 07:26:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16470</guid>
		<description>I also tested checkGetColorBoundsRect method, it does not perform too well compared to BitmapData.hitTest (2x slower on 20px objects, ~20-25 times slower on 100 px objects).</description>
		<content:encoded><![CDATA[<p>I also tested checkGetColorBoundsRect method, it does not perform too well compared to BitmapData.hitTest (2x slower on 20px objects, ~20-25 times slower on 100 px objects).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tonypa</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16469</link>
		<dc:creator>tonypa</dc:creator>
		<pubDate>Mon, 29 Jun 2009 06:55:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16469</guid>
		<description>Quote: &quot;If you can cache the BitmapData being compared, then BitmapData.hitTest performance is on par with the other techniques / APIs tested.&quot;

Thats not completely true because speed of BitmapData.hitTest is largely depending on the size of objects. For objects with size of only couple of pixels, its lightning-fast. But try objects of hundreds or thousands of pixels and it gets so slow that it cant even finish this test.

Other collision techniques, like math-based or checking bounds, are not affected by the size.

So you cant just compare these results directly.</description>
		<content:encoded><![CDATA[<p>Quote: &#8220;If you can cache the BitmapData being compared, then BitmapData.hitTest performance is on par with the other techniques / APIs tested.&#8221;</p>
<p>Thats not completely true because speed of BitmapData.hitTest is largely depending on the size of objects. For objects with size of only couple of pixels, its lightning-fast. But try objects of hundreds or thousands of pixels and it gets so slow that it cant even finish this test.</p>
<p>Other collision techniques, like math-based or checking bounds, are not affected by the size.</p>
<p>So you cant just compare these results directly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mikechambers</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16462</link>
		<dc:creator>mikechambers</dc:creator>
		<pubDate>Sat, 27 Jun 2009 23:17:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16462</guid>
		<description>@SP

Feel free to rewrite the circle collision and Ill add it to the test.

Thanks...

mike chambers

mesh@adobe.com</description>
		<content:encoded><![CDATA[<p>@SP</p>
<p>Feel free to rewrite the circle collision and Ill add it to the test.</p>
<p>Thanks&#8230;</p>
<p>mike chambers</p>
<p><a href="mailto:mesh@adobe.com">mesh@adobe.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mikechambers</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16460</link>
		<dc:creator>mikechambers</dc:creator>
		<pubDate>Sat, 27 Jun 2009 21:07:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16460</guid>
		<description>@tonya

Yeah, that should be faster, and is easy. Ill add another test using that.

@SP

The goal of this wasnt to super optimize everything, and find the absolute fastest way to do that (since that will largely depend on the application / content / game).

The goal was to give a baseline of relative performance between different high level APIs and techniques

mike chambers

mesh@adobe.com</description>
		<content:encoded><![CDATA[<p>@tonya</p>
<p>Yeah, that should be faster, and is easy. Ill add another test using that.</p>
<p>@SP</p>
<p>The goal of this wasnt to super optimize everything, and find the absolute fastest way to do that (since that will largely depend on the application / content / game).</p>
<p>The goal was to give a baseline of relative performance between different high level APIs and techniques</p>
<p>mike chambers</p>
<p><a href="mailto:mesh@adobe.com">mesh@adobe.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SP</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16459</link>
		<dc:creator>SP</dc:creator>
		<pubDate>Sat, 27 Jun 2009 19:43:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758#comment-16459</guid>
		<description>Great idea, I should do the same!

There&#039;s a LOT of room for optimizations in that collision code though.
Tonypa&#039;s circle-circle collision code could be around 10 times faster (maybe more?). 
It could be improved even more by replacing the
var r:Number = clip1.width / 2 + clip2.width / 2;
by
var r:Number = (clip1.width + clip2.width) * 0.5;

If instead of using DiplayObject.x, y, width or height you create a custom circle class with those properties it would improve a LOT more. DiplayObject.x and DiplayObject.y are slow, but DiplayObject.width and DiplayObject.height are EXTREMELY slow (seriously, ridiculously slow).
And if you really need speed, you can inline the code eliminating those 10000 function calls.

The same applies for many of the other checks!

Make sure you are not using the debug player in the tests! It gives awful results.

SP.</description>
		<content:encoded><![CDATA[<p>Great idea, I should do the same!</p>
<p>There&#8217;s a LOT of room for optimizations in that collision code though.<br />
Tonypa&#8217;s circle-circle collision code could be around 10 times faster (maybe more?).<br />
It could be improved even more by replacing the<br />
var r:Number = clip1.width / 2 + clip2.width / 2;<br />
by<br />
var r:Number = (clip1.width + clip2.width) * 0.5;</p>
<p>If instead of using DiplayObject.x, y, width or height you create a custom circle class with those properties it would improve a LOT more. DiplayObject.x and DiplayObject.y are slow, but DiplayObject.width and DiplayObject.height are EXTREMELY slow (seriously, ridiculously slow).<br />
And if you really need speed, you can inline the code eliminating those 10000 function calls.</p>
<p>The same applies for many of the other checks!</p>
<p>Make sure you are not using the debug player in the tests! It gives awful results.</p>
<p>SP.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
