package{ import flash.events.Event; import jiglib.geometry.JPlane; import jiglib.plugin.sandy3d.Sandy3DMesh; import jiglib.plugin.sandy3d.Sandy3DPhysics; import jiglib.physics.RigidBody; import jiglib.geometry.JSphere; import jiglib.geometry.JBox; import jiglib.math.*; import sandy.primitive.Box; import sandy.primitive.Plane3D; import sandy.core.scenegraph.Shape3D; import sandy.primitive.GeodesicSphere; import sandy.view.BasicView; 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; /** * In this example we use a test room to contain an experiment * in this case just a bouncing ball * * @author petit@petitpub.com */ public class RoomBall extends BasicView { private var physics:Sandy3DPhysics; private var bodies:Array; /** * Initiate the physics engine, add the shpere and start rendering. */ public function RoomBall() { // Initiate the BasicView super.init(600, 400); // Adjust the camera camera.z = -800; camera.y = 200; camera.lookAt(0, 200, 0); scene.light.setDirection(1,-1,2); // Create an instance of the physics engine physics = new Sandy3DPhysics(scene, 4); // Create the test room createRoom(); // Create the objects createObjects(); // Start rendering render(); } /* * Create our first testing room or test bench */ private function createRoom():void { // Create th floor var groundAttributes:MaterialAttributes = new MaterialAttributes( new LineAttributes(0.5,0xECECEC,0.6), new LightAttributes(true)); var groundMaterial:ColorMaterial = new ColorMaterial(0x26A6D0, 1, groundAttributes); var ground:RigidBody = physics.createGround("ground", new Appearance(groundMaterial), 600, 0, 10); ground.friction = 0.6; ground.restitution = 1; //ground.pitch (0.1); physics.getMesh(ground).enableForcedDepth = true; physics.getMesh(ground).forcedDepth = 4000; // Create the walls - the attitude of the physics planes seem crazy var attributes:MaterialAttributes = new MaterialAttributes( new LightAttributes(true)); var wallMaterial:ColorMaterial = new ColorMaterial(0x1B9A1B, 0.4, attributes); wallMaterial.lightingEnable = true; var frontMaterial:ColorMaterial = new ColorMaterial(0xAA1200, 0.2); var backMaterial:ColorMaterial = new ColorMaterial(0xFF552A);// 0xFFFF7F // Left wall var leftWall:RigidBody = physics.createCube("west", new Appearance(wallMaterial), 4, 600, 600); leftWall.x = -300; leftWall.y = 200; leftWall.z = 300; leftWall.movable = false; // Right wall var rightWall:RigidBody = physics.createCube( "east", new Appearance(wallMaterial), 4, 600, 600 ); rightWall.x = 300; rightWall.y = 200; rightWall.z = 300; rightWall.movable = false; // Back wall var backWall:RigidBody = physics.createCube("north", new Appearance(backMaterial), 600, 4, 600); backWall.z = 600; backWall.y = 200; backWall.movable = false; } /** * Create the objects and add them to the scene. * The create the physical couterpart and bind them together * Add the physical object to the physics engine */ private function createObjects():void { var ballAttributes2:MaterialAttributes = new MaterialAttributes(new PhongAttributes(true, 0.2, 15)); var ballMaterial2:ColorMaterial = new ColorMaterial(0x002BFF, 1, ballAttributes2); ballMaterial2.lightingEnable = true; var sphere:Shape3D = new GeodesicSphere("sphere", 15,5); sphere.appearance = new Appearance(ballMaterial2); rootNode.addChild(sphere); // Now bind this Sandy object to a JSphere var jsphere:JSphere = new JSphere(new Sandy3DMesh(sphere), 20); jsphere.y = 300; jsphere.restitution = 1; physics.addBody(jsphere); } /** * Overriding 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 ); } } }