zoukankan      html  css  js  c++  java
  • Box2D入门第一天

    开始学习Box2d,今天是第一天,上班好累的时候试着弄了弄Box2D,包括:

    1 创建世界

    2 创建debugdraw

    3 创建一个简单的盒子

    其中有很多东西不甚明了,

    贴上代码如下以备忘:

    package
    {
    	/**
    	 * @create_time Apr 13, 2012 4:00:54 PM
    	 * @author Ado
    	 * @E-mail: adodo08@163.com
    	 **/
    	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,10),true);
    			//调试用的,估计,待研究
    			drawDebug();
    			addEventListener(Event.ENTER_FRAME, update);
    			//让我很奇怪的是为什么监听不到鼠标事件
    			addEventListener(MouseEvent.CLICK, generateBody);
    		}
    		
    		private function generateBody(e:MouseEvent=null):void
    		{
    			//创建一个世界对象的流程,感觉很麻烦的样子,包括各类的数据
    			var shape:b2PolygonShape = new b2PolygonShape();
    			shape.SetAsBox(10/drawScale,10/drawScale);
    			var fixture:b2FixtureDef = new b2FixtureDef();
    			fixture.density = 1;
    			fixture.friction = 2;
    			fixture.restitution = 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();
    			generateBody();
    		}
    		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);//关联世界
    		}
    	}
    }
    

      

  • 相关阅读:
    在eclipse中进行Struts2项目的配置
    通过Java反射来理解泛型的本质
    Java动态加载类在功能模块开发中的作用
    让正常网页呈现黑白色调的方法
    养生-五谷:花生
    汉语-词语:男人
    地理-地点:白浮图镇
    地理-地点:鸡黍镇
    烹饪:杂粮
    烹饪:五谷
  • 原文地址:https://www.cnblogs.com/adoontheway/p/2446127.html
Copyright © 2011-2022 走看看