physics
Papervision3D with Box2DFlash Hello World
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.
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); } } }
Search
Recommended Books
Speaking at FITC Toronto
Recent Posts
- Moving to johnlindquist.com
- AsyncCommand with Robotlegs, Signals, Flight, MinimalComps
- Search Widget – Robotlegs, Signals, Flight, Minimal Comps, Yahoo Astra
- FDT Super Awesome March Deal
- FDT Theme Designer
- haXe Tutorial
- AS3 Signals Tutorial
- Preferred Video Tutorial Resolution?
- TweenMax – Tweening a timeline (Advanced Tweening)
- Robotlegs + Flight + Union Platform
- Back in the saddle
- Eclipse Theme Designer Preview
- RobotLegs Hello World Video Tutorial
- 10 Things Every Senior Flash Developer Should Know
- Efflex – 3D Effects for Flex
Recent Comments
- شقق للبيع في الاردن on Moving to johnlindquist.com
- Annakhan006 on Augmented Reality – Recursive Webcam
- Yarout on Augmented Reality – Recursive Webcam
- Vivon on about
- Josh @ Wall Stickers on Moving to johnlindquist.com
- list of lpn courses on SpringCamera3D and Driving a Car
- rn to bsn in montgomery al on archive
- PowerPoint Recovery on Eclipse Theme Designer Preview
- cheat mw3 on Test if a plane is within the view of the camera (aka testing if culled)
- Goa Hotels on Looking around the inside of a Sphere
Categories
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « May | ||||||
| 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 | |||
Archives
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


