Papervision3D with Box2DFlash Hello World

Saturday, December 20th, 2008 | examples, requests

I abhor the syntax for Box2DFlash, but I freely embrace it as an incredible 2d physics engine.

This example is about as basic as I can make it. I’ll follow up soon with more advanced examples with different shapes, mouse interactions, joints, etc.


source

package
{
	import Box2D.Collision.Shapes.b2CircleDef;
	import Box2D.Collision.Shapes.b2PolygonDef;
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2World;
 
	import flash.events.Event;
 
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class Box2DFlashWithPapervision3D extends BasicView
	{
		private var WORLD_SCALE:Number = 30;
		private var WIDTH:Number = 640;
		private var HEIGHT:Number = 480;
 
		private var world:b2World;
 
		private var iterations:int = 10;
		private var timeStep:Number = 1.0/30.0;
 
		public function Box2DFlashWithPapervision3D()
		{
			setupPapervision3D();
			createWorld();
			createFloor();
			createShapes();
 
			startRendering();
		}
 
		private function setupPapervision3D():void
		{
			camera.focus = 10;
			camera.zoom = 100;
		}
 
		private function createWorld():void {
			var worldBounds:b2AABB = new b2AABB();
			worldBounds.lowerBound = new b2Vec2( 0, 0 );
			worldBounds.upperBound = new b2Vec2(WIDTH/WORLD_SCALE, HEIGHT/WORLD_SCALE);
 
 			var gravity:b2Vec2 = new b2Vec2(0, 10);
			var sleep:Boolean = true;
 
			world = new b2World(worldBounds, gravity, sleep);
		}
 
		private function createFloor():void
		{
			// Create border of boxes
			var floorShapeDef:b2PolygonDef = new b2PolygonDef();
			var floorBodyDef:b2BodyDef = new b2BodyDef();
			var floor:b2Body;
 
			//bottom shape definition
			floorShapeDef.SetAsBox((WIDTH+40)/WORLD_SCALE/2, 100/WORLD_SCALE);
 
			// Bottom
			floorBodyDef.position = new b2Vec2(WIDTH/WORLD_SCALE/2, (HEIGHT+95)/WORLD_SCALE);
			floor = world.CreateBody(floorBodyDef);
			floor.CreateShape(floorShapeDef);
 
			floor.SetMassFromShapes();
		}
 
		private function createShapes():void
		{
			for (var i:Number = 0; i < 20; i++)
			{
				//radius for physics circle and sphere
				var radius:Number = Math.random() * 50 + 10;
 
				var bodyDef:b2BodyDef = new b2BodyDef();
				//random position toward the top of the stage
				bodyDef.position = new b2Vec2(Math.random() * WIDTH / WORLD_SCALE, Math.random() * 50 /WORLD_SCALE);
 
				var body:b2Body = world.CreateBody(bodyDef);
 
				var shapeDef:b2CircleDef = new b2CircleDef();
				shapeDef.radius = radius/WORLD_SCALE;
				shapeDef.density = 1;
				shapeDef.friction = .7;
				shapeDef.restitution = .7;
				body.CreateShape(shapeDef);
				body.SetMassFromShapes();
 
				var sphere:Sphere = new Sphere(null, radius);
 
				scene.addChild(sphere);
				body.m_userData = sphere;
			}
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			world.Step(timeStep, iterations);
 
			//sets the position of any DisplayObject3D to the body position
			for (var bb:b2Body = world.m_bodyList; bb; bb = bb.m_next)
			{
                if (bb.m_userData is DisplayObject3D)
                {
                        bb.m_userData.x = bb.GetPosition().x * WORLD_SCALE - WIDTH * .5;
                        bb.m_userData.y = -bb.GetPosition().y * WORLD_SCALE + HEIGHT * .5;
                        bb.m_userData.rotationZ = -bb.GetAngle() * (180/Math.PI);
                }
			}
			renderer.renderScene(scene, camera, viewport);
		}
	}
}

Tags: ,

11 Comments to Papervision3D with Box2DFlash Hello World

Rafael Lima
December 20, 2008

Very Awesome!
But, particularly I think what the APE is best. ^ ^

John Lindquist
December 20, 2008

@Rafael - Really? Why? I’m genuinely curious as I haven’t used APE in a looooong time.

Rafael Lima
December 20, 2008

APE Easiest for learning ;P

John Lindquist
December 20, 2008

:)

Timmy
December 20, 2008

That’s always pretty cool to use and understand physics Engine. Unfortunately, box2D still a 2D engine, and it is impossible for us to rotate the camera without displaying the cheat. All exemples with papervision and box2D are hortogonal view. Does APE support real 3D ? At this time, if you really need physics in perspective view, you could use WOWEngine that is a 3D physics Engine. Wowengine also use a Polygonal deflector and the coming version 2.0 will integrate RigidBody. I dont know if there is the best place to discuss about that, but if you are interested in, you can pm me. I’m glad to see pv3d.org updated each day. At all the pv3d team, you make good works and nice exemples.

John Lindquist
December 20, 2008

@Timmy - you make it sound like posting these 2d physics examples is a bad thing. There are still plenty of uses for 3d with 2d physics.

Also, I am fully aware of the WOW engine and have it on my list of examples to post in the future.

Timmy
December 20, 2008

@john - Absolutely not, posting these exemples are really a christmas gift. Thank you. And Box2D is really a great engine ( probably the best in as ).But some weeks ago, I needed some physics in a pvd3 personal work so I started to learn old box2d exemple on drupal.pv3d. I didnt waste my time because i was learning a little of box2d, but at the end that was not I was looking for. Depending of what you need box2d could be the best way.

Rafael Lima
December 20, 2008

Recalling that, WOW Engine extends APE. ^^

Mario
December 31, 2008

Very Cool! I don’t know if someone else had posted before about integrating box2d-pv3d but it’s the first time I see it, thanks for the tutorials and examples, definitely will give it a read at the rest of the tutorials and try some stuff with it.

As Rafael mentioned, APE is a lot easier to follow; Some time ago I needed some collision detection stuff and was eager to use Box2dFlash however I just couldn’t get it, I mean I wanted to have something working in 5 minutes and box2dflash requires some time to dig into it and see how it works (at least for lazy guys like me), and giving a look over APE and found it a lot easier to code and understand, and yet a powerful engine.

Cheers

Ricardo
January 20, 2009

Hello, first of thankyou for this blog, I’m learning a lot of papervision thanks to this.

I’ trying to use some Box2D mixed with papervision for a project, I understand most of your code, but I don’t get what’s going on with the WORLD_SCALE property, and I’m having problems getting the relationship between papervision object scale and box2d object scale right.

Can you shed some light over it?

Thanks

Ricardo
January 23, 2009

Nevermind, I found out. It’s a parameter that sets the realationship between flash units (pixels) and box2d units, meters or whatever.

Leave a comment