AwayPhysics is an extremely fast Alchemy port of the widely known Bullet Physics Engine. It is specifically designed for Away3D 4.x, providing an optimized, closely coupled physics library to Away3D users.
Sample
package { import away3d.containers.View3D; import away3d.debug.AwayStats; import away3d.entities.Mesh; import away3d.events.MouseEvent3D; import away3d.lights.PointLight; import away3d.materials.ColorMaterial; import away3d.primitives.ConeGeometry; import away3d.primitives.CubeGeometry; import away3d.primitives.CylinderGeometry; import away3d.primitives.PlaneGeometry; import away3d.primitives.SphereGeometry; import awayphysics.collision.shapes.AWPBoxShape; import awayphysics.collision.shapes.AWPConeShape; import awayphysics.collision.shapes.AWPCylinderShape; import awayphysics.collision.shapes.AWPSphereShape; import awayphysics.collision.shapes.AWPStaticPlaneShape; import awayphysics.dynamics.AWPDynamicsWorld; import awayphysics.dynamics.AWPRigidBody; import awayphysics.debug.AWPDebugDraw; import flash.display.Sprite; import flash.events.Event; import flash.geom.Vector3D; import away3d.materials.lightpickers.StaticLightPicker; [SWF(backgroundColor="#000000",frameRate="60",width="1024",height="768")] public class BasicTest extends Sprite { private var _view:View3D; private var _light:PointLight; private var _physicsWorld:AWPDynamicsWorld; private var _sphereShape:AWPSphereShape; private var _timeStep:Number = 1.0 / 60; private var debugDraw:AWPDebugDraw; public function BasicTest() { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); _view = new View3D(); this.addChild(_view); this.addChild(new AwayStats(_view)); _light = new PointLight(); _light.y = 2500; _light.z = -4000; _light.color = 0xffff00; _view.scene.addChild(_light); _view.camera.lens.far = 10000; _view.camera.y = _light.y; _view.camera.z = _light.z; _view.camera.rotationX = 25; // init the physics world _physicsWorld = AWPDynamicsWorld.getInstance(); _physicsWorld.initWithDbvtBroadphase(); debugDraw = new AWPDebugDraw(_view, _physicsWorld); debugDraw.debugMode |= AWPDebugDraw.DBG_DrawTransform; // create ground mesh var material:ColorMaterial = new ColorMaterial(0x252525); material.lightPicker = new StaticLightPicker([_light]); //material.lights = [_light]; var ground:Mesh = new Mesh(new PlaneGeometry(50000, 50000)); ground.material = material; ground.mouseEnabled = true; ground.addEventListener(MouseEvent3D.MOUSE_UP, onMouseUp); _view.scene.addChild(ground); // create ground shape and rigidbody var groundShape:AWPStaticPlaneShape = new AWPStaticPlaneShape(new Vector3D(0, 1, 0)); var groundRigidbody:AWPRigidBody = new AWPRigidBody(groundShape, ground, 0); _physicsWorld.addRigidBody(groundRigidbody); // create a wall var wall:Mesh = new Mesh(new CubeGeometry(20000, 2000, 100)); //var wall : Cube = new Cube(material, 20000, 2000, 100); //var material:ColorMaterial=new ColorMaterial(0xa90000); // the material will be affected by light //material.lightPicker = new StaticLightPicker([light]); wall.material = material; _view.scene.addChild(wall); var wallShape:AWPBoxShape = new AWPBoxShape(20000, 2000, 100); var wallRigidbody:AWPRigidBody = new AWPRigidBody(wallShape, wall, 0); _physicsWorld.addRigidBody(wallRigidbody); wallRigidbody.position = new Vector3D(0, 1000, 2000); //material = new ColorMaterial(0xfc6a11); //material.lightPicker = new LightPickerBase(); // create rigidbody shapes _sphereShape = new AWPSphereShape(100); var boxShape:AWPBoxShape = new AWPBoxShape(200, 200, 200); var cylinderShape:AWPCylinderShape = new AWPCylinderShape(100, 200); var coneShape:AWPConeShape = new AWPConeShape(100, 200); // create rigidbodies var mesh:Mesh; var body:AWPRigidBody; var numx:int = 2; var numy:int = 8; var numz:int = 1; var material:ColorMaterial; for (var i:int = 0; i < numx; i++) { for (var j:int = 0; j < numz; j++) { for (var k:int = 0; k < numy; k++) { // create boxes mesh = new Mesh(new CubeGeometry(200, 200, 200)); material=new ColorMaterial(0xa90000); // the material will be affected by light material.lightPicker = new StaticLightPicker([_light]); mesh.material = material; //mesh = new Cube(material, 200, 200, 200); _view.scene.addChild(mesh); body = new AWPRigidBody(boxShape, mesh, 1); body.friction = .9; body.ccdSweptSphereRadius = 0.5; body.ccdMotionThreshold = 1; body.position = new Vector3D(-1000 + i * 200, 100 + k * 200, j * 200); _physicsWorld.addRigidBody(body); // create cylinders mesh = new Mesh(new CylinderGeometry(200, 100, 200)); material=new ColorMaterial(0xa90000); // the material will be affected by light material.lightPicker = new StaticLightPicker([_light]); mesh.material = material; //mesh = new Cylinder(material, 100, 100, 200); _view.scene.addChild(mesh); body = new AWPRigidBody(cylinderShape, mesh, 1); body.friction = .9; body.ccdSweptSphereRadius = 0.5; body.ccdMotionThreshold = 1; body.position = new Vector3D(1000 + i * 200, 100 + k * 200, j * 200); _physicsWorld.addRigidBody(body); // create the Cones mesh = new Mesh(new ConeGeometry(100, 200)); material=new ColorMaterial(0xa90000); // the material will be affected by light material.lightPicker = new StaticLightPicker([_light]); mesh.material = material; //mesh = new Cone(material, 100, 200); _view.scene.addChild(mesh); body = new AWPRigidBody(coneShape, mesh, 1); body.friction = .9; body.ccdSweptSphereRadius = 0.5; body.ccdMotionThreshold = 1; body.position = new Vector3D(i * 200, 100 + k * 230, j * 200); _physicsWorld.addRigidBody(body); } } } stage.addEventListener(Event.ENTER_FRAME, handleEnterFrame); } private function onMouseUp(event:MouseEvent3D):void { var pos:Vector3D = _view.camera.position; var mpos:Vector3D = event.localPosition; var impulse:Vector3D = mpos.subtract(pos); impulse.normalize(); impulse.scaleBy(2000); // shoot a sphere var material:ColorMaterial = new ColorMaterial(0xb35b11); material.lightPicker = new StaticLightPicker([_light]); //material.lights = [_light]; //var sphere : Sphere = new Sphere(material, 100); var sphere:Mesh = new Mesh(new SphereGeometry(100)); //mesh = new Cylinder(material, 100, 100, 200); sphere.material = material; //_view.scene.addChild(sphere); var body:AWPRigidBody = new AWPRigidBody(_sphereShape, sphere, 2); body.position = pos; body.ccdSweptSphereRadius = 0.5; body.ccdMotionThreshold = 1; _physicsWorld.addRigidBody(body); body.applyCentralImpulse(impulse); } private function handleEnterFrame(e:Event):void { _physicsWorld.step(_timeStep, 1, _timeStep); debugDraw.debugDrawWorld(); _view.render(); } } }
Which version of away3d are you using this with. I’m new to both of these but if i link 3.6.0 swc i get a certain set of errors and if i link 4.0.0 swc of Away3d i get a different set of errors.
Hi, there! I think this code is using Away3D 4.0 as I could read. You can get deeper support from Away3D forums or AwayPhysics forum.
I have updated it for Away3D 4.1.
Thanks for the code snippet! I’ve updated the sample in the lib description.