<?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>SavageLook.com &#187; Code Demos</title>
	<atom:link href="http://savagelook.com/blog/category/code/feed" rel="self" type="application/rss+xml" />
	<link>http://savagelook.com/blog</link>
	<description>Blowing your mind, one line of code at a time</description>
	<lastBuildDate>Wed, 18 Jan 2012 13:38:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Quick Tip &#8211; C# property with abstract getter, concrete setter</title>
		<link>http://savagelook.com/blog/code/quick-tip-c-property-with-abstract-getter-concrete-setter</link>
		<comments>http://savagelook.com/blog/code/quick-tip-c-property-with-abstract-getter-concrete-setter#comments</comments>
		<pubDate>Tue, 21 Dec 2010 02:09:16 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quick Tip]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=1718</guid>
		<description><![CDATA[Here's a quick C# OOP tip for when your getter and setter need different levels of abstraction.<p><a href="http://savagelook.com/blog/code/quick-tip-c-property-with-abstract-getter-concrete-setter">Quick Tip &#8211; C# property with abstract getter, concrete setter</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><h1>The Problem</h1>
<p>On a recent .NET project I was defining an abstract class in C# when I came upon a unusual case: I needed a property that had an abstract getter, but a concrete setter.  In other words, the getter needed to be implemented by all derived classes and the setter does not, in fact its defined in the abstract class.  Nothing I like more than a good object oriented programming quandry.</p>
<p>Here&#8217;s some code to help make sense of it:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">class</span> BaseClass 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _baseValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">string</span> Value 
    <span style="color: #008000;">&#123;</span>
        get<span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">// the &quot;set&quot; will cause a compile error.  You can't define the get or set inside</span>
        <span style="color: #008080; font-style: italic;">// of an abstract property.</span>
        set 
        <span style="color: #008000;">&#123;</span>
            _baseValue <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>        
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-8526874234699762";
/* 728x90, created 12/20/10 */
google_ad_slot = "5105934733";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h1>The Solution</h1>
<p>While the above syntax will generate a compile time error, there is a fairly simple way to work around the issue.  Check out this code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">class</span> BaseClass 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _baseValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">string</span> ValueGet<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> ValueSet<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> baseValue<span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span>
        _baseValue <span style="color: #008000;">=</span> baseValue<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">string</span> Value 
    <span style="color: #008000;">&#123;</span>
        get 
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// as implemented by the derived class</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ValueGet</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        set 
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// as implemented by BaseClass, or derived class override</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ValueSet</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>        
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Here we delegated the setting and getting of the property to separate protected methods within BaseClass.  Now that 2 methods are used we can assign whether or not they are abstract separately.  The getter must be implemented by the derived classes, the setter may or may not be.  </p>
<p>And that&#8217;s it.  Obviously this code also works vice versa, with the setter being abstract and the getter being concrete.  It&#8217;s nice because from the perspective of someone using your code, <span style="color:#ff0000;">nothing changes with regards to the public Value property</span>.  The use of protected methods to defer overriding and abstraction help you avoid writing any unnecessarily redundant code in your derived classes. It&#8217;s a nice little trick to have in your pocket.</p>
<p>Happy C# OOP&#8217;ing!</p>
<p><a href="http://savagelook.com/blog/code/quick-tip-c-property-with-abstract-getter-concrete-setter">Quick Tip &#8211; C# property with abstract getter, concrete setter</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-1718"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/code/quick-tip-c-property-with-abstract-getter-concrete-setter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Offline packet capture analysis with C/C++ &amp; libpcap</title>
		<link>http://savagelook.com/blog/code/offline-packet-capture-processing-with-cc-and-libpcap</link>
		<comments>http://savagelook.com/blog/code/offline-packet-capture-processing-with-cc-and-libpcap#comments</comments>
		<pubDate>Mon, 20 Dec 2010 02:49:33 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[libpcap]]></category>
		<category><![CDATA[packet capture]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=1661</guid>
		<description><![CDATA[Programs like Wireshark can be used to save packet capture files.  Here's some code for ripping them apart<p><a href="http://savagelook.com/blog/code/offline-packet-capture-processing-with-cc-and-libpcap">Offline packet capture analysis with C/C++ &#038; libpcap</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><h1>The Overview</h1>
<p><iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=FFFFFF&#038;IS2=1&#038;npa=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=httpsavagelco-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;asins=1893939995" style="width:120px;height:240px;float:right;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe><br />
At the request of one of my faithful readers in my <a href="http://savagelook.com/blog/code/packet-capture-with-c-linux">original article on packet capture with libpcap</a>, I decided to post a guide to offline packet capture processing.  Why is this useful?  Because popular packet capture programs like <a href="http://www.wireshark.org/">Wireshark</a> or <a href="http://www.tcpdump.org/">tcpdump</a> can save captures to files that can be processed later.  You can then apply your specialized code to these previously captured packets.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-8526874234699762";
/* 468x60, created 12/19/10 */
google_ad_slot = "2496922186";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<div style="clear:both;"></div>
<h1>The Code</h1>
<p><strong>NOTE:</strong> This program makes use of the <a href="http://wiki.wireshark.org/SampleCaptures?action=AttachFile&#038;do=view&#038;target=http.cap">http.cap</a> Wireshark packet capture sample.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;pcap.h&gt;</span>
<span style="color: #339900;">#include &lt;net/ethernet.h&gt;</span>
<span style="color: #339900;">#include &lt;netinet/ip.h&gt;</span>
<span style="color: #339900;">#include &lt;netinet/in.h&gt;</span>
<span style="color: #339900;">#include &lt;netinet/tcp.h&gt;</span>
<span style="color: #339900;">#include &lt;arpa/inet.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> packetHandler<span style="color: #008000;">&#40;</span>u_char <span style="color: #000040;">*</span>userData, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> pcap_pkthdr<span style="color: #000040;">*</span> pkthdr, <span style="color: #0000ff;">const</span> u_char<span style="color: #000040;">*</span> packet<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	pcap_t <span style="color: #000040;">*</span>descr<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> errbuf<span style="color: #008000;">&#91;</span>PCAP_ERRBUF_SIZE<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// open capture file for offline processing</span>
	descr <span style="color: #000080;">=</span> pcap_open_offline<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;http.cap&quot;</span>, errbuf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>descr <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;pcap_open_live() failed: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> errbuf <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">// start packet processing loop, just like live capture</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pcap_loop<span style="color: #008000;">&#40;</span>descr, <span style="color: #0000dd;">0</span>, packetHandler, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;pcap_loop() failed: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> pcap_geterr<span style="color: #008000;">&#40;</span>descr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;capture finished&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> packetHandler<span style="color: #008000;">&#40;</span>u_char <span style="color: #000040;">*</span>userData, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> pcap_pkthdr<span style="color: #000040;">*</span> pkthdr, <span style="color: #0000ff;">const</span> u_char<span style="color: #000040;">*</span> packet<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> ether_header<span style="color: #000040;">*</span> ethernetHeader<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> ip<span style="color: #000040;">*</span> ipHeader<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> tcphdr<span style="color: #000040;">*</span> tcpHeader<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> sourceIp<span style="color: #008000;">&#91;</span>INET_ADDRSTRLEN<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> destIp<span style="color: #008000;">&#91;</span>INET_ADDRSTRLEN<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	u_int sourcePort, destPort<span style="color: #008080;">;</span>
	u_char <span style="color: #000040;">*</span>data<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> dataLength <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	string dataStr <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	ethernetHeader <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>packet<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>ntohs<span style="color: #008000;">&#40;</span>ethernetHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ether_type<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> ETHERTYPE_IP<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		ipHeader <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ip<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>packet <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		inet_ntop<span style="color: #008000;">&#40;</span>AF_INET, <span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span>ipHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ip_src<span style="color: #008000;">&#41;</span>, sourceIp, INET_ADDRSTRLEN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		inet_ntop<span style="color: #008000;">&#40;</span>AF_INET, <span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span>ipHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ip_dst<span style="color: #008000;">&#41;</span>, destIp, INET_ADDRSTRLEN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>ipHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ip_p <span style="color: #000080;">==</span> IPPROTO_TCP<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			tcpHeader <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>tcphdr<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>packet <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ip<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			sourcePort <span style="color: #000080;">=</span> ntohs<span style="color: #008000;">&#40;</span>tcpHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			destPort <span style="color: #000080;">=</span> ntohs<span style="color: #008000;">&#40;</span>tcpHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			data <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>u_char<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>packet <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ip<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> tcphdr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			dataLength <span style="color: #000080;">=</span> pkthdr<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>len <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ip<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> tcphdr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #666666;">// convert non-printable characters, other than carriage return, line feed,</span>
			<span style="color: #666666;">// or tab into periods when displayed.</span>
			<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> dataLength<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">32</span> <span style="color: #000040;">&amp;&amp;</span> data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">126</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">10</span> <span style="color: #000040;">||</span> data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">11</span> <span style="color: #000040;">||</span> data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">13</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
					dataStr <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span>data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
					dataStr <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;.&quot;</span><span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #666666;">// print the results</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> sourceIp <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> sourcePort <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; -&gt; &quot;</span> <span style="color: #000080;">&lt;&lt;</span> destIp <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> destPort <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>dataLength <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> dataStr <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-8526874234699762";
/* 728x90, created 12/19/10 */
google_ad_slot = "3702771303";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h1>The Breakdown</h1>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;pcap.h&gt;</span>
<span style="color: #339900;">#include &lt;net/ethernet.h&gt;</span>
<span style="color: #339900;">#include &lt;netinet/ip.h&gt;</span>
<span style="color: #339900;">#include &lt;netinet/in.h&gt;</span>
<span style="color: #339900;">#include &lt;netinet/tcp.h&gt;</span>
<span style="color: #339900;">#include &lt;arpa/inet.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> packetHandler<span style="color: #008000;">&#40;</span>u_char <span style="color: #000040;">*</span>userData, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> pcap_pkthdr<span style="color: #000040;">*</span> pkthdr, <span style="color: #0000ff;">const</span> u_char<span style="color: #000040;">*</span> packet<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>These are the includes and declarations necessary for reading the packet captures.  The first 2 are self explanatory, the following 5 includes might be less so.  These are used for parsing and transforming data found in packets.  The functions and structures included in these headers are integral to packet processing and are available natively on Linux systems (Ubuntu in this case).</p>
<hr /></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	pcap_t <span style="color: #000040;">*</span>descr<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> errbuf<span style="color: #008000;">&#91;</span>PCAP_ERRBUF_SIZE<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// open capture file for offline processing</span>
	descr <span style="color: #000080;">=</span> pcap_open_offline<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;http.cap&quot;</span>, errbuf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>descr <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;pcap_open_live() failed: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> errbuf <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span></pre></div></div>

<p>After entering the main execution, we go straight to opening our target packet capture file, <a href="http://wiki.wireshark.org/SampleCaptures?action=AttachFile&#038;do=view&#038;target=http.cap">http.cap</a>.  To do this we use <a href="http://www.manpagez.com/man/3/pcap_open_offline/">pcap_open_offline()</a> and give it the capture filename and an error buffer as parameters.  If all goes well, we get a pcap_t descriptor returned.  If not, check the error buffer for details.</p>
<hr /></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">        <span style="color: #666666;">// start packet processing loop, just like live capture</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pcap_loop<span style="color: #008000;">&#40;</span>descr, <span style="color: #0000dd;">0</span>, packetHandler, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;pcap_loop() failed: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> pcap_geterr<span style="color: #008000;">&#40;</span>descr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;capture finished&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Just like in a live packet capture, we use <a href="http://www.manpagez.com/man/3/pcap_loop/">pcap_loop()</a> to set up a handler callback for each packet to be processed. We give it the following:</p>
<ul>
<li>descr &#8211; the descriptor we just created with <a href="http://www.manpagez.com/man/3/pcap_open_offline/">pcap_open_offline()</a></li>
<li>count &#8211; 0 (zero), to indicate there is no limit to the number of packets we want to process</li>
<li>callback &#8211; The name of our packet handler function</li>
<li>userdata &#8211; NULL, to indicate that we will be passing no user defined data to the callack</li>
</ul>
<p>When the entire file has been processed, we will print the &#8220;capture complete&#8221; message and then exit.</p>
<hr /></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">void</span> packetHandler<span style="color: #008000;">&#40;</span>u_char <span style="color: #000040;">*</span>userData, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> pcap_pkthdr<span style="color: #000040;">*</span> pkthdr, <span style="color: #0000ff;">const</span> u_char<span style="color: #000040;">*</span> packet<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> ether_header<span style="color: #000040;">*</span> ethernetHeader<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> ip<span style="color: #000040;">*</span> ipHeader<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">struct</span> tcphdr<span style="color: #000040;">*</span> tcpHeader<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> sourceIp<span style="color: #008000;">&#91;</span>INET_ADDRSTRLEN<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> destIp<span style="color: #008000;">&#91;</span>INET_ADDRSTRLEN<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	u_int sourcePort, destPort<span style="color: #008080;">;</span>
	u_char <span style="color: #000040;">*</span>data<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> dataLength <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	string dataStr <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Here we define the packet handler callback, as per the libpcap specifications.  For more details, check out my <a href="http://savagelook.com/blog/code/packet-capture-with-c-linux">original post on packet capture</a>.  The following declarations define variables that will help us parse meaningful data out of the packets.  These include packet header data, IP addresses, source/destination ports, and payload data.  </p>
<p>There&#8217;s LOTS more useful information to be analyzed from the average packet.  Check out the structure defined in the network includes at the beginning of the code for more details.  Actually, it would probably be a hell of a lot easier to just download and fire up <a href="http://www.wireshark.org/">Wireshark</a>.  It will give you a greater appreciation for what can be learned from a packet.</p>
<hr /></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">        ethernetHeader <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>packet<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>ntohs<span style="color: #008000;">&#40;</span>ethernetHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ether_type<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> ETHERTYPE_IP<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		ipHeader <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ip<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>packet <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		inet_ntop<span style="color: #008000;">&#40;</span>AF_INET, <span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span>ipHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ip_src<span style="color: #008000;">&#41;</span>, sourceIp, INET_ADDRSTRLEN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		inet_ntop<span style="color: #008000;">&#40;</span>AF_INET, <span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span>ipHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ip_dst<span style="color: #008000;">&#41;</span>, destIp, INET_ADDRSTRLEN<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>I&#8217;m not going to delve to deeply into the specifics of network protocols, as that could be a post&#8230; check that&#8230; that could be a book of its own.  Basically here we are parsing the ethernet header from the packet and using its type to determine if it is an IP packet or not.  We use the <a href="http://linux.about.com/library/cmd/blcmdl3_ntohs.htm">ntohs()</a> to convert the type from network byte order to host byte order.  </p>
<p>If it is an IP packet, we parse out the IP header and use the <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/inet_ntop.html">inet_ntop()</a> function to convert the IP addresses found in the IP header into a human readable format (i.e., xxx.xxx.xxx.xxx).  <span style="color:#ff0000">In a lot of older examples you&#8217;ll see the use of <a href="http://beej.us/guide/bgnet/output/html/multipage/inet_ntoaman.html">inet_ntoa()</a>, but this is not thread-safe and is deprecated.</span></p>
<hr /></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>ipHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ip_p <span style="color: #000080;">==</span> IPPROTO_TCP<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			tcpHeader <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>tcphdr<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>packet <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ip<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			sourcePort <span style="color: #000080;">=</span> ntohs<span style="color: #008000;">&#40;</span>tcpHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			destPort <span style="color: #000080;">=</span> ntohs<span style="color: #008000;">&#40;</span>tcpHeader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			data <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>u_char<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>packet <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ip<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> tcphdr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			dataLength <span style="color: #000080;">=</span> pkthdr<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>len <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ether_header<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> ip<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> tcphdr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Similar to above, I use the IP header to determine if this is a TCP packet (they all should be since its a HTTP capture) and then parse out the TCP header.  With the TCP header we can then determine the source and destination ports, with <a href="http://linux.about.com/library/cmd/blcmdl3_ntohs.htm">ntohs()</a> again, and then determine the contents of the packet payload.</p>
<hr /></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">			<span style="color: #666666;">// convert non-printable characters, other than carriage return, line feed,</span>
			<span style="color: #666666;">// or tab into periods when displayed.</span>
			<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> dataLength<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">32</span> <span style="color: #000040;">&amp;&amp;</span> data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">126</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">10</span> <span style="color: #000040;">||</span> data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">11</span> <span style="color: #000040;">||</span> data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">13</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
					dataStr <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span>data<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
					dataStr <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;.&quot;</span><span style="color: #008080;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #666666;">// print the results</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> sourceIp <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> sourcePort <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; -&gt; &quot;</span> <span style="color: #000080;">&lt;&lt;</span> destIp <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> destPort <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>dataLength <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> dataStr <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In the final step of the packet handler we display the results of our rudimentary analysis.  First we iterate through the bytes of the payload and save it in a format that is human friendly.  If you try to print it out with the non-printable characters in there you will get some very messy results in your console.  After this cleanup we simply output the packet data we have extracted and display it in the console.</p>
<h1>The Summary</h1>
<p>So now that you can process packets offline, what do you want to do with them?  I don&#8217;t know about you, but aside from obvious applications to network analysis, <span style="color:#ff0000;">I&#8217;d like to use this data for trending, visualization, or even generative art and sound</span>.  But then again I&#8217;m weird.  What are you gonna do? </p>
<p><a href="http://savagelook.com/blog/code/offline-packet-capture-processing-with-cc-and-libpcap">Offline packet capture analysis with C/C++ &#038; libpcap</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-1661"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/code/offline-packet-capture-processing-with-cc-and-libpcap/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Create Your Own QR Code</title>
		<link>http://savagelook.com/blog/code/create-your-own-qr-code</link>
		<comments>http://savagelook.com/blog/code/create-your-own-qr-code#comments</comments>
		<pubDate>Thu, 09 Dec 2010 16:54:27 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[Google API]]></category>
		<category><![CDATA[qr]]></category>
		<category><![CDATA[qr code]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=1420</guid>
		<description><![CDATA[Learn how to make your own scannable QR code in about 5 seconds, or create a generator in about 30.<p><a href="http://savagelook.com/blog/code/create-your-own-qr-code">Create Your Own QR Code</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><h1>The Generator</h1>
<table>
<tr>
<td><strong>Text for QR Code:</strong><br />
<form onsubmit="return false;">
<input type="text" id="qrvalue" value="http://savagelook.com" style="width:250px;"></input>
<p><button onclick="document.getElementById('qrimg').src = 'http://chart.apis.google.com/chart?cht=qr&#038;chs=150x150&#038;chl=' + document.getElementById('qrvalue').value;">Encode</button></form>
</td>
<td><img id="qrimg" src="http://chart.apis.google.com/chart?cht=qr&#038;chs=150x150&#038;chl=http://savagelook.com"/></td>
</tr>
<tr>
<td colspan="2"><script type="text/javascript"><!--
google_ad_client = "pub-8526874234699762";
/* 728x90, created 12/19/10 */
google_ad_slot = "6406672624";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></td>
</tr>
</table>
<h1>The Overview</h1>
<p>QR codes are basically just bar codes on steroids.  They allow you to encode up to 4,296 characters in a format that can be read by most modern bar code scanning devices.  More specifically, QR codes make it much simpler to direct smart phone users to your website (or anywhere else you want).  </p>
<p>So why do you want to do this?  Here&#8217;s a few reasons that range in practicality:</p>
<ul>
<li>Direct mobile phones to personally hosted mobile applications (no need for market fees)</li>
<li>Direct users from physical storefronts or magazine articles to your website</li>
<li>Leave encoded messages for mobile users</li>
<li>To be super cool and trendy</li>
</ul>
<h1>The Code</h1>
<p>Here&#8217;s the Google Chart API URL you hit in order to create your own QR code.  Pop it into a browser and it will return your image.  There are more optional criteria you can send, which are detailed in the <a href="http://code.google.com/apis/chart/docs/gallery/qr_codes.html">API&#8217;s QR code section</a>.  Be sure to change the sections highlighted in <span style="color:#ff0000;">RED</span> to the values that fit your needs.</p>
<div style="border:1px solid #bbbbbb; background-color:#eeeeee; padding-left:15px;">
http://chart.apis.google.com/chart?cht=qr&#038;chs=<span style="color:#ff0000;">HEIGHT</span>x<span style="color:#ff0000;">WIDTH</span>&#038;chl=<span style="color:#ff0000;">YOURTEXT</span>
</div>
<p>Now let&#8217;s say you wanted to copy my website and have your own generator.  It&#8217;s pretty simple.  Here&#8217;s the tiny bit of HTML and javascript I used to make it happen:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>Text for QR Code:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> 
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">onsubmit</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;return false;&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;qrvalue&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://savagelook.com&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:250px;&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;document.getElementById('qrimg').src = 'http://chart.apis.google.com/chart?cht=qr&amp;chs=150x150&amp;chl=' + document.getElementById('qrvalue').value;&quot;</span>&gt;</span>Encode<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;qrimg&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://chart.apis.google.com/chart?cht=qr&amp;chs=150x150&amp;chl=http://savagelook.com&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></div></div>

<p>And here&#8217;s another version from <a href="http://ericharrison.info/qr/">Eric Harrison</a> with no tables</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;padding:15px 50px;&quot;</span>&gt;</span> 
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;qrimg&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://chart.apis.google.com/chart?cht=qr&amp;chs=150x150&amp;chl=http://savagelook.com&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;float:left;margin-right:25px;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> 
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">onsubmit</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;return false;&quot;</span>&gt;</span> 
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;qrvalue&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://savagelook.com&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:60%;font-size:125%;&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span> 
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;padding:5px;font-size:125%;margin-top:10px;&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;document.getElementById('qrimg').src = 'http://chart.apis.google.com/chart?cht=qr&amp;chs=150x150&amp;chl=' + document.getElementById('qrvalue').value;&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Encode&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> 
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span> 
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;clear:both;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> 
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>As you can see, its not rocket science, but it sure can add a little techie flare.  Have fun and be sure to let me know if you add a QR coding to your geek repetoire!</p>
<p><a href="http://savagelook.com/blog/code/create-your-own-qr-code">Create Your Own QR Code</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-1420"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/code/create-your-own-qr-code/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Box2D JS &#8211; Physics in HTML5 &amp; Javascript Guide</title>
		<link>http://savagelook.com/blog/code/box2d-js-physics-in-html5-javascript-guide</link>
		<comments>http://savagelook.com/blog/code/box2d-js-physics-in-html5-javascript-guide#comments</comments>
		<pubDate>Mon, 22 Nov 2010 04:29:01 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[box2d]]></category>
		<category><![CDATA[physics]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=1092</guid>
		<description><![CDATA[Here's how to use the Box2D JS library to do 2D physics with Javascript and HTML5. <p><a href="http://savagelook.com/blog/code/box2d-js-physics-in-html5-javascript-guide">Box2D JS &#8211; Physics in HTML5 &#038; Javascript Guide</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p style="text-align: center;"><a href="http://savagelook.com/demos/box2djs/helloworld.html" rel="shadowbox;width=800;height=500;"><img class="aligncenter size-full wp-image-1099" title="Box2D JS Hello World" src="http://savagelook.com/blog/wp-content/uploads/2010/11/helloworld_medium.jpg" alt="Box2D JS Hello World" width="624" height="275" /></a></p>
<p><strong>I hate to do it, but I highly recommend the <span style="color:#ff0000;">Google Chrome</span> browser for this demo.  In other browsers it may take a while to load and runs slower.</strong></p>
<p>&rarr; Click on the demo above or <a href="http://savagelook.com/demos/box2djs/helloworld.html">here</a>, then right click to view source<br />
&rarr; Download the <a href="http://sourceforge.net/projects/box2d-js/files/" target="_blank">Box2D JS library</a><br />
&rarr; Download the <a href="http://savagelook.com/demos/box2djs/box2djs.js">concatenated version</a> (~350KB)<br />
&rarr; Download the <a href="http://savagelook.com/demos/box2djs/box2djs.min.js">minified version</a> (~170 KB)</p>
<h1>The Overview</h1>
<p>Some of you might remember the <a href="http://savagelook.com/blog/code/box2dflashas3-fun">Box2DFlashAS3 demo</a> I did a while ago.  Well here&#8217;s its HTML5 counter-part, with the help of <a href="http://box2d-js.sourceforge.net/" target="_blank">Box2D JS</a>.  Box2D is the Javascript port of the <a href="http://box2dflash.sourceforge.net/" target="_blank">Box2DFlashAS3 library</a>, which in turn is a port of the <a href="http://www.box2d.org/">Box2D C++ library</a>.  Put simply, this library allows you to apply 2 dimensional physics to objects on your HTML5 canvas element.  Just click anywhere on the demo above to see what I mean.</p>
<p>The code behind this is a condensed, easier-to-follow version of the demo code available by viewing the page source at the <a href="http://box2d-js.sourceforge.net/" target="_blank">Box2D JS site</a>.  I personally found it a little tough to follow initially and really wanted to avoid the large amount of individual includes necessary to get it working.  To that end I created the concatenated and minified versions downloadable above.  So basically the includes necessary go from this:</p>
<div style="height:300px; overflow:scroll;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">if</span> IE<span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/excanvas.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;&lt;!</span><span style="color: #009900;">&#91;</span>endif<span style="color: #009900;">&#93;</span><span style="color: #339933;">--&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/prototype-1.6.0.2.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
&nbsp;
<span style="color: #339933;">&lt;!--</span> box2djs <span style="color: #339933;">--&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/common/b2Settings.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/common/math/b2Vec2.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/common/math/b2Mat22.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/common/math/b2Math.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2AABB.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2Bound.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2BoundValues.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2Pair.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2PairCallback.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2BufferedPair.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2PairManager.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2BroadPhase.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2Collision.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/Features.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2ContactID.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2ContactPoint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2Distance.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2Manifold.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2OBB.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/b2Proxy.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/ClipVertex.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/shapes/b2Shape.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/shapes/b2ShapeDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/shapes/b2BoxDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/shapes/b2CircleDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/shapes/b2CircleShape.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/shapes/b2MassData.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/shapes/b2PolyDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/collision/shapes/b2PolyShape.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/b2Body.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/b2BodyDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/b2CollisionFilter.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/b2Island.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/b2TimeStep.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2ContactNode.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2Contact.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2ContactConstraint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2ContactConstraintPoint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2ContactRegister.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2ContactSolver.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2CircleContact.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2Conservative.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2NullContact.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2PolyAndCircleContact.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/contacts/b2PolyContact.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/b2ContactManager.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/b2World.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/b2WorldListener.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2JointNode.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2Joint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2JointDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2DistanceJoint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2DistanceJointDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2Jacobian.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2GearJoint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2GearJointDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2MouseJoint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2MouseJointDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2PrismaticJoint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2PrismaticJointDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2PulleyJoint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2PulleyJointDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2RevoluteJoint.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">'js/box2d/dynamics/joints/b2RevoluteJointDef.js'</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

</div>
<p>to this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">if</span> IE<span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/excanvas.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;&lt;!</span><span style="color: #009900;">&#91;</span>endif<span style="color: #009900;">&#93;</span><span style="color: #339933;">--&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/prototype-1.6.0.2.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;box2djs.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>You still need to download Box2D JS for its dependencies on <a href="http://www.prototypejs.org/" target="_blank">Prototype</a> and <a href="http://excanvas.sourceforge.net/" target="_blank">excanvas</a>, but using my single file versions of the library will make getting started much cleaner and easier.  OK, enough of the back story, on to the code.</p>
<h1>The Code</h1>
<div style="height:400px; overflow:scroll;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
    &lt;head&gt;
	&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
	&lt;title&gt;SavageLook.com - Box2D JS Hello World&lt;/title&gt;
	&lt;!--[if IE]&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;lib/excanvas.js&quot;&gt;&lt;/script&gt;&lt;![endif]--&gt;
	&lt;script src=&quot;lib/prototype-1.6.0.2.js&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;box2djs.min.js&quot;&gt;&lt;/script&gt;
&nbsp;
	<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #003366; font-weight: bold;">var</span> world<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> ctx<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> canvasWidth<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> canvasHeight<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> canvasTop<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> canvasLeft<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> drawWorld<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> world.<span style="color: #660066;">m_jointList</span><span style="color: #339933;">;</span> j<span style="color: #339933;">;</span> j <span style="color: #339933;">=</span> j.<span style="color: #660066;">m_next</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			drawJoint<span style="color: #009900;">&#40;</span>j<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> world.<span style="color: #660066;">m_bodyList</span><span style="color: #339933;">;</span> b<span style="color: #339933;">;</span> b <span style="color: #339933;">=</span> b.<span style="color: #660066;">m_next</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> b.<span style="color: #660066;">GetShapeList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> s <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span> s <span style="color: #339933;">=</span> s.<span style="color: #660066;">GetNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				drawShape<span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 18px arial'</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">textAlign</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'center'</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#000000'</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Click the screen to add more objects&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 14px arial'</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Performance will vary by browser&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> drawJoint<span style="color: #009900;">&#40;</span>joint<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> b1 <span style="color: #339933;">=</span> joint.<span style="color: #660066;">m_body1</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> b2 <span style="color: #339933;">=</span> joint.<span style="color: #660066;">m_body2</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> x1 <span style="color: #339933;">=</span> b1.<span style="color: #660066;">m_position</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> x2 <span style="color: #339933;">=</span> b2.<span style="color: #660066;">m_position</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> p1 <span style="color: #339933;">=</span> joint.<span style="color: #660066;">GetAnchor1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> joint.<span style="color: #660066;">GetAnchor2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00eeee'</span><span style="color: #339933;">;</span>
		context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>joint.<span style="color: #660066;">m_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">case</span> b2Joint.<span style="color: #660066;">e_distanceJoint</span><span style="color: #339933;">:</span>
			context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">case</span> b2Joint.<span style="color: #660066;">e_pulleyJoint</span><span style="color: #339933;">:</span>
			<span style="color: #006600; font-style: italic;">// TODO</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">==</span> world.<span style="color: #660066;">m_groundBody</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> x2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b2 <span style="color: #339933;">==</span> world.<span style="color: #660066;">m_groundBody</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> x1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>x1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> x1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> x2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> drawShape<span style="color: #009900;">&#40;</span>shape<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#ffffff'</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>shape.<span style="color: #660066;">density</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;black&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>shape.<span style="color: #660066;">m_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">case</span> b2Shape.<span style="color: #660066;">e_circleShape</span><span style="color: #339933;">:</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> circle <span style="color: #339933;">=</span> shape<span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> pos <span style="color: #339933;">=</span> circle.<span style="color: #660066;">m_position</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> circle.<span style="color: #660066;">m_radius</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> segments <span style="color: #339933;">=</span> <span style="color: #CC0000;">16.0</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> theta <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> dtheta <span style="color: #339933;">=</span> <span style="color: #CC0000;">2.0</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">/</span> segments<span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// draw circle</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> r<span style="color: #339933;">,</span> pos.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> segments<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2Vec2<span style="color: #009900;">&#40;</span>r <span style="color: #339933;">*</span> Math.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> r <span style="color: #339933;">*</span> Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003366; font-weight: bold;">var</span> v <span style="color: #339933;">=</span> b2Math.<span style="color: #660066;">AddVV</span><span style="color: #009900;">&#40;</span>pos<span style="color: #339933;">,</span> d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>v.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> v.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					theta <span style="color: #339933;">+=</span> dtheta<span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> r<span style="color: #339933;">,</span> pos.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// draw radius</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> pos.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> ax <span style="color: #339933;">=</span> circle.<span style="color: #660066;">m_R</span>.<span style="color: #660066;">col1</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> pos2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2Vec2<span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> r <span style="color: #339933;">*</span> ax.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> pos.<span style="color: #660066;">y</span> <span style="color: #339933;">+</span> r <span style="color: #339933;">*</span> ax.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>pos2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> pos2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">case</span> b2Shape.<span style="color: #660066;">e_polyShape</span><span style="color: #339933;">:</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> poly <span style="color: #339933;">=</span> shape<span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> tV <span style="color: #339933;">=</span> b2Math.<span style="color: #660066;">AddVV</span><span style="color: #009900;">&#40;</span>poly.<span style="color: #660066;">m_position</span><span style="color: #339933;">,</span> b2Math.<span style="color: #660066;">b2MulMV</span><span style="color: #009900;">&#40;</span>poly.<span style="color: #660066;">m_R</span><span style="color: #339933;">,</span> poly.<span style="color: #660066;">m_vertices</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>tV.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> tV.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> poly.<span style="color: #660066;">m_vertexCount</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> v <span style="color: #339933;">=</span> b2Math.<span style="color: #660066;">AddVV</span><span style="color: #009900;">&#40;</span>poly.<span style="color: #660066;">m_position</span><span style="color: #339933;">,</span> b2Math.<span style="color: #660066;">b2MulMV</span><span style="color: #009900;">&#40;</span>poly.<span style="color: #660066;">m_R</span><span style="color: #339933;">,</span> poly.<span style="color: #660066;">m_vertices</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>v.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> v.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>tV.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> tV.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> createWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> worldAABB <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2AABB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		worldAABB.<span style="color: #660066;">minVertex</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		worldAABB.<span style="color: #660066;">maxVertex</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> gravity <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2Vec2<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> doSleep <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		world <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2World<span style="color: #009900;">&#40;</span>worldAABB<span style="color: #339933;">,</span> gravity<span style="color: #339933;">,</span> doSleep<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createGround<span style="color: #009900;">&#40;</span>world<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> world<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>		
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> createGround<span style="color: #009900;">&#40;</span>world<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> groundSd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BoxDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		groundSd.<span style="color: #660066;">extents</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		groundSd.<span style="color: #660066;">restitution</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> groundBd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BodyDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		groundBd.<span style="color: #660066;">AddShape</span><span style="color: #009900;">&#40;</span>groundSd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		groundBd.<span style="color: #660066;">position</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">470</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> world.<span style="color: #660066;">CreateBody</span><span style="color: #009900;">&#40;</span>groundBd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> createBall<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> ballSd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2CircleDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ballSd.<span style="color: #660066;">density</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">;</span>
		ballSd.<span style="color: #660066;">radius</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">;</span>
		ballSd.<span style="color: #660066;">restitution</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
		ballSd.<span style="color: #660066;">friction</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> ballBd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BodyDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ballBd.<span style="color: #660066;">AddShape</span><span style="color: #009900;">&#40;</span>ballSd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ballBd.<span style="color: #660066;">position</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> world.<span style="color: #660066;">CreateBody</span><span style="color: #009900;">&#40;</span>ballBd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> createHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// H</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">420</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">420</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">395</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">370</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">370</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// E</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">120</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">420</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">130</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">120</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// L</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">185</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// L</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">235</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// O</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">285</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">315</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// W</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">360</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">420</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">415</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// O</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">460</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">445</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">475</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">460</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// R</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">495</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">410</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">518</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">425</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">515</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">525</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">510</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// L</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">560</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">545</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// D</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">610</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">595</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">625</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">610</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// !</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">650</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">430</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">650</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">380</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> fixed<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>fixed<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> fixed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> boxSd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BoxDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>fixed<span style="color: #009900;">&#41;</span> boxSd.<span style="color: #660066;">density</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">;</span> 
		boxSd.<span style="color: #660066;">restitution</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span>
		boxSd.<span style="color: #660066;">friction</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">;</span>
		boxSd.<span style="color: #660066;">extents</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span>width<span style="color: #339933;">,</span> height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> boxBd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BodyDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		boxBd.<span style="color: #660066;">AddShape</span><span style="color: #009900;">&#40;</span>boxSd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		boxBd.<span style="color: #660066;">position</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> world.<span style="color: #660066;">CreateBody</span><span style="color: #009900;">&#40;</span>boxBd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> step<span style="color: #009900;">&#40;</span>cnt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> stepping <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> timeStep <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">/</span><span style="color: #CC0000;">60</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> iteration <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
		world.<span style="color: #660066;">Step</span><span style="color: #009900;">&#40;</span>timeStep<span style="color: #339933;">,</span> iteration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> canvasWidth<span style="color: #339933;">,</span> canvasHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		drawWorld<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setTimeout<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'step('</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>cnt <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// main entry point</span>
	Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		world <span style="color: #339933;">=</span> createWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ctx <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> canvasElm <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		canvasWidth <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>canvasElm.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		canvasHeight <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>canvasElm.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		canvasTop <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>canvasElm.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		canvasLeft <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>canvasElm.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		createHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0.5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #006600; font-style: italic;">//createBox(world, Event.pointerX(e), Event.pointerY(e), 10, 10, false);</span>
					createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> e.<span style="color: #660066;">clientX</span><span style="color: #339933;">,</span> e.<span style="color: #660066;">clientY</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
					createBall<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> Event.<span style="color: #660066;">pointerX</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> Event.<span style="color: #660066;">pointerY</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		step<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
    &lt;/head&gt;
    &lt;body style=&quot;margin:0px;&quot;&gt;
        &lt;canvas id=&quot;canvas&quot; width='800' height='500' style=&quot;background-color:#eeeeee;&quot;&gt;&lt;/canvas&gt;
    &lt;/body&gt;
 &lt;/html&gt;</pre></div></div>

</div>
<hr />
<h1>The Breakdown</h1>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #003366; font-weight: bold;">PUBLIC</span> <span style="color: #3366CC;">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span style="color: #3366CC;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html xmlns<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/html; charset=utf-8&quot;</span> <span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>SavageLook.<span style="color: #660066;">com</span> <span style="color: #339933;">-</span> Box2D JS Hello World<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;!--</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">if</span> IE<span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/excanvas.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;&lt;!</span><span style="color: #009900;">&#91;</span>endif<span style="color: #009900;">&#93;</span><span style="color: #339933;">--&gt;</span>
	<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;lib/prototype-1.6.0.2.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;box2djs.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>We start by including the scripts necessary to make Box2D JS work.  In order, we need excanvas (included in the Box2D JS distribution) in order to account for the fact that all current released version of Internet Explorer do not support the HTML canvas element.  Next we include the Prototype Javascript framework, also included with Box2D JS.  Finally we include my minified version of the library.  Now we can get started building physics into our canvas element. </p>
<hr />
<div style="height:300px; overflow:scroll;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #003366; font-weight: bold;">var</span> world<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> ctx<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> canvasWidth<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> canvasHeight<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> canvasTop<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> canvasLeft<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> drawWorld<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> world.<span style="color: #660066;">m_jointList</span><span style="color: #339933;">;</span> j<span style="color: #339933;">;</span> j <span style="color: #339933;">=</span> j.<span style="color: #660066;">m_next</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			drawJoint<span style="color: #009900;">&#40;</span>j<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> world.<span style="color: #660066;">m_bodyList</span><span style="color: #339933;">;</span> b<span style="color: #339933;">;</span> b <span style="color: #339933;">=</span> b.<span style="color: #660066;">m_next</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> b.<span style="color: #660066;">GetShapeList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> s <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span> s <span style="color: #339933;">=</span> s.<span style="color: #660066;">GetNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				drawShape<span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 18px arial'</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">textAlign</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'center'</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#000000'</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Click the screen to add more objects&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 14px arial'</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Performance will vary by browser&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>Here we declare our global variables that define the &#8220;world&#8221; the physics exist in and the context, dimensions, and position of the canvas element.</p>
<p>Also we have our drawWorld() function that will, as the name implies, draw the shapes and joints that compose the Box2D JS world.  Each of these objects is iterated through and drawn individually.  They are added to the world with the createBody() function.  An important thing to note is that in the case of this demo, drawWorld() will be called with each &#8220;step&#8221;.  Think of your canvas as an animation and each call to drawWorld() as a frame.  Should be a simple concept for you Flash devs out there <img src='http://savagelook.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<hr />
<div style="height:300px; overflow:scroll;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #003366; font-weight: bold;">function</span> drawJoint<span style="color: #009900;">&#40;</span>joint<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> b1 <span style="color: #339933;">=</span> joint.<span style="color: #660066;">m_body1</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> b2 <span style="color: #339933;">=</span> joint.<span style="color: #660066;">m_body2</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> x1 <span style="color: #339933;">=</span> b1.<span style="color: #660066;">m_position</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> x2 <span style="color: #339933;">=</span> b2.<span style="color: #660066;">m_position</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> p1 <span style="color: #339933;">=</span> joint.<span style="color: #660066;">GetAnchor1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> p2 <span style="color: #339933;">=</span> joint.<span style="color: #660066;">GetAnchor2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00eeee'</span><span style="color: #339933;">;</span>
		context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>joint.<span style="color: #660066;">m_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">case</span> b2Joint.<span style="color: #660066;">e_distanceJoint</span><span style="color: #339933;">:</span>
			context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">case</span> b2Joint.<span style="color: #660066;">e_pulleyJoint</span><span style="color: #339933;">:</span>
			<span style="color: #006600; font-style: italic;">// TODO</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b1 <span style="color: #339933;">==</span> world.<span style="color: #660066;">m_groundBody</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> x2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b2 <span style="color: #339933;">==</span> world.<span style="color: #660066;">m_groundBody</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> x1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>x1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> x1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>p1.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p1.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> x2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> drawShape<span style="color: #009900;">&#40;</span>shape<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#ffffff'</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>shape.<span style="color: #660066;">density</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;black&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>shape.<span style="color: #660066;">m_type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">case</span> b2Shape.<span style="color: #660066;">e_circleShape</span><span style="color: #339933;">:</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> circle <span style="color: #339933;">=</span> shape<span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> pos <span style="color: #339933;">=</span> circle.<span style="color: #660066;">m_position</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> circle.<span style="color: #660066;">m_radius</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> segments <span style="color: #339933;">=</span> <span style="color: #CC0000;">16.0</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> theta <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> dtheta <span style="color: #339933;">=</span> <span style="color: #CC0000;">2.0</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">/</span> segments<span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// draw circle</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> r<span style="color: #339933;">,</span> pos.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> segments<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2Vec2<span style="color: #009900;">&#40;</span>r <span style="color: #339933;">*</span> Math.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> r <span style="color: #339933;">*</span> Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003366; font-weight: bold;">var</span> v <span style="color: #339933;">=</span> b2Math.<span style="color: #660066;">AddVV</span><span style="color: #009900;">&#40;</span>pos<span style="color: #339933;">,</span> d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>v.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> v.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					theta <span style="color: #339933;">+=</span> dtheta<span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> r<span style="color: #339933;">,</span> pos.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// draw radius</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> pos.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> ax <span style="color: #339933;">=</span> circle.<span style="color: #660066;">m_R</span>.<span style="color: #660066;">col1</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> pos2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2Vec2<span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> r <span style="color: #339933;">*</span> ax.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> pos.<span style="color: #660066;">y</span> <span style="color: #339933;">+</span> r <span style="color: #339933;">*</span> ax.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>pos2.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> pos2.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">case</span> b2Shape.<span style="color: #660066;">e_polyShape</span><span style="color: #339933;">:</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> poly <span style="color: #339933;">=</span> shape<span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> tV <span style="color: #339933;">=</span> b2Math.<span style="color: #660066;">AddVV</span><span style="color: #009900;">&#40;</span>poly.<span style="color: #660066;">m_position</span><span style="color: #339933;">,</span> b2Math.<span style="color: #660066;">b2MulMV</span><span style="color: #009900;">&#40;</span>poly.<span style="color: #660066;">m_R</span><span style="color: #339933;">,</span> poly.<span style="color: #660066;">m_vertices</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>tV.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> tV.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> poly.<span style="color: #660066;">m_vertexCount</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> v <span style="color: #339933;">=</span> b2Math.<span style="color: #660066;">AddVV</span><span style="color: #009900;">&#40;</span>poly.<span style="color: #660066;">m_position</span><span style="color: #339933;">,</span> b2Math.<span style="color: #660066;">b2MulMV</span><span style="color: #009900;">&#40;</span>poly.<span style="color: #660066;">m_R</span><span style="color: #339933;">,</span> poly.<span style="color: #660066;">m_vertices</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>v.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> v.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>tV.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> tV.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>Above are the body drawing functions: drawJoint() and drawShape().  I&#8217;m not going to get into much detail here, but just know that these functions are responsible for taking the physics bodies and giving them a visual representation.  They make calls to the canvas context 2D drawing API to create the shapes that give us our falling rectangles and circles.  This is the simplest case and requires no external dependencies.  In practical cases though, you will more likely find images or other more clever uses like this one:</p>
<p><object width="640" height="390"><param name="movie" value="http://www.youtube.com/v/ZTwrQSOHdX0&#038;hl=en_US&#038;feature=player_embedded&#038;version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.youtube.com/v/ZTwrQSOHdX0&#038;hl=en_US&#038;feature=player_embedded&#038;version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></embed></object></p>
<hr />

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #003366; font-weight: bold;">function</span> createWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> worldAABB <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2AABB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		worldAABB.<span style="color: #660066;">minVertex</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		worldAABB.<span style="color: #660066;">maxVertex</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> gravity <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2Vec2<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> doSleep <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		world <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2World<span style="color: #009900;">&#40;</span>worldAABB<span style="color: #339933;">,</span> gravity<span style="color: #339933;">,</span> doSleep<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createGround<span style="color: #009900;">&#40;</span>world<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> world<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is where the Box2D JS physics world is created.  We define the bounds for the AABB physics with the minVertex and maxVertex properties and set the vector of gravity.  After we create those we apply them to a newly created world and create the ground that will be the base of our scene.</p>
<hr />
<div style="height:300px; overflow:scroll;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #003366; font-weight: bold;">function</span> createGround<span style="color: #009900;">&#40;</span>world<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> groundSd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BoxDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		groundSd.<span style="color: #660066;">extents</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		groundSd.<span style="color: #660066;">restitution</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> groundBd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BodyDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		groundBd.<span style="color: #660066;">AddShape</span><span style="color: #009900;">&#40;</span>groundSd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		groundBd.<span style="color: #660066;">position</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">470</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> world.<span style="color: #660066;">CreateBody</span><span style="color: #009900;">&#40;</span>groundBd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> createBall<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> ballSd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2CircleDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ballSd.<span style="color: #660066;">density</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">;</span>
		ballSd.<span style="color: #660066;">radius</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">;</span>
		ballSd.<span style="color: #660066;">restitution</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
		ballSd.<span style="color: #660066;">friction</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> ballBd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BodyDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ballBd.<span style="color: #660066;">AddShape</span><span style="color: #009900;">&#40;</span>ballSd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ballBd.<span style="color: #660066;">position</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> world.<span style="color: #660066;">CreateBody</span><span style="color: #009900;">&#40;</span>ballBd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> fixed<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>fixed<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> fixed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> boxSd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BoxDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>fixed<span style="color: #009900;">&#41;</span> boxSd.<span style="color: #660066;">density</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">;</span> 
		boxSd.<span style="color: #660066;">restitution</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span>
		boxSd.<span style="color: #660066;">friction</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">;</span>
		boxSd.<span style="color: #660066;">extents</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span>width<span style="color: #339933;">,</span> height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> boxBd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> b2BodyDef<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		boxBd.<span style="color: #660066;">AddShape</span><span style="color: #009900;">&#40;</span>boxSd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		boxBd.<span style="color: #660066;">position</span>.<span style="color: #660066;">Set</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> world.<span style="color: #660066;">CreateBody</span><span style="color: #009900;">&#40;</span>boxBd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>Above we have the body create functions: createGround(), createBall(), and createBox().  Other than the obvious, let&#8217;s talk about a few things going on here.  Each body is defined by a shape definition, and each shape definition has a number of properties that dictate how it will behave in the Box2D JS world  (see the <a href="http://www.kyucon.com/doc/box2d/" target="_blank">Box2D docs</a> for details).  Restitution, friction, and density affect how the shapes fall, move, and react.  </p>
<p>The extents define the dimensions of the shapes, but probably not how you are accustomed.  Extents represent the distance from one corner of the shape to its center.  So a 100&#215;100 box is actually defined by the extents shapeDef.extents.Set(50,50).  </p>
<p>The shape definition is then used to define a body definition.  The body is then positioned in the world.  The positioning, like extents, is also based on the center of the body, not its corner.  Finally, the newly defined body, based on the shape definition, is added the world with the CreateBody() function.</p>
<hr />
<div style="height:300px; overflow:scroll;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #003366; font-weight: bold;">function</span> createHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// H</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">420</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">420</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">395</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">370</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">370</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// E</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">120</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">420</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">130</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">120</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// L</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">185</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// L</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">235</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// O</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">285</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">315</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// W</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">360</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">420</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">415</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// O</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">460</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">445</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">475</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">460</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// R</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">495</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">410</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">518</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">425</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">515</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">525</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">390</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">510</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// L</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">560</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">545</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// D</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">610</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">435</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">595</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">625</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">405</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">610</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">375</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// !</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">650</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">430</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> <span style="color: #CC0000;">650</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">380</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>Here&#8217;s my addition to the Box2D JS code.  By using a series of stacked boxes I create the infamous programmer&#8217;s first message, &#8220;Hello World!&#8221; (minus the comma, sorry).  No science or mystery here, just a lot of extents and positions for the boxes that compose my message.  And yes, I did cheat on the &#8220;E&#8221; and make it fixed, but not even I can defy the laws of physics for the sake of a code demo.</p>
<hr />

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #003366; font-weight: bold;">function</span> step<span style="color: #009900;">&#40;</span>cnt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> stepping <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> timeStep <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">/</span><span style="color: #CC0000;">60</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> iteration <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
		world.<span style="color: #660066;">Step</span><span style="color: #009900;">&#40;</span>timeStep<span style="color: #339933;">,</span> iteration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> canvasWidth<span style="color: #339933;">,</span> canvasHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		drawWorld<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setTimeout<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'step('</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>cnt <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The step() function is the what makes the whole thing work.  step() is called over and over, at specified intervals, to create the animation of our scene on the canvas.  The world&#8217;s Step() function is first called to apply one iteration of physics to our world&#8217;s bodies.  Next we clear the visual representation of the scene so that it can be redrawn by our drawWorld() function.  Finally we set the interval timer so that step() will be called again.  Again, Flash devs will recognize this as a similar methodology as using the ENTER_FRAME event.</p>
<hr />
<div style="height:300px; overflow:scroll;">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #006600; font-style: italic;">// main entry point</span>
	Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		world <span style="color: #339933;">=</span> createWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ctx <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> canvasElm <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		canvasWidth <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>canvasElm.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		canvasHeight <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>canvasElm.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		canvasTop <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>canvasElm.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		canvasLeft <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>canvasElm.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		createHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0.5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #006600; font-style: italic;">//createBox(world, Event.pointerX(e), Event.pointerY(e), 10, 10, false);</span>
					createBox<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> e.<span style="color: #660066;">clientX</span><span style="color: #339933;">,</span> e.<span style="color: #660066;">clientY</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
					createBall<span style="color: #009900;">&#40;</span>world<span style="color: #339933;">,</span> Event.<span style="color: #660066;">pointerX</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> Event.<span style="color: #660066;">pointerY</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		step<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

</div>
<p>And this is where we kick everything off.  We use the Prototype event handling mechanism to wait until the window is loaded to start our code.  We first create the Box2D JS world and get the 2D context, dimensions, and position of our canvas.  After that I create the boxes that make up the Hello World message.  To finish up we listen for mouse clicks so that we can add more falling objects to the scene, since what good is a meticulously stacked Hello World if you can&#8217;t turn it into a pile of rubble?  The first step() is kicked off and our scene is ready to go! </p>
<hr />

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>body style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;margin:0px;&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>canvas id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;canvas&quot;</span> width<span style="color: #339933;">=</span><span style="color: #3366CC;">'800'</span> height<span style="color: #339933;">=</span><span style="color: #3366CC;">'500'</span> style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;background-color:#eeeeee;&quot;</span><span style="color: #339933;">&gt;&lt;/</span>canvas<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p>And to round out the breakdown, here&#8217;s the actual instance of the canvas element.  Its a very simple container and allows for all the heavy lifting to be done in the Javascript.  Just a reminder, the canvas element will NOT work in Internet Explorer unless you have the conditional check that includes excanvas if necessary, or if you happen to be test driving the IE9 beta.</p>
<h1>The Summary</h1>
<p>OK, well that turned out a lot longer and wordier than I was expecting, but 2D physics being utilized in HTML5 and Javascript is a simple topic.  Hopefully, though, if you made it all the way through you have a much better understanding of how they all play together and how you will create Newtonian worlds of your own.</p>
<p><strong>&lt;small_rant&gt;</strong>The difference in performance between different browsers is enough to drive you crazy.  In my personal experience, Chrome > Firefox > IE.  I know its early in the game and that <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie-9/home" target="_blank">IE 9</a> and <a href="http://www.mozilla.com/en-US/firefox/beta/" target="_blank">Firefox 4</a> will likely be right up to par with Chrome, but its this lag and inconsistency across the board that has me spending most of my web development time in Flash and AS3.</p>
<p>Also, I had trouble making this work in all versions of IE, so if you run into any problems with a particular browser, please let me know.<strong>&lt;/small_rant&gt;</strong></p>
<p><strong>NOTE:</strong> The concatenated version of Box2D JS is a convenience I offer so that you don&#8217;t have to do all the individual includes shown on the Box2D JS site&#8217;s demos.  The minified version is that same concatenated version run through <a href="http://www.crockford.com/javascript/jsmin.html" target="_blank">JSMin</a>.  Both versions are based on version 0.1.0 of Box2D JS.  I am not a contributor to the Box2D JS project, just someone making it simpler to get into.  </p>
<p><a href="http://savagelook.com/blog/code/box2d-js-physics-in-html5-javascript-guide">Box2D JS &#8211; Physics in HTML5 &#038; Javascript Guide</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-1092"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/code/box2d-js-physics-in-html5-javascript-guide/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Packet capture with C++ &amp; Linux</title>
		<link>http://savagelook.com/blog/code/packet-capture-with-c-linux</link>
		<comments>http://savagelook.com/blog/code/packet-capture-with-c-linux#comments</comments>
		<pubDate>Wed, 17 Nov 2010 16:28:37 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[packet capture]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=1033</guid>
		<description><![CDATA[Use the C library libpcap to capture a network packet in Linux.  What you do with it is up to you<p><a href="http://savagelook.com/blog/code/packet-capture-with-c-linux">Packet capture with C++ &#038; Linux</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>As part of a larger project I&#8217;m working on I need to refamiliarize myself with some old friends: C and <a href="http://www.tcpdump.org/" target="_blank">libcap</a>.  While I do have a healthy dislike for coding in C, I have a much healthier respect for those who have used it to create some pretty fantastic stuff.  It still takes conscious effort to not spend all my time wrapping C code in C++ objects, but I digress.</p>
<p>libpcap is a C library used to capture and analyze network packets off the wire, otherwise known as packet sniffing.  Some of the more prominent applications out there using it right now are <a href="http://www.snort.org/" target="_blank">Snort intrusion detection system</a>, <a href="http://www.wireshark.org/" target="_blank">Wireshark network analyzer</a> (formerly known as Ethereal), and the proprietor of libpcap, <a href="http://www.tcpdump.org/" target="_blank">tcpdump</a>.</p>
<p>Why would you want to be able to analyze packets in code and not use one of these applications?  Because packet sniffing, in the hands of someone knowledgeable in network protocols, can be a great, non-intrusive way to gather discrete or statistical information and tie it to other coded business logic.  There&#8217;s also that little, evil, <a href="http://www.blackhat.com/" target="_blank">don-your-black-hat</a>, I-wanna-be-a-hacker kind of feel to seeing packets you have no explicit reason to see.  Motives aside, let&#8217;s see a simple example of how it works in Linux with a little C++ tacked on.</p>
<h1>The Code</h1>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;iostream&gt;</span>
<span style="color: #339933;">#include &lt;pcap.h&gt;</span>
&nbsp;
using namespace std<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> packetCount <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> packetHandler<span style="color: #009900;">&#40;</span>u_char <span style="color: #339933;">*</span>userData<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">struct</span> pcap_pkthdr<span style="color: #339933;">*</span> pkthdr<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u_char<span style="color: #339933;">*</span> packet<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #339933;">++</span>packetCount <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot; packet(s) captured&quot;</span> <span style="color: #339933;">&lt;&lt;</span> endl<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>dev<span style="color: #339933;">;</span>
	pcap_t <span style="color: #339933;">*</span>descr<span style="color: #339933;">;</span>
	<span style="color: #993333;">char</span> errbuf<span style="color: #009900;">&#91;</span>PCAP_ERRBUF_SIZE<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	dev <span style="color: #339933;">=</span> pcap_lookupdev<span style="color: #009900;">&#40;</span>errbuf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>dev <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;pcap_lookupdev() failed: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> errbuf <span style="color: #339933;">&lt;&lt;</span> endl<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	descr <span style="color: #339933;">=</span> pcap_open_live<span style="color: #009900;">&#40;</span>dev<span style="color: #339933;">,</span> BUFSIZ<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> errbuf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>descr <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;pcap_open_live() failed: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> errbuf <span style="color: #339933;">&lt;&lt;</span> endl<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>pcap_loop<span style="color: #009900;">&#40;</span>descr<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> packetHandler<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;pcap_loop() failed: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> pcap_geterr<span style="color: #009900;">&#40;</span>descr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;capture finished&quot;</span> <span style="color: #339933;">&lt;&lt;</span> endl<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h1>The Output</h1>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">1</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">2</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">3</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">4</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">5</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">6</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">7</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">8</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">9</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
<span style="color: #000000;">10</span> packet<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> captured
capture finished</pre></div></div>

<p>In this very basic example we are simply setting up a network interface to use libpcap, capturing a few packets, and exiting with a message.  Pretty vanilla, but there&#8217;s a good bit going on here.  let&#8217;s break it down in more detail.</p>
<h1>The Breakdown</h1>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;iostream&gt;</span>
<span style="color: #339933;">#include &lt;pcap.h&gt;</span>
using namespace std<span style="color: #339933;">;</span></pre></div></div>

<p>Here along with standard includes we also include pcap.h.  This is the header file that is necessary for all libpcap operations and constants.</p>
<hr />

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">int</span> packetCount <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> packetHandler<span style="color: #009900;">&#40;</span>u_char <span style="color: #339933;">*</span>userData<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">struct</span> pcap_pkthdr<span style="color: #339933;">*</span> pkthdr<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> u_char<span style="color: #339933;">*</span> packet<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #339933;">++</span>packetCount <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot; packet(s) captured&quot;</span> <span style="color: #339933;">&lt;&lt;</span> endl<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the callback packet handler function that will do all the heavy lifting when it comes to network analysis.  This callback is referenced later by the <a href="http://www.manpagez.com/man/3/pcap_loop/" target="_blank">pcap_loop()</a> function.  Every packet that pcap receives will be passed to this function.  Here&#8217;s the explanation of the parameters:</p>
<ul>
<li><strong>userData </strong>- a pointer to user defined data that can be passed into each callback, as defined in the <a href="http://www.manpagez.com/man/3/pcap_loop/" target="_blank">pcap_loop()</a> call&#8217;s 4th parameter.</li>
<li><strong>pkthdr </strong>- the pcap packet header that includes relevant information about the packet as it relates to pcap&#8217;s capture.</li>
<li><strong>packet </strong>- a pointer to the actual packet that will be analyzed.</li>
</ul>
<p>For the sake of this example, we won&#8217;t be doing any protocol analysis and will simply be counting the number of packets that pass over our listening interface.</p>
<hr />

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>dev<span style="color: #339933;">;</span>
	pcap_t <span style="color: #339933;">*</span>descr<span style="color: #339933;">;</span>
	<span style="color: #993333;">char</span> errbuf<span style="color: #009900;">&#91;</span>PCAP_ERRBUF_SIZE<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The start of our packet capture program.</p>
<ul>
<li><strong>dev</strong> &#8211; this will hold the name of the interface on which we will sniff</li>
<li><strong>descr</strong> &#8211; The descriptor, or handle, for the libpcap packet capture</li>
<li><strong>errbuf</strong> &#8211; a character buffer to contain any potential errors from libpcap.  The max error size is defined by <strong>PCAP_ERRBUF_SIZE</strong> in pcap.h.</li>
</ul>
<hr /></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	dev <span style="color: #339933;">=</span> pcap_lookupdev<span style="color: #009900;">&#40;</span>errbuf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>dev <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;pcap_lookupdev() failed: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> errbuf <span style="color: #339933;">&lt;&lt;</span> endl<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here we use <a href="http://www.manpagez.com/man/3/pcap_lookupdev/" target="_blank">pcap_lookupdev()</a> to find an available network interface on which to sniff.  The name of the interface is returned in the <strong>dev</strong> character array.  If there is an error or if no interface is available, <a href="http://www.manpagez.com/man/3/pcap_lookupdev/" target="_blank">pcap_lookupdev()</a> returns a NULL value and fills the <strong>errbuf</strong> with the relevant error information.  Returning a failure value then populating an error buffer is common practice for libpcap functions that end in error. </p>
<hr />

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	descr <span style="color: #339933;">=</span> pcap_open_live<span style="color: #009900;">&#40;</span>dev<span style="color: #339933;">,</span> BUFSIZ<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> errbuf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>descr <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;pcap_open_live() failed: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> errbuf <span style="color: #339933;">&lt;&lt;</span> endl<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next we create our packet capture descriptor using <a href="http://www.manpagez.com/man/3/pcap_open_live/" target="_blank">pcap_open_live()</a>.  This is how we will enable our target network interface for sniffing.  It will return a valid descriptor on success, a NULL on failure.  The parameters are as follows:</p>
<ul>
<li><strong>device</strong> &#8211; the name of the network interface to be used by libpcap for sniffing.  We found it using <a href="http://www.manpagez.com/man/3/pcap_lookupdev/" target="_blank">pcap_lookupdev()</a>.</li>
<li><strong>snaplen</strong> &#8211; the maximum snap length, or packet size, to be handled by this descriptor.  We using the system defined <strong>BUFSIZ</strong> constant.</li>
<li><strong>promisc</strong> &#8211; Determines whether or not the interface will listen in <a href="http://en.wikipedia.org/wiki/Promiscuous_mode" target="_blank">promiscuous mode</a>.  Promiscuous mode will analyze packets not specifically addressed for your machine, like if you were attached to a hub or a <a href="http://en.wikipedia.org/wiki/Port_mirroring" target="_blank">mirrored switch port</a>.  1 turns it on, 0 turns it off.</li>
<li><strong>to_ms</strong> &#8211; this is the packet read timeout in milliseconds.  Specify -1 for no timeout.</li>
<li><strong>errbuf</strong> &#8211; In case of an error, this is where a descriptive message will be left.</li>
</ul>
<hr />

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>pcap_loop<span style="color: #009900;">&#40;</span>descr<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> packetHandler<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;pcap_loop() failed: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> pcap_geterr<span style="color: #009900;">&#40;</span>descr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;capture finished&quot;</span> <span style="color: #339933;">&lt;&lt;</span> endl<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And here&#8217;s the call we&#8217;ve been waiting for: <a href="http://www.manpagez.com/man/3/pcap_loop/" target="_blank">pcap_loop()</a>.  This will take our pcap descriptor and start sniffing packets, sending them all to our packet handler callback function, aptly named packetHandler().  Here&#8217;s <a href="http://www.manpagez.com/man/3/pcap_loop/" target="_blank">pcap_loop()&#8217;s</a> parameters:</p>
<ul>
<li><strong>descr</strong> &#8211; the packet capture descriptor that we have initialized in the previous steps.</li>
<li><strong>count</strong> &#8211; the number of packets to capture before <a href="http://www.manpagez.com/man/3/pcap_loop/" target="_blank">pcap_loop()</a> exits.  Use -1 or 0 to use no limit.</li>
<li><strong>callback</strong> &#8211; this is the callback function that is called every time pcap sniffs a packet.  As specified above in the packetHandler() function, it receives relevant user data, pcap headers, and full packet data.  It is the work horse of the packet analysis process.  If creating a callback of your own, be sure to follow the function signature given in packetHandler().</li>
<li><strong>userData</strong> &#8211; this is an array of unsigned bytes to be sent in with each packet.  You can use it to hold any relevant user data you would like to send to the callback as its first paramater.   You can also specify NULL if you wish to pass no user data to the callback.</li>
</ul>
<hr />
<h1>The Summary</h1>
<p>With this basic example you can now use C/C++ to capture network packets.  But keeping a count of packets isn&#8217;t terribly interesting, is it?  No, I don&#8217;t think so either.  So what should I do next?  If you managed to make it all the way to this summary, you can help me decide what aspects of packet capture or C/C++ to show case next.  Comment on one of the following or ad your own idea and I&#8217;ll probably do it next!</p>
<ul>
<li>Learn how to use <a href="http://en.wikipedia.org/wiki/Berkeley_Packet_Filter" target="_blank">Berkeley packet filters</a> to focus and make more efficient your packet sniffing.</li>
<li>Process offline packet capture dumps from some of the other programs I mentioned earlier.  It&#8217;s a critical skill for testing.</li>
<li>Use libpcap to learn more about the interfaces on your system?</li>
<li>Wrap this ugly C code in some much more developer friendly C++ objects.</li>
<li>Get right into the nitty gritty and start learning how to analyze packet data.</li>
</ul>
<p>The choice is your&#8217;s guys. </p>
<p><a href="http://savagelook.com/blog/code/packet-capture-with-c-linux">Packet capture with C++ &#038; Linux</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-1033"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/code/packet-capture-with-c-linux/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Away3D Drunk Simulator</title>
		<link>http://savagelook.com/blog/away3d/away3d-drunk-simulator</link>
		<comments>http://savagelook.com/blog/away3d/away3d-drunk-simulator#comments</comments>
		<pubDate>Fri, 15 Oct 2010 17:59:14 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[motion blur]]></category>
		<category><![CDATA[tweenlite]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=971</guid>
		<description><![CDATA[Its happy hour, Away3D style! Feel like a staggering drunk with this simulation<p><a href="http://savagelook.com/blog/away3d/away3d-drunk-simulator">Away3D Drunk Simulator</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://savagelook.com/demos/away3d_drunk/away3d_drunk.html" rel="shadowbox;width=800;height=600;"><img class="alignnone size-full wp-image-973" title="Away3D Drunk Simulator" src="http://savagelook.com/blog/wp-content/uploads/2010/10/drunk.jpg" alt="Away3D Drunk Simulator" width="638" height="478" /></a></p>
<p>&rarr; <a href="http://savagelook.com/demos/away3d_drunk/away3d_drunk.html" rel="shadowbox;width=800;height=600;">Click here</a> or the image above for the demo.<br />
&rarr; View the <a href="http://savagelook.com/demos/away3d_drunk/srcview/index.html">source code</a>.</p>
<p>I decided to have a little more fun with the <a href="http://savagelook.com/blog/away3d/away3d-motion-blur">motion blur technique that I posted</a> about earlier this week.  Rather than blur individual objects in the scene, I blurred the entire scene giving a you a view through beer goggles engineered in Actionscript.  And to get in the mood I poured myself a glass of Single Barrel Jack Daniel&#8217;s.  OK, probably wasn&#8217;t necessary, but Jack Daniel&#8217;s is like bacon: it makes everything better.</p>
<p>Be sure to play with the slider at the top of the demo.  Moving it from left to right will gradually increase your level of intoxication, making the motion blur more extreme and longer lasting.  This in turn has the effect of making the frame rate really low, but it actually works well in the demo because it also simulates the delayed reflexes of being wasted.  At least thats how I plan to explain away occasional single digit FPS.</p>
<p>Having fun haphazardly swaying through my little 3d scene.  While this seems like a goofy effect, it probably has some pretty interesting applications in games.  It seems like a great way to simulate a &#8220;shell shocked&#8221; effect in a battle or perhaps blurred vision when moving at extremely fast speeds.  Got any other idea?</p>
<p><a href="http://savagelook.com/blog/away3d/away3d-drunk-simulator">Away3D Drunk Simulator</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-971"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/away3d/away3d-drunk-simulator/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Away3D Motion Blur</title>
		<link>http://savagelook.com/blog/away3d/away3d-motion-blur</link>
		<comments>http://savagelook.com/blog/away3d/away3d-motion-blur#comments</comments>
		<pubDate>Thu, 14 Oct 2010 13:47:13 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[motion blur]]></category>
		<category><![CDATA[tweenlite]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=954</guid>
		<description><![CDATA[Inspired by a nearly empty bottle of Jack Daniel's, I loaded up Away3D and decided to purposely blur my vision<p><a href="http://savagelook.com/blog/away3d/away3d-motion-blur">Away3D Motion Blur</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a rel="shadowbox;width=800;height=600;" href="http://savagelook.com/demos/away3d_motion/away3d_motion.html"><img class="alignnone size-full wp-image-955" title="Away3D Motion Blur" src="http://savagelook.com/blog/wp-content/uploads/2010/10/motionblur.jpg" alt="Away3D Motion Blur" width="556" height="417" /></a></p>
<p>→ <a rel="shadowbox;width=800;height=600;" href="http://savagelook.com/demos/away3d_motion/away3d_motion.html">Click here</a> or the image above for the demo.<br />
→ View the <a href="http://savagelook.com/demos/away3d_motion/srcview/index.html">source code</a>.</p>
<p>This was a just for fun idea that popped into my head yesterday.  I&#8217;m always trying to find programmatic ways to add to the appeal of some of my 3D effects.  What better way to jazz up some 3D objects than to view them as though you&#8217;re wasted?!</p>
<p>I&#8217;ve got a lot of irons in the fire lately, so this one was quick and dirty.  Basically I create an extra View3D in which to render the &#8220;blurred&#8221; objects.  I take a snapshot of this view (50 in fact), overlay it on a separate background View3D, and use <a href="http://www.greensock.com/tweenlite/" target="_blank">TweenLite</a> to gradually fade the opacity of each snapshot.</p>
<p>In the end you get this pretty neat motion blur effect on your 3D objects.  There&#8217;s a lot more that could be added to this to spice it up.  If I had more time to devote to it I would add a BlurFilter toggle, which makes the motion blur look better but affects performance.  Other settings you could vary to alter the effect are:</p>
<ul>
<li>The speed of the objects</li>
<li>The number of snapshots taken</li>
<li>How fast the snapshots fade</li>
<li>The scale of the fading snapshots</li>
</ul>
<p>and I&#8217;m sure there&#8217;s more.  Try it out.  You could give your scenes a truly inebriated look by using only one View3D.  By doing this you&#8217;ll motion blur the entire viewable scene.  Maybe I&#8217;ll strap on some Away3D beer goggles in the future and give that a shot!  Let me know if you beat me to it.</p>
<p><a href="http://savagelook.com/blog/away3d/away3d-motion-blur">Away3D Motion Blur</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-954"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/away3d/away3d-motion-blur/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Away3D Morphing with HeightMapModifier</title>
		<link>http://savagelook.com/blog/away3d/away3d-morphing-with-heightmapmodifier</link>
		<comments>http://savagelook.com/blog/away3d/away3d-morphing-with-heightmapmodifier#comments</comments>
		<pubDate>Mon, 04 Oct 2010 12:38:39 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[heightmap]]></category>
		<category><![CDATA[HeightMapModifier]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=925</guid>
		<description><![CDATA[The best Away3D mesh morphing yet.  This time using the HeightMapModifier and EnviroBitmapMaterial classes<p><a href="http://savagelook.com/blog/away3d/away3d-morphing-with-heightmapmodifier">Away3D Morphing with HeightMapModifier</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a rel="shadowbox;width=800;height=600;" href="http://savagelook.com/demos/away3d_HeightMapModifier/away3d_HeightMapModifier.html"><img class="alignnone size-full wp-image-929" title="Away3H HeightMapModifier and EnviroBitmapMaterial" src="http://savagelook.com/blog/wp-content/uploads/2010/10/hmm.jpg" alt="Away3H HeightMapModifier and EnviroBitmapMaterial" width="560" height="417" /></a></p>
<p>&rarr; <a href="http://savagelook.com/demos/away3d_HeightMapModifier/away3d_HeightMapModifier.html">Click here</a> or the image above for the demo.<br />
&rarr; View the <a href="http://savagelook.com/demos/away3d_HeightMapModifier/srcview/index.html">source code</a>.</p>
<p>As usual, shortly after finishing an Away3D demo an Away3d dev (this time <a href="http://www.closier.nl/blog/" target="_blank">Fabrice Closier</a>) suggests a better way to do it.  In my previous 2 demos (<a href="http://savagelook.com/blog/away3d/dynamic-heightmaps-in-away3d">part 1</a> and <a href="http://savagelook.com/blog/away3d/away3d-mesh-morphing">part 2</a>) I created fluid and morphing effects on planar meshes using a modified version of the SkinExtrude class.</p>
<table>
<tr>
<td>
<div id="attachment_857" class="wp-caption alignnone" style="width: 256px"><a href="http://savagelook.com/blog/away3d/dynamic-heightmaps-in-away3d"><img class="size-full wp-image-857" title="Away3D water with heightmaps" src="http://savagelook.com/blog/wp-content/uploads/2010/09/water.jpg" alt="Away3D water with heightmaps" width="246" height="184" /></a><p class="wp-caption-text">Dynamic Height Maps, Part 1</p></div>
</td>
<td>
<div id="attachment_904" class="wp-caption alignnone" style="width: 291px"><a href="http://savagelook.com/blog/away3d/away3d-mesh-morphing"><img class="size-full wp-image-904" title="Away3D Mesh Morphing" src="http://savagelook.com/blog/wp-content/uploads/2010/09/morph.jpg" alt="Away3D Mesh Morphing" width="281" height="178" /></a><p class="wp-caption-text">Dynamic Height Maps, Part 2</p></div>
</td>
</tr>
</table>
<p>As per Fabrice&#8217;s suggestion, this time I did the same using the HeightMapModifier class.  No modifications to Away3D source code necessary and it works on any mesh, planar or otherwise.  In this example I apply dynamic height maps to a sphere to create some pretty wild meshes.  Be sure to toggle to wireframe mode so that you can get a good look at how the vertices move with each height map transition.  Here&#8217;s a snippet of how it works:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;heightmap.jpg&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hmImage:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> heightData:BitmapData = Cast.<span style="color: #006600;">bitmap</span><span style="color: #66cc66;">&#40;</span>hmImage<span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> sphere:Sphere = <span style="color: #000000; font-weight: bold;">new</span> Sphere<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> heightMapModifier:HeightMapModifier = <span style="color: #000000; font-weight: bold;">new</span> HeightMapModifier<span style="color: #66cc66;">&#40;</span>sphere, heightData<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// offset determines the minimum height value of each vertice </span>
heightMapModifier.<span style="color: #006600;">offset</span> = <span style="color: #cc66cc;">300</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// scale is the multiplier for the calculated height values</span>
heightMapModifier.<span style="color: #006600;">scale</span> = ELEVATION_HEIGHT;
&nbsp;
<span style="color: #808080; font-style: italic;">// execute the modifier</span>
heightMapModifier.<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>One other cool note to remember is that you can also use negative values for scale.  By doing so you can create indents rather than bumps on your mesh.  See what happens when you turn the &#8220;Elevation height&#8221; all the way down on this demo.</p>
<p>Also in the example I utilize the EnviroBitmapMaterial to give our target mesh a shiny, reflective appearance.  True reflection is very processor intensive and is outside the realm of feasibility right now for 3D Flash.  But by using environment materials, we can &#8220;fake&#8221; a reflective surface by using an environment map that closely matches the Away3D scene.  In this case the environment map I used for the EnviroBitmapMaterial is one of the images I used to create the skybox for the scene.  You&#8217;ll notice the reflective effect even more as the mesh morphs or when you orbit around it.</p>
<p>There you have it, mesh morphing and reflective materials in Away3D.  With a little luck, this&#8217;ll be the last time you have to hear me talk about it for a while!</p>
<p><a href="http://savagelook.com/blog/away3d/away3d-morphing-with-heightmapmodifier">Away3D Morphing with HeightMapModifier</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-925"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/away3d/away3d-morphing-with-heightmapmodifier/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Away3D Mesh Morphing</title>
		<link>http://savagelook.com/blog/away3d/away3d-mesh-morphing</link>
		<comments>http://savagelook.com/blog/away3d/away3d-mesh-morphing#comments</comments>
		<pubDate>Thu, 30 Sep 2010 13:50:53 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[heightmap]]></category>
		<category><![CDATA[morph]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=902</guid>
		<description><![CDATA[Using the same principles in my last post I morph one Away3d mesh to another using height map transitioning <p><a href="http://savagelook.com/blog/away3d/away3d-mesh-morphing">Away3D Mesh Morphing</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a rel="shadowbox;width=800;height=600;" href="http://savagelook.com/demos/away3d_morph/away3d_morph.html"><img class="alignnone size-full wp-image-904" title="Away3D Mesh Morphing" src="http://savagelook.com/blog/wp-content/uploads/2010/09/morph.jpg" alt="Away3D Mesh Morphing" width="561" height="355" /></a></p>
<p>→ <a rel="shadowbox;width=800;height=600;" href="http://savagelook.com/demos/away3d_morph/away3d_morph.html">Click here</a> or the image above for the demo.<br />
→ View the <a href="http://savagelook.com/demos/away3d_morph/srcview/index.html">source code</a>.</p>
<p>As I have a way of doing, I immediately started working on a better demo upon finishing the <a href="http://savagelook.com/blog/away3d/dynamic-heightmaps-in-away3d">last one on height maps in Away3D</a>.  In this demo I use simple bitmap transitioning and my modified Away3D SkinExtrude class (SavageLookSkinExtrude) to create a morphing effect.  Basically one mesh&#8217;s vertices will smoothly transition to the position of the next mesh&#8217;s vertices based on the given height maps.</p>
<p>I mentioned <a href="http://exey.ru/blog/home/fluid-simulation-pv3d-and-away3d" target="_blank">Exey Panteleev&#8217;s fluid simulation in PaperVision3D and Away3D</a> in my last post as a method superior to dynamic height maps for creating a flowing, fluid mesh in Away3D.  I still stand by the point.  But as shown in this demo, dynamic height maps offer many more possibilities as provided by the control you have over the 2D height maps.  In fact, you can have a designer, with no knowledge of Away3D of 3D math, create your height maps and you can handle the morphing.</p>
<div id="attachment_912" class="wp-caption alignnone" style="width: 563px"><a href="http://savagelook.com/blog/wp-content/uploads/2010/09/morph2.jpg" rel="shadowbox[sbpost-902];player=img;"><img class="size-full wp-image-912   " title="Morphing SavageLook letters" src="http://savagelook.com/blog/wp-content/uploads/2010/09/morph2.jpg" alt="Morphing SavageLook letters" width="553" height="389" /></a><p class="wp-caption-text">Morphing SavageLook Letters</p></div>
<p>The shapes you can morph are only limited by your imagination&#8230; OK, that&#8217;s not entirely true.  The shapes will be limited by your ability to create, or find, height maps that suit your needs.  Also, with this method you are limited to morphing the plane that is the base of the SkinExtrude, or in this case the SavageLookSkinExtrude.  Morphing other 3D meshes or primitives is beyond the scope of this technique, and to be honest, beyond my expertise at this point in time.</p>
<p>Even with those limitations, though, this method offers some really eye catching dimension to the toolkit of a hopelessly code driven 3D artist like myself.  Play around with it and let me know if you come up with something cool.  I&#8217;d love to see this method work with some more interesting height maps, like facial maps.</p>
<p><strong>NOTE:</strong> I&#8217;m currently inquiring on the <a href="http://groups.google.com/group/away3d-dev/browse_thread/thread/f5dd17752c1d051f" target="_blank">Away3D Dev list</a> how to get the subdivision size of the SkinExtrude smaller so that I can get more accuracy and precision.  Unfortunately as it stands triangles start to disappear when the subdivision size goes lower than 20.  I&#8217;ll update this post if we come up with a solution.  Please let me know if you come up with one yourself.</p>
<p><a href="http://savagelook.com/blog/away3d/away3d-mesh-morphing">Away3D Mesh Morphing</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-902"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/away3d/away3d-mesh-morphing/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dynamic Heightmaps in Away3D</title>
		<link>http://savagelook.com/blog/away3d/dynamic-heightmaps-in-away3d</link>
		<comments>http://savagelook.com/blog/away3d/dynamic-heightmaps-in-away3d#comments</comments>
		<pubDate>Wed, 29 Sep 2010 12:42:59 +0000</pubDate>
		<dc:creator>Tony Lukasavage</dc:creator>
				<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code Demos]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[fluid]]></category>
		<category><![CDATA[heightmap]]></category>

		<guid isPermaLink="false">http://savagelook.com/blog/?p=851</guid>
		<description><![CDATA[Use dynamic gradient height maps, Elevation, and SkinExtrude in Away3D to create flowing 3D meshes<p><a href="http://savagelook.com/blog/away3d/dynamic-heightmaps-in-away3d">Dynamic Heightmaps in Away3D</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a rel="shadowbox;height=600;width=800;" href="http://savagelook.com/demos/away3d_heightmap/away3d_heightmap.html"><img class="alignnone size-full wp-image-857" title="Away3D water with heightmaps" src="http://savagelook.com/blog/wp-content/uploads/2010/09/water.jpg" alt="Away3D water with heightmaps" width="561" height="420" /></a></p>
<p>→ Click the image above for the demo (or <a rel="shadowbox;height=600;width=800;" href="http://savagelook.com/demos/away3d_heightmap/away3d_heightmap.html">click here</a>).<br />
→ View the <a href="http://savagelook.com/demos/away3d_heightmap/srcview/index.html">source code</a>.</p>
<p>While there might be other simpler ways to do it (<a href="http://code.google.com/p/as3dmod/" target="_blank">AS3Mod</a> comes to mind), I found myself working with the Elevation and SkinExtrude classes in Away3D and created some cool flowing effects.  The basic idea is that you dynamically create a gradient bitmap in AS3, tween its appearance on each frame, and use that bitmap as a heightmap for a SkinExtrude.  This will give you the effects shown in the demo above.</p>
<p>I had to make a few slight changes to the existing SkinExtrude class in order to make it more efficient to reuse in each frame.  This is the <a href="http://savagelook.com/demos/away3d_heightmap/srcview/" target="_self">SavageLookSkinExtrude class</a> found in the source code.  It is identical to the SkinExtrude class found in away3d.extrusions, except for the annotated sections that allow me to generate the heightmap extrusion more than once per instantiation.</p>
<div id="attachment_866" class="wp-caption alignnone" style="width: 554px"><a rel="shadowbox;height=400;width=600;" href="http://exey.ru/blog/fluidsimulationPV3D/index.html"><img class="size-full wp-image-866 " title="Exey Panteleev's PV3D &amp; Away3D Fluid Simulation" src="http://savagelook.com/blog/wp-content/uploads/2010/09/pvwater.jpg" alt="Exey Panteleev's PV3D &amp; Away3D Fluid Simulation" width="544" height="358" /></a><p class="wp-caption-text">Exey Panteleev&#39;s PV3D &amp; Away3D Fluid Simulation</p></div>
<p>The inspiration for this demo came from 2 places.  The first is <a href="http://exey.ru/blog/home/fluid-simulation-pv3d-and-away3d" target="_blank">Exey Panteleev&#8217;s (much better) fluid simulation</a>. The code displayed on his page is for PaperVision3D, but there&#8217;s a link for the Away3D code as well.  All things being equal, you should be following the footsteps of Exey since his solution performs much better than mine.   I just did this demo cause frankly I&#8217;m not that good at 3D math and I could barely understand the math involved in his solution. The only real advantage of my method is that you have more control over the pattern as it is driven by the bitmap.</p>
<table>
<tbody>
<tr>
<td>
<p><div id="attachment_870" class="wp-caption alignnone" style="width: 330px"><a href="http://jasonbejot.com/wp-content/uploads/2009/08/TerrainTutorial.swf" rel="shadowbox[sbpost-851];width=640;height=385;"><img class="size-full wp-image-870  " title="Jason Bejot's Away3D Terrain" src="http://savagelook.com/blog/wp-content/uploads/2010/09/jason.jpg" alt="Jason Bejot's Away3D Terrain" width="320" height="256" /></a><br />
<p class="wp-caption-text">Jason Bejot&#39;s Away3D Terrain</p></div></td>
<td>
<p><div id="attachment_874" class="wp-caption alignnone" style="width: 266px"><a rel="shadowbox" href="http://savagelook.com/blog/wp-content/uploads/2010/09/base1.jpg"><img class="size-full wp-image-874 " title="Height Map for Away3D Terrain" src="http://savagelook.com/blog/wp-content/uploads/2010/09/base1.jpg" alt="Height Map for Away3D Terrain" width="256" height="256" /></a><p class="wp-caption-text">Height Map for Away3D Terrain</p></div></td>
</tr>
</tbody>
</table>
<p>My other source of inspiration, as well as pure tutelage with Away3D height maps and SkinExtrude, is <a href="http://jasonbejot.com/?p=307" target="_blank">Jason Bejot&#8217;s &#8220;Create Terrain in Away3D&#8221; tutorial</a>.  Jason gives a very easy to follow and concise look at how you can use Away3D&#8217;s Elevation and SkinExtrude classes in conjunction with grayscale height maps to create realistic terrain.  I basically just took his method and put it in motion.</p>
<p>As you can see, there&#8217;s more than one way to skin this cat.  Play around with it and see what kind of creations you can make with heightmaps and SkinExtrude.  I don&#8217;t have the time to play with it now, but I feel like bitmaps given the PixelBender treatment might create some pretty wild 3D objects.</p>
<p><a href="http://savagelook.com/blog/away3d/dynamic-heightmaps-in-away3d">Dynamic Heightmaps in Away3D</a> is a post from: <a href="http://savagelook.com/blog">SavageLook.com</a></p>
<div class="shr-publisher-851"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://savagelook.com/blog/away3d/dynamic-heightmaps-in-away3d/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
