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);//关联世界
        }
    }
}

  

 

posted @   Ado_On  阅读(392)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示