<?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>Visual Core &#187; Java</title>
	<atom:link href="http://visualcore.com/index.php/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://visualcore.com</link>
	<description>An amazing repository of useless junk</description>
	<lastBuildDate>Sat, 18 Jun 2011 05:12:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Berkeley Hackathon 2009</title>
		<link>http://visualcore.com/index.php/2009/02/berkeley-hackathon-2009/</link>
		<comments>http://visualcore.com/index.php/2009/02/berkeley-hackathon-2009/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 07:09:02 +0000</pubDate>
		<dc:creator>Jeremy Cowles</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UCB]]></category>

		<guid isPermaLink="false">http://visualcore.com/wp/?p=42</guid>
		<description><![CDATA[I participated in the Berkeley Hackathon 2009, last night from 6pm to 12 noon the next day (pictures). The goal was to build the most impressive or useful application possible in 18 hours, in teams of one to four.
Our team (of two) took 2nd place and won laptops! There were a ton of really awesome [...]]]></description>
			<content:encoded><![CDATA[<p>I participated in the Berkeley <a href="http://startup.berkeley.edu/2009/02/17/recap-hackathon-2009/" target="_BLANK">Hackathon 2009</a>, last night from 6pm to 12 noon the next day (<a href="http://startup.berkeley.edu/2009/02/13/hackathon-2009/" target="_BLANK">pictures</a>). The goal was to build the most impressive or useful application possible in 18 hours, in teams of one to four.</p>
<p>Our team (of two) took 2nd place and won laptops! There were a ton of really awesome projects &#8211; my favorite was the Missile Command game that, when your cities got bombed, it actually corrupted a selectable application&#8217;s memory because &#8220;games don&#8217;t have harsh enough consequences.&#8221; Failure to protect your cities resulted in the death of an application before your very eyes.</p>
<p>The first place was a web API that allowed you to run an application in a thick AND thin client at the same time &#8211; not just pushing bitmaps, but actually rendered in HTML!</p>
<p>Our entry was a 3D visualization of news data &amp; relationships in Java &amp; OpenGL &#8211; I want to make a few tweaks, but I hope to post it soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualcore.com/index.php/2009/02/berkeley-hackathon-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cheap Collision Detection in Processing</title>
		<link>http://visualcore.com/index.php/2009/01/cheap-collision-detection-in-processing/</link>
		<comments>http://visualcore.com/index.php/2009/01/cheap-collision-detection-in-processing/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 19:53:53 +0000</pubDate>
		<dc:creator>Jeremy Cowles</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://visualcore.com/wp/?p=46</guid>
		<description><![CDATA[
In the process of writing the BUGS! sketch I stumbled onto a quick and simple collision detection algorithm, so I figured I would share it. For the purpose of this article I will be assuming 2D, however it should work in 3D just as well.
The detection system is implemented in Processing, which is basically Java [...]]]></description>
			<content:encoded><![CDATA[<p><a href="/SketchViewer.aspx?base=collisiondetection_jan06a" target="_blank"><img style="border: none" src="/images/articles/cd/cd-screen.png" alt="Collision Detection Sketch" /></a></p>
<p>In the process of writing the BUGS! sketch I stumbled onto a quick and simple collision detection algorithm, so I figured I would share it. For the purpose of this article I will be assuming 2D, however it should work in 3D just as well.</p>
<p>The detection system is implemented in <a href="http://processing.org/" target="_blank">Processing</a>, which is basically Java specialized for multimedia. The core algorithm is independent of any Processing specific features and it should be easy to port to any other language.</p>
<h4>Motivation</h4>
<p>I made a 2D flocking/gravitation visualization, and I wanted the particles to &#8220;follow&#8221; each other, but only when an object is close to another object. The code developed here is a cleaned up and simplified version of this original sketch. You may want to take a quick look at the <a href="/SketchViewer.aspx?base=collisiondetection_jan06a" target="_blank">final product</a> before starting to get an idea of what I am trying to accomplish.</p>
<p>Particles are updated on every frame, and then drawn. During the update step, each particle can move freely, but then must look for nearby particles that it could possibly follow. The brute force method would be to compare all combinations of objects and check for a collision, requiring 2^(n-1) comparisons (where n is the number of particles), resulting in O(2^n) time complexity just for the collision detection phase. But time is precious and I wanted to allow for a large number of particles, so O(2^n) was unacceptable. I figured, if two particles are more than 2x the particle size away from each other, they don&#8217;t need to be tested at all. This is the core of the Algorithm that follows.</p>
<h4>The Uniform Grid Algorithm</h4>
<p>The technique I stumbled upon is acutally well known and documented; it is known as a uniform grid [1]. Since particles only collide when in the same general area, the simulation area can be decomposed into small sectors:<br />
<img style="border: none" src="/images/articles/cd/sectors.png" alt="Sectors" /><br />
This way, only particles occupying the same sector need to be compared, drastically reducing the total number of comparisons.</p>
<p>Notice that in the example, the number of comparisons for the red dot is reduced from 7 to 2; the total number of comparisons is reduced from 128 to 6! This example glosses over the issue of particle size, which will increase the number of required comparisons, but it does illustrate the general performance improvement.</p>
<h4>Data Structure</h4>
<p>The speedup obtained by the uniform grid is almost entirely based on a simple data structure. Below is a high level definition of the algorithms components, as well as  descriptions of how they will work:</p>
<ul>
<li>Particle: An object. In this simulation a particle is simply a circle.</li>
<li>Grid: The collection of all sectors, which together composes the simulation    space.</li>
<li>Sector: A square area that may or may not contain particles. The size    of each sector should be the maximum size of the largest particle at any rotation. This ensures that any particle will occupy at most 4 sectors.</li>
<li>The sector a particle occupies is represented by an x,y coordinate pair, just like a regular point. The sector is uniquely determined by the x,y world coordinate of the particle, divided by the sector size. For example, if the particle world location is (110,210), and the sector size is 10, then the sector location is (11, 21).</li>
<li>All particles are bounded by <a href="http://en.wikipedia.org/wiki/Bounding_volume" target="_blank">axis aligned bounding boxes</a> (AABBs). An AABB is a rectangle bounding the entire particle. The location of the current particle is determined by the four corners of the AABB:<br />
<img style="border: none" src="/images/articles/cd/aabb.png" alt="AABB" /><br />
So a single object can exist in up to 4 different sectors on the grid at once.</li>
<li>When a particle moves, its status in the grid must also be updated. For this reason, finding sectors in the grid must be *extremely* fast. (An alternative approach is to *not* update the particles as they move, but rather to rebuild the grid from scratch at every frame. It&#8217;s easy to argue that this may actually be faster than updating the Grid, due to the overhead of finding the particle to remove it during the update phase.)</li>
</ul>
<h4>Collision Queries</h4>
<p>Queries are the primary action performed on the grid. A query uses the grid data structure to retrieve candidates for collision tests.</p>
<ul>
<li>Given a point P=(x,y) in world coordinates, a query begins by translating the world coords into sector coords.</li>
<li>The sector contents are particles, and each particle is compared to P</li>
<li>The comparison to P is done by component wise clamping P to the AABB of the current object, to produce the point Q:<br />
<img style="border: none" src="/images/articles/cd/clampPtoQ.png" alt="Clamping P to produce Q" /><br />
The distance (d) between P and Q is the distance from the given point to the AABB. If P = Q, then the point P is inside the AABB.</li>
<li>Once the AABB test passes, we know that the point and the particle are very close, but not necessarily colliding. Since my simulation uses circles, my final detail test is: dist(P, center(Particle)) &lt; radius. In words: test if the distance from point P to the center of the circle is less than the radius of the Particle being tested.</li>
</ul>
<p>Notice that the lookup of particles is amost effortless. The major work here is simply testing the point P against the particle in two phases. The first phase is a broad phase using AABBs, and the second phase is a detail phase using the actual geometry of the object.</p>
<p>You may be thinking that a two stage testing process is overkill for testing circles, and you are right. It would actually be faster to test the circle directly, but I&#8217;m assuming here that you may want to use geometry other than circles, in which case you would do an expensive polygon test in the detail phase.</p>
<h4>Implementing the Grid</h4>
<p>The sectors compose an infinite grid that covers the world. A <a href="http://en.wikipedia.org/wiki/Flyweight_pattern" target="_blank">flyweight</a> hash table is used to index the grid, since an infinite list would take a **really** long time to search.</p>
<p>The grid is a 2-dimensional array, the index into the array is a sector coordinate moded by the sector size. This gives the illusion of an infinitely large array, but actually only contains (sector size)^2 elements. This is like a circular array in two dimensions, resulting in a toroidal array.</p>
<p>The down side is that many sectors map to the same array entry. To accomidate for this, each entry in the array is an ArrayList of sectors. To find a specific sector, the list is searched until the requested sector is found. This search adds an additional O(n) lookup overhead, however, in the average case, n (the number of sectors contained in the array entry) is very small, so the slowdown is minimal. The public Grid members are as follows:</p>
<p><code><br />
class Grid {<br />
int sectorWidth<br />
int sectorHeight</code></p>
<p><code>void addParticle(Particle obj)<br />
void removeParticle(Particle obj)</p>
<p></code></p>
<p><code> Sector getSector(float x, float y)<br />
ArrayList getSectors(Particle obj)<br />
}<br />
</code></p>
<p>Now that objects can be stored, and retrieved, it&#8217;s time to add the ability to query it.</p>
<h4>Putting the Grid to Use</h4>
<p>I&#8217;ve created a small simulation of bouncing particles exibiting the collision detection system and the uniform grid. In the simulation each particle is given a random velocity (vx, vy) which describes their x and y movement in terms of pixels per second. During each frame, the following actions are performed, given the time elapsed since the last frame, dt:</p>
<p><code><br />
1. Find the new x,y coords of the particle using:<br />
x = dt * vx + x<br />
y = dt * vy + y<br />
2. Calculate a small step size dx,dy by dividing<br />
the movement by 5<br />
3. Step from the current position to the new position<br />
using dx,dy<br />
4. At each step, check for collisions<br />
5. If a collision is detected, step back one step and<br />
invert the velocity of both particles<br />
</code></p>
<p>The reason for stepping slowly between the current position to the new position is to avoid tunneling, in which the step size of a particle is larger than the particle itself and allows it to step through another particle.</p>
<p><a href="/SketchViewer.aspx?base=collisiondetection_jan06a" target="_blank">View</a> the uniform grid in action!</p>
<h4>Problems</h4>
<p>When optimizing collision detection, there is a great deal of give and take, every optimization you make results in new errors in the detection mechanism. The same is true for this method. The problem of tunneling can still occur, even with the additional precautions I&#8217;ve taken here. This can be seen in my simulation when two particles get &#8220;stuck&#8221; together. They become stuck due to the fact that they one particle has actually stepped *inside* another particle. However, because tunneling is really more of an animation issue, I&#8217;m not going to attempt to solve it here.</p>
<p>The second problem is if you have objects of highly varying sizes. A uniform grid works best for objects of similar size, when the size varies greatly, the grid becomes inefficient. To solve this problem, use a hierarchical grid instead [2].</p>
<p>The final problem is that the collisions are not very realistic. This is not a problem with the detection system at all, but a problem with how velocities are calculated during a collision. A much nicer way to do this would be to use a concept from physics known as an elastic collision. The formula for an elastic collision is simple, however, I didn&#8217;t want to muddy the waters here with any addition details unrelated to collision detection.</p>
<h4>Conclusion</h4>
<p>The uniform grid concept greatly improves performance of collision detection by using spatial locality of the particles being tested. I hope this tutorial has provided a good starting point for a fast collision detection system, despite some minor issues. Perhaps in the future, I will continue to build upon this framework and refine some of the problems presented here.</p>
<h4>References:</h4>
<p>[1] C. Ericson. Real Time Collision Detection, Morgan Kaufmann Publishers, pages 285-290, 2005.<br />
[2] C. Ericson. Real Time Collision Detection, Morgan Kaufmann Publishers, pages 300-306, 2005.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualcore.com/index.php/2009/01/cheap-collision-detection-in-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Gift of Processing</title>
		<link>http://visualcore.com/index.php/2008/12/the-gift-of-processing/</link>
		<comments>http://visualcore.com/index.php/2008/12/the-gift-of-processing/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 02:42:30 +0000</pubDate>
		<dc:creator>Jeremy Cowles</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://visualcore.com/wp/?p=48</guid>
		<description><![CDATA[This Christmas, I got an awesome gift from a friend, it was this book:

I have never really played with processing, but programming for the sole purpose of a creative outlet sounds awesome to me. I&#8217;ve created a couple little sketches already, one is very similar to my old Particle Accelerator app.
I hope to replace the [...]]]></description>
			<content:encoded><![CDATA[<p>This Christmas, I got an awesome gift from a friend, it was this book:</p>
<p><a style="float:right" href="http://www.amazon.com/Processing-Programming-Handbook-Designers-Artists/dp/0262182629/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1230608018&amp;sr=8-1" target="_blank"><img src="http://ecx.images-amazon.com/images/I/51UPLtYbwkL._SL500_AA240_.jpg" border="0" alt="" /></a></p>
<p>I have never really played with processing, but programming for the sole purpose of a creative outlet sounds awesome to me. I&#8217;ve created a couple little sketches already, one is very similar to my old Particle Accelerator app.</p>
<p>I hope to replace the infinitely useless &#8220;Links&#8221; page with a processing &#8220;Sketches&#8221; page in the near future. Hey, and maybe I will even make the RSS feeds work&#8230; hmm, maybe not. <img src='http://visualcore.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks man!</p>
<p><span style="font-weight:bold">Update 12/31/2008:</span> the <a href="Sketches.aspx">Sketches</a> page is up!</p>
]]></content:encoded>
			<wfw:commentRss>http://visualcore.com/index.php/2008/12/the-gift-of-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The long winter ahead</title>
		<link>http://visualcore.com/index.php/2008/09/the-long-winter-ahead/</link>
		<comments>http://visualcore.com/index.php/2008/09/the-long-winter-ahead/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 04:44:37 +0000</pubDate>
		<dc:creator>Jeremy Cowles</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://visualcore.com/wp/?p=62</guid>
		<description><![CDATA[Well, my fall classes have started, and they are starting off pretty intensely so I probably won&#8217;t be posting the fractal finder code for a while. If you are interested, e-mail me or leave a comment, and I will make some time to clean it up and post it.
]]></description>
			<content:encoded><![CDATA[<p>Well, my fall classes have started, and they are starting off pretty intensely so I probably won&#8217;t be posting the fractal finder code for a while. If you are interested, e-mail me or leave a comment, and I will make some time to clean it up and post it.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualcore.com/index.php/2008/09/the-long-winter-ahead/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding Interesting Fractals</title>
		<link>http://visualcore.com/index.php/2008/08/finding-interesting-fractals/</link>
		<comments>http://visualcore.com/index.php/2008/08/finding-interesting-fractals/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 03:20:01 +0000</pubDate>
		<dc:creator>Jeremy Cowles</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://visualcore.com/wp/?p=64</guid>
		<description><![CDATA[The fractal generator I posted visualizes sin(z)*c type fractals, where c is some constant. The constant c is actually where all the action happens, because if you choose the wrong c, you get a very boring fractal (as I noted on the project, the c value for the fractal I posted came from Paul Bourke). [...]]]></description>
			<content:encoded><![CDATA[<p>The fractal generator I posted visualizes sin(z)*c type fractals, where c is some constant. The constant c is actually where all the action happens, because if you choose the wrong c, you get a very boring fractal (as I noted on the project, the c value for the fractal I posted came from <a href="http://local.wasp.uwa.edu.au/%7Epbourke/fractals/juliaset/" target="_blank">Paul Bourke</a>). The difficulty of finding these constants gave me a new idea, use the fractal generator to find interesting constants. So here is my fractal finder algorithm:</p>
<p><code><br />
1. Specify a sample fractal area, such as 10 x 10 pixels<br />
2. Select a minimum and maximum range for the constant<br />
(real and imaginary parts)<br />
3. Loop over every possible constant in this range and<br />
process the 10 x 10 fractal<br />
4. Record the activity of the generated fractal<br />
5. Display constants with activity greater than some<br />
threshold<br />
</code></p>
<p>So &#8220;activity&#8221; should probably be defined better. I didn&#8217;t want to get crazy and calculate the true <a href="http://www.visualcore.com/post/2008/03/22/Sobel-Edge-Detector-in-VBNET.aspx" target="_blank">sobel-style</a> activity in the image, because this would take forever to process a large number of fractals. Instead, since the actual output for each pixel is in the range [0, 255], I stored a count of these values into an array of 255 elements. So if the value 15 comes out once, array[15] is incremented. As each fractal is processed, the activity of the fractal is the number of array elements greater than zero.</p>
<p>One problem with this method is that all elements in the array could have a value of 1, while all the true activity takes place in array[255]. Also, with an array of 10&#215;10 (which I&#8217;ve been using for testing), the maximum possible activity is 100. Although, this could easily be resolved by using a larger sample size, say 16&#215;16.</p>
<p>I&#8217;m still playing with this, but I&#8217;ll post the code soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualcore.com/index.php/2008/08/finding-interesting-fractals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fractal Visualizer</title>
		<link>http://visualcore.com/index.php/2008/08/fractal-visualizer/</link>
		<comments>http://visualcore.com/index.php/2008/08/fractal-visualizer/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 06:21:27 +0000</pubDate>
		<dc:creator>Jeremy Cowles</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://visualcore.com/wp/?p=79</guid>
		<description><![CDATA[
For the past couple days I&#8217;ve been working on a fractal visualizer. It&#8217;s really slow and messy right now, but you&#8217;re free to look at the source code. If/when I get it dialed in, I&#8217;ll re-post it. I created it in Java, but the code could easily be translated to C# or VB.
]]></description>
			<content:encoded><![CDATA[<p><a href="/images/articles/fractal-large.png" target="_blank"><img src="/images/articles/fractal-small.png" alt="Sample Output" style="border: medium none ;"></a></p>
<p>For the past couple days I&#8217;ve been working on a fractal visualizer. It&#8217;s really slow and messy right now, but you&#8217;re free to look at the <a href="/downloads/visualcore.fractalizer.jar">source code</a>. If/when I get it dialed in, I&#8217;ll re-post it. I created it in Java, but the code could easily be translated to C# or VB.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualcore.com/index.php/2008/08/fractal-visualizer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

