zoukankan      html  css  js  c++  java
  • 不同类型刚体接触测试

    一个刚体与任意类型的感应刚体接触,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+"
    ";
            }
            
        };
    }

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

  • 相关阅读:
    [翻译]关于堆和堆栈
    sql 字符+数值 混合排序 lcs
    证明DataReader分页的可行性 lcs
    谈谈我对小公司、大公司及个人成长的见解 lcs
    sina 通用js代码说明 lcs
    Linux系统下生成证书 https证书
    【转】51单片机外部中断的C51编程
    【转】如何建立个人网站
    【转】关于C51的中断编程[原创]
    【转】毫不费力:破解加密PDF文档就使用这两三招
  • 原文地址:https://www.cnblogs.com/kingBook/p/5193025.html
Copyright © 2011-2022 走看看