<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cold Constructs &#187; Programming</title>
	<atom:link href="http://coldconstructs.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://coldconstructs.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 30 Aug 2010 19:28:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=3539</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pixel-perfect collision detection with 5000+ particles</title>
		<link>http://coldconstructs.com/2009/11/pixel-perfect-collision-detection-with-5000-particles/</link>
		<comments>http://coldconstructs.com/2009/11/pixel-perfect-collision-detection-with-5000-particles/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 19:39:49 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[collision detection]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[particles]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://coldconstructs.com/?p=172</guid>
		<description><![CDATA[And yes, it runs perfectly fine. I&#8217;ve run it at 60+ FPS with 7,000 particles, but that actually isn&#8217;t the limitation (unless your particles are crunching heavy math for eg movement). Rather it&#8217;s the size and number of sprites that we&#8217;re colliding with the particles. Click the image below to pop a lightbox with the [...]]]></description>
			<content:encoded><![CDATA[<p>And yes, it runs perfectly fine. I&#8217;ve run it at 60+ FPS with 7,000 particles, but that actually isn&#8217;t the limitation (unless your particles are crunching heavy math for eg movement). Rather it&#8217;s the size and number of sprites that we&#8217;re colliding with the particles. Click the image below to pop a lightbox with the demo:</p>
<p><a href="http://labs.coldconstructs.com/e/vonLocalCD/bin/vonLocalCD.swf?width=600&amp;height=600" rel="prettyPhoto" title="Adjust the scroll bars at the top to change the parameters."><img src="http://labs.coldconstructs.com/i/vonlocalcd.jpg" alt="vonLocal Collision Detection" class="centered" /></a></p>
<p>To squeeze all the juice out of Flash I employed a couple tricks. <strong>The first</strong> was the particles themselves&#8211;they&#8217;re blitted to a single bitmap which is used as the source image for grabbing collision data from. The particles are also drawn with the raster engine in Flash (multiple <code>setPixel32()</code> ops to give the illusion of a line&#8230; a choppy one anyway) instead of the vector renderer (<code>lineTo()</code>). <strong>The second trick</strong> was to only grab a Vector of pixels from the regions we cared about (within sprite boundaries) every so often, then to loop through the Vector and test it against our desired conditions. Also, since the particle bitmap is more sparse than our sprites as far as opaque pixels go, we test the particle bitmap first, resulting in a lot fewer passes on the first round of conditional statements.</p>
<p><span id="more-172"></span></p>
<p>So my poor man&#8217;s method of making lines is done by interpolating the position of a particle from the previous to the current frame, for a total of 3 <code>setPixel32()</code> operations per iteration. (We can use <code>setPixel()</code> for a solid performance gain, but at a different kind of cost that I&#8217;ll explain later.) To give the particles a long tail we simply decrease the alpha of the particle bitmap a little bit every frame, making older particles fade more as time goes by. This means we can skip <code>fillRect()</code> to clear the bitmap every frame, but I&#8217;m wondering if that&#8217;s faster or slower than <code>ColorTransform</code>. Haven&#8217;t tested, for shame.</p>
<p>I&#8217;ll admit that I haven&#8217;t tried this method with bitmap-based particles but those would work just as well, in theory (using blitted rendering). One of my current projects demands those trailing things so I didn&#8217;t look into it. If you try it, please comment on this post below with your findings <img src="http://coldconstructs.com/random/pint.gif"></img><br />
<em>Click on the right-side arrow to unroll this block of code.</em></p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p172code3'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1723"><td class="code" id="p172code3"><pre class="actionscript3" style="font-family:monospace;">particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> _numParticles<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	p = particles<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// get our velocity from the perlin noise map</span>
	vel = noiseMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getPixel</span><span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span>x<span style="color: #000066; font-weight: bold;">&gt;&gt;</span>3<span style="color: #000066; font-weight: bold;">,</span> p<span style="color: #000066; font-weight: bold;">.</span>y<span style="color: #000066; font-weight: bold;">&gt;&gt;</span>3<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// because noiseMap is 1/9 the size of particleMap</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> brightness<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vel <span style="color: #000066; font-weight: bold;">/</span> 0xFFFFFF<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> speed<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = 0<span style="color: #000066; font-weight: bold;">.</span>1 <span style="color: #000066; font-weight: bold;">+</span> brightness <span style="color: #000066; font-weight: bold;">*</span> p<span style="color: #000066; font-weight: bold;">.</span>speed<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = 360 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>brightness <span style="color: #000066; font-weight: bold;">*</span> p<span style="color: #000066; font-weight: bold;">.</span>wander<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.0175</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// roughly PI / 180 to convert to radians</span>
	p<span style="color: #000066; font-weight: bold;">.</span>vx = <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> speed<span style="color: #000066; font-weight: bold;">;</span>
	p<span style="color: #000066; font-weight: bold;">.</span>vy = <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> speed<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// absolute value for velocity calculations</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> pvx<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = p<span style="color: #000066; font-weight: bold;">.</span>vx <span style="color: #000066; font-weight: bold;">&lt;</span> 0 <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #000066; font-weight: bold;">-</span>p<span style="color: #000066; font-weight: bold;">.</span>vx <span style="color: #000066; font-weight: bold;">:</span> p<span style="color: #000066; font-weight: bold;">.</span>vx<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// use bitwise sign flippage</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> pvy<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = p<span style="color: #000066; font-weight: bold;">.</span>vy <span style="color: #000066; font-weight: bold;">&lt;</span> 0 <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #000066; font-weight: bold;">-</span>p<span style="color: #000066; font-weight: bold;">.</span>vy <span style="color: #000066; font-weight: bold;">:</span> p<span style="color: #000066; font-weight: bold;">.</span>vy<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// poor man's lineTo ENGAGED</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> nx<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span>vx <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> 1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// where we are now + half the distance to where we will be</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> ny<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span>vy <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> 1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> mx<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = nx <span style="color: #000066; font-weight: bold;">-</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// then half THAT distance for a third position...</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> my<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = ny <span style="color: #000066; font-weight: bold;">-</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// actually apply velocity to pixel</span>
	p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= p<span style="color: #000066; font-weight: bold;">.</span>vx<span style="color: #000066; font-weight: bold;">;</span>
	p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= p<span style="color: #000066; font-weight: bold;">.</span>vy<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// bounds-check</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&lt;</span> 0<span style="color: #000000;">&#41;</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = h <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&gt;</span> h<span style="color: #000000;">&#41;</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">&lt;</span> 0<span style="color: #000000;">&#41;</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = w <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">&gt;</span> w<span style="color: #000000;">&#41;</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// pick our color based on speed</span>
	speed <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000000; font-weight:bold;">60</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>speed <span style="color: #000066; font-weight: bold;">&gt;</span> 255<span style="color: #000000;">&#41;</span> speed = <span style="color: #000000; font-weight:bold;">255</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #004993;">color</span> = 255 <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> 24 <span style="color: #000066; font-weight: bold;">|</span> speed <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> 16 <span style="color: #000066; font-weight: bold;">|</span> speed <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> 8 <span style="color: #000066; font-weight: bold;">|</span> speed<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// render the particle three times, each in different places to get a line-ish thing going</span>
	particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setPixel32</span><span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">color</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setPixel32</span><span style="color: #000000;">&#40;</span>mx<span style="color: #000066; font-weight: bold;">,</span> my<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">color</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setPixel32</span><span style="color: #000000;">&#40;</span>nx<span style="color: #000066; font-weight: bold;">,</span> ny<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">color</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #009900; font-style: italic;">// and we're done. release the pixels!</span>
particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">unlock</span><span style="color: #000000;">&#40;</span>_r<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>So that gives us some spotty-but-close-enough trails to help with our collision detection. It works like this: We create a single bitmap to use as a canvas for our particles. We render those particles using <code>setPixel32()</code> as mentioned above. <strong>Sprites will use this bitmap as a source for collision data.</strong> Every other frame or so (more on this later) the sprites will grab a section of pixels from the particle bitmap using something like <code>getVector()</code> (>= Flash 10) or <code>getPixels()</code> (< Flash 10), using their own position and dimensions for the <code>sourceRect</code> parameter.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p172code4'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1724"><td class="line_numbers"><pre>133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
</pre></td><td class="code" id="p172code4"><pre class="actionscript3" style="font-family:monospace;">shipVec = buffer<span style="color: #000066; font-weight: bold;">.</span>getVector<span style="color: #000000;">&#40;</span>_rc<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// get an array of pixels from the whole sprite</span>
_rc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// change rect to particleBMD's coordinate space</span>
_rc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
pixVec = _particlebmd<span style="color: #000066; font-weight: bold;">.</span>getVector<span style="color: #000000;">&#40;</span>_rc<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// grab an array of the same area of pixels that our sprite is occupying</span>
<span style="color: #6699cc; font-weight: bold;">var</span> a<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=uint%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:uint.html"><span style="color: #004993;">uint</span></a><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
buffer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> _l<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	_pval = pixVec<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// first look at the particle's image for opaque pixels</span>
	a = _pval <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>a <span style="color: #000066; font-weight: bold;">&lt;</span> 100<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">continue</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// it found an opaque-ish pixel, so let's see if there's an opaque pixel in the ship there too</span>
	_sval = shipVec<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	a = _sval <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>a <span style="color: #000066; font-weight: bold;">&lt;</span> 100<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">continue</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #3f5fbf;">/* if we get this far then we have a collision because one
	 * of the ship's pixels is overlapping a particle's pixel.
	 * we're only going to collide with red-colored particles though */</span>
	_pval = _pval <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> 16 <span style="color: #000066; font-weight: bold;">&amp;</span> 0xFF<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// red channel extraction</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>_pval <span style="color: #000066; font-weight: bold;">&gt;</span> 100<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> px<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">%</span> w<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// translate 1d array coordinate to 2d grid coordinates</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> py<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">/</span> h<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		buffer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setPixel32</span><span style="color: #000000;">&#40;</span>px<span style="color: #000066; font-weight: bold;">,</span>py<span style="color: #000066; font-weight: bold;">,</span>0xFF4e88c9<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// colliding pixels are painted on the top of our sprite in a cold blue-ish color</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
buffer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">unlock</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>As you can see on line 133 and 136, each sprite has a block of pixels ready to examine on each tick. The sprite loops through that block and compares every pixel of its own with the pixels in the Vector it got. If there's a match (eg both pixels are opaque) then look at the particle's pixel in detail to see if it's the <del datetime="2009-10-10T20:40:55+00:00">droid</del> one we're looking for and record its findings. </p>
<p>So you can alter line 151 so that the sprites check for specific colors in the particle bitmap, like anything brighter than dark grey (which I did here, or anything with an alpha greater than <code>0.1</code> or... use your imagination I guess?).</p>
<p>Because grabbing a new Vector from the particle bitmap can be so expensive (depending on the size of the sprite), you can time limit the collection so it only samples the particle bitmap once in a short while. In the demo SWF above I sample it once every 100 milliseconds or so, which comes out to about 24 samples a second (given 25 milliseconds per frame at 60 FPS)... as opposed to 2400 samples a second. The casual human eye can't tell the difference but it yields a significant increase in performance. Most performance tricks I've learned are based are smoke and mirror stuff like this.</p>
<p>You can use <code>setPixel()</code> (no alpha) but you'll <em>have</em> to test for color--obviously for a color that isn't what the background of the particle bitmap is, so it's a little more tricky. It's also totally possible to use other sprites as particles as long as you blit them (using <code>copyPixels()</code> or <code>setVector()</code>) to a single bitmap that can be checked by other sprites that collide with them. If you don't know what blitting is, 8bitRocket <a href="http://www.8bitrocket.com/newsdisplay.aspx?newspage=13430">has a good rundown</a> (it's where I first learned about it actually).</p>
<p>And that's pretty much it. You can download the example files/source below.</p>
<p><a title="download source" href="http://labs.coldconstructs.com/e/vonLocalCD.zip"><img class="centered" src="http://coldconstructs.com/siteFiles/download_button.png" alt="download source" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://coldconstructs.com/2009/11/pixel-perfect-collision-detection-with-5000-particles/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Beware the rounding error (or &#8220;Avoiding Out of Range errors&#8221;)</title>
		<link>http://coldconstructs.com/2009/10/beware-the-rounding-error/</link>
		<comments>http://coldconstructs.com/2009/10/beware-the-rounding-error/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 20:00:35 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ActionScript 3]]></category>

		<guid isPermaLink="false">http://coldconstructs.com/?p=223</guid>
		<description><![CDATA[When iterating through an array that was generated by some built-in function (such as getVector()), check to make sure you&#8217;re rounding the input first. If you get an &#8220;out of range&#8221; error but your code is fucking perfect, then it&#8217;s probably because the rectangle used as input by getVector is skipping a row of pixels [...]]]></description>
			<content:encoded><![CDATA[<p>When iterating through an array that was generated by some built-in function (such as <code>getVector()</code>), check to make sure you&#8217;re rounding the input first. If you get an &#8220;out of range&#8221; error but your code is fucking perfect, then it&#8217;s probably because the rectangle used as input by <code>getVector</code> is skipping a row of pixels because the x,y of said rectangle is being rounded UP. For example, <code>12.51</code> becomes <code>13</code> but since the rectangle is limited by certain dimensions it simply (stupidly, moronically, fucking ridiculously) truncates the dimensions by one row or column. So when it turns the pixel data into a 1D array it&#8217;s missing a whole chunk of data which causes your pre-computed (with the correct dimensions) array length to be <em>too high</em>&#8230; hence the iterator will hit a number outside the array&#8217;s range.</p>
<p>Judging by the number of results Google returns, this is a pretty fringe case (that or I&#8217;m exceptionally stupid&#8230; hush). Regardless, it&#8217;s a problem that has been plaguing me for the past few months. If it weren&#8217;t for my genius programmery brother I would have never found it either. But thanks to him I&#8217;m now savvy to what&#8217;s termed as &#8220;rounding errors&#8221;. They even <em>sound</em> evil.</p>
<p>Here&#8217;s an example with code&#8230;<br />
<span id="more-223"></span><br />
Below we define our dimensions grabbed from pixels (raw <code>bitmapData</code> from some loaded file elsewhere). We just want to analyze a quarter of the image, hence we&#8217;re dividing both dimensions by 2. We also instantiate the array (a fixed-length <code>Vector</code>) we&#8217;ll be iterating through:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p223code8'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2238"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p223code8"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> w<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> h<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> _l<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = w <span style="color: #000066; font-weight: bold;">*</span> h<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> _rc<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=rectangle%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:rectangle.html"><span style="color: #004993;">Rectangle</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=rectangle%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:rectangle.html"><span style="color: #004993;">Rectangle</span></a><span style="color: #000000;">&#40;</span>0<span style="color: #000066; font-weight: bold;">,</span> 0<span style="color: #000066; font-weight: bold;">,</span> w<span style="color: #000066; font-weight: bold;">,</span> h<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
array = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>uint<span style="color: #000066; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span>_l<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>Awesome. Now somewhere else in the code (say, an update method) we want to assign a chunk of pixel data to the array so we can pick through them one by one:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p223code9'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2239"><td class="line_numbers"><pre>6
7
8
9
10
11
12
</pre></td><td class="code" id="p223code9"><pre class="actionscript3" style="font-family:monospace;">_rc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span>
_rc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
array = pixels<span style="color: #000066; font-weight: bold;">.</span>getVector<span style="color: #000000;">&#40;</span>_rc<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> _l<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	_pixel = array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">alpha</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=uint%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:uint.html"><span style="color: #004993;">uint</span></a> = _pixel <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Alright, looks mighty fine. We&#8217;re just checkin&#8217; out the alpha value in those pixels, you know whatever. BUT WAIT. While we&#8217;re running this code, the values for <code> pixels.x</code> and <code> pixels.y</code> are changing because our source image is moving around the stage. And guess what? Without pixel snapping on, we&#8217;re getting floating point values for those coordinates <strong>even though they&#8217;re typed as <code>int</code>s</strong>. This wouldn&#8217;t normally be a problem but since we&#8217;re extracting other pixel data using those values, they need to be floored or else we&#8217;ll get the rounding error I discussed above.</p>
<p>So to fix this bitch we simply change lines 6 and 7 to:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p223code10'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p22310"><td class="line_numbers"><pre>6
7
</pre></td><td class="code" id="p223code10"><pre class="actionscript3" style="font-family:monospace;">_rc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
_rc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>&#8230;and viola, no more error. (I used <code>int()</code> instead of <code>Math.floor()</code> because it&#8217;s hella faster). So I hope that helped someone out there because I sure as hell needed it <img src='http://coldconstructs.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  Good luck and have fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://coldconstructs.com/2009/10/beware-the-rounding-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AS3 to Pixel Bender guide</title>
		<link>http://coldconstructs.com/2009/10/pixel-bender-gap-guide/</link>
		<comments>http://coldconstructs.com/2009/10/pixel-bender-gap-guide/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 22:00:23 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Discussions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Pixel Bender]]></category>

		<guid isPermaLink="false">http://coldconstructs.com/?p=134</guid>
		<description><![CDATA[

Best resources for beginners
Syntax overview
More performance tips
Realtime use of Pixel Bender

Asynchronous mode
Synchronous mode


Conclusion


When I set out to write a very simple Pixel Bender (PB) kernel/script thingy, I expected it to be relatively straight-forward, mostly because Adobe has been so good writing quality documentation for its products and/or there is a wealth of info on their [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://coldconstructs.com/2009/10/pixel-bender-gap-guide/#toc-best-resources-for-beginners">Best resources for beginners</a></li>
<li><a href="http://coldconstructs.com/2009/10/pixel-bender-gap-guide/#toc-syntax-overview">Syntax overview</a></li>
<li><a href="http://coldconstructs.com/2009/10/pixel-bender-gap-guide/#toc-more-performance-tips">More performance tips</a></li>
<li><a href="http://coldconstructs.com/2009/10/pixel-bender-gap-guide/#toc-realtime-use-of-pixel-bender">Realtime use of Pixel Bender</a>
<ol>
<li><a href="http://coldconstructs.com/2009/10/pixel-bender-gap-guide/#toc-asynchronous-mode">Asynchronous mode</a></li>
<li><a href="http://coldconstructs.com/2009/10/pixel-bender-gap-guide/#toc-synchronous-mode">Synchronous mode</a></li>
</ol>
</li>
<li><a href="http://coldconstructs.com/2009/10/pixel-bender-gap-guide/#toc-conclusion">Conclusion</a></li>
</ol>
</div>
<p>When I set out to write a very simple Pixel Bender (<em>PB</em>) kernel/script thingy, I expected it to be relatively straight-forward, mostly because Adobe has been so good writing quality documentation for its products and/or there is a wealth of info on their products produced by their users. Unfortunately I didn&#8217;t find the dev guide in the Help menu and I missed some key AS3 bits from the links I&#8217;ll post below, but even so I still had a lot of trouble finding some info that really should already have been out on the interwebs. So this post is to fill in the gaps when going from AS3 to Pixel Bender.<br />
Hope it helps <img src="http://coldconstructs.com/random/pint.gif"></p>
<h4 id="toc-best-resources-for-beginners">Best resources for beginners</h4>
<p>Here&#8217;s the best tutorials and explanations I&#8217;ve found so far:</p>
<ul>
<li><a href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS3E659D01-10C0-479d-8175-B40950BBC223.html">The official guide</a> and <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/index.html">reference</a>. Slap that F1 key! Also, don&#8217;t forget the Help menu in the toolkit like I did (there&#8217;s the PB-specific language spec and dev guide there).</li>
<li><a href="http://www.flashmagazine.com/tutorials/detail/using_pixel_bender_to_calculate_information/">Using PB as a calculator</a>. Completes the official tutorial.</li>
<li><a href="http://www.kaourantin.net/2008/05/adobe-pixel-bender-in-flash-player-10.html">Great in-depth article</a> with general+performance tips.</li>
<li>Very <a href="http://blog.gamingyourway.com/default.aspx">straight-forward examples</a> + comedy. Can&#8217;t go wrong there.</li>
<li><a href="http://www.derschmale.com/2009/07/23/some-flash-pixel-bender-performance-tips-benchmarks/">Nice user article</a> on optimizations and some speed tests (turns out, for input <code>bitmapData</code> beats <code>ByteArray </code>beats <code>Vector.<T></code>)</li>
</ul>
<h4 id="toc-syntax-overview">Syntax overview</h4>
<p>As a casual programmer of high-level languages and no mid- to low-level ones, I was thrown off by PB&#8217;s awkward syntax. It&#8217;s strongly typed, which is fine, except I&#8217;m not familiar with low-level languages like C or any previous shader language (PB is based on GLSL from what I hear). Here&#8217;s a basic difference:</p>
<blockquote><p>AS3: <code>var i:Number = 12;</code><br />
&#8230;in PB is: <code>float i = 12.0;</code><br />
The decimal in <code>12.0</code> tells PB it&#8217;s a floating point number. If it was just <code>12</code> PB would think it&#8217;s an <code>int</code>.</p></blockquote>
<p>When dealing with &#8220;vectors&#8221; (which are arrays, as in Flash 10&#8217;s odd use of the word &#8220;vector&#8221;) it&#8217;s <code>float2 i = float2(12.0, 2.0)</code>. Notice there&#8217;s no brackets or anything suggesting any type of array present. It&#8217;s simply the type + how big the array is, eg <code>float3</code>. It goes up to 4, for the 4 channels in images: Red, Green, Blue, Alpha). Then, as you can see, intializing the array is a simple matter of putting in the numbers you said would be there. So <code>float4(1.0, 24.2, 0.1, 3.4)</code> is valid whereas <code>float2(1.0, 24.2, 2)</code> is not, because there&#8217;s an extra number in there and it&#8217;s an <code>int</code> (adding insult to injury).</p>
<p><span id="more-134"></span></p>
<p>An important thing to keep in mind is that Pixel Bender sees channels in the order described above (RGBA). This little detail cost me a bit of time because I&#8217;m used to AS3&#8217;s order of ARGB&#8211;the alpha channel is first in AS3 while it&#8217;s <em>last</em> in PB.</p>
<p>To use any of the built-in functions you simply call them as they appear in the reference doc: <code>all(x); atan(x,y);</code> etc., no <code>Math</code> calls like we have to do in Flash (this through me for a loop, lemme tell ya).</p>
<p>For those of you who use the shortcut operations like <code>var i:int = foo < bar ? foo : bar;</code>, then you'll be pleased to hear they're supported in PB too. Just about the only thing "Hydra" (formally the codename for Pixel Bender, now--I guess--the name of the PB Flash implementation) does not support is loops of any kind<strong> and their related break/continue statements</strong>. That last bit is bad because there are times when I want to stop the filter if a certain condition is met. You just have to design your logic to take this into account from the start.</p>
<p>That's the basics but take special note that <code>shader.data</code> is actually an array, so you can do some naughty stuff like iterate through some kinky data and set the input as <code>shader.data['whatever'+i].input = kinky[i]</code>. Also note that in order to set the input for a shader in AS3 you <strong>must</strong> set it to <code>shader.data.vdipb.INPUT = inputImage</code> where <code>vdipb</code> is the variable name you declared in the PB shader itself.</p>
<h4 id="toc-more-performance-tips">More performance tips</h4>
<p>Turns out that after I converted my kernel from using boolean methods (like <code>boolean4()</code> etc.) to <code>if</code> statements, it consistently executed faster. I also got some fractional FPS increases by using the age-old optimization of putting real values (eg 1.2) in the evaluation statement instead of references (eg pixel.r). See the last blog I linked above for some other obvious and not-so-obvious performance tips.</p>
<p>PB math is much faster than straight AS3 math but it's slightly brighter (I wouldn't say it <em>shines</em> per se...) when it runs in asynchronous mode, which is enabled by using <code>ShaderJob</code>. You use <code>addEventListener(Event:ShaderEvent...)</code> and then run <code>start(false</code>) to put it in asynchronous mode (the param is <code>waitForCompletion</code> and it's already <code>false</code> by default). See <a href="http://www.boostworthy.com/blog/?p=243">Ryan's post</a> for a great example of how the <code>ShaderJob</code> has to be setup (<a href="http://blog.gamingyourway.com/default.aspx">Squize's post</a>, linked above, is an even clearer example).</p>
<h4 id="toc-realtime-use-of-pixel-bender">Realtime use of Pixel Bender</h4>
<h5 id="toc-asynchronous-mode">Asynchronous mode</h5>
<p>Reading the above linked performance tips for PB you, like me, might be thinking "OH MAN I can rule the universe with ShaderJob doing bitchwork like heavy collision solving" and you'd be <em>totally wrong</em>. You only have to set the input for a shader once, but you <em>do</em> have to create <strong>a new ShaderJob per computation of the filter every frame/tick</strong>. This is the depressing because <strong>ShaderJob takes a LONG time to start up</strong> and it, unlike the job itself, <em>does</em> use CPU from Flash's main thread--you do not get the asynchronous hotness until the actual job is started. And it is <em>sloooooowwwww</em>. Unless you're doing some serious number crunching or temporary special effect, you will not see a huge benefit from using PB in your game.</p>
<p>To be clear, ShaderJob works well enough for realtime usage but my experiments have shown that it's not fast enough to be executed every frame (unless you're SWF is running at 20 FPS or so). I tried to get it to fulfill my game's collision detection system by processing the entire map, but PB just couldn't handle the job. Still, I do recommend you try it (using Squize's recursive implementation) and see if it works for you. Just keep your hopes down <img src='http://coldconstructs.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h5 id="toc-synchronous-mode">Synchronous mode</h5>
<p>You can set <code>ShaderJob.start(true)</code> but if you're going the synchronous route, I suggest applying your Pixel Bender kernel by using the filter method. It starts up much faster than ShaderJob and doesn't have to be restarted every time you want to update the input. You do have to wait for it to finish processing before it will move to the next line in your code though, but as I mentioned above, PB math is a lot faster than AS3's Math library. So if you're doing something intense(r) like severe image distortion then PB is definitely worth a shot, but if it's just conditionals then you're probably better off in pure AS3 land.</p>
<h4 id="toc-conclusion">Conclusion</h4>
<p>Besides doing complex calculations, another place where PB would really help though is processing large (like 600 or 10000 pixels wide or bigger) images. It loops through each pixel faster than AS3 does. But remember that in AS3 you can do loops, so easily skipping pixels (using the <code>continue</code> statement) can result in some significant performance gains if you're not processing all the pixels in the input data (eg skipping transparent pixels). There's a lot of pro/cons so experimenting is, as always, the way to go (if you can afford it).</p>
<p>So there it is, all the info I wish was out there when I started learning PB (<em>I can't stop thinking of Peanut Butter!</em>). Hope some of it was helpful and of course I could be wrong about something (except about declaring input once, I've heard otherwise but in practice it worked fine) so please don't hesitate to correct me if so. Good luck and have fun <img src="http://coldconstructs.com/random/pint.gif"></p>
]]></content:encoded>
			<wfw:commentRss>http://coldconstructs.com/2009/10/pixel-bender-gap-guide/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
