kingBook

导航

不同类型刚体接触测试

一个刚体与任意类型的感应刚体接触,preSlove和postSlove将会忽略不执行只执行。测试代码如下:

package{
    import Box2D.Common.Math.b2Vec2;
    import Box2D.Dynamics.b2Body;
    import flash.text.TextField;
    import Box2D.Dynamics.Contacts.b2Contact;
    import Box2D.Collision.b2Manifold;
    import Box2D.Dynamics.b2ContactImpulse;
    import com.bit101.components.TextArea;
    import com.bit101.components.PushButton;
    import flash.events.MouseEvent;


    public class Main extends BaseMain{
        
        
        public function Main(){
            super(new b2Vec2(0,0));
        }
        
        private var _box:b2Body;
        private var _textArea:TextArea;
        private var _clearBtn:PushButton;
        
        override protected function init():void{
            //static sensor
            createTxt(60,200,"static sensor");
            var staticSensorBox:b2Body=createBox(100,100,60,200);
            staticSensorBox.SetType(b2Body.b2_staticBody);
            staticSensorBox.SetSensor(true);
            staticSensorBox.SetAllowMovement(false);
            staticSensorBox.SetUserData({type:"static sensor"});
            //static
            createTxt(60,400,"static");
            var staticBox:b2Body=createBox(100,100,60,400);
            staticBox.SetType(b2Body.b2_staticBody);
            staticBox.SetAllowMovement(false);
            staticBox.SetUserData({type:"static"});
            
            
            //kinematic sensor
            createTxt(300,200,"kinematic sensor");
            var kinematicSensorBox:b2Body=createBox(100,100,300,200);
            kinematicSensorBox.SetType(b2Body.b2_kinematicBody);
            kinematicSensorBox.SetSensor(true);
            kinematicSensorBox.SetAllowMovement(false);
            kinematicSensorBox.SetUserData({type:"kinematic sensor"});
            //kinematic
            createTxt(300,400,"kinematic");
            var kinematicBox:b2Body=createBox(100,100,300,400);
            kinematicBox.SetType(b2Body.b2_kinematicBody);
            kinematicBox.SetAllowMovement(false);
            kinematicBox.SetUserData({type:"kinematic"});
            
            
            //dynamic sensor
            createTxt(500,200,"dynamic sensor");
            var dynamicSensorBox:b2Body=createBox(100,100,500,200);
            dynamicSensorBox.SetType(b2Body.b2_dynamicBody);
            dynamicSensorBox.SetSensor(true);
            dynamicSensorBox.SetAllowMovement(false);
            dynamicSensorBox.SetUserData({type:"kinematic sensor"});
            //dynamic
            createTxt(500,400,"dynamic");
            var dynamicBox:b2Body=createBox(100,100,500,400);
            dynamicBox.SetType(b2Body.b2_dynamicBody);
            dynamicBox.SetAllowMovement(false);
            dynamicBox.SetUserData({type:"kinematic"});
            
            var box:b2Body=createBox(40,40,300,40);
            box.SetContactBeginCallback(contactBegin);
            box.SetPreSolveCallback(preSolve);
            box.SetPostSolveCallback(postSolve);
            box.SetContactEndCallback(contactEnd);
            _box=box;
            
            _textArea=new TextArea(null,10,10,"");
            addChildAt(_textArea,0);
            
            _clearBtn=new PushButton(this,220,10,"clear",clearHandler);
            _clearBtn.setSize(50,20);
        }
        
        private function contactBegin(contact:b2Contact):void{
            contactHandler(contact,"begin");
        }
        private function preSolve(contact:b2Contact,oldManifold:b2Manifold):void{
            contactHandler(contact,"pre");
        }
        private function postSolve(contact:b2Contact,impulse:b2ContactImpulse):void{
            contactHandler(contact,"post");
        }
        private function contactEnd(contact:b2Contact):void{
            contactHandler(contact,"end");
        }
        
        private function contactHandler(contact:b2Contact,cbType:String):void{
            var b1:b2Body=contact.GetFixtureA().GetBody();
            var b2:b2Body=contact.GetFixtureB().GetBody();
            var ob:b2Body=(b1==_box)?b2:b1;
            var otype:String=ob.GetUserData().type;
            print("bodyType:"+otype, "cbType:"+cbType);
        }
        
        private function createTxt(x:Number,y:Number,text:String):void{
            var txt:TextField=new TextField();
            txt.htmlText="<font color='#ffffff' size='15'>"+text+"</font>";
            txt.x=x-txt.textWidth*0.5;
            txt.y=y-txt.textHeight*0.5;
            addChild(txt);
        }
        
        private function clearHandler(e:MouseEvent):void{
            _textArea.text="";
        }
        
        private function print(... params):void{
            var text:String="";
            for(var i:int=0; i<params.length; i++) text+=params[i]+" ";
            
            _textArea.text+=text+"\n";
        }
        
    };
}

源码下载:https://yunpan.cn/cxx6C2BgWAYqY  访问密码 a205

 

posted on 2016-02-16 16:00  kingBook  阅读(374)  评论(0编辑  收藏  举报