package{ import flash.events.Event; import jiglib.geometry.JBox; import jiglib.geometry.JSphere; import jiglib.plugin.sandy3d.Sandy3DPhysics; import jiglib.plugin.sandy3d.Sandy3DMesh; import jiglib.physics.RigidBody; import jiglib.geometry.JCapsule; import sandy.core.scenegraph.Shape3D; import sandy.primitive.GeodesicSphere; import sandy.primitive.Sphere; import sandy.primitive.Cylinder; import sandy.primitive.Box; import sandy.primitive.Plane3D; import sandy.materials.attributes.LightAttributes; import sandy.materials.attributes.LineAttributes; import sandy.materials.attributes.PhongAttributes; import sandy.materials.attributes.MaterialAttributes; import sandy.materials.attributes.GouraudAttributes; import sandy.materials.ColorMaterial; import sandy.materials.Appearance; import sandy.view.BasicView; /** * The application shows how falling objects interact in the JigLib physis system * * @author petit@petitpub.com */ public class FallingDebries extends BasicView { private var physics:Sandy3DPhysics; private var bodies:Array; /** * Initiate the physics engine, create the scene and start rendering. */ public function FallingDebries() { // Initiate the BasicView super.init(600, 400); camera.z = -400; camera.y = 100; camera.lookAt(0, 0, 0); // Create an instance of the physics engine physics = new Sandy3DPhysics(scene, 3); bodies = new Array(); // Create the objexts createScene(); // Start rendering render(); } /** * Create the scene */ private function createScene():void { // Create some good materials var attributes:MaterialAttributes = new MaterialAttributes( /*new GouraudAttributes(true),*/ new LightAttributes(true)); var ballMaterial:ColorMaterial = new ColorMaterial(0xAA1200, 1, attributes); var ballAttributes2:MaterialAttributes = new MaterialAttributes(new PhongAttributes(true, 0.2, 15)); var ballMaterial2:ColorMaterial = new ColorMaterial(0xCE24F9, 1, ballAttributes2); var cylMaterial:ColorMaterial = new ColorMaterial(0xE0B42F, 0.3, attributes); var boxMaterial:ColorMaterial = new ColorMaterial(0x10E465, 1, attributes); var boxMaterial2:ColorMaterial = new ColorMaterial(0xFFAA00, 1, attributes); var groundAttributes:MaterialAttributes = new MaterialAttributes( new LineAttributes(0.5,0xECECEC,0.6), new LightAttributes(true)); var groundMaterial:ColorMaterial = new ColorMaterial(0x26A6D0, 1, groundAttributes); // Make the materials accept lighting ballMaterial.lightingEnable = true; cylMaterial.lightingEnable = true; boxMaterial.lightingEnable = true; boxMaterial2.lightingEnable = true; ballMaterial.lightingEnable = true; ballMaterial2.lightingEnable = true; // Create some boxes, cylinders and spheres at different heights, material and restitutions for ( var i:int; i < 10; i++) { var length:Number = randRange(20, 50); var cylinder:Shape3D = new Cylinder("cyl" + i, 10, length, 12, 4); cylinder.appearance = new Appearance(ballMaterial); rootNode.addChild(cylinder); // Now bind this Sandy object to a JCapsule var capsule:JCapsule = new JCapsule( new Sandy3DMesh(cylinder), 10,length); capsule.y = randRange(250, 550); capsule.x = randRange(0, 200) - 100; capsule.z = randRange(0, 200) - 100; capsule.rotationZ = randRange(0, 120); capsule.mass = randRange(1, 5); capsule.friction = 0.4; physics.addBody(capsule); } for ( var j:int; j < 10; j++) { var boxLength:Number = randRange(20, 40); var box:Shape3D = new Box("box"+j, 15, boxLength, 15,"quad", 2); box.appearance = (j % 2 == 0? new Appearance(boxMaterial): new Appearance(boxMaterial2)); box.useSingleContainer = false; rootNode.addChild(box); // Now bind this Sandy object to a JBox var jbox:JBox = new JBox( new Sandy3DMesh(box), 10, 10,length); jbox.y = randRange(150, 600); jbox.x = randRange(0, 150) - 50; jbox.rotationZ = randRange(0, 120); jbox.rotationX = randRange(0, 120); jbox.mass = randRange(1, 5); jbox.friction = 0.6; physics.addBody(jbox); } for ( var k:int; k < 5; k++) { var sphere:Shape3D = new GeodesicSphere("sphere"+k, 15,5); sphere.appearance = (new Appearance(ballMaterial2)); //box.useSingleContainer = false; rootNode.addChild(sphere); // Now bind this Sandy object to a JSphere var jsphere:JSphere = new JSphere( new Sandy3DMesh(sphere), 15); jsphere.y = randRange(450, 1200); jsphere.x = randRange(0, 150) - 50; jsphere.mass = randRange(3, 6); jsphere.restitution = 0.8; jsphere.friction = 0.6; physics.addBody(jsphere); } var sphere2:RigidBody = physics.createSphere("sphere", new Appearance( ballMaterial ), 20, 10, 10); //sphere2.y = 300; sphere2.x = -160; sphere2.mass = 5; sphere2.restitution = 2; // -- **** We may have to construct this ourselves to get it right ? var ground:RigidBody = physics.createGround("ground", new Appearance(groundMaterial), 400, -50, 10); physics.getMesh(ground).enableForcedDepth = true; physics.getMesh(ground).forcedDepth = 4000; ground.yaw(0.05); ground.friction = 0.6; ground.restitution = 1; } /** * Genrates random number inside a range */ private function randRange(minVal:Number, maxVal:Number):Number { return Math.random() * (maxVal - minVal) + minVal; } /** * Overrides the simpleRender method to call the time step method of the rendering engine. * @param event The ENTER_FRAME or timer event - the heart beat of Sandy */ override public function simpleRender( event:Event = null ):void { physics.step( ); super.simpleRender( event ); } } }