<?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: Pixel-Perfect Collision Detection Revisited</title>
	<atom:link href="http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/feed/" rel="self" type="application/rss+xml" />
	<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/</link>
	<description>Gamedev 2.0</description>
	<lastBuildDate>Thu, 02 Feb 2012 21:08:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Thomas Lecoz</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-25207</link>
		<dc:creator>Thomas Lecoz</dc:creator>
		<pubDate>Sun, 22 Jan 2012 05:48:00 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-25207</guid>
		<description>Hello !

Actually, it&#039;s possible to optimize it a little bit :)
intersectionRect is useless, it&#039;s defined by the bounding box of the tested object.
Math.floor is useless too if you base the whole work on  bitmapData.
Matrix calculations are useless too : only 4 substractions is needed. 
BlendMode is useless : if you base the find-color function on static colors, you can easily know (via trace) what color you&#039;re looking for if collide

Here is an exemple :


//1) At the beginning, before the collision detection process :


//bgData is the bitmapData of the background
//mcData is the bitmapData of the moving object

var bgData:BitmapData = new BitmapData(100, 100, true, 0xff123456);var mcData:BitmapData = new BitmapData(100, 100, true, 0xff987654);

//We assume that the position of the background  are 0,0
//mcx = the position x of the moving object 
//mcy = the position y of the moving object

var mcx:int = 75;var mcy:int = 95;


//I set defined colors to my objects//I want a red-color with 0.5 alpha for the backgroundbgData.colorTransform(bgData.rect, new ColorTransform(0, 0, 0, 1, 255, 0, 0, 255));
bgData.colorTransform(bgData.rect, new ColorTransform(1, 1, 1, 0.5)); 

//and a green_color with 0.5 alpha for the moving object 

mcData.colorTransform(mcData.rect, new ColorTransform(0, 0, 0, 1, 0, 255, 0, 255));			
mcData.colorTransform(mcData.rect, new ColorTransform(1, 1, 1, 0.5));//I set a new Rectangle  and a new Point that I will use in collision detection processvar rect:Rectangle = new Rectangle();var pt:Point = new Point();//I get width &amp; height of background into variablesvar bgw:int = bgData.width;var bgh:int = bgData.height;//now, still at start, I want to know what will be the collision colorvar test:BitmapData = mcData.clone();test.copyPixels(bgData,mcData.rect,new Point(),null,null,true);var collisionColor:int = test.getPixel32(1,1);//2)now that all is ready, I can process the collision detection fastly :)//I&#039;m looking for bounding  intersectionsrect.width = bdw - mcx;rect.height = bgh - mcy;if(rect.width &lt; 0 &#124;&#124; rect.height  doesn&#039;t colliderect.x = mcx;
rect.y = mcy;var productData:BitmapData = mc.clone(); productData.copyPixels(bgData, rect,pt, null, null, true);trace(&quot;collisionRect = &quot;+productData.getColorBoundsRect(0xffffffff,
collisionColor , true) );++</description>
		<content:encoded><![CDATA[<p>Hello !</p>
<p>Actually, it&#8217;s possible to optimize it a little bit :)<br />
intersectionRect is useless, it&#8217;s defined by the bounding box of the tested object.<br />
Math.floor is useless too if you base the whole work on  bitmapData.<br />
Matrix calculations are useless too : only 4 substractions is needed. <br />
BlendMode is useless : if you base the find-color function on static colors, you can easily know (via trace) what color you&#8217;re looking for if collide</p>
<p>Here is an exemple :</p>
<p>//1) At the beginning, before the collision detection process :</p>
<p>//bgData is the bitmapData of the background<br />
//mcData is the bitmapData of the moving object</p>
<p>var bgData:BitmapData = new BitmapData(100, 100, true, 0xff123456);var mcData:BitmapData = new BitmapData(100, 100, true, 0xff987654);</p>
<p>//We assume that the position of the background  are 0,0<br />
//mcx = the position x of the moving object <br />
//mcy = the position y of the moving object</p>
<p>var mcx:int = 75;var mcy:int = 95;</p>
<p>//I set defined colors to my objects//I want a red-color with 0.5 alpha for the backgroundbgData.colorTransform(bgData.rect, new ColorTransform(0, 0, 0, 1, 255, 0, 0, 255));<br />
bgData.colorTransform(bgData.rect, new ColorTransform(1, 1, 1, 0.5)); </p>
<p>//and a green_color with 0.5 alpha for the moving object </p>
<p>mcData.colorTransform(mcData.rect, new ColorTransform(0, 0, 0, 1, 0, 255, 0, 255));<br />
mcData.colorTransform(mcData.rect, new ColorTransform(1, 1, 1, 0.5));//I set a new Rectangle  and a new Point that I will use in collision detection processvar rect:Rectangle = new Rectangle();var pt:Point = new Point();//I get width &amp; height of background into variablesvar bgw:int = bgData.width;var bgh:int = bgData.height;//now, still at start, I want to know what will be the collision colorvar test:BitmapData = mcData.clone();test.copyPixels(bgData,mcData.rect,new Point(),null,null,true);var collisionColor:int = test.getPixel32(1,1);//2)now that all is ready, I can process the collision detection fastly :)//I&#8217;m looking for bounding  intersectionsrect.width = bdw &#8211; mcx;rect.height = bgh &#8211; mcy;if(rect.width &lt; 0 || rect.height  doesn&#8217;t colliderect.x = mcx;<br />
rect.y = mcy;var productData:BitmapData = mc.clone(); productData.copyPixels(bgData, rect,pt, null, null, true);trace(&#8220;collisionRect = &#8220;+productData.getColorBoundsRect(0xffffffff,<br />
collisionColor , true) );++</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Collision detection alternatives to hitTest and hitTestObject &#124;</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-24783</link>
		<dc:creator>Collision detection alternatives to hitTest and hitTestObject &#124;</dc:creator>
		<pubDate>Mon, 20 Jun 2011 21:34:57 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-24783</guid>
		<description>[...] Troy Gilbert&#8217;s Pixel-Perfect Collision [...]</description>
		<content:encoded><![CDATA[<p>[...] Troy Gilbert&#8217;s Pixel-Perfect Collision [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Milton Maciel Loyola</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-24664</link>
		<dc:creator>Milton Maciel Loyola</dc:creator>
		<pubDate>Tue, 08 Feb 2011 03:19:00 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-24664</guid>
		<description>Wow! Thanks man! Much better performance than the Collision Detection Kit.</description>
		<content:encoded><![CDATA[<p>Wow! Thanks man! Much better performance than the Collision Detection Kit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AlColella</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-24660</link>
		<dc:creator>AlColella</dc:creator>
		<pubDate>Wed, 02 Feb 2011 21:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-24660</guid>
		<description>Men! Try use &quot;this&quot; on commonParent:DisplayObjectContainer argument.
I used stage but didn´t work.
With &quot;this&quot; all works fine!!</description>
		<content:encoded><![CDATA[<p>Men! Try use &#8220;this&#8221; on commonParent:DisplayObjectContainer argument.<br />
I used stage but didn´t work.<br />
With &#8220;this&#8221; all works fine!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J.d. Ballard</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-24656</link>
		<dc:creator>J.d. Ballard</dc:creator>
		<pubDate>Thu, 27 Jan 2011 23:05:00 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-24656</guid>
		<description>Ah, nevermind, works like a charm; parent cannot be the stage object or a Sprite. Use a MovieClip.</description>
		<content:encoded><![CDATA[<p>Ah, nevermind, works like a charm; parent cannot be the stage object or a Sprite. Use a MovieClip.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J.d. Ballard</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-24655</link>
		<dc:creator>J.d. Ballard</dc:creator>
		<pubDate>Thu, 27 Jan 2011 22:17:00 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-24655</guid>
		<description>yep pixelPerfect = false returns a 0 width/height rectangle. Any fixes? I&#039;ll dig around and see if I can&#039;t fix it myself.</description>
		<content:encoded><![CDATA[<p>yep pixelPerfect = false returns a 0 width/height rectangle. Any fixes? I&#8217;ll dig around and see if I can&#8217;t fix it myself.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Hardy</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-24479</link>
		<dc:creator>Aaron Hardy</dc:creator>
		<pubDate>Tue, 28 Sep 2010 16:15:00 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-24479</guid>
		<description>I&#039;m seeing something similar to what Evan&#039;s seeing.  Take this example:

var wrapper:Sprite = new Sprite();
			addChild(wrapper);
			
			var glyph:Sprite;
			
			var existingGlyphs:Sprite = new Sprite();
			for (var i:uint; i &lt; 50; i++)
			{
				glyph = new Clef();
				glyph.x = i * 10;
				existingGlyphs.addChild(glyph);
			}
			wrapper.addChild(existingGlyphs);
			
			glyph = new Clef();
			glyph.y = 20;
			//addChild(glyph);
			//wrapper.addChild(glyph);
			
			var collision:Rectangle = PixelPerfectCollisionDetection.getCollisionRect(glyph, existingGlyphs, wrapper, true);
			//var collision:Rectangle = HitTest.complexIntersectionRectangle(existingGlyphs, glyph);
			existingGlyphs.x += collision.width;
			trace(collision);
			addChild(glyph);

Notice that glyph is not added to the stage until after the collision detection.  The traced output is (x=-14, y=-26, w=0, h=0).  However, if I add the glyph to wrapper before the detection, the output is (x=-9, y=-25, w=22, h=48) which is correct.  Note that these &quot;glyphs&quot; are symbols with a registration point in the center.  I&#039;ll try to dig into it a bit but if you post a fix that&#039;d be awesome to hear about.
</description>
		<content:encoded><![CDATA[<p>I&#8217;m seeing something similar to what Evan&#8217;s seeing.  Take this example:</p>
<p>var wrapper:Sprite = new Sprite();<br />
			addChild(wrapper);</p>
<p>			var glyph:Sprite;</p>
<p>			var existingGlyphs:Sprite = new Sprite();<br />
			for (var i:uint; i < 50; i++)<br />
			{<br />
				glyph = new Clef();<br />
				glyph.x = i * 10;<br />
				existingGlyphs.addChild(glyph);<br />
			}<br />
			wrapper.addChild(existingGlyphs);</p>
<p>			glyph = new Clef();<br />
			glyph.y = 20;<br />
			//addChild(glyph);<br />
			//wrapper.addChild(glyph);</p>
<p>			var collision:Rectangle = PixelPerfectCollisionDetection.getCollisionRect(glyph, existingGlyphs, wrapper, true);<br />
			//var collision:Rectangle = HitTest.complexIntersectionRectangle(existingGlyphs, glyph);<br />
			existingGlyphs.x += collision.width;<br />
			trace(collision);<br />
			addChild(glyph);</p>
<p>Notice that glyph is not added to the stage until after the collision detection.  The traced output is (x=-14, y=-26, w=0, h=0).  However, if I add the glyph to wrapper before the detection, the output is (x=-9, y=-25, w=22, h=48) which is correct.  Note that these &#8220;glyphs&#8221; are symbols with a registration point in the center.  I&#8217;ll try to dig into it a bit but if you post a fix that&#8217;d be awesome to hear about.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dude</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-24171</link>
		<dc:creator>Dude</dc:creator>
		<pubDate>Wed, 08 Sep 2010 11:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-24171</guid>
		<description>nice code but it doesn&#039;t seem to work well with rotated mc&#039;s</description>
		<content:encoded><![CDATA[<p>nice code but it doesn&#8217;t seem to work well with rotated mc&#8217;s</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benjamin Jaeger</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-23738</link>
		<dc:creator>Benjamin Jaeger</dc:creator>
		<pubDate>Thu, 24 Jun 2010 14:58:05 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-23738</guid>
		<description>YOU RULE&lt;br&gt;&lt;br&gt;THANKS :D</description>
		<content:encoded><![CDATA[<p>YOU RULE</p>
<p>THANKS :D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evan M</title>
		<link>http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/comment-page-1/#comment-23717</link>
		<dc:creator>Evan M</dc:creator>
		<pubDate>Tue, 18 May 2010 11:39:00 +0000</pubDate>
		<guid isPermaLink="false">http://troygilbert.com/?p=214#comment-23717</guid>
		<description>This runs great so long as I don&#039;t set the &quot;pixelPrecise&quot; argument to &quot;true&quot;. Once I do that then the collision rectangle never get made, its width and height remain at 0. I think I&#039;m implementing this correctly and I&#039;ve toyed with the tolerance and with various colliding objects, all to no effect. When the boolean is set to &quot;false&quot; this runs like a dream. Much faster than the CDK, which I&#039;ve also used and was disappointed with the ridiculous lag.</description>
		<content:encoded><![CDATA[<p>This runs great so long as I don&#39;t set the &#8220;pixelPrecise&#8221; argument to &#8220;true&#8221;. Once I do that then the collision rectangle never get made, its width and height remain at 0. I think I&#39;m implementing this correctly and I&#39;ve toyed with the tolerance and with various colliding objects, all to no effect. When the boolean is set to &#8220;false&#8221; this runs like a dream. Much faster than the CDK, which I&#39;ve also used and was disappointed with the ridiculous lag.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

