Test03 - Mesh Collision

From Flare3D - WIKI
Jump to: navigation, search
package 
{
	import flare.basic.*;
	import flare.core.*;
	import flare.materials.*;
	import flare.materials.filters.*;
	import flare.physics.collision.*;
	import flare.physics.core.*;
	import flare.physics.core.PhysicsMesh;
	import flare.physics.core.PhysicsSphere;
	import flare.physics.core.RigidBody;
	import flare.primitives.*;
	import flare.system.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
 
	[SWF( width = 800, height = 600, frameRate = 30 )]
 
	public class Test03_MeshCollision extends Sprite 
	{
		private var scene:Scene3D;
		private var physics:PhysicsSystemManager;
		private var model:Pivot3D;
		private var sphereMaterial:Shader3D;
 
		public function Test03_MeshCollision() 
		{
			//Creates the scene from a f3d file
			scene = new Viewer3D(this, "../resources/sceneMeshes.f3d");			
 
			//This demo works in centimeters and grams and seconds 
 
			//Initialize the physics manager
			//By default physics engine use grid clasification(The performance is increased when exists lot of objects)
			physics = PhysicsSystemManager.getInstance();
 
			//Setting world limits, objects outside of these limits are automatically desactivated and flarephysics don't waste time. 
			var worldLimit:AABB = new AABB();			
			worldLimit.addPoint(new Vector3D( -1000, -200, -1000));
			worldLimit.addPoint(new Vector3D( 1000, 100, 1000));
			physics.worldLimit = worldLimit;
 
			//Set the gravity acceleration
			//By default, it is set to 9.81 mts. This demo use centimeters, so we set gravity to 981 cms.
			physics.gravity = new Vector3D(0, -981, 0);						
 
			//Reduce physics accuracy to increase performance. By default is MEDIUM (trade off between performance and accuracy,
                        //the greater the accuracy the lower the performance)
			physics.accuracy = PhysicsSystemManager.MEDIUM_ACCURACY;					
 
			//Define a callback when scene was completed load			
			//Geometry can not be accessed before 
			scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent );	
 
			//Define a material for spheres
			sphereMaterial = new Shader3D( "", [new ColorFilter( 0xc8c864 )] );
 
			//Create a label to show instructions			
			var controls: TextField = new TextField();			
			controls.autoSize = TextFieldAutoSize.LEFT;
			controls.defaultTextFormat = new TextFormat( "Helvetica" , 14, 0xffffff);
			addChild(controls);
			controls.htmlText =	"A: Add spheres";
			controls.x = 5;
			controls.y = 5;					
		}
 
		private function completeEvent(e:Event):void 
		{
			scene.removeEventListener( Scene3D.COMPLETE_EVENT, completeEvent );
 
			//Create the physical representation for the first teapot
			//The representation for this pivot3D is a TriangleMesh (better collision detection accuracy)
			//By definition, TriangleMesh is static
			var tmp1: Pivot3D = scene.getChildByName("teapot01");			
			tmp1.addComponent(new PhysicsMesh());
 
			//Create the physical representation for the second teapot
			//The representation for this pivot3D is a Sphere (poor collision detection accuracy)
			var tmp2: Pivot3D = scene.getChildByName("teapot02");			
			tmp2.addComponent(new PhysicsSphere());
 
			//The teapot02 must be static (non movable)
			(tmp2.components[0] as RigidBody).movable = false;
 
			//Create the physical representation for the floor (a box)			
			var tmp3: Pivot3D = scene.getChildByName("plane");			
			tmp3.addComponent(new PhysicsBox());
			//The floor must be static (non movable)
			(tmp3.components[0] as RigidBody).movable = false;
 
			//Define update event
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );		
		}
 
		private function addBalls():void
		{
			//ADD 2 BALLS
 
			//Create the visual for FIRST BALL. The sphere radius is 3 cm					
			var sphere1:Sphere = new Sphere("sphere1", 3, 16, sphereMaterial);			
			sphere1.setPosition(-25 + (Math.random()-0.5) * 20, 50, 0);
			scene.addChild( sphere1 );			
			//Create the physical representation for the sphere and link to the visual			
			sphere1.addComponent( new PhysicsSphere() );
 
			//
			//Create the visual for SECOND BALL. The sphere radius is 3 cm				
			var sphere2:Sphere = new Sphere("sphere2", 3, 16, sphereMaterial);			
			sphere2.setPosition(25 + (Math.random()-0.5) * 20, 50, 0);
			scene.addChild( sphere2 );			
			//Create the physical representation for the sphere and link to the visual			
			sphere2.addComponent( new PhysicsSphere() );
		}
 
		private function updateEvent(e:Event):void 
		{
			//add same balls
			if ( Input3D.keyHit( Input3D.A ) )
				addBalls();
 
			//Update physics engine
			physics.step();
		}
	}
}
blog comments powered by Disqus
Personal tools
Namespaces
Variants
Actions
Navigation
Survival kit
Flare3D Physics (Beta)
Toolbox