0

我正在尝试遵循一些关于 Box2D 的教程,但不知何故我似乎无法应用重力。下面是我的代码(带有我为教程阅读的 URL),也许我忽略了一些东西?谢谢你的帮助!

package
{
    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 org.osflash.signals.natives.NativeSignal;

    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;

    /*
     * http://active.tutsplus.com/tutorials/games/introduction-to-box2d-for-flash-and-as3/
     * http://blog.allanbishop.com/box2d-2-1a-tutorial-part-1/
     * http://plasticsturgeon.com/2010/08/making-an-as3-game-in-box2d-flash-version-2-0-hello-world-box2d/
     */
    [SWF(backgroundColor="#cccccc", frameRate="30", width="1280", height="720")]
    public class Floor extends Sprite
    {
        private const   WIDTH                   :uint = 1280;
        private const   HEIGHT                  :uint = 720;
        private const   FPS                     :uint = 30;
        private const   NUM_PIXELS_PER_METER    :uint = 30;
        private const   TIMESTEP                :Number = 0 / NUM_PIXELS_PER_METER;
        private const   VELOCITY_ITERATIONS     :uint = 6;
        private const   POSITION_ITERATIONS     :uint = 2;

        private var _world      :b2World;
        private var _gravity    :b2Vec2;

        private var _updated    :NativeSignal;


        public function Floor()
        {
            init();
        }


        private function init():void
        {
            initStage();
            initBox2D();
            _updated = new NativeSignal (   this,
                                            Event.ENTER_FRAME,
                                            Event
                                        );
            _updated.add(onUpdated);
        }


        private function initStage():void
        {
            stage.frameRate = FPS;
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
        }


        private function initBox2D():void
        {
            _gravity = new b2Vec2(0, 10);
            _world = new b2World(_gravity, true);


            var vector:b2Vec2 = new b2Vec2();


            // floor
            vector.x = (WIDTH * 0.5) / NUM_PIXELS_PER_METER;
            vector.y = (HEIGHT * 0.75) / NUM_PIXELS_PER_METER;
            var floorBodyDef:b2BodyDef = new b2BodyDef();
            floorBodyDef.position.Set(vector.x, vector.y);

            var floorBody:b2Body = _world.CreateBody(floorBodyDef);

            vector.x = (WIDTH * 0.5) / NUM_PIXELS_PER_METER;
            vector.y = 32 / NUM_PIXELS_PER_METER;
            var floorBox:b2PolygonShape = new b2PolygonShape();
            floorBox.SetAsBox(vector.x, vector.y);

            var floorFixture:b2FixtureDef = new b2FixtureDef();
            floorFixture.shape = floorBox;
            floorFixture.density = 1;
            floorFixture.friction = 1;

            floorBody.CreateFixture(floorFixture);


            // box
            vector.x = (WIDTH * 0.5) / NUM_PIXELS_PER_METER;
            vector.y = (HEIGHT * 0.125) / NUM_PIXELS_PER_METER;
            var boxBodyDef:b2BodyDef = new b2BodyDef();
            boxBodyDef.type = b2Body.b2_dynamicBody;
            boxBodyDef.position.Set(vector.x, vector.y);

            var boxBody:b2Body = _world.CreateBody(boxBodyDef);

            var box:b2PolygonShape = new b2PolygonShape();
            box.SetAsBox(1, 1);

            var boxFixtureDef:b2FixtureDef = new b2FixtureDef();
            boxFixtureDef.shape = box;
            boxFixtureDef.density = 1;
            boxFixtureDef.friction = 0.3;
            boxFixtureDef.restitution = 0.1;

            boxBody.CreateFixture(boxFixtureDef);


            // debug
            var debug:Sprite = new Sprite();
            addChild(debug);
            var debugDraw:b2DebugDraw = new b2DebugDraw();
            debugDraw.SetSprite(debug);
            debugDraw.SetDrawScale(NUM_PIXELS_PER_METER);
            debugDraw.SetLineThickness(1.0);
            debugDraw.SetAlpha(1);
            debugDraw.SetFillAlpha(0.4);
            debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
            _world.SetDebugDraw(debugDraw);
        }


        private function onUpdated(_:Event):void
        {
            _world.Step (   TIMESTEP, 
                            VELOCITY_ITERATIONS, 
                            POSITION_ITERATIONS
                        );
            _world.ClearForces();
            _world.DrawDebugData();
        }
    }
}
4

1 回答 1

0

对不起,我是个白痴,当从其他地方复制 TIMESTEP 常量时,我​​得到的结果是 0 ......我的错。

于 2012-05-31T19:29:07.303 回答