<?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>Miguel Moraleda &#187; JigLibFlash</title>
	<atom:link href="http://as3.miguelmoraleda.com/tag/jiglibflash/feed/" rel="self" type="application/rss+xml" />
	<link>http://as3.miguelmoraleda.com</link>
	<description>Unity3D &#38; Actionscript examples, resources, source code, etc</description>
	<lastBuildDate>Fri, 16 Apr 2010 12:55:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Tutorial JigLibFlash  &#8211; How to create basic physics 3d scene.</title>
		<link>http://as3.miguelmoraleda.com/2009/04/24/tutorial-jiglibflash-how-to-create-basic-physics-3d-scenetutorial-jiglibflash-como-crear-una-scena-en-3d-con-fisica/</link>
		<comments>http://as3.miguelmoraleda.com/2009/04/24/tutorial-jiglibflash-how-to-create-basic-physics-3d-scenetutorial-jiglibflash-como-crear-una-scena-en-3d-con-fisica/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 00:27:37 +0000</pubDate>
		<dc:creator>miguelMoraleda</dc:creator>
				<category><![CDATA[JigLibFlash]]></category>

		<guid isPermaLink="false">http://as3.miguelmoraleda.com/?p=113</guid>
		<description><![CDATA[Introduction Jiglibflash is a open source 3D physics engine. Official Web. In this tutorial we will create a simple scene with some cubes and spheres. Those object will simulate physics. The first thing we need is to create a basic 3d scene. JigLibFlash actually support Papervision3D, Away3D and Sandy3D. For this tutorial I will be [...]]]></description>
			<content:encoded><![CDATA[<p><br/></p>
<h1>Introduction</h1>
<p><br/><br />
Jiglibflash is a open source 3D physics engine. <a href="http://www.jiglibflash.com/">Official Web</a>.<br />
In this tutorial we will create a simple scene with some cubes and spheres. Those object will simulate physics.<br />
The first thing we need is to create a basic 3d scene. JigLibFlash actually support Papervision3D, Away3D and Sandy3D. For this tutorial I will be using Papervision3d.</p>
<p><br/></p>
<h1>Lets begin</h1>
<p><br/><br />
To create our scene we need a viewport, camera, render engine and scene. All this things are implemented in the BasicView class, for these reason our main class will extends from there. To get more information about it, read the next post  by Andy Zupko <a href="http://blog.zupko.info/?p=96">BasicView &#8211; Making Life In Papervision a Little Easier</a></p>
<p>Our class at this moment.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">papervision3d</span>.<span style="color: #006600;">view</span>.<span style="color: #006600;">BasicView</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> BasicView
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			startRendering<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> onRenderTick<span style="color: #66cc66;">&#40;</span>event:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">onRenderTick</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>If you compile the code now, you won&#8217;t see anything, but internally we have all neccesary things to render 3d objects. With this base we can start with the physics.  In JigLibFlash you will find the Papervision3DPhysics  class, it is a plugin to  handle easier the engine and also have some functions to create objects.</p>
<p>For our example we need to add a new instance of Papervision3DPhysics in the main class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> jiglib.<span style="color: #006600;">plugin</span>.<span style="color: #006600;">papervision3d</span>.<span style="color: #006600;">Papervision3DPhysics</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">papervision3d</span>.<span style="color: #006600;">view</span>.<span style="color: #006600;">BasicView</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> BasicView
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _physics:Papervision3DPhysics;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			initPhysics<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			startRendering<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> initPhysics<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
                         <span style="color: #808080; font-style: italic;">//need the scene instance (Scene3D) and the speed for the engine</span>
			_physics = <span style="color: #000000; font-weight: bold;">new</span> Papervision3DPhysics<span style="color: #66cc66;">&#40;</span>scene, <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> onRenderTick<span style="color: #66cc66;">&#40;</span>event:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
                        <span style="color: #808080; font-style: italic;">//update the engine in each frame</span>
			_physics.<span style="color: #006600;">step</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">onRenderTick</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now we have the scene ready to add objects. Again Papervision3DPhysics  is a very helpful class, because it has some functions to create basic objects like Grounds, Cubes or Spheres.<br />
<br/></p>
<h1><strong>How To create objects</strong></h1>
<p><br/></p>
<h2><strong>Create Ground</strong></h2>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> createGround<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> mat:WireframeMaterial = <span style="color: #000000; font-weight: bold;">new</span> WireframeMaterial<span style="color: #66cc66;">&#40;</span>0xCCCCCC<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> ground:RigidBody = _physics.<span style="color: #006600;">createGround</span><span style="color: #66cc66;">&#40;</span>mat, <span style="color: #cc66cc;">1000</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h2><strong>Create Cube</strong></h2>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> createBox<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> materials:MaterialsList = <span style="color: #000000; font-weight: bold;">new</span> MaterialsList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	materials.<span style="color: #006600;">addMaterial</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> WireframeMaterial<span style="color: #66cc66;">&#40;</span>0xFF4444<span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;all&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> box:RigidBody = _physics.<span style="color: #006600;">createCube</span><span style="color: #66cc66;">&#40;</span>materials, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><br/></p>
<h2><strong>Create Sphere</strong></h2>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> createSphere<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> ball:RigidBody = _physics.<span style="color: #006600;">createSphere</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> WireframeMaterial<span style="color: #66cc66;">&#40;</span>0xFF44FF<span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>These are the functions that you can use with Papervision3DPhysics. Don&#8217;t forget that Papervision3DPhysics is a class to get things easier and faster.<br />
I hope this tutorial results helpful for you and motivates you to learn more about JigLib. Any suggests will be great.</p>
<p><a href="http://www.miguelmoraleda.com/examples/JigLibFlashTutorial/">VIEW EXAMPLE</a><br />
<a href="http://www.miguelmoraleda.com/examples/JigLibFlashTutorial.rar">DOWNLOAD TUTORIAL FILES</a></p>
<p><br><script type="text/javascript"><!--
google_ad_client = "pub-8685425838841676";
google_ad_slot = "6019231894";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<br></p>
]]></content:encoded>
			<wfw:commentRss>http://as3.miguelmoraleda.com/2009/04/24/tutorial-jiglibflash-how-to-create-basic-physics-3d-scenetutorial-jiglibflash-como-crear-una-scena-en-3d-con-fisica/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

