<?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>V // Pixelnerve &#187; sphere</title>
	<atom:link href="http://www.pixelnerve.com/v/tag/sphere/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pixelnerve.com/v</link>
	<description></description>
	<lastBuildDate>Sun, 30 Oct 2011 23:43:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Ray-Sphere intersection</title>
		<link>http://www.pixelnerve.com/v/2009/02/08/ray-sphere-intersection/</link>
		<comments>http://www.pixelnerve.com/v/2009/02/08/ray-sphere-intersection/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 21:02:54 +0000</pubDate>
		<dc:creator>victormartins</dc:creator>
				<category><![CDATA[3d]]></category>
		<category><![CDATA[Graphics programming]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[ray]]></category>
		<category><![CDATA[realtime]]></category>
		<category><![CDATA[sphere]]></category>

		<guid isPermaLink="false">http://www.pixelnerve.com/v/?p=312</guid>
		<description><![CDATA[After the creation of [We.are.in.Helsinki,3D] i came across a problemwhen using GL_SELECT opengl mode for intersections and picking. I got few complains that when picking a generator the application freezed. I had two options:  1. Make a simplified version of the generator&#8217;s spheres to render on the GL_SELECT mode. 2. Create a function that would [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">After the creation of [We.are.in.Helsinki,3D] i came across a problemwhen using GL_SELECT opengl mode for intersections and picking. I got few complains that when picking a generator the application freezed. I had two options:  1. Make a simplified version of the generator&#8217;s spheres to render on the GL_SELECT mode. 2. Create a function that would tell me which object i clicked on using ray-sphere intersections.</p>
<p style="text-align: justify;">I took option 2 and made an example in processing. It uses my own library, Vitamin. Its in the zipfile, make sure it is in the right place.</p>
<p>Download: <a title="intersection_ray_sphere" href="http://www.pixelnerve.com/downloads/processing/intersectRaySphere.zip" target="_blank">http://www.pixelnerve.com/downloads/processing/intersectRaySphere.zip</a></p>
<p style="text-align: justify;">Explanation:<br />
First i already had my mouse position in 3D. Just had to convert it to world-space and put it in  the magic function that would tell me if the ray has hit anything, in this case a sphere. Magic function below:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// This function is part of the VRay class.</span>
<span style="color: #666666; font-style: italic;">// Takes a point and a radius and returns number of hits</span>
<span style="color: #000066; font-weight: bold;">int</span> intersectRaySphereHits<span style="color: #009900;">&#40;</span> Vector3 center, <span style="color: #000066; font-weight: bold;">float</span> radius <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Solve quadratic equation</span>
  <span style="color: #000066; font-weight: bold;">float</span> a <span style="color: #339933;">=</span> _direction.<span style="color: #006633;">lengthSqr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0.0</span><span style="color: #009900;">&#41;</span>  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">float</span> b <span style="color: #339933;">=</span> 2.0f <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>_point.<span style="color: #006633;">dot</span><span style="color: #009900;">&#40;</span>_direction<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> _direction.<span style="color: #006633;">dot</span><span style="color: #009900;">&#40;</span>center<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Vector3 tempDiff <span style="color: #339933;">=</span> Vector3.<span style="color: #006633;">sub</span><span style="color: #009900;">&#40;</span> center, _point <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">float</span> c <span style="color: #339933;">=</span> tempDiff.<span style="color: #006633;">lengthSqr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>radius<span style="color: #339933;">*</span>radius<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">float</span> disc <span style="color: #339933;">=</span> b <span style="color: #339933;">*</span> b <span style="color: #339933;">-</span> <span style="color: #cc66cc;">4</span> <span style="color: #339933;">*</span> a <span style="color: #339933;">*</span> c<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> disc <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> 0.0f <span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">int</span> numIntersections<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>disc <span style="color: #339933;">==</span> 0.0f<span style="color: #009900;">&#41;</span>
    numIntersections <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">else</span>
    numIntersections <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">return</span> numIntersections<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.pixelnerve.com/v/2009/02/08/ray-sphere-intersection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

