[papervision3d2] 碰撞检测

      做生活 2008-6-30 0:54

代码如下:
 package ex {
	import flash.display.Sprite; 	
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.objects.primitives.Cylinder;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.cameras.Camera3D;
	import flash.events.Event;
	import flash.display.StageScaleMode; 
	public class Ex5 extends Sprite {
       	private var mx:int=50;
       	private var mz:int=50;
		public var p:DisplayObject3D = new DisplayObject3D();
		public var sph:Sphere; 
		public var v:Viewport3D;
		public var r:BasicRenderEngine;
		public var ds:Scene3D;
		public var dc:Camera3D; 		  
		public function Ex5() {
			stage.scaleMode=StageScaleMode.NO_SCALE;
			v = new Viewport3D(400, 400);
			addChild(v); 
			r = new BasicRenderEngine();
			ds= new Scene3D();
			dc = new Camera3D();
			var f:Plane = new Plane(new WireframeMaterial(), 10000,10000,10,10);  
			f.y = 0;
			f.pitch(90);
			ds.addChild(f);			 
			dc.y = 300;
			dc.z = 1500;
			sph= new Sphere(new WireframeMaterial(), 20, 10, 8);
			sph.x = (Math.random() * 40) - 20; 
			sph.z = (Math.random() * 40) - 20;
			sph.y = 10
			ds.addChild(sph);
			dc.target=sph      	
			for (var x:Number = 0; x <200; x++) {
				var cy:Cylinder=new Cylinder(new WireframeMaterial(),30,100);
				cy.y=60;
				cy.x=Math.random()*8000-4000;
				cy.z=Math.random()*8000-4000;
				p.addChild(cy); 
			}
         	ds.addChild(p);
         	addEventListener(Event.ENTER_FRAME, enterFrameHandle); 
		}
		protected function enterFrameHandle(e:Event):void {
			for (var item:String in p.children) { 
				var o:DisplayObject3D = p.getChildByName(item); 
				dohittest(sph,o);
			} 
			sph.x+=mx;
			sph.z+=mz;
			dc.x=sph.x;
			dc.target=sph;
			constrain(sph);
			r.renderScene(ds,dc,v);       

       }       
       private function dohittest(o1:DisplayObject3D, o2:DisplayObject3D):void {
			if (o1.hitTestObject(o2)) {			
				mx=-(mx+Math.random()*10);
				mz=-(mz+Math.random()*10);
			}
       }       
		private function constrain(o:DisplayObject3D):void {
			if (o.x>5000) mx=-(Math.random()*30+20);
			if (o.x< -5000) mx=(Math.random()*30+20);
			if (o.z>5000) mz=-(Math.random()*30+20);
			if (o.z<-5000) mz=(Math.random()*30+20);
		}

    }  
} 
标签集:TAGS:as3 example Flash greatwhite
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}