zoukankan      html  css  js  c++  java
  • Box2D范例三修版

    package
    {
    	/**
    	 * @create_time Apr 13, 2012 4:00:54 PM
    	 * @author Ado
    	 * @E-mail: adodo08@163.com
    	 **/
    	import Box2D.Collision.Shapes.b2CircleShape;
    	import Box2D.Collision.Shapes.b2PolygonShape;
    	import Box2D.Common.Math.b2Vec2;
    	import Box2D.Dynamics.b2Body;
    	import Box2D.Dynamics.b2BodyDef;
    	import Box2D.Dynamics.b2DebugDraw;
    	import Box2D.Dynamics.b2FixtureDef;
    	import Box2D.Dynamics.b2World;
    	
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    
    	[SWF(width=1000,height=600,frameRate=24,backgroundColor=0xcccccc)]
    	public class BoxTester extends Sprite
    	{
    		private var world:b2World;
    		private var worldSprite:Sprite;
    		private var drawScale:Number=20;
    		public function BoxTester()
    		{
    			//创建世界,两个参数:重力与是否睡眠
    			world = new b2World(new b2Vec2(0,0),true);
    			//地板
    			createFloor(0,(stage.stageHeight - 10)/drawScale,stage.stageWidth/drawScale,10/drawScale);
    			//天花板
    			createFloor(0,0,stage.stageWidth/drawScale,10/drawScale);
    			//左墙
    			createFloor(0,0,10/drawScale,stage.stageHeight/drawScale);
    			//右墙
    			createFloor((stage.stageWidth-10)/drawScale,0,10/drawScale,stage.stageHeight/drawScale);
    			//调试用的,估计,待研究
    			drawDebug();
    			addEventListener(Event.ENTER_FRAME, update);
    			//让我很奇怪的是为什么监听不到鼠标事件
    			stage.addEventListener(MouseEvent.CLICK, generateBody);
    		}
    		private function createFloor($x:Number,$y:Number,$w:Number,$h:Number):void
    		{
    			var shape:b2PolygonShape = new b2PolygonShape();
    			shape.SetAsBox($w,$h);
    			var fixture:b2FixtureDef = new b2FixtureDef();
    			fixture.density = 1;
    			fixture.friction = 1;
    			fixture.shape = shape;
    			fixture.restitution = 1;
    			var bodyDef:b2BodyDef = new b2BodyDef();
    			bodyDef.type = b2Body.b2_staticBody;
    			bodyDef.position.Set($x,$y);
    			var floor:b2Body = world.CreateBody(bodyDef);
    			floor.CreateFixture(fixture);
    		}
    		private function generateBody(e:MouseEvent=null):void
    		{
    			var shape:b2CircleShape = new b2CircleShape(10/drawScale);
    			
    			var fixture:b2FixtureDef = new b2FixtureDef();
    			fixture.density = 1;
    			fixture.friction = 2;
    			fixture.restitution = 0.1;
    			fixture.shape = shape;
    			var bodyDef:b2BodyDef = new b2BodyDef();
    			bodyDef.type = b2Body.b2_dynamicBody;
    			bodyDef.position.Set(this.mouseX/drawScale, this.mouseY/drawScale);
    			var body:b2Body = world.CreateBody(bodyDef);
    			body.CreateFixture(fixture);
    		}
    		
    		private function update(e:Event):void
    		{
    			world.Step(1/drawScale,10,10);
    			world.ClearForces();
    			world.DrawDebugData();
    			world.SetGravity(new b2Vec2(5-Math.random()*10, 5-Math.random()*10));
    		}
    		private function drawDebug():void
    		{
    			var debugDraw:b2DebugDraw = new b2DebugDraw();
    			var debugSprite:Sprite = new Sprite();
    			addChild(debugSprite);//讲debugdraw的关联视图添加到舞台上
    			debugDraw.SetSprite(debugSprite);//关联sprite到debugdraw
    			debugDraw.SetDrawScale(drawScale);//设置debugdraw的比例
    			debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit);//?
    			debugDraw.SetFillAlpha(0.5);//设置背景填充透明度
    			world.SetDebugDraw(debugDraw);//关联世界
    		}
    	}
    }
    

      将重力改成随机的了,蛮好玩的

  • 相关阅读:
    更新user的方法
    django里的http协议
    django的第一个问题
    一台机器上配置多个ip地址;访问宿主机上的容器
    virtio 之后的数据直连
    virtio是啥子
    perf的采样模式和统计模式
    perf的统计模式: 突破口: x86_perf_event_update
    arp_filter的验证,使用net namespace
    阿里云Windows 2008一键安装包配置php web环境图文安装教程(IIS+Php+Mysql)
  • 原文地址:https://www.cnblogs.com/adoontheway/p/2468423.html
Copyright © 2011-2022 走看看