Archive

Archive for the ‘JigLibFlash’ Category

Tutorial JigLibFlash – How to create basic physics 3d scene.

April 24th, 2009 miguelMoraleda 10 comments


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 using Papervision3d.


Lets begin



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 BasicView – Making Life In Papervision a Little Easier

Our class at this moment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package
{
	import flash.events.Event;
	import org.papervision3d.view.BasicView;
 
	public class Main extends BasicView
	{
		public function Main()
		{
			super(0, 0, true);
			startRendering();
		}
 
		override protected function onRenderTick(event:Event = null):void
		{
			super.onRenderTick(event);
		}
	}
}

If you compile the code now, you won’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.

For our example we need to add a new instance of Papervision3DPhysics in the main class.

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
package
{
	import flash.events.Event;
	import jiglib.plugin.papervision3d.Papervision3DPhysics;
	import org.papervision3d.view.BasicView;
 
	public class Main extends BasicView
	{
 
		private var _physics:Papervision3DPhysics;
 
		public function Main()
		{
			super(0, 0, true);
 
			initPhysics();
			startRendering();
		}
 
		private function initPhysics():void
		{
                         //need the scene instance (Scene3D) and the speed for the engine
			_physics = new Papervision3DPhysics(scene, 7);
		}
 
		override protected function onRenderTick(event:Event = null):void
		{
                        //update the engine in each frame
			_physics.step();
			super.onRenderTick(event);
		}
	}
}

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.

How To create objects


Create Ground


1
2
3
4
5
private function createGround():void
{
	var mat:WireframeMaterial = new WireframeMaterial(0xCCCCCC);
	var ground:RigidBody = _physics.createGround(mat, 1000, 0);
}

Create Cube


1
2
3
4
5
6
private function createBox():void
{
	var materials:MaterialsList = new MaterialsList();
	materials.addMaterial(new WireframeMaterial(0xFF4444), "all");
	var box:RigidBody = _physics.createCube(materials, 100, 100, 100, 3, 3, 3);
}


Create Sphere


1
2
3
4
private function createSphere():void
{
	var ball:RigidBody = _physics.createSphere(new WireframeMaterial(0xFF44FF), 50);
}

These are the functions that you can use with Papervision3DPhysics. Don’t forget that Papervision3DPhysics is a class to get things easier and faster.
I hope this tutorial results helpful for you and motivates you to learn more about JigLib. Any suggests will be great.

VIEW EXAMPLE
DOWNLOAD TUTORIAL FILES



Categories: JigLibFlash Tags:
Get Adobe Flash playerPlugin by wpburn.com wordpress themes