<?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>Easy Flex &#187; perspectiveProjection</title>
	<atom:link href="http://evtimmy.com/tag/perspectiveprojection/feed/" rel="self" type="application/rss+xml" />
	<link>http://evtimmy.com</link>
	<description>Evtim on Flex SDK</description>
	<lastBuildDate>Thu, 03 Nov 2011 07:18:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Calculating the Projected Bounds using Utils3D.projectVector()</title>
		<link>http://evtimmy.com/2009/12/calculating-the-projected-bounds-using-utils3dprojectvector/</link>
		<comments>http://evtimmy.com/2009/12/calculating-the-projected-bounds-using-utils3dprojectvector/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 07:23:47 +0000</pubDate>
		<dc:creator>Evtim</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[bounds]]></category>
		<category><![CDATA[perspectiveProjection]]></category>
		<category><![CDATA[Utils3D]]></category>

		<guid isPermaLink="false">http://evtimmy.com/?p=173</guid>
		<description><![CDATA[If you are playing with objects in 3D and want their projected bounds, there&#8217;s a neat flash functionality for that &#8211; Utils3D.projectVector(). However it may not be that apparent how to use it. Here I&#8217;m showing a small helper function that does the trick. The projectVector() method actually takes a Matrix3D for a parameter. Note [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://evtimmy.com/wordpress/wp-content/uploads/2009/12/projectedbounds.swf"><img class="size-full wp-image-178 alignleft" title="projectedbounds" src="http://evtimmy.com/wordpress/wp-content/uploads/2009/12/projectedbounds.png" alt="projectedbounds" width="142" height="124" /></a>If you are playing with objects in 3D and want their projected bounds, there&#8217;s a neat flash functionality for that &#8211; Utils3D.projectVector(). However it may not be that apparent how to use it. Here I&#8217;m showing a small helper function that does the trick.<br />
<span id="more-173"></span>The projectVector() method actually takes a Matrix3D for a parameter. Note that here you should pass in the Matrix3D of the object in question combined with the perspective projection of its parent. This is not as trivial as it seems, as you&#8217;d need to also translate by the projectionCenter and the focalLength:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>16
17
18
19
20
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// Setup the matrix</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> centerX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = projection<span style="color: #000066; font-weight: bold;">.</span>projectionCenter<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> centerY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = projection<span style="color: #000066; font-weight: bold;">.</span>projectionCenter<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">.</span>appendTranslation<span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>centerX<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">-</span>centerY<span style="color: #000066; font-weight: bold;">,</span> projection<span style="color: #000066; font-weight: bold;">.</span>focalLength<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">.</span>append<span style="color: #000000;">&#40;</span>projection<span style="color: #000066; font-weight: bold;">.</span>toMatrix3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>After that just pass in the matrix and the point to be projected to Utils3D.projectVector(). Then translate the result by centerX and centerY.<br />
Here&#8217;s the helper function source, it takes in bounds, the matrix of the object and the perspective projection and returns the projected 2D bounds:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> projectBounds<span style="color: #000000;">&#40;</span>bounds<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Rectangle</span><span style="color: #000066; font-weight: bold;">,</span>
 <span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">:</span>Matrix3D<span style="color: #000066; font-weight: bold;">,</span>
 projection<span style="color: #000066; font-weight: bold;">:</span>PerspectiveProjection<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Rectangle</span>
 <span style="color: #000000;">&#123;</span>
 <span style="color: #009900; font-style: italic;">// Setup the matrix</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> centerX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = projection<span style="color: #000066; font-weight: bold;">.</span>projectionCenter<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> centerY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = projection<span style="color: #000066; font-weight: bold;">.</span>projectionCenter<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">.</span>appendTranslation<span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>centerX<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">-</span>centerY<span style="color: #000066; font-weight: bold;">,</span> projection<span style="color: #000066; font-weight: bold;">.</span>focalLength<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">.</span>append<span style="color: #000000;">&#40;</span>projection<span style="color: #000066; font-weight: bold;">.</span>toMatrix3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Project the corner points</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> pt1<span style="color: #000066; font-weight: bold;">:</span>Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span>bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">left</span><span style="color: #000066; font-weight: bold;">,</span> bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">top</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> pt2<span style="color: #000066; font-weight: bold;">:</span>Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span>bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">right</span><span style="color: #000066; font-weight: bold;">,</span> bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">top</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> pt3<span style="color: #000066; font-weight: bold;">:</span>Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span>bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">left</span><span style="color: #000066; font-weight: bold;">,</span> bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bottom</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> pt4<span style="color: #000066; font-weight: bold;">:</span>Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span>bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">right</span><span style="color: #000066; font-weight: bold;">,</span> bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bottom</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 pt1 = Utils3D<span style="color: #000066; font-weight: bold;">.</span>projectVector<span style="color: #000000;">&#40;</span><span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">,</span> pt1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 pt2 = Utils3D<span style="color: #000066; font-weight: bold;">.</span>projectVector<span style="color: #000000;">&#40;</span><span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">,</span> pt2<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 pt3 = Utils3D<span style="color: #000066; font-weight: bold;">.</span>projectVector<span style="color: #000000;">&#40;</span><span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">,</span> pt3<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 pt4 = Utils3D<span style="color: #000066; font-weight: bold;">.</span>projectVector<span style="color: #000000;">&#40;</span><span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">,</span> pt4<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Find the bounding box in 2D</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> maxX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>pt1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> pt2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>pt3<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> pt4<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> minX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span>pt1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> pt2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span>pt3<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> pt4<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> maxY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>pt1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> pt2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>pt3<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> pt4<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #6699cc; font-weight: bold;">var</span> minY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span>pt1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> pt2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span>pt3<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> pt4<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Add back the projection center</span>
 bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = minX <span style="color: #000066; font-weight: bold;">+</span> centerX<span style="color: #000066; font-weight: bold;">;</span>
 bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = minY <span style="color: #000066; font-weight: bold;">+</span> centerY<span style="color: #000066; font-weight: bold;">;</span>
 bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = maxX <span style="color: #000066; font-weight: bold;">-</span> minX<span style="color: #000066; font-weight: bold;">;</span>
 bounds<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = maxY <span style="color: #000066; font-weight: bold;">-</span> minY<span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #0033ff; font-weight: bold;">return</span> bounds<span style="color: #000066; font-weight: bold;">;</span>
 <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The full source is here <a href='http://evtimmy.com/wordpress/wp-content/uploads/2009/12/projectedbounds.mxml'>projectedbounds.mxml</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://evtimmy.com/2009/12/calculating-the-projected-bounds-using-utils3dprojectvector/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

