Layabox 实现画 刚体线
生活随笔
收集整理的這篇文章主要介紹了
Layabox 实现画 刚体线
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Layabox 的剛體里面只有一個b2PolygonShape,而為了實現(xiàn)畫剛體線,我們需要用到復(fù)合Shape,也就是多個Shape組成一個剛體。
此時如果用Layabox自帶的Collider就實現(xiàn)不了,所以我們需要繼承ColliderBase來實現(xiàn)復(fù)合體,下面是代碼
package {import laya.physics.ColliderBase;import laya.physics.Physics;import laya.maths.MathUtil;public class RectLineCollider extends ColliderBase{private var _lineWidth:Number = 5;public function set lineWidth(val:Number):void{_lineWidth = val;}private var _fixtures:Array = [];public function setPoints(points:Array,re:Boolean = true ):void{if(points==null || points.length<4) return;_shape = [];var _x:Number = 0;var _y:Number = 0;var len:int = points.length;for (var i:int = 2; i < len-1; i+=2) {var x1:Number = points[i-2];var y1:Number = points[i-1];var x2:Number = points[i];var y2:Number = points[i+1];var cx:Number = (x1+x2)*0.5;var cy:Number = (y1+y2)*0.5;var dx:Number = x1-x2;var dy:Number = y1-y2;var linelen:Number = Math.sqrt(dx*dx+dy*dy);var angle:Number = Math.atan2(y2 - y1, x2 - x1)- Math.PI*0.5;var s:* = new window.box2d.b2PolygonShape();s.SetAsBox(_lineWidth/2/Physics.PIXEL_RATIO,linelen/2/Physics.PIXEL_RATIO, new window.box2d.b2Vec2(cx / Physics.PIXEL_RATIO, cy / Physics.PIXEL_RATIO), angle);_shape.push(s);}if (re) refresh();}public override function refresh():void {if (enabled && rigidBody) {var body:* = rigidBody.body;if (_fixtures.length>0) {for(var j:int = 0 ;j<_fixtures.length;++j){if (_fixtures[j].GetBody() == rigidBody.body) {rigidBody.body.DestroyFixture(_fixtures[j]);}_fixtures[j].Destroy();}_fixtures.length = 0;}for(var i:int = 0 ;i<_shape.length;++i){var def:* = new window.box2d.b2FixtureDef();def.density = density;def.friction = friction;def.isSensor = isSensor;def.restitution = restitution;def.shape = _shape[i];def.filter.groupIndex = rigidBody.group;def.filter.categoryBits = rigidBody.category;def.filter.maskBits = rigidBody.mask;fixture = body.CreateFixture(def);fixture.collider = this;}}}override protected function _onDestroy():void {if (rigidBody) {if (_fixtures.length>0) {for(var i:int = 0 ;i<_fixtures.length;++i){if (_fixtures[i].GetBody() == rigidBody.body) {rigidBody.body.DestroyFixture(_fixtures[i]);}}_fixtures.length = 0;}rigidBody = null;_shape = null;}}} }場景的需要啟用物理引擎
Physics.enable({gravity:50000});
PhysicsDebugDraw.enable();
并且在畫線的時候,還不能和其他對象穿插,也就是線不能畫到其他剛體里面,所以需要射線判斷。
private function addLine():void{var isDown:Boolean = false;var prevX:Number = 0;var prevY:Number = 0;var line:Sprite = null;var points:Array = null;var color:String = "#ff0000";var lineSize:Number = 5;Laya.stage.on(Event.MOUSE_DOWN,this,function(e:Event):void{if(isDown) return;isDown = true;prevX = Laya.stage.mouseX;prevY = Laya.stage.mouseY;if(points==null) points = [];points.push(prevX,prevY);line = new Sprite();line.mouseEnabled = false;Laya.stage.addChild(line);});Laya.stage.on(Event.MOUSE_MOVE,this,function(e:Event):void{if(isDown && line && distance(Laya.stage.mouseX,Laya.stage.mouseY,prevX,prevY)>10){//ray checkvar rayHit:Object = new Object();if(!rayCast(prevX,prevY,Laya.stage.mouseX,Laya.stage.mouseY,rayHit)){line.graphics.drawLine(prevX,prevY,Laya.stage.mouseX,Laya.stage.mouseY,color,lineSize);prevX = Laya.stage.mouseX;prevY = Laya.stage.mouseY;points.push(prevX,prevY);}}});Laya.stage.on(Event.MOUSE_UP,this,function(e:Event):void{if(isDown && points!=null && points.length>3){isDown = false;var col:RectLineCollider = line.addComponent(RectLineCollider);col.setPoints(points)var rb:RigidBody = line.addComponent(RigidBody);rb.allowRotation = true;}else if(line!=null){line.destroy();line = null;}points = null;}); }private function rayCast(startX:Number,startY:Number,endX:Number,endY:Number,outHitInfo:Object):Boolean{var world:*= Physics.I.world;var result:int = 0;world.RayCast(function(fixture,point,normal,fraction):int{outHitInfo.fixture = fixture;outHitInfo.point = point;outHitInfo.normal = normal;outHitInfo.fraction = fraction;result = 1;return 0;//只檢測一個},{x:startX/Physics.PIXEL_RATIO,y:startY/Physics.PIXEL_RATIO},{x:endX/Physics.PIXEL_RATIO,y:endY/Physics.PIXEL_RATIO});return result; }?
總結(jié)
以上是生活随笔為你收集整理的Layabox 实现画 刚体线的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机社交游戏与触动用户的环节
- 下一篇: 【免费软件测试视频-0022】——Win