<?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>Chasing &#187; Projects</title>
	<atom:link href="http://chase.ratchetsoftware.com/category/work/projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://chase.ratchetsoftware.com</link>
	<description>Chase Gray's blog with solutions to various problems by a curious american Ph.D. student.</description>
	<lastBuildDate>Wed, 09 Jun 2010 05:13:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Reading Advantage Database Server Files (.ADT) in Ruby</title>
		<link>http://chase.ratchetsoftware.com/2010/02/reading-advantage-database-server-files-adt-in-ruby/</link>
		<comments>http://chase.ratchetsoftware.com/2010/02/reading-advantage-database-server-files-adt-in-ruby/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 01:45:50 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Ruby-ADT]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=172</guid>
		<description><![CDATA[I recently received some data in the Advantage Database Server file format (.adt). In the past I have worked with DBF, Access, and .xls. I was able to find some way to retrieve data from these formats using open source software. The open source solutions were much easier to work with, usually not requiring any [...]]]></description>
			<content:encoded><![CDATA[<p>I recently received some data in the <a href="http://www.sybase.com/products/databasemanagement/advantagedatabaseserver">Advantage Database Server</a> file format (.adt).  In the past I have worked with DBF, Access, and .xls.  I was able to find some way to retrieve data from these formats using open source software.  The open source solutions were much easier to work with, usually not requiring any sort of driver installation, etc.</p>
<p>I searched thoroughly for an easy way to retrieve information from this set of .ADT files I was dealing with.  Sybase provides tools to work with the data, but all of them require using their software or drivers developed by them.  I would have settled for this if it wasn&#8217;t such a hassle to figure out how to use them.  I asked the question on <a href="http://stackoverflow.com/questions/2289519/automate-conversion-of-sybase-adt-files-to-sql">Stackoverflow.com</a> and got an answer from one of the employee&#8217;s of Sybase.  While helpful, the suggested Perl driver was one of the things I was avoiding.  I did attempt it and after a little hassle, moved on to creating a simpler solution.</p>
<p><span id="more-172"></span>One solution was to simply use Sybase&#8217;s provided &#8220;<a href="http://devzone.advantagedatabase.com/dz/content.aspx?key=20&amp;Release=13">Advantage Data Architect</a>&#8221; software to open the .ADT files and export them all to SQL.  This is possible with their software, but it would not work for my situation.  The developers of the program outputting these .adt files used a separate embedded database for each new report that was being worked on.  This meant there was hundreds of folders, each containing 30-40 .ADT files making up their own embedded database.  I needed something that could automate the extraction of this data.</p>
<h2>Data Format</h2>
<p>To cut this short, I decided to open up the .ADT files with a hex editor and see if I could get the information I needed out.  I only needed the column names, their types, and the actual rows of data.  To aid anybody else doing something similar to this, I&#8217;ll include some of my notes about the format here:</p>
<ul>
<li>The data is stored at the very end of the file. </li>
<li>The byte offset when the data starts is located at byte 32 and is 32-bits</li>
<li>Byte 24 contains a 32-bit integer specifying the number of rows</li>
<li>Byte 36 contains the a 32-bit number representing the number of bytes each record consumes</li>
<li>The header is 400 bytes</li>
<li>Column information is after the header, before the data.  Each column entry is 200 bytes.</li>
<li>The number of columns can be calculated (data_offset-400)/200</li>
<li>Column names consume the first 128 of each column entry.</li>
<li>Byte 130 contains a 16-bit integer representing the type of data.</li>
<li>Byte 136 contains a 16-bit integer, the length of the field in bytes</li>
<li>Character = 04, Double = 0A, Integer = 0B, Shortint = 0C, CICharacter = 14, Date = 03, Time = 0D, Timestamp = 0E, Autoinc = 0F</li>
</ul>
<p>This was enough information to get what I wanted.</p>
<h2>Ruby-ADT Gem</h2>
<p>I took the existing <a href="http://github.com/infused/dbf">DBF gem</a> by Keith Morrison.  Most of the credit for this gem goes to him as I borrowed the structure heavily from his gem.</p>
<p>I&#8217;ll provide an example of what is now possible with my new ADT gem.  No drivers are required and it&#8217;s as simple as installing the gem.  I&#8217;ll include a snippet of the readme from my <a href="http://github.com/chasemgray/Ruby-ADT">Ruby-ADT gem</a> on github, borrowed heavily again from the DBF gem.</p>
<h3>Installation</h3>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">gem install ruby<span style="color:#006600; font-weight:bold;">-</span>adt</pre></div></div>

<h3>Basic Usage</h3>
<p>Load an ADT file:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'adt'</span>
&nbsp;
table = <span style="color:#6666ff; font-weight:bold;">ADT::Table</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test.adt&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Enumerate all records</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">table.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>record<span style="color:#006600; font-weight:bold;">|</span>          
	<span style="color:#CC0066; font-weight:bold;">puts</span> record.<span style="color:#9900CC;">name</span>          
	<span style="color:#CC0066; font-weight:bold;">puts</span> record.<span style="color:#9900CC;">email</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Load a single record using <tt>record</tt> or <tt>find</tt></p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">table.<span style="color:#9900CC;">record</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>
table.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Attributes can also be accessed through the attributes hash in  original or underscored form or as an accessor method using the underscored name.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">table.<span style="color:#9900CC;">record</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;PhoneBook&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
table.<span style="color:#9900CC;">record</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;phone_book&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
table.<span style="color:#9900CC;">record</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">phone_book</span></pre></div></div>

<p>Search for records using a simple hash format.  Multiple search  criteria are ANDed. Use the block form of find if the resulting recordset could be  large otherwise all records will be loaded into memory.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># find all records with first_name equal to Keith</span>
&nbsp;
table.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:first_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'Keith'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>record<span style="color:#006600; font-weight:bold;">|</span>  
	<span style="color:#CC0066; font-weight:bold;">puts</span> record.<span style="color:#9900CC;">last_name</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># find all records with first_name equal to Keith and last_name equal to Morrison</span>
table.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:first_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Keith'</span>, <span style="color:#ff3333; font-weight:bold;">:last_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'Morrison'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>record<span style="color:#006600; font-weight:bold;">|</span>   
	<span style="color:#CC0066; font-weight:bold;">puts</span> record.<span style="color:#9900CC;">last_name</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># find the first record with first_name equal to Keith</span>
&nbsp;
table.<span style="color:#9900CC;">find</span> <span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:first_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Keith'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># find record number 10 </span>
&nbsp;
table.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<h3>Migrating to ActiveRecord</h3>
<p>An example of migrating a DBF book table to ActiveRecord using a  migration:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'adt'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CreateBooks <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>      
	<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>            
		table = <span style="color:#6666ff; font-weight:bold;">ADT::Table</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'db/adt/books.adt'</span><span style="color:#006600; font-weight:bold;">&#41;</span>            
		<span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span>table.<span style="color:#9900CC;">schema</span><span style="color:#006600; font-weight:bold;">&#41;</span>        
		table.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>record<span style="color:#006600; font-weight:bold;">|</span>                  
			Book.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span>record.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#41;</span>            
		<span style="color:#9966CC; font-weight:bold;">end</span>      
	<span style="color:#9966CC; font-weight:bold;">end</span>  
	<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>        
		drop_table <span style="color:#ff3333; font-weight:bold;">:books</span>    
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Limitations and known bugs</h3>
<ul>
<li>ADT is read-only</li>
<li>External index files are not used</li>
<li>Dates are not currently handled correctly.</li>
</ul>
<h2>Future Directions</h2>
<p>I am going to continue working on this library.  I have already heard from a few others that found this useful immediately after I posted it online.  I was surprised people could find it so quickly.  I&#8217;m sure there are bugs.  I would appreciate any problems to be reported to the <a href="http://github.com/chasemgray/Ruby-ADT">Ruby-ADT Github page</a>.  Emails are also welcome, but any issues should be directed to the github page and I&#8217;ll get to them when I can.</p>
</p>
<p>Hope this helps somebody out there dealing with this format.</p>
</p>
<p>- Chase Gray</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2010/02/reading-advantage-database-server-files-adt-in-ruby/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Converting Multiple Microsoft Access Databases into a Single MySQL Database &#8211; Rails, Ruby, MDBTools</title>
		<link>http://chase.ratchetsoftware.com/2009/06/converting-multiple-microsoft-access-databases-into-a-single-mysql-database-rails-ruby-mdbtools/</link>
		<comments>http://chase.ratchetsoftware.com/2009/06/converting-multiple-microsoft-access-databases-into-a-single-mysql-database-rails-ruby-mdbtools/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 21:04:25 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=168</guid>
		<description><![CDATA[Some of the data I&#8217;ve been dealing with lately is in Microsoft Access databases (.mdb files). I&#8217;ve been surprised with the lack of support for manipulating these files outside of Access. There are a couple solutions, but nothing satisfied me needs. The worst data set came to me in a few hundred different .mdb files. [...]]]></description>
			<content:encoded><![CDATA[<p>Some of the data I&#8217;ve been dealing with lately is in Microsoft Access databases (.mdb files).  I&#8217;ve been surprised with the lack of support for manipulating these files outside of Access.  There are a couple solutions, but nothing satisfied me needs.  The worst data set came to me in a few hundred different .mdb files.  It looked like the person that exported the database exported a copy of the database for each unique facility.  So each facility had a file, but the schema for each file was exactly the same.  It was apparent that in order to do any sort of complex queries on the data, the files would have to be combined.  I was recommended a tool called mdbtools by somebody at a local Ruby meetup.  I ended up using this along with Ruby inside of a Rake task to convert all my .mdb files into a single MySQL database.  <span id="more-168"></span></p>
<h3>Prerequisites</h3>
<p>The first thing I needed to do was to successfully configure and compile mdbtools. This probably took the longest as there are a few different version out there.  I had the most success with these instructions from the author&#8217;s git repository, <a href="http://www.automatthew.com/2008/02/how-to-compile-mdbtools-on-mac-os-x.html?showComment=1219246620000">http://www.automatthew.com/2008/02/how-to-compile-mdbtools-on-mac-os-x.html?showComment=1219246620000</a>. I was also able to compile this version on my ubuntu machine pretty easily.  The biggest hangup was with a couple of dependencies.  I also had a weird error where the autogen.sh was not detecting my libtools library, but I knew this was installed.  I commented out this check in the autogen.sh and things all worked, luckily.</p>
<p>Understanding mdb-tools was not very difficult.  It contains various libaries for interfacing with it through C, etc.  The only thing I cared about were the command line tools that it provided, listed <a href="http://mdbtools.sourceforge.net/install/x53.htm">here</a>. The commands I will use in this post are mdb-tables, mdb-schema, mdb-export.</p>
<p>The other requirement is to have a MySQL table created ready with permissions, etc. that will end up holding the data from the combined .mdb files.</p>
<h3>Step 1: Extracting the Schema</h3>
<p>One important thing to note about my situation is that all the .mdb files have the same schema.  Knowing this, I was able to simply find a random .mdb file and base my MySQL schema on this.</p>
<p>I have my .mdb files stored in a shared directory and so within my Rake task I used the following code to grab the first one (I&#8217;m sure there is a better way).</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">first_mdb_file = <span style="color:#996600;">''</span>
<span style="color:#008000; font-style:italic;">#get first mdb file</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">entries</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{RAILS_ROOT}/../shared/system/data_sources/SC/sc_mdb_files/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>mdb_file<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> mdb_file =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#9900CC;">mdb</span><span style="color:#006600; font-weight:bold;">/</span>
    first_mdb_file = <span style="color:#996600;">&quot;#{RAILS_ROOT}/../shared/system/data_sources/SC/sc_mdb_files/#{mdb_file}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">break</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Knowing the path to the first .mdb file the schema can be extracted by getting the result of the following shell command (the code surrounded in backticks is executed on the command line of the system).  The -S flag tells it to santize some of the table names by replacing spaces with underscore, etc.  I needed this for my tables because there were some problems without it.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">schema_sql = <span style="color:#996600;">`mdb-schema -S #{first_mdb_file} mysql`</span></pre></div></div>

<h3>Step 2: Building the Tables</h3>
<p>Since I&#8217;m doing this in a Rake task in Rails (you could do this in plain ruby), I am using an ActiveRecord connection to interface with my MySQL database.  I have a class called ExternalDatabase that allows me to connect to a different database without modifying the one that my main Rails program uses.  I will list it below, I don&#8217;t remember exactly where I got it from.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#Class used to connect to external databases without modifying the exiisting connection used by Rails</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Data::ExternalDatabase</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">abstract_class</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">set_source</span><span style="color:#006600; font-weight:bold;">&#40;</span>configuration_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    source = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">configurations</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;sources&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>configuration_name.<span style="color:#5A0A0A; font-weight:bold;">to_s</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    establish_connection source
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">set_configuration</span><span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#41;</span>
    source = config
    establish_connection source
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>As you can see, I also have a section in my database.yml where i list connection details for various source databases.  This isn&#8217;t related to this post though.</p>
<p>So, assuming we have this class, we can use the following code to load our new schema into out MySQL database.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Data::ExternalDatabase</span>.<span style="color:#9900CC;">set_configuration</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:adapter</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;mysql&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:host</span>     <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;localhost&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:username</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> YOUR_DATABASE_USERNAME,
<span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> YOUR_DATABASE_PASSWORD,
<span style="color:#ff3333; font-weight:bold;">:database</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> YOUR_DATABASE_NAME,
<span style="color:#006600; font-weight:bold;">&#41;</span>
db =  <span style="color:#6666ff; font-weight:bold;">Data::ExternalDatabase</span>.<span style="color:#9900CC;">connection</span> 
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;adding if exists clause to drop tables&quot;</span>
&nbsp;
schema_sql = schema_sql.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>DROP TABLE <span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'DROP TABLE IF EXISTS <span style="color:#000099;">\1</span>;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
schema_sql = schema_sql.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/--</span>.<span style="color:#006600; font-weight:bold;">*</span>$<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;executing schema sql&quot;</span>
lines = schema_sql.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">';'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
lines.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sql_line<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> sql_line.<span style="color:#9900CC;">blank</span>?
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span>sql_line<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The first part builds my database configuration hash that I pass in to ExternalDatabase.  I save the connection into a variable called &#8216;db&#8217;.  One of the important things to note about this and mdb-tools is that it does not check if the table currently exists before trying to drop the table.  So I added a snippet of code that removes the comments and adds &#8216;IF EXISTS&#8217; to the &#8216;DROP TABLE&#8217; clause in the schema sql.</p>
<p>For some reason, trying to execute this entire schema sql string with db.execute causes various errors that I couldn&#8217;t figure out.  By separating the schema sql into individual sql statements terminated with semicolons seemed to fix this problem.  The last bit of code does the separation into lines and executes the sql on the database. After all this, the database should have the correct schema.</p>
<h3>Step 3: Load Data</h3>
<p>One of the last things we want to do is actually load all the data from the different mdb files into the single MySQL database.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">tables = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">`mdb-tables #{first_mdb_file}`</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">entries</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{RAILS_ROOT}/../shared/system/data_sources/SC/sc_mdb_files/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>mdb_file<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> mdb_file =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#9900CC;">mdb</span><span style="color:#006600; font-weight:bold;">/</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Current file: #{mdb_file}&quot;</span>
    tables.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>table<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Current table: #{table}&quot;</span>
      insert_sql = <span style="color:#996600;">`mdb-export -I -S #{RAILS_ROOT}/../shared/system/data_sources/SC/sc_mdb_files/#{mdb_file} #{table}`</span>
      insert_sql = insert_sql.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>;<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      insert_sql = insert_sql.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span>?\<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>\s<span style="color:#006600; font-weight:bold;">+</span>INSERT<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'<span style="color:#000099;">\1</span>;<span style="color:#000099;">\2</span>'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      lines = insert_sql.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">';'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      lines.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sql_line<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> sql_line.<span style="color:#9900CC;">blank</span>?
        db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span>sql_line<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So the first line runs mdb-tools command line tool &#8216;mdb-tables&#8217; to get a list of the tables separated by spaces.  I could have done this through my previously created schema as well.</p>
<p>This code goes through every .mdb file in my directory and uses mdb-tools to extract the data as sql &#8216;INSERT&#8217; statements from the command-line.</p>
<p>In the inner-most loop, I clean up the returned value from mdb-export because it does not include semi-colons after each INSERT statement.  I also remove any semi-colons before adding any because some of my fields had semi-colons in them.  Again, I separate each INSERT statement into a single statement and execute these individually in the last section of the code.</p>
<p>After all this you should have all your data combined into a single MySQL database.  One problem might exists though.  Certain tables, such as tables that contain general purpose labels, etc. for other tables to refer to, might contain duplicates.  This wouldn&#8217;t have caused a big problem for me because I just needed to extract data, but if you needed to continue using this database you would need to remove the duplicates.</p>
<h3>Final Step: Removing Duplicate Rows</h3>
<p>There seem to be a lot of different ways to remove duplicate rows from a MySQL table.  I wish there was a simple solution, but I couldn&#8217;t find one.  The following code does the following:</p>
<ol>
<li>Create a temporary table that has the same schema as the table we are removing duplicates from.</li>
<li>Insert all distinct rows into this new temporary table.</li>
<li>remove all values from the original table.</li>
<li>Insert all of the rows from the temporary table back into the original table.</li>
<li>Finally, drop the temporary table.</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">tables.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>table<span style="color:#006600; font-weight:bold;">|</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;CREATE TABLE temp LIKE #{table}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;INSERT INTO temp select distinct * FROM #{table}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;TRUNCATE #{table}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;INSERT INTO #{table} select * from temp&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  db.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;DROP TABLE temp&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Thanks for reading, I hope this was helpful to others that have some similar problems.</p>
<p>- Chase</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2009/06/converting-multiple-microsoft-access-databases-into-a-single-mysql-database-rails-ruby-mdbtools/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Uniq for Array or Hash with a Deeply Nested Structure</title>
		<link>http://chase.ratchetsoftware.com/2009/01/uniq-for-array-or-hash-with-a-deeply-nested-structure/</link>
		<comments>http://chase.ratchetsoftware.com/2009/01/uniq-for-array-or-hash-with-a-deeply-nested-structure/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 23:32:56 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=148</guid>
		<description><![CDATA[Most people have had some experience with ruby&#8217;s built in #uniq method for Arrays. Internally, this method finds the unique items in the array by creating a hash internally, and this internal comparison is done with the #eql? method. If an item in the array is a Hash, then #eql? simply uses the object_id, generated [...]]]></description>
			<content:encoded><![CDATA[<p>Most people have had some experience with ruby&#8217;s built in #<a title="Array Uniq Method" href="http://www.ruby-doc.org/core/classes/Array.html#M002237">uniq method for Arrays</a>.  Internally, this method finds the unique items in the array by creating a hash internally, and this internal comparison is done with the #eql? method.  If an item in the array is a Hash, then #eql? simply uses the object_id, generated by <code>the #hash method, </code>to determine whether it is equal to another object in the array.  There are many solutions online each with s light variations and goals.  I found myself in need of a uniq method for an array containing items in an arbitrarily deep nested structure (ie many sub-hashes and arrays). <span id="more-148"></span></p>
<p>I found myself working with some data in a hash generated from scraping a website.  The website had some duplicate content and I didn&#8217;t feel like it was necessary to clutter my scraping code with tests for duplicate content while it was scraping.  After scraping the data and turning it into a hash, I wished to remove the duplicates with some post-processing.  I felt like this should be pretty simple, except that the elements I wanted to test for uniqueness contained a deeply nested structure of hashes and arrays.  Here is an example an element in an array I wanted to uniqify:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#123;</span>:name <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob'</span>, <span style="color:#ff3333; font-weight:bold;">:posts</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:title <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'First Post!'</span> <span style="color:#ff3333; font-weight:bold;">:comments</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:commenter <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Alice'</span>, <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Hi bob!'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The #uniq method does not work as expected when there is a Hash element in the array, it only uses the object_id to determine uniqueness. There are some solutions online for dealing with this.</p>
<p>If you know that each element inside the array is a hash with no sub-hashes you can use the solution presented in the answer of <a href="http://stackoverflow.com/questions/181091/how-do-i-get-the-unique-elements-from-an-array-of-hashes-in-ruby">this</a> stackoverflow.com question.  The first solution uses #inject to build a new array with only unique items.  Whether an item is included in the array already or not is determined with the Hash&#8217;s #include method.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">a.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>result,h<span style="color:#006600; font-weight:bold;">|</span> result <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> h <span style="color:#9966CC; font-weight:bold;">unless</span> result.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>; result <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The second solution presented in the same post:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">a.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>h<span style="color:#006600; font-weight:bold;">|</span> h.<span style="color:#9900CC;">to_a</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">uniq</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#123;</span>k <span style="color:#006600; font-weight:bold;">=&gt;</span> v<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>This flattens all of your hashes into an array.  It then calls uniq on the resulting array after all the hashes are flattened.  This means that for an item to be unique it must have the same key AND value.  After finding the unique elements, it then maps the items back into a hash.</p>
<p>In <a href="http://mikeburnscoder.wordpress.com/2008/01/18/uniquify-an-array-of-hashes-in-ruby/">this</a> post, Mike Burns solve this problem by redefining #hash and #eql?.  If you are doing this kind of comparison often, this would probably be a better approach.  He does note though, that by doing this the #dup method no longer works as expected.</p>
<p>So it was obvious through searching that I was going to have to figure this out on my own.  I was a little worried that I would have to implement an ugly recursive solution that searches the entire tree to find all duplicates.  While looking into this solution, I felt that it would be very nice if #uniq would accept a block that would allow me to return true if the given parameter is a duplicate, thus removing it from the returned array.  This is not implemented in my current ruby version (1.8.6), but there is an example of how to do this on <a href="http://redhanded.hobix.com/bits/klemmeSSilentHash.html">this blog post</a>.  Hopefully, more (all) of these methods in the ruby core will accept blocks someday.</p>
<p>I began to write a method that would search my tree-like structure of hashes and arrays and I felt like it was a very ugly solution.  I wanted something simple that would just take a line or two and get the job done.  So I showed an example of the problem to my non-computer scientist wife and she said, &#8220;why don&#8217;t you just compare how the two lists look when printed out?&#8221;  I was a little shocked that I hadn&#8217;t thought of this.  It isn&#8217;t the most efficient solution and it relies on the #inspect method, which is probably pretty slow because it has to determine the structure of all the hashes in order to print them in a readable format.   The old solution I was cooking up would also have to traverse the tree, similar to what I expected #inspect was doing.  In addition to the #inspect call, there would be a string comparison for each element.  I was willing to accept both of these for a short elegant solution to my problem.</p>
<p>The issue that came to my mind first was whether hashes get printed out the same way every time.  In Ruby 1.9, it seems that Hashes are going to be stored in the same order that they were entered.  In previous versions, I&#8217;m assuming that they are stored in the order of their generated hash.   This could cause a problem if the data someone was entering could be in different orders.  For my purposes though, the data would be entered in the same order if it was a duplicate.</p>
<p>So, after all of that, to uniqify an array containing a deeply nested structure, I present the following solution:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> nested_uniq
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">delete_if</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&#91;</span>item<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">any</span>? <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>other_item<span style="color:#006600; font-weight:bold;">|</span> item.<span style="color:#9900CC;">inspect</span> == other_item.<span style="color:#9900CC;">inspect</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> nested_uniq!
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">replace</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">nested_uniq</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I&#8217;ll walk through it because it may seem a bit hard to understand at first.  I go through every element in the array with delete_if to determine if the element should be deleted from the array.  Then, for all elements except the one in question, I check to see if #any? of them are equivalent to the element in question.  This equivalence is determined by doing a string comparison of what the #inspect method returns for that element.</p>
<p>Here is an IRB test</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span>  test = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'5'</span>, <span style="color:#ff3333; font-weight:bold;">:test</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'10'</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'5'</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'2'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:to <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'1'</span>, <span style="color:#ff3333; font-weight:bold;">:yeah</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'8'</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:hello <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'5'</span>, <span style="color:#ff3333; font-weight:bold;">:test</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'10'</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'5'</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'2'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:test<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;10&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;2&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:hello</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:to<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;1&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:yeah</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;8&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:test<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;10&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;2&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:hello</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> test.<span style="color:#9900CC;">delete_if</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span>test <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&#91;</span>item<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">any</span>? <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>other_item<span style="color:#006600; font-weight:bold;">|</span> item.<span style="color:#9900CC;">inspect</span> == other_item.<span style="color:#9900CC;">inspect</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:to<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;1&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:yeah</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;8&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:test<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:hello<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;10&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:hell</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:yeah<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;2&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:hello</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;5&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Hope this is helpful to someone!  I am very interested to hear suggestions and comments on alternative ways to uniqify an array with elements that have a nested structure.</p>
<p>-Chase</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2009/01/uniq-for-array-or-hash-with-a-deeply-nested-structure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>URL Archive System or: URL Hacking Made Easy</title>
		<link>http://chase.ratchetsoftware.com/2008/11/url-archive-system-or-url-hacking-made-easy/</link>
		<comments>http://chase.ratchetsoftware.com/2008/11/url-archive-system-or-url-hacking-made-easy/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 02:53:32 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[project]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=125</guid>
		<description><![CDATA[Have you ever found yourself slightly modifying a URL to try to find something you know used to exist or should exist but you keep getting that dreaded 404 page?  Perhaps you were trying to find something that shouldn&#8217;t be online anymore but it was simply unlinked to, benevolent purposes or otherwise? Another example might [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever found yourself slightly modifying a URL to try to find something you know used to exist or should exist but you keep getting that dreaded 404 page?  Perhaps you were trying to find something that shouldn&#8217;t be online anymore but it was simply unlinked to, benevolent purposes or otherwise? Another example might be that a site&#8217;s main homepage is down and so many users are unable to access the site.  If they had a list of URLs they could see which are still valid very quickly with the URL Archive and go to the site through those URLs.  <a href="http://www.googleguide.com/cached_pages.html">Google cache </a>and <a href="http://www.archive.org/web/web.php">Wayback Machine</a> get you pretty close to what you&#8217;re after but sometimes they just fall a little too short.  Some things are beyond even<em> </em>Google&#8217;s giant umbrella of web applications (at least for now).</p>
<p>I&#8217;m sure an application focusing more on URLs is somebody&#8217;s 10% project somewhere sitting on the backburner until they have time to finish it.  It seems like almost anything you&#8217;d want to do has a corresponding web application.  When I come across a need that doesn&#8217;t I just feel like that void should soon be filled.  I&#8217;m going to outline the application that I believe would fill this void.  Maybe in future posts I&#8217;ll also walkthrough my attempts to do it as my personal .5% project.<span id="more-125"></span></p>
<h3>Doesn&#8217;t this already exist?</h3>
<p>When giving the quick summary of what I envisioned for the project the first question I get is, &#8220;Doesn&#8217;t Google Cache already do that?&#8221;  My initial reaction is that if it did, I would already be using it for that purpose.  After thinking about it, it doesn&#8217;t seem like that far of a stretch for Wayback Machine or Google Cache to archive URLs as well.  I feel like the real kicker is that in order to make it useful there would have to be extra processing and utilities for the URL archive.  The information is already there, so what needs to be done is create an interface for analyzing the URL a site has.  Before talking about the ideal solution, first let&#8217;s look at what is offered by Google Cache and Wayback Machine already to compare.</p>
<p>To get this sort of functionality out of Google Cache as far as I know is to use a &#8220;site:&#8221; search.  So, typing in the search, &#8220;site:arena.cse.sc.edu&#8221;, I get the following:</p>
<div id="attachment_127" class="wp-caption alignnone" style="width: 510px"><a href="http://chase.ratchetsoftware.com/wp-content/2008/11/picture-1.png" rel="lightbox[125]"><img class="size-full wp-image-127" title="ARENA Google Site: Result" src="http://chase.ratchetsoftware.com/wp-content/2008/11/picture-1.png" alt="Result for site:arena.cse.sc.edu" width="500" height="271" /></a><p class="wp-caption-text">Result for site:arena.cse.sc.edu</p></div>
<p>I know that our research group&#8217;s website has lists of most of the pdfs that we have published as well a number of static pages that total more than the 17 that are listed in these results.  Of course, this site isn&#8217;t really optimized for search engines, so that might also not be helping.  Searching for just pdfs returns a measly 7 results.  Some of our papers are listed as links to other sites such as IEEE, which causes them to not get listed in these results.  Alternatively, if I could somehow easily browse all the URLs that exists for this domain, I might be able to see more information about what the website actually contains internally (more on this later).  Another experiment was to search for, &#8220;site:chase.ratchetsoftware.com&#8221;, and because this site is optimized for search engines, most of the URLs that exist were found pretty easily.  There is still no way to really tell what the site contains as an overview without going to the site or reading the provided description.  Put another way, it would be interesting to be able to see a something like a content tree that lets a viewer easily see what is contained in the site with a quick glance.  These are all things I believe can be solved and would be useful, but Google Cache and Wayback Machine are not the tools to do it.</p>
<h3>What would an ideal solution look like?</h3>
<p>I&#8217;ve given a few examples where Google Cache and Wayback Machine don&#8217;t offer what I am looking for.  I think to improve my argument it is best to talk about what the URL archive system would be in a perfect world.  It&#8217;s hard to see the difference in something that doesn&#8217;t yet exist.</p>
<p>I am going to list the features I have in mind and then explain any that aren&#8217;t obvious:</p>
<ol>
<li>Provide a list of all URLs ever found for a given domain.</li>
<li>When a user views a list, it should check and display whether that URL is still valid.</li>
<li>Organize the list of URLs so that it is tractable by a normal user.  It should have collapsible SubURLs so that only top level URLs are in the master list.</li>
<li>Provide a filter so that only URLs matching the given filter are shown.</li>
<li>Provide the ability to click on a URL to make that the new root URL for our list.</li>
<li>A utility that can display a graphical tree view of the page and its links.  If the page links to an outside page, then show the link with the icon to that page if available.  This view would ideally provide enough information and &#8220;graphics&#8221; to give the user a good idea of what is available on the site.</li>
<li>If the site uses dynamic URLs, perhaps for a site written in rails or php, there should be some sort of analysis of the GET arguments from what the system can figure out.  This might be an area where users can submit additional information to improve the analysis of a web applications generated URL addresses.</li>
<li>One of the coolest ones.  A firefox plugin that automatically helps you when trying to figure out URLs based on what is stored in the URL Archive.  So if you&#8217;re trying to modify a URL slightly, you can get real-time help and see which URLs are still valid, etc.  Firefox is good right now with it&#8217;s new location bar that automatically searches your history and all that stuff.  It&#8217;d be even better if it could help you with long URLs that you haven&#8217;t typed in before!</li>
<li>Another plugin idea would be to provide screen readers easier access to a site&#8217;s content.  If all we had are the URLs and perhaps keywords for those URLs, a blind person might be able to more easily find content quicker.</li>
<li>One more plugin would be to have a sidebar while browsing sites, and as you browse you get an updated list of URLs that have existed at your current location on the site.  This would put a sidebar on your left with all the links you might see on that site and all the URLs that you might not see but existed in the past.  This would be useful for quickly navigating huge sites, especially ones that are moving their links around a lot.</li>
<li>The last idea I can think of right now is that the system could analyze how the URLs are changing and suggest to users URLs that may be used in the future or are currently used but not linked to based on the patterns in  the current URLs and their change history.  This might be useful if someone is trying to URL hack but doesn&#8217;t have any idea where to start.</li>
</ol>
<p>I feel like number six is the one that needs further explanation in order to seem as cool as it does to me.  I believe there are already existing tools that will show you a tree view of a website.  There are definitely tools to show you this information for your own site, cause I&#8217;ve used them before.  So how does number six differ from these tools?  The answer is that it doesn&#8217;t differ dramatically, but hopefully it would offer more information because the purpose is slightly different.  Let&#8217;s give an example of a user that is looking for a site that offers ebooks in pdf form online somewhere.  The user might go to various sites by searching through Google and waste time at many sites that seem to offer ebooks but don&#8217;t have that large of a selection.  If the user could easily view a graph of each site before visiting, and turned on a filter for &#8220;.pdf&#8221;, he could hopefully see a tree that branches out into many different URLs leading to ebooks he&#8217;s interested in.  He should be able to quickly get an idea of the quantity, quality, and link validity of the site&#8217;s offerings.  Each node could be clicked on to make that node the new root node in the tree to perhaps get more specific information about a certain path.   There are many possibilities for graphically browsing a site&#8217;s URLs and URL relationships.  To me, this seems like a separate application entirely.  It might be that the site should be developed with a SOAP or REST interface and another application could be developed that provides this tree view of websites.</p>
<h3>Potential problems and Questions</h3>
<p>Not all good things come from having a URL Archive.  All the <a href="http://searchsecurity.techtarget.com/news/article/0,,sid14_gci1315588,00.html">security concerns</a> that come with Google Cache and Wayback Machine will also affect the URL Archive.  Some information that people wished to remove from the internet by unlinking it might be affected in a negative way by a system that indexes URLs forever.  Analyzng the GET parameters of a web application might be good for some users, but a malicious user could probably use this information to make the website work as it wasn&#8217;t intended.  Most of these problems come with developing this type of application.  There are tradeoffs and we just need to try to do our best to mitigate them while still providing a good service.</p>
<p>Another issue is the fact that I have no idea how to store such a large amount of information as this system would require.  I don&#8217;t even know where to begin with the indexing of URLs on the Internet.  It seems like it would take way too long with this little virtual machine on a server somewhere that I have.  I&#8217;d love to hear some comments about this.  How would one create an application like this that has to store such large amounts of information and process so much new data all the time?  Some resources relating to this would probably make for some great bathroom pulp <img src='http://chase.ratchetsoftware.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h3>Call for help</h3>
<p>In my current situation I have way too much on my plate to try to develop an application for which there would be no immediate gain.  I need to work on my own business to try to get that going before I have to get a real job <img src='http://chase.ratchetsoftware.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .  I also have an seemingly endless amount of Ph.D. work.  Despite this, I would more than willing to contribute to a project that was working on this solution.  I would even be happy to set up a Rails application on this server and with a Git repository if there were a couple developers interested in working on it.  All I&#8217;d need to distract me from my other work is a little motivation from other people&#8217;s interest, so please let me know in the comments if this is something you&#8217;d like to use or have needed in the past.</p>
<p>Thanks for reading,</p>
<p>- Chase Gray</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2008/11/url-archive-system-or-url-hacking-made-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Necklace Simulation</title>
		<link>http://chase.ratchetsoftware.com/2008/09/necklace-simulation/</link>
		<comments>http://chase.ratchetsoftware.com/2008/09/necklace-simulation/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 19:51:28 +0000</pubDate>
		<dc:creator>chasemgray</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[physics]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://chase.ratchetsoftware.com/?p=54</guid>
		<description><![CDATA[I had some ideas that required some sort of necklace simulator through a web interface.  I didn&#8217;t have much experience programming anything complex in ActionScript, but I was sure that would be the way to go.  I have seen some amazing work done with Flash applications over the past few years and I had no [...]]]></description>
			<content:encoded><![CDATA[<p>I had some ideas that required some sort of necklace simulator through a web interface.  I didn&#8217;t have much experience programming anything complex in ActionScript, but I was sure that would be the way to go.  I have seen some amazing work done with Flash applications over the past few years and I had no doubt that Flash could handle such an application.  So I set out on one weekend to put together a quick prototype for my idea.</p>
<p><span id="more-54"></span></p>
<p>Of course the first question I had was what physics libraries are available for Flash these days.  I had some experience in the past using Newton and various wrappers in C++ for a game project.  My experience with Newton would end up helping me pick up a new physics engine relatively easy.</p>
<p>There were a couple of choices to choose from and I started my search by reading some reviews found on <a title="Geek Glue Actionscript Physics Engine Overview" href="http://http://geekglue.blogspot.com/2008/01/actionscript-2d-physics-engines.html" target="_blank">GeekGlue.</a> From this I was able to reduce my search to the <a title="APE" href="http://www.cove.org/ape/index.htm" target="_blank">Actionscript Physics Engine (APE)</a> and<a title="Box2D Flash ActionScript 3" href="http://box2dflash.sourceforge.net/" target="_blank"> Box2DFlashAS3</a>.  APE seemed much easier to get started with, but I wasn&#8217;t sure if it would accomplish what I wanted without a lot of massaging.  After further reading on some forums, I found that the rope simulation in APE is much more limited because there is only one type of joint available at this time.  Box2D has 6 different types of joints and their provided rope bridge looked a little better.  The fact that Box2D had ragdoll physics examples made me feel that I would have as much control as I needed to create the necklace simulation I wanted.</p>
<p>After choosing Box2D I wanted to take their rope &#8220;springy&#8221; bridge demo and see if that could be easily turned into some sort of free falling rope.  This was not as simple as adding links and changing a few variables like I had hoped.  If you simply add links to the rope, after adding too many the iterative solver breaks the joints with the default configuration.  I had to search for a while to figure out why this was.  After some searching, I found that a joint can only handle about a 10:1 weight ratio before giving out.  The solution to this is the increase the number of iterations of decrease the step time.  This of course will slow down your simulation depending on how much you have to increase the number of iterations.  I had to change mine from about 10 to 40.  My simulation was still running at a decent speed but in the future I am not sure how this will hold up with more physics bodies flying around.  This was a huge problem for a while for me and I was very glad to finally find a solution.  If you have joints that are breaking due to too much force being applied to them is too much without adding more iterations to the engine so it can accurately solve the joint forces.  I hope someone with a similar problem can find this post because it took me a while to find a solution on a forum somewhere.</p>
<p>I am not concerned with creating a nice looking application at this time since this was just a weekend project I was doing on the side.  The goals for the prototype of my necklace simulator I wanted to have  the following:</p>
<ol>
<li>A rope like necklace that could be moved around by the user with realistic physics.</li>
<li>Two end pieces on the necklace that were different than the rest of the necklace and could be connected to things.</li>
<li>Two or more connectors in the world that could have a necklace endpiece connected to them and unattached when the user wants to move the necklace again.</li>
<li>A prototype of a bead which can be moved by the user and placed on the necklace.</li>
<li>A background of a neck or something so that it is easy to see what the necklace would look like on a person.</li>
</ol>
<p>For my resulting application, I simply modified the default Test class provided with Box2DFlashAS3 to have instances of the objects in the world such as the rope and beads.  Previously it was a base class for various tests.  This might not be the best solution, but it was fine for my prototype to see if things would work.  I&#8217;ll include the entire source here and highlight my changes as well.</p>
<p><strong>Playground.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #3f5fbf;">/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/</span>
&nbsp;
<span style="color: #9900cc; font-weight: bold;">package</span> TestBed<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Main;
	<span style="color: #0033ff; font-weight: bold;">import</span> General.Input;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">getTimer</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Playground<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Playground<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
			m_sprite = Main.m_sprite;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> worldAABB<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			worldAABB.lowerBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1000.0</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1000.0</span><span style="color: #000000;">&#41;</span>;
			worldAABB.upperBound.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1000.0</span>, <span style="color: #000000; font-weight:bold;">1000.0</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Define the gravity vector</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> gravity<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.0</span>, <span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Allow bodies to sleep</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> doSleep<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
&nbsp;
			<span style="color: #009900;">// Construct a world object</span>
			m_world = <span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span>worldAABB, gravity, doSleep<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// set debug draw</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> dbgDraw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//var dbgSprite:Sprite = new Sprite();</span>
			<span style="color: #009900;">//m_sprite.addChild(dbgSprite);</span>
			dbgDraw.m_sprite = m_sprite;
			dbgDraw.m_drawScale = <span style="color: #000000; font-weight:bold;">30.0</span>;
			dbgDraw.m_fillAlpha = <span style="color: #000000; font-weight:bold;">0.3</span>;
			dbgDraw.m_lineThickness = <span style="color: #000000; font-weight:bold;">1.0</span>;
			dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit <span style="color: #000000; font-weight: bold;">|</span> b2DebugDraw.e_jointBit;
			m_world.SetDebugDraw<span style="color: #000000;">&#40;</span>dbgDraw<span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Create border of boxes</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> wallSd<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> wallBd<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> wallB<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
			<span style="color: #009900;">// Left</span>
			wallBd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">95</span> <span style="color: #000000; font-weight: bold;">/</span> m_physScale, <span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			wallSd.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			wallB = m_world.CreateBody<span style="color: #000000;">&#40;</span>wallBd<span style="color: #000000;">&#41;</span>;
			wallB.CreateShape<span style="color: #000000;">&#40;</span>wallSd<span style="color: #000000;">&#41;</span>;
			wallB.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// Right</span>
			wallBd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">640</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">95</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> m_physScale, <span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			wallB = m_world.CreateBody<span style="color: #000000;">&#40;</span>wallBd<span style="color: #000000;">&#41;</span>;
			wallB.CreateShape<span style="color: #000000;">&#40;</span>wallSd<span style="color: #000000;">&#41;</span>;
			wallB.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// Top</span>
			wallBd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">640</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">95</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			wallSd.SetAsBox<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">680</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//wallB = m_world.CreateBody(wallBd);</span>
			<span style="color: #009900;">//wallB.CreateShape(wallSd);</span>
			<span style="color: #009900;">//wallB.SetMassFromShapes();</span>
			<span style="color: #009900;">// Bottom</span>
			wallBd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">640</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">95</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			wallB = m_world.CreateBody<span style="color: #000000;">&#40;</span>wallBd<span style="color: #000000;">&#41;</span>;
			wallB.CreateShape<span style="color: #000000;">&#40;</span>wallSd<span style="color: #000000;">&#41;</span>;
			wallB.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			m_rope = <span style="color: #0033ff; font-weight: bold;">new</span> Rope<span style="color: #000000;">&#40;</span>m_world, m_physScale<span style="color: #000000;">&#41;</span>;
&nbsp;
			m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
			m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
&nbsp;
			m_leftHook = <span style="color: #0033ff; font-weight: bold;">new</span> Hook<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">175</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			m_rightHook = <span style="color: #0033ff; font-weight: bold;">new</span> Hook<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
			m_hookJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			m_bead = <span style="color: #0033ff; font-weight: bold;">new</span> Bead<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #000000; font-weight:bold;">7.5</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Update<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #009900;">// Update mouse joint</span>
			UpdateMouseWorld<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			MouseDestroy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			MouseDrag<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Update physics</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> physStart<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_world.Step<span style="color: #000000;">&#40;</span>m_timeStep, m_iterations<span style="color: #000000;">&#41;</span>;
			Main.m_fpsCounter.updatePhys<span style="color: #000000;">&#40;</span>physStart<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// Member Data</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_world<span style="color: #000000; font-weight: bold;">:</span>b2World;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_mouseJoint<span style="color: #000000; font-weight: bold;">:</span>b2MouseJoint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_iterations<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">40</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_timeStep<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_physScale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #009900;">// world mouse position</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mouseXWorldPhys<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mouseYWorldPhys<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mouseXWorld<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> mouseYWorld<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #009900;">// Sprite to draw in to</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rope<span style="color: #000000; font-weight: bold;">:</span>Rope;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_hookJointDef<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHookJoint<span style="color: #000000; font-weight: bold;">:</span>b2Joint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHookJoint<span style="color: #000000; font-weight: bold;">:</span>b2Joint;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHook<span style="color: #000000; font-weight: bold;">:</span>Hook;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHook<span style="color: #000000; font-weight: bold;">:</span>Hook;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_bead<span style="color: #000000; font-weight: bold;">:</span>Bead;
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// Update mouseWorld</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> UpdateMouseWorld<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			mouseXWorldPhys = <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseX</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale;
			mouseYWorldPhys = <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale; 
&nbsp;
			mouseXWorld = <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseX</span><span style="color: #000000;">&#41;</span>;
			mouseYWorld = <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// Mouse Drag</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MouseDrag<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// mouse press</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseDown</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #000000; font-weight: bold;">!</span>m_mouseJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body = GetBodyAtMouse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> md<span style="color: #000000; font-weight: bold;">:</span>b2MouseJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2MouseJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					md.body1 = m_world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					md.body2 = body;
					md.<span style="color: #004993;">target</span>.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
					md.maxForce = <span style="color: #000000; font-weight:bold;">600.0</span> <span style="color: #000000; font-weight: bold;">*</span> body.GetMass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					md.timeStep = m_timeStep;
					m_mouseJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>md<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2MouseJoint;
					body.WakeUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_rope.m_leftHook <span style="color: #000000; font-weight: bold;">||</span> body == m_rope.m_rightHook<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHookJoint <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; body == m_leftHookJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
								m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span>;
								m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
						<span style="color: #000000;">&#125;</span>
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHookJoint <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; body == m_rightHookJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
								m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span>;
								m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_bead.m_body<span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						m_bead.SetCollidable<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900;">// mouse release</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>Input.<span style="color: #004993;">mouseDown</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">//get body being clicked on</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> clickedBody<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_mouseJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>;
					m_mouseJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>clickedBody == m_bead.m_body<span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						m_bead.SetCollidable<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rope.IsAttachable<span style="color: #000000;">&#40;</span> clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
&nbsp;
						<span style="color: #6699cc; font-weight: bold;">var</span> ground<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHook.IsTouching<span style="color: #000000;">&#40;</span>clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
							<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;test&quot;</span><span style="color: #000000;">&#41;</span>;
							m_hookJointDef.Initialize<span style="color: #000000;">&#40;</span>m_leftHook.m_body,  clickedBody, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
							<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
								m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span>;
								m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
							<span style="color: #000000;">&#125;</span>
							m_leftHookJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>m_hookJointDef<span style="color: #000000;">&#41;</span>;
						<span style="color: #000000;">&#125;</span>
						<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHook.IsTouching<span style="color: #000000;">&#40;</span> clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
							<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
								m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span>;
								m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
							<span style="color: #000000;">&#125;</span>
							m_hookJointDef.Initialize<span style="color: #000000;">&#40;</span>m_rightHook.m_body,  clickedBody,  <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
							m_rightHookJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>m_hookJointDef<span style="color: #000000;">&#41;</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900;">// mouse move</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> p2<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
				m_mouseJoint.SetTarget<span style="color: #000000;">&#40;</span>p2<span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// Mouse Destroy</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MouseDestroy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// mouse press</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>Input.<span style="color: #004993;">mouseDown</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; Input.isKeyPressed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">68</span><span style="color: #3f5fbf;">/*D*/</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body = GetBodyAtMouse<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					m_world.DestroyBody<span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">return</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//======================</span>
		<span style="color: #009900;">// GetBodyAtMouse</span>
		<span style="color: #009900;">//======================</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> mousePVec<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> GetBodyAtMouse<span style="color: #000000;">&#40;</span>includeStatic<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>b2Body<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// Make a small box.</span>
			mousePVec.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> aabb<span style="color: #000000; font-weight: bold;">:</span>b2AABB = <span style="color: #0033ff; font-weight: bold;">new</span> b2AABB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			aabb.lowerBound.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">0.001</span>, mouseYWorldPhys <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">0.001</span><span style="color: #000000;">&#41;</span>;
			aabb.upperBound.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.001</span>, mouseYWorldPhys <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.001</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Query the world for overlapping shapes.</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> k_maxCount<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">10</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> shapes<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> count<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = m_world.Query<span style="color: #000000;">&#40;</span>aabb, shapes, k_maxCount<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body = <span style="color: #0033ff; font-weight: bold;">null</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&amp;</span>lt; count; <span style="color: #000000; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>shapes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.IsStatic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> == <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000; font-weight: bold;">||</span> includeStatic<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> tShape<span style="color: #000000; font-weight: bold;">:</span>b2Shape = shapes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2Shape;
					<span style="color: #6699cc; font-weight: bold;">var</span> inside<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = tShape.TestPoint<span style="color: #000000;">&#40;</span>tShape.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetXForm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, mousePVec<span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>inside<span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						body = tShape.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> body;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The first change is at the end of the constructor where I create the items that are in the world.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">m_rope = <span style="color: #0033ff; font-weight: bold;">new</span> Rope<span style="color: #000000;">&#40;</span>m_world, m_physScale<span style="color: #000000;">&#41;</span>;
&nbsp;
m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
&nbsp;
m_leftHook = <span style="color: #0033ff; font-weight: bold;">new</span> Hook<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">175</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
m_rightHook = <span style="color: #0033ff; font-weight: bold;">new</span> Hook<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale, <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;
m_hookJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
m_bead = <span style="color: #0033ff; font-weight: bold;">new</span> Bead<span style="color: #000000;">&#40;</span>m_world, m_physScale, <span style="color: #000000; font-weight:bold;">7.5</span><span style="color: #000000; font-weight: bold;">/</span>m_physScale<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>The second noticable difference is the addition of the member variables.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rope<span style="color: #000000; font-weight: bold;">:</span>Rope;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_hookJointDef<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHookJoint<span style="color: #000000; font-weight: bold;">:</span>b2Joint;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHookJoint<span style="color: #000000; font-weight: bold;">:</span>b2Joint;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHook<span style="color: #000000; font-weight: bold;">:</span>Hook;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHook<span style="color: #000000; font-weight: bold;">:</span>Hook;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_bead<span style="color: #000000; font-weight: bold;">:</span>Bead;</pre></div></div>

<p>In the mouse drag function are the most important additions.  In the first part of this function we are focusing on mouse presses, or an initiated drag.  The first thing we do is check to see if we clicked on a joint at one of our hooking points, if so we want to destroy that joint and let the mouse move the necklace wherever it wants to.  We also want the user to be able to click a bead and place it on the necklace without colliding with the necklace while trying to drag the bead, this is also check for here, we turn off collisions for a bead if it is being dragged.</p>
<p>\The second part of the mouse drag function is when the mouse button is relased after dragging has occurred.  When this happens, we check to see if we were holding a bead, if we were we set it to be collidable, so that it will stay on the necklace if it was placed there.  We are also checking to see if the right or left hook are clicked on with one of the end pieces of the necklace.  If they are then we create a new joint between the hook and the necklace endpiece.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//======================</span>
<span style="color: #009900;">//======================</span>
<span style="color: #009900;">// Mouse Drag</span>
<span style="color: #009900;">//======================</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MouseDrag<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// mouse press</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>Input.<span style="color: #004993;">mouseDown</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #000000; font-weight: bold;">!</span>m_mouseJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body = GetBodyAtMouse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> md<span style="color: #000000; font-weight: bold;">:</span>b2MouseJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2MouseJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			md.body1 = m_world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			md.body2 = body;
			md.<span style="color: #004993;">target</span>.Set<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
			md.maxForce = <span style="color: #000000; font-weight:bold;">600.0</span> <span style="color: #000000; font-weight: bold;">*</span> body.GetMass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			md.timeStep = m_timeStep;
			m_mouseJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>md<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> b2MouseJoint;
			body.WakeUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_rope.m_leftHook <span style="color: #000000; font-weight: bold;">||</span> body == m_rope.m_rightHook<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHookJoint <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; body == m_leftHookJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
						m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span>;
						m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHookJoint <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; body == m_rightHookJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
						m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span>;
						m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_bead.m_body<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				m_bead.SetCollidable<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">// mouse release</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>Input.<span style="color: #004993;">mouseDown</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//get body being clicked on</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> clickedBody<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_mouseJoint.GetBody2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>;
			m_mouseJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>clickedBody == m_bead.m_body<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				m_bead.SetCollidable<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rope.IsAttachable<span style="color: #000000;">&#40;</span> clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #6699cc; font-weight: bold;">var</span> ground<span style="color: #000000; font-weight: bold;">:</span>b2Body = m_world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHook.IsTouching<span style="color: #000000;">&#40;</span>clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;test&quot;</span><span style="color: #000000;">&#41;</span>;
					m_hookJointDef.Initialize<span style="color: #000000;">&#40;</span>m_leftHook.m_body,  clickedBody, <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
						m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_leftHookJoint<span style="color: #000000;">&#41;</span>;
						m_leftHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
					<span style="color: #000000;">&#125;</span>
					m_leftHookJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>m_hookJointDef<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHook.IsTouching<span style="color: #000000;">&#40;</span> clickedBody<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
						m_world.DestroyJoint<span style="color: #000000;">&#40;</span>m_rightHookJoint<span style="color: #000000;">&#41;</span>;
						m_rightHookJoint = <span style="color: #0033ff; font-weight: bold;">null</span>;
					<span style="color: #000000;">&#125;</span>
					m_hookJointDef.Initialize<span style="color: #000000;">&#40;</span>m_rightHook.m_body,  clickedBody,  <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
					m_rightHookJoint = m_world.CreateJoint<span style="color: #000000;">&#40;</span>m_hookJointDef<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">// mouse move</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>m_mouseJoint<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> p2<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span>mouseXWorldPhys, mouseYWorldPhys<span style="color: #000000;">&#41;</span>;
		m_mouseJoint.SetTarget<span style="color: #000000;">&#40;</span>p2<span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There are three types that are used in the Playground.as file that we haven&#8217;t seen and are not in Box2D.  Those are the Rope, Bead, and Hook classes.</p>
<p>The rope is the most complicated and has the most new code.  When instantiated it builds itself vertically at a hard coded location.  It has an endpiece on each end that is different than the rest of the necklace.   Initially I made all pieces of the necklace a rectangle and connected them end to end.  This proved to be a mistake because when there was too much bending or twisting between two rectangle shapes at a joint they would sometimes not know how to fix themselves.  It would look like a permanent crease between the joint.  I was unable to figure out how to fix this problem with rectangles and I figured collisions between spheres is always cheaper so I moved everything to circles.</p>
<p>So the result is that the rope is made up of anywhere between 10 and 60 circles.  The values for the number of circles and the size of them is currently hard coded in the rope.as file.  Most of what is happening during the building of the rope is pretty obvious.  I changed a couple of the collision properties to make things look more realistic.</p>
<p>Each end piece is special and can be attached to a connector in the stage.  The isAttachable function checks to see if a given body can be attached to something.  I did this so it was easy to change which things could be attached in the future.</p>
<p>One last thing to note is that I made the end pieces collidable with the necklace pieces but the necklace pieces are not collidable with themselves.  This saves a lot of computation I&#8217;m sure but the main reason was so that you could more easily find an endpiece among a pile of necklace since it should be sitting on top.</p>
<p><strong>Rope.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Rope<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Rope<span style="color: #000000;">&#40;</span>world<span style="color: #000000; font-weight: bold;">:</span>b2World, physScale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #009900;">// Set block length &amp;amp; width &amp;amp; overlap(to make block connections look smoother)</span>
		m_blockLength =<span style="color: #000000; font-weight:bold;">3.5</span>;
		m_blockWidth = <span style="color: #000000; font-weight:bold;">3.5</span>;
		m_overlap = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">4.5</span>;
		<span style="color: #009900;">// Set end piece paramaters</span>
		m_circleRadius = <span style="color: #000000; font-weight:bold;">3</span>;
&nbsp;
		<span style="color: #009900;">// Starting height for necklace</span>
		m_startingHeight = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">200</span>; 
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> vel<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		vel.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> ground<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetGroundBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> anchor<span style="color: #000000; font-weight: bold;">:</span>b2Vec2 = <span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #6699cc; font-weight: bold;">var</span> body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
		<span style="color: #009900;">// Bridge</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> sd<span style="color: #000000; font-weight: bold;">:</span>b2PolygonDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			sd.SetAsBox<span style="color: #000000;">&#40;</span>m_blockLength <span style="color: #000000; font-weight: bold;">/</span> physScale, m_blockWidth <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
			sd.density = <span style="color: #000000; font-weight:bold;">20</span>;
			sd.friction = .8;
			sd.restitution = <span style="color: #000000; font-weight:bold;">0.0</span>;
			sd.<span style="color: #004993;">filter</span>.groupIndex = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> cd<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			cd.density = <span style="color: #000000; font-weight:bold;">20.0</span>;
			cd.radius = <span style="color: #000000;">&#40;</span>m_circleRadius<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale;
			cd.restitution = <span style="color: #000000; font-weight:bold;">0.0</span>;
			cd.friction=<span style="color: #000000; font-weight:bold;">0.8</span>;
			cd.<span style="color: #004993;">filter</span>.groupIndex = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">3</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> bd<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			bd.<span style="color: #004993;">angle</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">90</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> jd<span style="color: #000000; font-weight: bold;">:</span>b2RevoluteJointDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2RevoluteJointDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			const numPlanks<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">50</span>;
&nbsp;
			jd.lowerAngle = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>;
			jd.upperAngle = <span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>;
			jd.enableLimit = <span style="color: #0033ff; font-weight: bold;">false</span>;
			jd.collideConnected = <span style="color: #0033ff; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> prevBody<span style="color: #000000; font-weight: bold;">:</span>b2Body = ground;
&nbsp;
			bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">/</span> physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>physScale<span style="color: #000000;">&#41;</span>
			<span style="color: #009900;">//bd.isBullet = true;</span>
			body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>sd<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			body.SetLinearVelocity<span style="color: #000000;">&#40;</span>vel<span style="color: #000000;">&#41;</span>;
			m_leftHook = prevBody = body;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&amp;</span>lt; numPlanks; <span style="color: #000000; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">/</span> physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> m_blockLength <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">-</span> m_overlap<span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
				bd.isBullet = <span style="color: #0033ff; font-weight: bold;">false</span>;
				bd.linearDamping = <span style="color: #000000; font-weight:bold;">0.1</span>;
				bd.angularDamping = <span style="color: #000000; font-weight:bold;">0.5</span>;
				body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
				body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
				body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
				anchor.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">/</span> physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">-</span> m_overlap<span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
&nbsp;
				jd.Initialize<span style="color: #000000;">&#40;</span>prevBody, body, anchor<span style="color: #000000;">&#41;</span>;
&nbsp;
				world.CreateJoint<span style="color: #000000;">&#40;</span>jd<span style="color: #000000;">&#41;</span>;
&nbsp;
				prevBody = body;
			<span style="color: #000000;">&#125;</span>
			bd.linearDamping = <span style="color: #000000; font-weight:bold;">0</span>;
			bd.angularDamping = <span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #009900;">//bd.isBullet = true;</span>
			bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000; font-weight: bold;">/</span>physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> m_blockLength <span style="color: #000000; font-weight: bold;">+</span> m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>numPlanks <span style="color: #000000; font-weight: bold;">-</span> m_overlap<span style="color: #000000; font-weight: bold;">*</span>numPlanks<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
			body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
			body.CreateShape<span style="color: #000000;">&#40;</span>sd<span style="color: #000000;">&#41;</span>;
			body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			m_rightHook = body;
			anchor.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000000; font-weight: bold;">/</span> physScale, <span style="color: #000000;">&#40;</span>m_startingHeight <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>m_blockLength<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>numPlanks<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">-</span> m_overlap<span style="color: #000000; font-weight: bold;">*</span>numPlanks<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> physScale<span style="color: #000000;">&#41;</span>;
			jd.Initialize<span style="color: #000000;">&#40;</span>prevBody, body, anchor<span style="color: #000000;">&#41;</span>;
			world.CreateJoint<span style="color: #000000;">&#40;</span>jd<span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> IsAttachable<span style="color: #000000;">&#40;</span>body<span style="color: #000000; font-weight: bold;">:</span>b2Body<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Boolean</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>body == m_leftHook <span style="color: #000000; font-weight: bold;">||</span> body == m_rightHook<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">true</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">//======================</span>
	<span style="color: #009900;">// Member Data</span>
	<span style="color: #009900;">//======================</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_blockLength<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_blockWidth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_circleRadius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_overlap<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_startingHeight<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_leftHook<span style="color: #000000; font-weight: bold;">:</span>b2Body;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_rightHook<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Creating a bead class that could be placed on the necklace was one thing I was unsure of how to do initially.  The end result for my prototype was to create three circle shapes on a single body and to make the center one a sensor.  A sensor is something that can collide with other objects but doesn&#8217;t cause any actual physical response.  It is essentially not collidable but still keeps track of its collisions.</p>
<p>So the constructor creates this complex body.  I found very little documentation on how to make a simple complex body out of simple shapes online.  It was very easy though and if you look at the bead.as constructor you can see how I did it.  The most confusing part was how I was supposed to offset the different shapes in the body.  This is accomplished via the Shape class&#8217; localPosition attribute.  By modifying this attribute I could offset the shape in the body it will eventually be a part of.</p>
<p>The last thing in this class is the setCollidable function.  I needed a way to easily make all of the parts of the bead a sensor temporarily.  This was so that the user could drag the bead and not collide with anything and then let go and it would become collidable again.<br />
<strong>Bead.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Bead<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Bead<span style="color: #000000;">&#40;</span>world<span style="color: #000000; font-weight: bold;">:</span>b2World, physScale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		m_circleRadius = radius;
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> cd<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		cd.density = <span style="color: #000000; font-weight:bold;">0.01</span>;
		cd.radius = <span style="color: #000000;">&#40;</span>m_circleRadius<span style="color: #000000;">&#41;</span>;
		cd.friction = <span style="color: #000000; font-weight:bold;">0.1</span>;
		cd.restitution = <span style="color: #000000; font-weight:bold;">0.1</span>;
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> bd<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		cd.localPosition.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">15</span> <span style="color: #000000; font-weight: bold;">/</span> physScale;
&nbsp;
		bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000; font-weight: bold;">/</span>physScale, <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000; font-weight: bold;">/</span>physScale<span style="color: #000000;">&#41;</span>
		bd.isBullet = <span style="color: #0033ff; font-weight: bold;">true</span>;
		m_body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
		m_body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
		cd.localPosition.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">15</span> <span style="color: #000000; font-weight: bold;">/</span> physScale;
		m_body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
		cd.localPosition.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">0</span>;
		cd.isSensor = <span style="color: #0033ff; font-weight: bold;">true</span>;
		m_centerShape = m_body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
		m_body.SetMassFromShapes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> SetCollidable<span style="color: #000000;">&#40;</span>toggle<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> currentShape<span style="color: #000000; font-weight: bold;">:</span>b2Shape = m_body.GetShapeList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span>currentShape; currentShape; currentShape = currentShape.m_next<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>currentShape == m_centerShape<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">continue</span>;
			<span style="color: #000000;">&#125;</span>
			currentShape.m_isSensor = <span style="color: #000000; font-weight: bold;">!</span>toggle;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">//======================</span>
	<span style="color: #009900;">// Member Data</span>
	<span style="color: #009900;">//======================</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_circleRadius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_position<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_centerShape<span style="color: #000000; font-weight: bold;">:</span>b2Shape;
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The hook class is very simple and just creates a circle in the location where it is supposed to be.  I temporarily just used the physics circle to make it easy to test.  I don&#8217;t plan to have a physics body be the connector piece in the future unless I am using so sort of callback function.  I think it is easy enough to use the isTouching function to check if a body is within touching range of this hook.<br />
<strong>Hook.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Hook<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Hook<span style="color: #000000;">&#40;</span>world<span style="color: #000000; font-weight: bold;">:</span>b2World, physScale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,  <span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">:</span>b2Vec2, radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		m_circleRadius = radius;
		m_position = <span style="color: #004993;">position</span>;
&nbsp;
		<span style="color: #6699cc; font-weight: bold;">var</span> cd<span style="color: #000000; font-weight: bold;">:</span>b2CircleDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2CircleDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			cd.density = <span style="color: #000000; font-weight:bold;">1.0</span>;
			cd.radius = <span style="color: #000000;">&#40;</span>m_circleRadius<span style="color: #000000;">&#41;</span>;
			cd.isSensor = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> bd<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			bd.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>m_position.<span style="color: #004993;">x</span>, m_position.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>
			m_body = world.CreateBody<span style="color: #000000;">&#40;</span>bd<span style="color: #000000;">&#41;</span>;
			m_body.CreateShape<span style="color: #000000;">&#40;</span>cd<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> IsTouching<span style="color: #000000;">&#40;</span>body<span style="color: #000000; font-weight: bold;">:</span>b2Body<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">position</span> = body.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.Copy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #004993;">position</span>.Subtract<span style="color: #000000;">&#40;</span>m_position<span style="color: #000000;">&#41;</span>;
		<span style="color: #004993;">position</span>.Abs<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">position</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot; x units away.&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;radius of circle is &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> m_circleRadius<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">position</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&amp;</span>lt; m_circleRadius <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #004993;">position</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">&amp;</span>lt; m_circleRadius<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">true</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #009900;">//======================</span>
	<span style="color: #009900;">// Member Data</span>
	<span style="color: #009900;">//======================</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_circleRadius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_position<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> m_body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The resulting application turned out pretty good.  I was not totally satisfied with placing a bead on the necklace, but I ran out of time and had to do real work by that time.  I think I could make the bead automatically orient correctly and highlight itself when it is being placed on the right spot on the necklace.  Besides that problem I am satisfied with the it as a prototype for a future application I might develop.</p>
<p>Here it is:</p>
<p><strong>Beadr Prototype 1:</strong></p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_beadr_1277311540"
			class="flashmovie"
			width="600"
			height="400">
	<param name="movie" value="http://chase.ratchetsoftware.com/media/beadr.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://chase.ratchetsoftware.com/media/beadr.swf"
			name="fm_beadr_1277311540"
			width="600"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>.</p>
]]></content:encoded>
			<wfw:commentRss>http://chase.ratchetsoftware.com/2008/09/necklace-simulation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
