<?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>Ben McChesney&#039;s Blog &#187; tutorial</title>
	<atom:link href="http://www.benmcchesney.com/blog/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benmcchesney.com/blog</link>
	<description>Making stuff and sharing it.</description>
	<lastBuildDate>Mon, 19 Dec 2011 04:37:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Attraction / Repulsion Pixels in Open Frameworks</title>
		<link>http://www.benmcchesney.com/blog/2011/07/attraction-repulsion-pixels-in-open-frameworks/</link>
		<comments>http://www.benmcchesney.com/blog/2011/07/attraction-repulsion-pixels-in-open-frameworks/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 07:42:25 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Interactive]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[creative]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[open]]></category>
		<category><![CDATA[open frameworks]]></category>
		<category><![CDATA[openGL]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://www.benmcchesney.com/blog/?p=407</guid>
		<description><![CDATA[As part of learning Open Frameworks I&#8217;ve been porting over simple examples from other languages such as actionscript or processing. This example is inspired from the post the  RGB Sinks and Spring post from flashAndMath.com. The code is actually much simpler in open frameworks because of overloaded operators. Short example I made as part of ...]]></description>
			<content:encoded><![CDATA[<p>As part of learning Open Frameworks I&#8217;ve been porting over simple examples from other languages such as actionscript or processing. This example is inspired from the post the <a href="http://www.flashandmath.com/advanced/rgbsinks/"> RGB Sinks and Spring post</a> from flashAndMath.com. The code is actually much simpler in open frameworks because of overloaded operators.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/07/points_pixelForce.png"><img class="aligncenter size-full wp-image-443" title="Pixel Force with Triangles" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/07/points_pixelForce.png" alt="" width="830" height="625" /></a></p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/07/triangles_pixelForce.png"><img class="aligncenter size-full wp-image-444" title="Pixel Force with Triangles" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/07/triangles_pixelForce.png" alt="" width="799" height="764" /></a></p>
<p><span id="more-407"></span></p>
<p>Short example I made as part of learning open frameworks. I will post a video tutorial detailing through my process. Code is available on m github : https://github.com/benMcChesney/PixelForces</p>
<p>I&#8217;ll be using xCode OSX FAT Release 0062. I use xCode because I find it much easier to use over Visual Studio Express for importing external addons and libraries. If you are new to open frameworks development I strongly recomment visiting http://www.openframeworks.cc/setup for a detailed view on how to get setup with your IDE of choice. The Open Frameworks forums are excellent for solving almost any compiler specific problem you might be having.</p>
<p>First we&#8217;ll need to create a new example to work with. Copy the &#8220;emptyExample&#8221; inside YOUR_OF_Directory/apps/examples/emptyExample and rename it to something cooler sounding like : &#8220;PixelForces&#8221; . Next step is to find a picture to use as your the color for particles. Any picture will do, kittens, godzilla, you name it! The resolution of the picture I chose was 512 × 391. Once acquired place this file inside bin/data so that the app will be able to find it easily.</p>
<p>Create a new header file and call it Particle. To start out we will create a basic empty constructor and give it three properties.</p>
<p>1 ) OfColor color &#8211; to know what color to draw</p>
<p>2 ) ofPoint position &#8211; for where to draw it.</p>
<p>3) ofPoint velocity &#8211; the speed that each particle will move every frame.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:650px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">//</span><br />
<span style="color: #666666;">// Particle.h</span><br />
<span style="color: #666666;">// PixelForces</span><br />
<span style="color: #666666;">//</span><br />
<span style="color: #666666;">// A Simple Particle Class</span><br />
<span style="color: #666666;">//</span><br />
<span style="color: #339900;">#ifndef _PARTICLE</span><br />
<span style="color: #339900;">#define _PARTICLE</span><br />
<br />
<span style="color: #339900;">#include &quot;ofMain.h&quot;</span><br />
<br />
<span style="color: #0000ff;">class</span> Particle<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span><br />
Particle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
Particle<span style="color: #008000;">&#40;</span> ofPoint _position , ofColor _color <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
position <span style="color: #000080;">=</span> _position <span style="color: #008080;">;</span><br />
color <span style="color: #000080;">=</span> _color <span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
ofPoint position , velocity <span style="color: #008080;">;</span><br />
ofColor color <span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<span style="color: #339900;">#endif</span></div></td></tr></tbody></table></div>
<p>Now navigate to the testApp.h and be sure to include Particle.h at the top of the file.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &quot;Particle.h&quot;</span></div></td></tr></tbody></table></div>
<p>Then we will have to add in the variables needed for this example.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ofImage image <span style="color: #008080;">;</span> <span style="color: #666666;">//image to load</span><br />
<span style="color: #0000ff;">int</span> sampling <span style="color: #008080;">;</span> <span style="color: #666666;">//pixels to skip</span><br />
vector particles <span style="color: #008080;">;</span> <span style="color: #666666;">//vector to store pixels</span><br />
<span style="color: #0000ff;">int</span> numParticles <span style="color: #008080;">;</span>          <span style="color: #666666;">//Number of particles</span><br />
<br />
<span style="color: #666666;">//Spring and Sink Factors</span><br />
<span style="color: #0000ff;">bool</span> springEnabled <span style="color: #008080;">;</span>        <span style="color: #666666;">//toggle whether particles return to their origin</span><br />
<span style="color: #0000ff;">float</span> forceRadius <span style="color: #008080;">;</span>                          <span style="color: #666666;">//radius of repellent/attraction force</span><br />
<span style="color: #0000ff;">float</span> velocityDampingFactor <span style="color: #008080;">;</span>   <span style="color: #666666;">//damping factor to slow the particles down</span><br />
<span style="color: #0000ff;">float</span> springFactor <span style="color: #008080;">;</span>                  <span style="color: #666666;">//how much the particle &quot;springs&quot; back to origin</span><br />
<span style="color: #0000ff;">int</span> cursorMode <span style="color: #008080;">;</span>                            <span style="color: #666666;">//whether the cursor is attracting or repelling</span></div></td></tr></tbody></table></div>
<p>Now if we navigate to testApp::setup( ) inside testApp.cpp we can see how we load in and image and get the pixels we need. This code is from an article by Daniel Shiffman on the processing learning site http://processing.org/learning/pixels/ . Be sure to check it out and lots of other great lessons that are great examples to play with. The image is stored as an array of unsigned chars for the RGB channels of each pixel and occupies 3 indicies per pixel. If our image had transparency it would be RGBA and each pixel would take up 4 indices.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:650px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> testApp<span style="color: #008080;">::</span><span style="color: #007788;">setup</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #666666;">//Set the background to black</span><br />
ofBackground<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">0</span> , <span style="color: #0000dd;">0</span> , <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<span style="color: #666666;">//load our image inside bin/data</span><br />
image.<span style="color: #007788;">loadImage</span> <span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">&quot;grandTetons_small.jpg&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<span style="color: #666666;">//if the app performs slowly raise this number</span><br />
sampling <span style="color: #000080;">=</span> <span style="color: #0000dd;">4</span> <span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">//Retrieve the pixels from the loaded image</span><br />
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> pixels <span style="color: #000080;">=</span> image.<span style="color: #007788;">getPixels</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<span style="color: #666666;">//store width and height for optimization and clarity</span><br />
<span style="color: #0000ff;">int</span> w <span style="color: #000080;">=</span> image.<span style="color: #007788;">width</span> <span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">int</span> h <span style="color: #000080;">=</span> image.<span style="color: #007788;">height</span> <span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">//offsets to center the particles on screen</span><br />
<span style="color: #0000ff;">int</span> xOffset <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>ofGetWidth<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> w <span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span><span style="color: #0000dd;">2</span> <span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">int</span> yOffset <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>ofGetHeight<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> h <span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span><span style="color: #0000dd;">2</span> <span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">//Loop through all the rows</span><br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">;</span> x <span style="color: #000080;">&lt;</span> w <span style="color: #008080;">;</span> x<span style="color: #000040;">+</span><span style="color: #000080;">=</span>sampling <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #666666;">//Loop through all the columns</span><br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> y <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">;</span> y <span style="color: #000080;">&lt;</span> h <span style="color: #008080;">;</span> y<span style="color: #000040;">+</span><span style="color: #000080;">=</span>sampling <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #666666;">//Pixels are stored as unsigned char ( 0 255 ) as RGB</span><br />
<span style="color: #666666;">//If our image had transparency it would be 4 for RGBA</span><br />
<span style="color: #0000ff;">int</span> index <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> y <span style="color: #000040;">*</span> w <span style="color: #000040;">+</span> x <span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">3</span> <span style="color: #008080;">;</span><br />
ofColor color <span style="color: #008080;">;</span><br />
color.<span style="color: #007788;">r</span> <span style="color: #000080;">=</span> pixels<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span> <span style="color: #008080;">;</span> <span style="color: #666666;">//red pixel</span><br />
color.<span style="color: #007788;">g</span> <span style="color: #000080;">=</span> pixels<span style="color: #008000;">&#91;</span>index<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008080;">;</span> <span style="color: #666666;">//blue pixel</span><br />
color.<span style="color: #007788;">b</span> <span style="color: #000080;">=</span> pixels<span style="color: #008000;">&#91;</span>index<span style="color: #000040;">+</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008080;">;</span> <span style="color: #666666;">//green pixel</span><br />
particles.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span> Particle <span style="color: #008000;">&#40;</span> ofPoint <span style="color: #008000;">&#40;</span> x <span style="color: #000040;">+</span> xOffset , y <span style="color: #000040;">+</span> yOffset <span style="color: #008000;">&#41;</span> , color <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
ofSetFrameRate<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">30</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<br />
numParticles <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> image.<span style="color: #007788;">width</span> <span style="color: #000040;">*</span> image.<span style="color: #007788;">height</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> sampling <span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Next we have to change the draw( ) function to loop through a vector of particles. In c++ vectors are stored as a linked list which is a very quick way to iterate through a list of items.</p>
<p>Coming from Actionscript it took me a while to wrap my head around pointers. You can read more about them here http://www.cplusplus.com/forum/articles/9621/ . For now just know that pointers are used to reference a location in memory similar to how an object works in OOP.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> testApp<span style="color: #008080;">::</span><span style="color: #007788;">draw</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #666666;">//Create an iterator to cycle through the vector</span><br />
std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> p <span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> p <span style="color: #000080;">=</span> particles.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> p <span style="color: #000040;">!</span><span style="color: #000080;">=</span> particles.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> p<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #666666;">//access the color via the pointer p</span><br />
<span style="color: #666666;">//when using pointers you have to use &quot;-&gt;&quot; syntax instead of &quot;.&quot;</span><br />
ofColor color <span style="color: #000080;">=</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>color <span style="color: #008080;">;</span><br />
ofSetColor <span style="color: #008000;">&#40;</span> color.<span style="color: #007788;">r</span> , color.<span style="color: #007788;">g</span> , color.<span style="color: #007788;">b</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
ofCircle <span style="color: #008000;">&#40;</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position.<span style="color: #007788;">x</span> , p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position.<span style="color: #007788;">y</span> , <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>Next let&#8217;s create a simple update( ) function.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> testApp<span style="color: #008080;">::</span><span style="color: #007788;">update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #666666;">//Create an iterator to cycle through the vector</span><br />
<br />
std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #008080;">:</span>iterator p <span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> p <span style="color: #000080;">=</span> particles.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> p <span style="color: #000040;">!</span><span style="color: #000080;">=</span> particles.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> p<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span><br />
<br />
<span style="color: #008000;">&#123;</span><br />
<br />
p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position <span style="color: #000040;">+</span><span style="color: #000080;">=</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>velocity <span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p style="text-align: center;">When you run the example it should look like this !<br />
<a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/07/first_step.png"><img class="aligncenter size-large wp-image-422" title="first_step" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/07/first_step-1024x806.png" alt="" width="819" height="645" /></a></p>
<p>Now we have particles but they aren&#8217;t moving! Let&#8217;s add a variable of type ofPoint velocity to our particle class.<br />
Inside the constructor let&#8217;s set it to a random number like so</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">velocity <span style="color: #000080;">=</span> ofPoint <span style="color: #008000;">&#40;</span> ofRandom <span style="color: #008000;">&#40;</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">5</span> , <span style="color: #0000dd;">5</span> <span style="color: #008000;">&#41;</span> , ofRandom <span style="color: #008000;">&#40;</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">5</span> , <span style="color: #0000dd;">5</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>Now our particles are moving but sort of all over and it&#8217;s not very cool. But this is a starting point! Let&#8217;s add two more ofPoint variables to our Particle class</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ofPoint acceleration <span style="color: #008080;">;</span>          <span style="color: #666666;">//smoothing applied to velocity</span><br />
<br />
ofPoint spawnPoint <span style="color: #008080;">;</span>            <span style="color: #666666;">//original location to line up the picture</span></div></td></tr></tbody></table></div>
<p>Inside the constructor set spawnPoint to the argument ofPoint _position. This will be used for the particles to seek back and realign after some time. Inside testApp::setup( ) we can now set the other variables for the forces.</p>
<p>&nbsp;</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">//Set spring and sink values</span><br />
<br />
cursorMode <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">;</span><br />
<br />
forceRadius <span style="color: #000080;">=</span> <span style="color: #0000dd;">45</span> <span style="color: #008080;">;</span><br />
<br />
velocityDampingFactor <span style="color: #000080;">=</span> <span style="color:#800080;">0.85</span> <span style="color: #008080;">;</span><br />
<br />
springFactor <span style="color: #000080;">=</span> <span style="color:#800080;">0.12</span> <span style="color: #008080;">;</span><br />
<br />
springEnabled <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span> <span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>Next we will have to make some changes to our update function. For simplicity we&#8217;ll use the mouseX and mouseY as the center point of our force. The first step is to create three variables to use for checking values within looped values.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:650px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ofPoint diff <span style="color: #008080;">;</span> <span style="color: #666666;">//Difference between particle and mouse</span><br />
<span style="color: #0000ff;">float</span> dist <span style="color: #008080;">;</span> <span style="color: #666666;">//distance from particle to mouse ( as the crow flies )</span><br />
<span style="color: #0000ff;">float</span> ratio <span style="color: #008080;">;</span> <span style="color: #666666;">//Ratio of how strong the effect is calculated by: 1 + (-dist/maxDistance) ;</span><br />
<span style="color: #0000ff;">const</span> ofPoint mousePosition <span style="color: #000080;">=</span> ofPoint<span style="color: #008000;">&#40;</span> mouseX , mouseY <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> <span style="color: #666666;">//Allocate and retrieve mouse values once.</span><br />
<br />
<span style="color: #666666;">//Create an iterator to cycle through the vector</span><br />
<br />
std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #008080;">;::</span><span style="color: #007788;">iterator</span> p <span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> p <span style="color: #000080;">=</span> particles.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> p <span style="color: #000040;">!</span><span style="color: #000080;">=</span> particles.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> p<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span><br />
<br />
<span style="color: #008000;">&#123;</span><br />
<br />
ratio <span style="color: #000080;">=</span> <span style="color:#800080;">1.0f</span> <span style="color: #008080;">;</span><br />
<br />
p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>velocity <span style="color: #000040;">*</span><span style="color: #000080;">=</span> friction <span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">//reset acceleration every frame</span><br />
<br />
p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>acceleration <span style="color: #000080;">=</span> ofPoint<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<br />
diff <span style="color: #000080;">=</span> mousePosition <span style="color: #000040;">-</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position <span style="color: #008080;">;</span><br />
<br />
dist <span style="color: #000080;">=</span> ofDist<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">0</span> , <span style="color: #0000dd;">0</span> , diff.<span style="color: #007788;">x</span> , diff.<span style="color: #007788;">y</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">//If within the zone of interaction</span><br />
<br />
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> dist <span style="color: #000080;">&lt;</span> forceRadius <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> ratio <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #000040;">-</span> dist <span style="color: #000040;">/</span> forceRadius <span style="color: #008080;">;</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>acceleration <span style="color: #000040;">-</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> diff <span style="color: #000040;">*</span> ratio<span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> springEnabled <span style="color: #008000;">&#41;</span><br />
<br />
<span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #666666;">//Move back to the original position</span><br />
<br />
p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>acceleration.<span style="color: #007788;">x</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> springFactor <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>spawnPoint.<span style="color: #007788;">x</span> <span style="color: #000040;">-</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position.<span style="color: #007788;">x</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>acceleration.<span style="color: #007788;">y</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> springFactor <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>spawnPoint.<span style="color: #007788;">y</span> <span style="color: #000040;">-</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position.<span style="color: #007788;">y</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>velocity <span style="color: #000040;">+</span><span style="color: #000080;">=</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>acceleration <span style="color: #000040;">*</span> ratio <span style="color: #008080;">;</span><br />
<br />
p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position <span style="color: #000040;">+</span><span style="color: #000080;">=</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>velocity <span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now the for loop has gone through a fairly drastic change. There&#8217;s a default ratio of 1.0f so that particles outside of the zone of influence do not get affected by the mouse. Then we are applying friction to slow down the particles&#8217; velocity for a more lifelike and settling effect. The desired velocity of moving away for each particle is calculated by subtracting the mouse&#8217;s X,Y with the particles X ,Y and getting the distance. By adding these same two vectors we gain the desired vector to steer toward the mouse. We adjust the ratio based on how far away the particle is and then if springs are enabled we add the desired velocity to the acceleration. The overall acceleration is then modified by the ratio and we offset the position.</p>
<p>&nbsp;</p>
<p>Let&#8217;s add some debugging output text to keep track of the application. Skipping down to the bottom of testApp::draw( ) ;</p>
<p>&nbsp;</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ofSetColor <span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">255</span> , <span style="color: #0000dd;">255</span> , <span style="color: #0000dd;">255</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<br />
string output <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;S :: Springs on/off : &quot;</span> <span style="color: #000040;">+</span> ofToString<span style="color: #008000;">&#40;</span>springEnabled<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span><br />
<br />
<span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span> C :: CursorMode repel/attract &quot;</span> <span style="color: #000040;">+</span> ofToString<span style="color: #008000;">&#40;</span> cursorMode <span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span><br />
<br />
<span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span> # of particles : &quot;</span> <span style="color: #000040;">+</span> ofToString<span style="color: #008000;">&#40;</span> numParticles <span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span><br />
<br />
<span style="color: #FF0000;">&quot; <span style="color: #000099; font-weight: bold;">\n</span> fps:&quot;</span> <span style="color: #000040;">+</span>ofToString<span style="color: #008000;">&#40;</span> ofGetFrameRate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span><br />
<br />
ofDrawBitmapString<span style="color: #008000;">&#40;</span>output ,<span style="color: #0000dd;">20</span>,<span style="color: #0000dd;">666</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>Here we are building a long string and creating newlines by using the &#8220;\n&#8221; character. Then with a simple call to ofDrawBitmapString( string , x , y ) ;</p>
<p>One more bit of code for the keyboard listeners to toggle modes and make this more fun</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span> key <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'c'</span><span style="color: #008080;">:</span><br />
<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'C'</span><span style="color: #008080;">:</span><br />
cursorMode <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> cursorMode <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">1</span> <span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">break</span> <span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'s'</span><span style="color: #008080;">:</span><br />
<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'S'</span><span style="color: #008080;">:</span><br />
springEnabled <span style="color: #000080;">=</span> <span style="color: #000040;">!</span>springEnabled <span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">break</span> <span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Attraction and repulsion! The only thing that could make this better would be more pixels to play with. Looking through some examples in the <a href="github.com/ofTheo/ofxKinect">ofxKinect github</a> I saw how openGL was used to draw individual points of an image in real time. By changing the draw function I was able to increase my sampling by 2x and still had a good frame rate.</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:650px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">//Begin the openGL Drawing Mode</span><br />
<br />
glBegin<span style="color: #008000;">&#40;</span>GL_POINTS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">//Triangles look Cool too</span><br />
<br />
<span style="color: #666666;">//glBegin(GL_TRIANGLES);</span><br />
<br />
<span style="color: #666666;">//Create an iterator to cycle through the vector</span><br />
<br />
std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> p <span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> p <span style="color: #000080;">=</span> particles.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> p <span style="color: #000040;">!</span><span style="color: #000080;">=</span> particles.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> p<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span><br />
<br />
<span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #666666;">//get the color and draw a point</span><br />
<br />
glColor3ub<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span>p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>color.<span style="color: #007788;">r</span>,<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span>p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>color.<span style="color: #007788;">g</span>,<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span>p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>color.<span style="color: #007788;">b</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
glVertex3f<span style="color: #008000;">&#40;</span>p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position.<span style="color: #007788;">x</span>, p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>position.<span style="color: #007788;">y</span> , <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
glEnd<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>The code for this is on my github and <a href="https://github.com/benMcChesney/PixelForces/commit/a871266096e95583a48ade2c51d72b25d1527caf"> available here </a> The next step is to create some sliders, using Frame buffers objects and make some sound analysis.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benmcchesney.com/blog/2011/07/attraction-repulsion-pixels-in-open-frameworks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kinect Hacks</title>
		<link>http://www.benmcchesney.com/blog/2011/02/kinect-hacks/</link>
		<comments>http://www.benmcchesney.com/blog/2011/02/kinect-hacks/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 08:24:21 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Interactive]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[creative]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[Kinect]]></category>
		<category><![CDATA[open]]></category>

		<guid isPermaLink="false">http://www.benmcchesney.com/blog/?p=386</guid>
		<description><![CDATA[I&#8217;ve been tinkering with the Kinect and I wanted to release what applications I had been able to create. I was inspired from other people&#8217;s examples and forum posts and wanted to contribute back what I could. All the examples are on my github to ensure that I&#8217;ll be able to easily update them in ...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been tinkering with the Kinect and I wanted to release what applications I had been able to create. I was inspired from other people&#8217;s examples and forum posts and wanted to contribute back what I could. All the examples are on <a href="https://github.com/benMcChesney/KinectHacks">my github</a> to ensure that I&#8217;ll be able to easily update them in the future. The first set of examples were all completed using <a href="http://openframeworks.cc">open frameworks</a> a c++ creative coding framework.</p>
<p>ofxSoundDraw<br />
This example was covered more in depth in a previous blog post. Using a local TUIO server to track hands it plays a sound byte if there&#8217;s enough movement in a designated zone.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/DrawKinect1.png"><img class="aligncenter size-full wp-image-393" title="DrawKinect" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/DrawKinect1.png" alt="" width="480" height="409" /></a></p>
<p>ofxKinectBox2D</p>
<p>This is the first example I got running with box2D. The circles are simple rigid bodies and when two hands are present it spawns an attraction force at the midpoint between both hands.</p>
<p style="text-align: center;"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/box2D.png"><img class="aligncenter size-full wp-image-388" title="box2D" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/box2D.png" alt="" width="644" height="561" /></a></p>
<p><span id="more-386"></span></p>
<p>FireIce</p>
<p>Taking the above example a little farther. Particles have two types : fire + ice. When opposites combine both pieces are rendered inert.</p>
<p style="text-align: center;"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/fireIce.png"><img class="aligncenter size-full wp-image-394" title="Fire and Ice" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/fireIce.png" alt="" width="644" height="561" /></a></p>
<p>ofxBounceSound<br />
Similar to the above example, but crashing groups of particles generates a piano sound.</p>
<p style="text-align: center;"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/BounceGame.png"><img class="aligncenter size-full wp-image-387" title="BounceGame" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/BounceGame.png" alt="" width="644" height="561" /></a></p>
<p>ofxUserMask<br />
Using openCV and .mov renders it tracks a user and draws a point cloud field. The idea was that at a concert or other event to dynamically mask that movie to a user.</p>
<p style="text-align: center;"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/ofxUserMask.png"><img class="aligncenter size-large wp-image-392" title="ofxUserMask" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/ofxUserMask-1024x806.png" alt="" width="573" height="451" /></a></p>
<p>ofxKinectSequence<br />
My latest Kinect + open frameworks project. I began by looking back at my sound example and decided to expand it&#8217;s functionality. Now the app features 4 tracks and loops allowing the user to create a soundscape. The tracks and rhythms are all preset now in code but I will continue to update it to make it a more robust system.</p>
<p style="text-align: center;"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/ofxSequence1.png"><img class="aligncenter size-full wp-image-395" title="ofxSequence" src="http://www.benmcchesney.com/blog/wp-content/uploads/2011/02/ofxSequence1.png" alt="" width="644" height="561" /></a></p>
<p>OpenNI_Unity3d</p>
<p>This example uses OpenNI, a middleware layer ( between software and hardware ), for natural user interaction. A module of this system is NITE skeleton tracking which allows for some great, accurate avatar interactions.</p>
<p>The most current source of information regarding this driver is this forum post : http://forum.unity3d.com/threads/67982-Kinect-plugin/</p>
<p>BernardFrancois has graciously provided this in-depth walkthrough of how to install the drivers on page 4. Also posted below :</p>
<p>In general I found windows installation much simpler than the command line route I had to go through on OSX.</p>
<p>&#8221;<br />
Installing the kinect driver, OpenNI and NITE SDK<br />
* download the OpenNI binaries from http://www.openni.org/?q=node/2 (in this case: OpenNI 1.0 Alpha build 23 binaries for Win32)<br />
* download the NITE SDK from http://www.primesense.com/?p=515 (in this case: NITE 1.3 Beta build 17 SDK for Windows)<br />
* download SensorKinect from https://github.com/avin2/SensorKinect<br />
* install OpenNI<br />
* install NITE SDK and use the following free license key frop openni.org: 0KOIk2JeIBYClPWVnMoRKn5cdY4=<br />
* unzip the SensorKinect file<br />
* connect the Kinect device<br />
* open the device manager (Start &gt; Run&#8230; &gt; devmgmt.msc) and verify that &#8216;Xbox NUI Motor&#8217; is in the list (but with an exclamation mark)<br />
* Right-click on the device and choose &#8216;Update Driver&#8217; and make windows look for a driver in the avin2-SensorKinect-b7cd39d\Platform\Win32\Driver folder. This will install a device called &#8216;Kinect Motor&#8217;.<br />
* reboot your computer when asked<br />
* After rebooting, two new devices will be found. Repeat the same process by pointing the wizard to the driver folder. One of the two new devices will fail to install (the one for the audio), the one that will succeed will add a new device called &#8216;Kinect Camera&#8217;.<br />
* install the executable the zip file in the folder avin2-SensorKinect-b7cd39d\Bin<br />
* run C:\Program Files\OpenNI\Samples\Bin\Release\NiUserTracker.exe to test if everything works<br />
* if it fails, run C:\Program Files\OpenNI\Tools\vcredist_x86.exe<br />
installing the kinect unity example<br />
* download the official UnityWrapper from OpenNI from https://github.com/OpenNI/UnityWrapper<br />
* make sure Unity3 or Unity3 pro is installed</p>
<p>&#8221; &#8211; Quoted from user &#8216;BernardFrancois&#8217; Unity3d Forums</p>
<p><iframe src="http://player.vimeo.com/video/20233290" frameborder="0" width="640" height="480"></iframe></p>
<p><a href="http://vimeo.com/20233290">Unity3d NITE Destroy City</a> from <a href="http://vimeo.com/user1688289">Ben McChesney</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Currently the NITE wrapper for unity3D only works on windows. NITE was recently ported to Mac with the help of Roxlu ( roxlu.com ) and an example is included with openFrameworks 0062, but it takes a bit of configuring to get it up and running. I included a few prefabs in my scene to make it more interesting than destroying cubes. Fire hydrants that spurts Water, Lampposts that short-circuit, and a building with destructible parts make the scene much more fun.</p>
<p>Kinect Hacking has been a fun and successful hobby. There are some real possibilities integrating it with unity3d, and I think that openNI and NITE will open up even more possibilities. I found myself wanting to detect if hands were open or closed for ui control: otherwise user input is a little limited.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benmcchesney.com/blog/2011/02/kinect-hacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading a Collada file in Away3D</title>
		<link>http://www.benmcchesney.com/blog/2010/08/loading-a-collada-file-in-away3d/</link>
		<comments>http://www.benmcchesney.com/blog/2010/08/loading-a-collada-file-in-away3d/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 06:05:32 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Interactive]]></category>
		<category><![CDATA[Motion]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[action]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[cinema4d]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.benmcchesney.com/blog/?p=302</guid>
		<description><![CDATA[Recently I had been doing a little more work in Away3D and I needed to use an object that was more complex than a preset primitive. After looking through some of the Away3D examples I  tried bringing in my own model from Cinema4D. I quickly found that setting up and exporting your file correctly is ...]]></description>
			<content:encoded><![CDATA[<p>Recently I had been doing a little more work in Away3D and I needed to use an object that was more complex than a preset primitive. After looking through some of the Away3D examples I  tried bringing in my own model from Cinema4D. I quickly found that setting up and exporting your file correctly is half the battle. In this tutorial I&#8217;ll explain how to create a primitive in cinema4D export it correctly for Away3D inside flash. Source code is available at the bottom. Use the right/left arrows to switch models and the up/down to change the texture. Below is the final product:</p>
<p><span id="more-302"></span></p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ColladaExample_346401466"
			class="flashmovie"
			width="800"
			height="600">
	<param name="movie" value="http://www.benmcchesney.com/blog/ColladaExample.swf" />
	<param name="play" value="false" />
	<param name="scale" value="noscale" />
	<param name="allowfullscreen" value="false" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benmcchesney.com/blog/ColladaExample.swf"
			name="fm_ColladaExample_346401466"
			width="800"
			height="600">
		<param name="play" value="false" />
		<param name="scale" value="noscale" />
		<param name="allowfullscreen" value="false" />
	<!--<![endif]-->
		

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>It all starts in Cinema4D. I&#8217;m using Cinema4D R11. Create a new project and start with a tube primitive.  Below are some of the parameters I used</p>
<div id="attachment_304" class="wp-caption alignnone" style="width: 353px"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/01_tut_sphereProperties.png"><img class="size-full wp-image-304" title="01_tut_sphereProperties" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/01_tut_sphereProperties.png" alt="" width="343" height="331" /></a><p class="wp-caption-text">Tube Properties</p></div>
<p>Then select the primtive and hit the &#8220;C&#8221; button. This will convert it to a polygon object. Your object manager should now look like this:</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/03_tut_sphereObject.png"><img class="alignnone size-full wp-image-306" title="03_tut_sphereObject" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/03_tut_sphereObject.png" alt="" width="415" height="84" /></a></p>
<p>Now we will add a new material. I downloaded a metal texture off of <a href="http://cgtextures.com">CG Textures </a> and reduced it&#8217;s size to 256 x 256.  In a 3d system inside flash the two biggest things that can quickly bring your project down are the number of vertices in your model and the dimensions of your texture. Some general numbers are that a scene can support around 6,000 vertices give or take. So it&#8217;s great for simple scenes but for anything larger you might want to try another 3d environment like <a href="http://unity3d.com">unity3d.</a></p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/05_tut_textureSettings.png"><img class="alignnone size-full wp-image-308" title="05_tut_textureSettings" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/05_tut_textureSettings.png" alt="" width="518" height="529" /></a></p>
<p>and when you get prompted to copy a version of your texture file to the textures folder hit yes, it will save some hassle later with the file path. Also be sure to rename the texture to something recognizable like &#8220;RingMat&#8221; so that it can be identified later.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/04_tut_copyPath.png"><img class="alignnone size-full wp-image-307" title="04_tut_copyPath" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/04_tut_copyPath.png" alt="" width="500" height="236" /></a></p>
<p>Now switch over to vertices mode and hit apple + a to select all the points. Then select functions &gt; triangulate. In Cinema4D will default to drawing quads or units of 4 points. Inside Away3D it draws with triangles so we need to make this adjustment.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/06_tut_triangulate.png"><img class="alignnone size-full wp-image-309" title="06_tut_triangulate" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/06_tut_triangulate.png" alt="" width="701" height="536" /></a>Now we switch over to BodyPaint UV Edit where we can quickly adjust the UV Map so it doesn&#8217;t look quite so hideous. If you are new to UV mapping and 3d in general UV mapping allows you to customize and control how your 2d texture appears on geometry and shapes in 3d. UV Mapping is an art in itself and can take weeks to do perfectly. For this exampl and for other simple tasks the UV Editor inside Cinema4D can take a lot of the work out of it.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/07_tut_bpUVEdit.png"><img class="alignnone size-full wp-image-310" title="07_tut_bpUVEdit" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/07_tut_bpUVEdit.png" alt="" width="183" height="423" /></a></p>
<p>Now select the UV Mapping wizard.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/08_tut_iconWizard.png"><img class="alignnone size-full wp-image-311" title="08_tut_iconWizard" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/08_tut_iconWizard.png" alt="" width="660" height="107" /></a></p>
<p>Now just have to go through all the steps of the wizard.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/09_tut_uvwizard_object.png"><img class="alignnone size-full wp-image-312" title="09_tut_uvwizard_object" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/09_tut_uvwizard_object.png" alt="" width="460" height="396" /></a></p>
<p>We will be using the object option.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/10_tut_uvwizard_cubic.png"><img class="alignnone size-full wp-image-313" title="10_tut_uvwizard_cubic" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/10_tut_uvwizard_cubic.png" alt="" width="460" height="396" /></a></p>
<p>There are multiple ways you can map coordinates to a texture. Cubic Mapping will work fine in this case. Cubic maps generally make simpler uv coordinate systems. Angle mapping by default will have a collection of smaller more precise parts.</p>
<p><img class="alignnone size-full wp-image-314" title="11_tut_uvwizard_channels" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/11_tut_uvwizard_channels.png" alt="" width="460" height="396" /></p>
<p>Now select the color channel until it has a &#8211; through it&#8217;s check box. Otherwise the UV Wizard will overwrite the texture that we already set. If we had a more complicated texture scheme we&#8217;d want to map other channels too. But for now color will suffice. Now switch over to polygons mode. Hit cmd + a to select all the polygons. If the selected polygons are orange that means their normals are facing the correct direction.</p>
<p>For the non initiated when a 3d renderer draws all the vertices it draws them in order and this order determines which way it faces. The flat solid part drawn between them is also called a face. A 3d engine does not render faces that do not have their normals facing the camera. So they save on resources by only rendering half of what we need too.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/12_tut_normals.png"><img class="alignnone size-full wp-image-315" title="12_tut_normals" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/12_tut_normals.png" alt="" width="734" height="517" /></a></p>
<p>Now the good part: we export! In this screenshot is also a rendering of what the new UV Map looks like.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/13_tut_export.png"><img class="alignnone size-full wp-image-316" title="13_tut_export" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/13_tut_export.png" alt="" width="675" height="491" /></a></p>
<p>Export the file in into your project. You will need to also copy the /tex folder into wherever you export your .dae ( collada ) file. This is because inside the Collada file it specifies a URL to it&#8217;s default material. Open up the Collada file in your preferred text editor. TextMate or BBEdit are popular choices: I&#8217;ll be using BBEdit.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/14_tut_colladaFile_initFrom.png"><img class="alignnone size-full wp-image-317" title="14_tut_colladaFile_initFrom" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/14_tut_colladaFile_initFrom.png" alt="" width="508" height="162" /></a></p>
<p>A Collada file is basically a large XML File with specific nodes for a 3d scene including vertices, UV Coordinates, and texture parameters. Inside &lt;libraray_image&gt; there is the &lt;init_from&gt; that specifies the URL of a UV texture. If you are using Cinema4D 11.5 it will provide an absolute url but you can change it to relative like the screenshot above.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/15_tut_colladaLibrary.png"><img class="alignnone size-full wp-image-318" title="15_tut_colladaLibrary" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/15_tut_colladaLibrary.png" alt="" width="564" height="188" /></a></p>
<p>Inside the &lt;library_materials&gt; you can spot the texture we named inside Cinema4D. We will use &#8220;ID3&#8243; inside Away3D to get the material info we need.</p>
<p>Now onto the code. The code is written with a .fla and a document class but it will work as pure actionscript. There is nothing linked in the library or any other dependencies. When I use Away3D in a project I usually place all the 3d information inside a seperate class but here the document class should be fine. I always make a createAway3D function()</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/16_tut_away3DCreate.png"><img class="alignnone size-full wp-image-319" title="16_tut_away3DCreate" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/16_tut_away3DCreate.png" alt="" width="870" height="348" /></a></p>
<p>In this function we create a test object to make sure our scene renders. Away3D is composed of a few basic parts. First we have the View3D: think of this as a sprite that will hold and render your 3d scene. The Camera is how the scene will be viewed: it&#8217;s the window to your 3d world. Views are also comprised of scenes which is a holder for all of your 3D Objects. The last component of a view is the renderer: this is what will actually rasterize your 3d scene into a 2d image every frame.  In Away3D when you create an 3d Object you pass in a separate object with all the properties that you want initialized. The x + y properties dictate where the center of the scene is. The view will automatically be full stage size.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/17_tut_loadCollada.png"><img class="alignnone size-full wp-image-320" title="17_tut_loadCollada" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/17_tut_loadCollada.png" alt="" width="849" height="248" /></a></p>
<p>To call the collada file you create a new Loader3D class and use the static Collada.load() method. In the document class you can see the onLoaderSuccess Method but it&#8217;s fairly straight forward. In a collada file we have to access the material through the MaterialData class. Here we plug in the ID3 that was identified in the Collada .dae file.</p>
<p>One last concept that I used in this code is Bitwise operations. Bitwise operations are quicker ways of doing simple operations. There are a whole slew of bitwise operations that can be quickly googled for. This bitwise operation will cycle through all the items in the array and when it reaches the end will return to zero.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/18_tut_bitwise.png"><img class="alignnone size-full wp-image-321" title="18_tut_bitwise" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/08/18_tut_bitwise.png" alt="" width="819" height="362" /></a></p>
<p>That&#8217;s it! Full Collada model support. If you are having trouble seeing your model make sure that your normals are correct and try adjust the size drastically. Sometimes my models come out with .25 , 5 , or 90 for the scale. It&#8217;s all relative. The source code is <a href="http://benmcchesney.com/blog/Collada_Tutorial.zip">available here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benmcchesney.com/blog/2010/08/loading-a-collada-file-in-away3d/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WordDNA: Twitter + Flickr + Away3D</title>
		<link>http://www.benmcchesney.com/blog/2010/03/worddna-twitter-flicker-away3d/</link>
		<comments>http://www.benmcchesney.com/blog/2010/03/worddna-twitter-flicker-away3d/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 15:46:52 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Interactive]]></category>
		<category><![CDATA[Motion]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[away]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[camera movement]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[mashup]]></category>
		<category><![CDATA[photo]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.benmcchesney.com/blog/?p=242</guid>
		<description><![CDATA[Source Code is available here Recently I had been wanting to do some more interactive 3D type of projects. I wanted to create something beyond just a pre-rendered sequence that simulated camera movement. I have previously been using papervision3D but wanted to expand my knowledge and check out the competition. Papervision X has been long ...]]></description>
			<content:encoded><![CDATA[<p>Source Code is <a href="http://benmcchesney.com/blog/wordDNA.zip"> available here </a></p>
<p>Recently I had been wanting to do some more interactive 3D type of projects. I wanted to create something beyond just a pre-rendered sequence that simulated camera movement. I have previously been using <a href="http://www.papervision3d.org"> papervision3D</a> but wanted to expand my knowledge and check out the competition. Papervision X has been long expected but with Ralph Hauwert <a href="http://www.unitZeroOne.com"> Unit Zero One</a> leaving the team I thought it was time to move on. Away3D was very highly recommended by my colleague <a href="http://www.kylebeikirch.com"> Kyle Beikirch</a> for it&#8217;s more frequent updates to SVN and ease of interactivity. Since I was exactly sure what would be interactive when I started: I gave it a shot.</p>
<p><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/flitter_screenshot.png"><img class="alignnone size-full wp-image-250" title="flitter_screenshot" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/flitter_screenshot.png" alt="Build Animation of wordDNA" width="806" height="702" /></a></p>
<p><span id="more-242"></span></p>
<p>In what I did there wasn&#8217;t really a huge difference between the images but I did like how Away3D uses associative arrays much like <a href="http://www.greensock.com/">Greensock Tweening platform </a> instead of traditional constructors. If I didn&#8217;t want to pre-render as many assets as possible I would need an internet source to grab them from. I thought that combining the antics of twitter with somewhat related photos could yield some interesting results. At first I grabbed XML from the twitter time line, but I wanted to have more control over my results. I used the Tweetr Twitter API, and got a lot of code inspiration from Lindsey Burtner&#8217;s <a href="http://lindsayburtner.com/blog/?p=176" > Word Feeder</a>. Lindsey helped me last year when I first began mashing together the last.fm API, and proved an invaluable resource, so props to her.</p>
<p>Let&#8217;s dive into some of the key functions that make this work:</p>
<p>Once the Flickr and Twitter data has been collected we then insert it into a 2D MovieClip and prepare it for 3D.</p>
<div id="attachment_248" class="wp-caption alignnone" style="width: 895px"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/prepareBitmap.png"><img class="size-full wp-image-248" title="prepareBitmap" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/prepareBitmap.png" alt="Dynamic Draw of Movie Clip to Bitmap Material" width="885" height="458" /></a><p class="wp-caption-text">Dynamic Draw of Movie Clip to Bitmap Material</p></div>
<p>We prepare it for 3D by simple flattening it with transparency.</p>
<div id="attachment_246" class="wp-caption alignnone" style="width: 858px"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/makeBitmapFrom.png"><img class="size-full wp-image-246" title="makeBitmapFrom" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/makeBitmapFrom.png" alt="Document Class creates Bitmap Material" width="848" height="185" /></a><p class="wp-caption-text">Document Class creates Bitmap Material</p></div>
<p>I wanted to organize the planes in  a Helix and found <ahref="http://theflashblog.com/?p=306" >this post </a> from Lee Brimelow&#8217;s blog and modified it for my needs. The by rotating the scene 360 degrees we get this cool spin effect for the zoom out.</p>
<div id="attachment_247" class="wp-caption alignnone" style="width: 810px"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/makeHelix.png"><img class="size-full wp-image-247" title="makeHelix" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/makeHelix.png" alt="Make a Helix" width="800" height="604" /></a><p class="wp-caption-text">Make a Double Helix our of Shapes</p></div>
<p>Now we need a way to navigate between planes, at first it was very linear but that got boring quickly. So I added in a new random index each movement to make it feel more exploratory. Adding a mouse event to planes would have been the next logical step to give users some control.</p>
<div id="attachment_249" class="wp-caption alignnone" style="width: 752px"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/randomIndex.png"><img class="size-full wp-image-249" title="randomIndex" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/randomIndex.png" alt="Generating a Random Index from an Array" width="742" height="339" /></a><p class="wp-caption-text">Generating a Random Index from an Array</p></div>
<p>Now the fun part: the camera. First we get the distance to to the next target and tweak it so that that camera movement is smooth for all distances.</p>
<p>The Camera has two tweens:</p>
<p>1) To move away from the plane randomly around it in 3D space</p>
<p>2) To move towards the new target</p>
<p>We change the focus of the camera by tweening an empty displayObject3D this would be our &#8220;null object&#8221; in 3d or After Effects.</p>
<p>The bezier gives the tween a nice smooth and organic feel, something that is lacking with a lot of fully 3d sites.</p>
<p>The newY just calculates which direction the camera should swoop in depending if the target is above or below it.</p>
<div id="attachment_244" class="wp-caption alignnone" style="width: 770px"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/camera_1.png"><img class="size-full wp-image-244" title="camera_1" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/camera_1.png" alt="Camera AS3 Text" width="760" height="405" /></a><p class="wp-caption-text">Moving the Camera</p></div>
<p>We save the initial values of our current target so that we can return it to it&#8217;s rightful place the user is done looking at it. In our tweenCameraDone() function we save the coordinates of the camera in case we need to reset it.</p>
<div id="attachment_245" class="wp-caption alignnone" style="width: 890px"><a href="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/camera_2.png"><img class="size-full wp-image-245" title="camera_2" src="http://www.benmcchesney.com/blog/wp-content/uploads/2010/03/camera_2.png" alt="Moving the Camera pt 2" width="880" height="322" /></a><p class="wp-caption-text">Moving the Camera pt 2</p></div>
<p>That&#8217;s it! I had fun making it and mixing interactivity and 3d Camera. Play with it and let me know what you come up with!</p>
<p>Source Code is <a href="http://benmcchesney.com/blog/wordDNA.zip"> available here </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benmcchesney.com/blog/2010/03/worddna-twitter-flicker-away3d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oh yeah TEXT? My name is Leo Wong and I say BOOM! Papervision3D Text Explode Sample</title>
		<link>http://www.benmcchesney.com/blog/2009/12/oh-yeah-text-i-say-boom-papervision3d-text-explode-sample/</link>
		<comments>http://www.benmcchesney.com/blog/2009/12/oh-yeah-text-i-say-boom-papervision3d-text-explode-sample/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 23:30:26 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Interactive]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[3]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[papervision3D]]></category>
		<category><![CDATA[papervistion]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[type]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://www.benmcchesney.com/blog/?p=180</guid>
		<description><![CDATA[Experiment in papervision3D to get the text inside a dynamic textField to explode in X,Y,Z space and then reform. Code Available]]></description>
			<content:encoded><![CDATA[<p>I was in the lab yesterday and saw my friend animating text and 3D and it did not seem very appetizing. I really regret having to do tedious time-consuming things by hand. If there is ever a procedural way to generate or simulate something I&#8217;m willing to try. So I put together this little example.</p>
<p>*UPDATE*</p>
<p>I was able to get the zDepth working by applying simple blur filters to the bitmaps of the BitmapMaterials inside papervision3D. I also added in an input text field so that users could change the text without having to recompile. I have some bigger ideas for this down the road, like sequencing camera tweens between each letter. And exploding a whole paragraph word for word. I had some trouble with making the letter interactive, but it&#8217;s always been a bit of a pain to get events working with papervision3D. But for now: I&#8217;m happy where it is. The code is extremely well documented and if you have any questions just shoot me a line at : ben.mcchesney@gmail.com</p>
<p>The update source code is located <a href="http://www.benmcchesney.com/blog/textExplode_changeText.zip"> here</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img title="movieclip_zsort" src="http://www.benmcchesney.com/blog/wp-content/uploads/2009/12/movieclip_zsort.png" alt="z-sort issue" width="566" height="442" /></p>
<p><span id="more-180"></span></p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_TextExplode_change_1481937795"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="TextExplode_change.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="TextExplode_change.swf"
			name="fm_TextExplode_change_1481937795"
			width="550"
			height="400">
	<!--<![endif]-->
		

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>Click on my name to get the explode effect, and click again to revert to the whole word.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_textJumble_954205633"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="textJumble.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="textJumble.swf"
			name="fm_textJumble_954205633"
			width="550"
			height="400">
	<!--<![endif]-->
		

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>First I created a sample using just 2d MovieClips in Flash. Using the text of a dynamic textField on the stage it breaks down and seperates each text into it&#8217;s own textField with it&#8217;s own movieClip container. By the character&#8217;s inside a movieClip I was able to apply TweenLite&#8217;s blurFilter and fake depth of field, by checking the zDepth of the letter</p>
<p>But this solution wasn&#8217;t as appetizing. It lacked the interactivity that I was trying to emulate. I wanted to be able to explode the text and explore the wreckage. The Flash player 10 also has an issue with z-sorting. Since all of the 3d is just a raster illusion the zDepth is fake. If a child that has display index than another child with a lower z-depth the illusion is broken. So I could either write my own methods to parse through each array item and update the displayList constantly or I could go in a different direction.</p>
<p>.I was going to create a version of textExplode using an actionscript 3D api : Papervision3D (<a href="http://papervision3D.org">Papervision3D Site</a>) . In papervision you have to create a small plane for each letter and draw a bitmap of it. You then take this bitmap and apply it to the geometry of the plane.</p>
<div id="attachment_186" class="wp-caption alignnone" style="width: 798px"><img class="size-full wp-image-186 " title="BitmapCodeSample" src="http://www.benmcchesney.com/blog/wp-content/uploads/2009/12/picture-2.png" alt="By creating a bitmapData object to draw the movieClip we can cast it as a BitmapMaterial and apply it to a plane." width="788" height="356" /><p class="wp-caption-text">By creating a bitmapData object to draw the movieClip we can cast it as a BitmapMaterial and apply it to a plane.</p></div>
<p>I had been used to the old version of papervision3D which meant constructing a scene with viewports, cameras , renderers, and scenes. But in papervision 2.0 by simple extending the BasicView Class, all of these assets are extendable for your connivence. The result was a quickly developed, dynamic 3d text explode code set.</p>
<p>Another different technique I used was the use of Vectors. Vectors are a new data type in Flash Player 10 and are essentially arrays with of one and only one data type. This allows for strict data typing in for all array function increasing performance. It&#8217;s less noticeable in this example, but in a physics simulation where it&#8217;s stepping through each particle&#8217;s force application every frame. The increased limit of particles or general smoothness would be more obvious.<br />
More information can be found on Mike Chambers Blog : <a href="http://www.mikechambers.com/blog/2008/08/19/using-vectors-in-actionscript-3-and-flash-player-10/"> here </a></p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_TextExplode_461084715"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="TextExplode.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="TextExplode.swf"
			name="fm_TextExplode_461084715"
			width="550"
			height="400">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>The next steps are to apply depth of field by checking z-depth and being able to change the textField while the swf is running. I would also like to increase the interactivity by being able to focus and zoom to certain letter and really get the exploration feel. Another change that&#8217;s on the to-do list is a second stage breakup of the letters bitmaps into tiny triangles or squares for more fodder to explode.</p>
<p>I&#8217;ll keep posting as I get farther along and make it more and more extensible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benmcchesney.com/blog/2009/12/oh-yeah-text-i-say-boom-papervision3d-text-explode-sample/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cinema4D Projectionman Tutorial</title>
		<link>http://www.benmcchesney.com/blog/2009/04/cinema4d-projectionman-tutorial/</link>
		<comments>http://www.benmcchesney.com/blog/2009/04/cinema4d-projectionman-tutorial/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 23:44:48 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Motion]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[cinema4d]]></category>
		<category><![CDATA[mapping]]></category>
		<category><![CDATA[photograph]]></category>
		<category><![CDATA[projection]]></category>
		<category><![CDATA[projectionman]]></category>
		<category><![CDATA[r11]]></category>

		<guid isPermaLink="false">http://www.benmcchesney.com/blog/?p=99</guid>
		<description><![CDATA[I found a lack of resources available to learn cinema 4D&#8217;s new camera mapping module, so I played around with it and created a tutorial so that others could see it power and ease of use. 1.Take a picture of a hallway. a.Hallways are a good starting point for camera mapping, simple geometry to project ...]]></description>
			<content:encoded><![CDATA[<p>I found a lack of resources available to learn cinema 4D&#8217;s new camera mapping module, so I played around with it and created a tutorial so that others could see it power and ease of use.</p>
<p>1.Take a picture of a hallway.<br />
a.Hallways are a good starting point for camera mapping, simple geometry to<br />
project onto itʼs essentially 5 cubes: ﬂoor, ceiling, left wall, right wall, and the end<br />
of the hallway.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 44.png" alt="" width="513" height="345" /></p>
<p><a href="hallway.mov">Rendered QT Movie</a></p>
<p><a href="projectionMan_sourceFiles.zip">Project Source Files</a></p>
<p><span id="more-99"></span></p>
<p>2.I ran some adjustment layers to stylize the photo a little bit</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 2.png" alt="" /></p>
<p>b.ﬁrst I ran a black and white ﬁlter over it.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 3.png" alt="" /></p>
<p>c.Then a hue/saturation layer to make it seem a little darker and creepier, I turned the<br />
opacity on the adjustment layer down to get a more subtle effect than just colorizing<br />
the layer.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 4.png" alt="" /></p>
<p>d.a simple curves layer to bring out the shadows just a little more<br />
e.duplicate the original photo and set the blending mode to overlay, this produces a<br />
night effect for what weʼre going for a creepy hallway.</p>
<p>2.Now that we are done adjusting the photo save a of the photograph without layers<br />
and bring that into photoshop.<br />
b.split the photograph into layers based on the ﬁve peices of geometry that we will<br />
be projecting this on, naming layers is important once we get into cinema.<br />
ProjectionMan has the ability to read alpha channels so masking will work just<br />
ﬁne. Save this PSD ﬁle with layers to something you can remember like<br />
“hallway_brokenup.psd”</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 5.png" alt="" width="571" height="574" /></p>
<p>3.Now into cinema4D. The ﬁrst thing weʼre going to do is add a doodle object you can<br />
do this via the menu tools &gt; doodle &gt; add Doodle Frame. Under load bitmap under<br />
Doodle Object properties &gt; then load the PSD broken up into layers.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 7.png" alt="" /></p>
<p>4.Add a visibility tag under cinema4D tags and lower the visibility a little bit so you can<br />
see the 3d viewport.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 8.png" alt="" /></p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 9.png" alt="" /></p>
<p>5.Alright now a little explanation as to how camera mapping works. Essentially you are<br />
using a stationary camera to “project” textures onto simplistic models, allowing you to<br />
do simple slow camera movements.</p>
<p>6.So we create ﬁve cubes and label them: ﬂoor, ceiling, right wall, left wall, and<br />
backpane. Rotate and scale each cube until they visually line up with the walls in the<br />
photograph. Donʼt worry about getting them exactly just yet weʼll have to do tweaking<br />
once we project textures. Create a camera and position it so that it lines up with the<br />
doodle object, this is camera weʼll be using to project textures. Here is the geometry I<br />
came up with just by eyeballing it.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 11.png" alt="" /></p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 12.png" alt="" /></p>
<p>7.Now for texturing, add the PSD ﬁle to the luminance channel.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 10.png" alt="" /></p>
<p>8.Apply the texture to any of the cubes and under projection select Camera Mapping<br />
and select the “CameraMap” camera we created. Under size X, Y click calculate and<br />
that will project the material at the correct resolution allowing for a little more<br />
movement and detail.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 13.png" alt="" /></p>
<p>9.Under window select “Projection Man” to bring up the new UI for Cinema R11. Drag<br />
each of the cubes under the PSD, under the Camera projection. Select the correct<br />
corresponding layers with their alpha channel IE backPane (cube) should be<br />
assigned backpane (PSD layer), etc etc. Weʼre loading the luminance and alpha<br />
channels for this example.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 15.png" alt="" width="693" height="322" /></p>
<p>10. Now render a scene so we can see how everything meshes together, if there are<br />
black streaks near the edges try readjusting the height, width, and depth of each<br />
cube, surprisingly I found that the depth of a cube has the biggest impact in getting<br />
those “stretch marks” around the edges to disappear. Because camera mapping<br />
rendering is so quick, this is actually a relatively painless to make lots of little<br />
adjustments and keep re-rendering the scene. Here is an example of stretching, this<br />
was ﬁxed by decreasing the width of the cube.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 16.png" alt="" /></p>
<p>11.Now for the animation:<br />
duplicate the camera mapping camera and rename it “moveCamera” or<br />
something similar. Now weʼre just going to animate this camera slightly over 180<br />
frames to get a nice smooth transition that still maintains that 3d feel.</p>
<p><img src="http://benmcchesney.com/cmMedia/Picture 14.png" alt="" /></p>
<p>12.You may experience more stretching as you animate forward, keep scaling and<br />
changing the geometry, but remember that camera mapping has itʼs limits and can<br />
only go so far with 1 photograph before quality starts to diminish.</p>
<p>13. Viola! Camera mapping using ProjectionMan and PSD layers.</p>
<p><a href="http://www.benmcchesney.com/blog/hallway.mov">Rendered QT Movie</a></p>
<p><a href="http://www.benmcchesney.com/blog/projectionMan_sourceFiles.zip">Project Source Files</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benmcchesney.com/blog/2009/04/cinema4d-projectionman-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://www.benmcchesney.com/blog/hallway.mov" length="1799383" type="video/quicktime" />
		</item>
	</channel>
</rss>

