box2d实时绘制圆形方法之三:以鼠标指针处为圆心随点击时间而增大

Box2D everyinch 1711℃ 0评论
package{
	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="800",height="600",frameRate="32",backgroundColor="0x000000")]
	public class DrawCircle3 extends Sprite{
		private var world:b2World;
		private var timeStep:Number;
		private var iterations:uint;
		private var pixelsPerMeter:Number = 30;
		private var initX:Number = 0.0;
		private var initY:Number = 0.0;
		private var sprite:Sprite;
		private var drawing:Boolean = false;
		
		[Embed(source="assets/assets.swf",symbol="ball")]
		private var Ball:Class;
		
		public function DrawCircle3(){
			createWorld();
			makeDebugDraw();
			
			addWall(400/pixelsPerMeter,585/pixelsPerMeter,400/pixelsPerMeter,15/pixelsPerMeter);
			addWall(5/pixelsPerMeter,300/pixelsPerMeter,5/pixelsPerMeter,300/pixelsPerMeter);
			addWall(795/pixelsPerMeter,300/pixelsPerMeter,5/pixelsPerMeter,300/pixelsPerMeter);
					
			addEventListener(Event.ENTER_FRAME,onEnterframe);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		
		private function createWorld():void{
			var gravity:b2Vec2 = new b2Vec2(0.0,9.8);
			var doSleep:Boolean = true;
			world = new b2World(gravity,doSleep);
			world.SetWarmStarting(true);
			timeStep = 1.0/30.0;
			iterations = 10;
		}

		private function makeDebugDraw():void{
			var debugDraw:b2DebugDraw = new b2DebugDraw();
			var debugSprite:Sprite = new Sprite();
			addChild(debugSprite);
			debugDraw.SetSprite(debugSprite);
			debugDraw.SetDrawScale(30.0);
			debugDraw.SetFillAlpha(0.5);
			debugDraw.SetLineThickness(1.0);
			debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
			world.SetDebugDraw(debugDraw);
		}
		
		private function addWall(x:Number,y:Number,hx:Number,hy:Number):void{
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set(x,y);
			var body:b2Body = world.CreateBody(bodyDef);
			var shape:b2PolygonShape = new b2PolygonShape();
			shape.SetAsBox(hx,hy);
			body.CreateFixture2(shape);
		}
				
		private function onMouseDown(e:MouseEvent):void{
			initX = e.localX;
			initY = e.localY;
			sprite = new Ball();
			sprite.x = mouseX;
			sprite.y = mouseY;
			sprite.width = 1;
			sprite.height = 1;
			addChild(sprite);
			drawing = true;
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
		}
		
		private function onMouseMove(e:MouseEvent):void{
			if(drawing){
				sprite.x = mouseX;
				sprite.y = mouseY;
			}
		}
		
		private function onMouseUp(e:MouseEvent):void{
			if(drawing){
				drawing = false;
				addCircle(mouseX/pixelsPerMeter,mouseY/pixelsPerMeter,sprite.width/2/pixelsPerMeter,sprite);
			}
		}
		
		private function addCircle(x:Number,y:Number,radius:Number,texture:Sprite):void{
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.type = b2Body.b2_dynamicBody;
			bodyDef.position.Set(x,y);
			var circle:b2CircleShape = new b2CircleShape();
			circle.SetRadius(radius);
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = circle;
			fixtureDef.density = 2.0;
			fixtureDef.friction = 2.0;
			fixtureDef.restitution = 0.7;
			bodyDef.userData = texture;
			var body:b2Body = world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
			addChild(bodyDef.userData);
		}

		private function onEnterframe(e:Event):void{
			world.Step(timeStep,iterations,iterations);
			world.ClearForces();
			world.DrawDebugData();
			if(drawing){
				sprite.width += 2;
				sprite.height += 2;
				if(sprite.x - sprite.width/2 <10 || sprite.x + sprite.width/2 > 790 ||
				   sprite.y - sprite.height/2 < 0 || sprite.y + sprite.height/2 > 570){
					drawing = false;
					addCircle(mouseX/pixelsPerMeter,mouseY/pixelsPerMeter,sprite.width/2/pixelsPerMeter,sprite);
				}
			}
			for(var body:b2Body = world.GetBodyList();body;body = body.GetNext()){
				if(body.GetUserData() is Sprite){
					body.GetUserData().x = body.GetPosition().x*pixelsPerMeter;
					body.GetUserData().y = body.GetPosition().y*pixelsPerMeter;
					body.GetUserData().rotation = body.GetAngle()*180/Math.PI;
				}
			}
		}
	}
}



转载请注明:陈童的博客 » box2d实时绘制圆形方法之三:以鼠标指针处为圆心随点击时间而增大

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

无觅相关文章插件,快速提升流量