Papervision3D with Box2DFlash Part 2

Saturday, December 20th, 2008 | examples

Please view the previous “Hello World” example before looking at this one.

In part 2, we’re adding walls and cubes with the code below:


source

private function createWalls():void
{
	 var wallShapeDef:b2PolygonDef = new b2PolygonDef();
	var wallBodyDef:b2BodyDef = new b2BodyDef();
	var wall:b2Body;
 
	//Left and Right Shape Definition
	wallShapeDef.SetAsBox(100/WORLD_SCALE, (HEIGHT+40)/WORLD_SCALE/2);
 
	// Left
	wallBodyDef.position = new b2Vec2(-95 / WORLD_SCALE, HEIGHT/WORLD_SCALE/2);
	wall = world.CreateBody(wallBodyDef);
	wall.CreateShape(wallShapeDef);
 
	// Right
	wallBodyDef.position = new b2Vec2((WIDTH+95) / WORLD_SCALE, HEIGHT/WORLD_SCALE/2);
	wall = world.CreateBody(wallBodyDef);
	wall.CreateShape(wallShapeDef);
}
private function createCubes():void
{
	for (var i:Number = 0; i < 10; i++)
	{
		var boxWidth:Number = Math.random() * 20 + 10;
 
		var boxBody:b2BodyDef = new b2BodyDef();
		boxBody.position = new b2Vec2(Math.random() * WIDTH / WORLD_SCALE, Math.random() * 50 /WORLD_SCALE);
 
		var box:b2Body = world.CreateBody(boxBody);
 
		var boxShape:b2PolygonDef = new b2PolygonDef();
		boxShape.SetAsBox(boxWidth/WORLD_SCALE, boxWidth/WORLD_SCALE);
		boxShape.density = .7;
		boxShape.friction = .3;
		boxShape.restitution = .9;
 
		box.CreateShape(boxShape);
 
		box.SetMassFromShapes();
 
		var materialsList:MaterialsList = new MaterialsList();
		materialsList.addMaterial(new WireframeMaterial(Math.random() * 0xffffff), "all");
		var cube:Cube = new Cube(materialsList, boxWidth * 2, boxWidth * 2, boxWidth * 2);
 
		scene.addChild(cube);
		box.m_userData = cube;
	}
}

Tags: ,

blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

December 2008
M T W T F S S
« Nov   Jan »
1234567
891011121314
15161718192021
22232425262728
293031  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...