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

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


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:
  1. Rodolfo Lopez Navarrate
    August 26th, 2009 at 12:20 | #1

    Hola muchas grcias ya estuve jugando un buen rato con esto para unproyecto de la empresa donde trabajo con este gran ejemplo , me podrias pasar tu mail o blog , saludos , va bye , y muchas gracias de nuevo

  2. December 6th, 2009 at 05:27 | #2

    Excellent tyvm just what I needed! Spot on.

  3. January 27th, 2010 at 14:01 | #3

    Great job man!!! Thanks

  4. harv
    February 3rd, 2010 at 09:06 | #4

    i cant open the fla file….

    any ideas?

    also my main.as is showing errors like this:

    1084: Syntax error: expecting identifier before lessthan.

  5. February 3rd, 2010 at 09:23 | #5

    Hi harv,

    The fla file is saved for Flash CS4, probably you are using a older version. I just downloaded the example and compile it without errors. I’m sure the code have not error. Try to download the example again and try :)

  6. harv
    February 3rd, 2010 at 09:43 | #6

    thanks for the reply Miguil,
    yeah i was using cs3… so does this mean its impossible?
    also in general trying to use jiglibflash i keep getting errors saying that vector and vector3d are not compile time constants… is this due to flash cs3 too?@miguelMoraleda

  7. February 3rd, 2010 at 09:58 | #7

    Hi harv,

    Could be possible if I send you the fla file saved for CS3, BUUUTT, you can create a new FLA file.. my file only contains the document class definition. The if you create a new one and the Main in the document class is enough to the example works.. About the vector class, you get this error becouse this classes only exist for FP10.. you need to use the JigLibFlash for FP9, this version don’t use the vector classes. :)

  8. harv
    February 3rd, 2010 at 10:06 | #8

    @miguelMoraleda
    Thanks so much. thats great. i’ll let you know how it turns out!

  9. ncuz
    April 13th, 2010 at 10:33 | #9

    Hai miguel, it`s a good.. I love it
    can you help me? I can`t find the JiglibFlash library.. =(

  10. April 13th, 2010 at 10:38 | #10

    ncuz: You have to use svn to get the library. Check out this links to the google code project.

    You can find the sourcecode at http://code.google.com/p/jiglibflash/.
    The SVN is located at: http://code.google.com/p/jiglibflash/source/checkout.

Spam Protection by WP-SpamFree

Get Adobe Flash playerPlugin by wpburn.com wordpress themes