fahad
fahad

Reputation: 29

Box2DFlash ApplyForce()

I am developing a new game by using Box2dFlash. I write a simple code in which when I click in stage a ball will move. I use ApplyForce() function to accomplish this. But the problem is when the ball start move it does not stop until it goes to a corner of the stage. It continuously move to a given velocity. Is there any way to decrease the velocity of the ball per time when it is moving? My code is as follow -

        package
        {   
            import Box2D.Collision.Shapes.b2CircleShape;
            import Box2D.Collision.Shapes.b2PolygonShape;
            import Box2D.Common.Math.b2Vec2;
            import Box2D.Common.Math.b2Vec3;
            import Box2D.Dynamics.b2Body;
            import Box2D.Dynamics.b2BodyDef;
            import Box2D.Dynamics.b2DebugDraw;
            import Box2D.Dynamics.b2FixtureDef;
            import Box2D.Dynamics.b2World;
            import flash.display.MovieClip; 
            import flash.display.Sprite;
            import flash.events.Event;
            import flash.events.KeyboardEvent;
            import flash.events.MouseEvent;

            public class Main extends MovieClip
            {   
                private var world:b2World;
                private const scale:Number = 30;
                private var b1:b2Body;

                public function Main()
                {
                    if (stage) init();
                    else addEventListener(Event.ADDED_TO_STAGE, init);
                    trace("physics1...");
                }
                private function init(e:Event=null):void
                {
                    removeEventListener(Event.ADDED_TO_STAGE, init);
                    getStarted();
                }
                private function getStarted():void 
                {
                    createWorld();
                    debugDraw();
                    drawBox(stage.stageWidth / 2, stage.stageHeight - 5, stage.stageWidth, 10);
                    drawBox(stage.stageWidth / 2, 5, stage.stageWidth, 10);
                    drawBox(5, stage.stageHeight / 2, 10, stage.stageHeight);
                    drawBox(stage.stageWidth - 5, stage.stageHeight / 2, 10, stage.stageHeight);
                    b1 = drawBall(10, stage.stageWidth / 2, stage.stageHeight / 2);
                    b1.GetFixtureList().SetRestitution(0.4);
                    b1.GetFixtureList().SetFriction(1.0);
                    b1.GetFixtureList().SetDensity(20);
                    addEventListener(Event.ENTER_FRAME, update);
                    stage.addEventListener(MouseEvent.CLICK, onClick);
                }
                private function onClick(e:MouseEvent):void 
                {
                    b1.ApplyForce(new b2Vec2(200, 200), b1.GetWorldCenter());
                }
                private function update(e:Event):void 
                {
                    world.Step(1 / 30, 10, 10);
                    world.ClearForces();
                    world.DrawDebugData();
                }
                private function createWorld():void
                {
                    world = new b2World(new b2Vec2(0, 0), true);
                }
                private function debugDraw():void
                {
                    var dd:b2DebugDraw = new b2DebugDraw();
                    var sp:Sprite = new Sprite();
                    addChild(sp);
                    dd.SetSprite(sp);
                    dd.SetDrawScale(scale);
                    dd.SetFlags(b2DebugDraw.e_shapeBit);
                    world.SetDebugDraw(dd);
                }
                private function drawBall(r:Number, _x:Number, _y:Number):b2Body
                {
                    var bd:b2BodyDef = new b2BodyDef();
                    bd.position.Set(_x / scale, _y / scale);
                    bd.type = b2Body.b2_dynamicBody;
                    var fd:b2FixtureDef = new b2FixtureDef();
                    var ps:b2PolygonShape = new b2PolygonShape();
                    ps.SetAsBox(r / scale, r / scale);
                    fd.shape = new b2CircleShape(r / scale);
                    var b:b2Body = world.CreateBody(bd);
                    b.CreateFixture(fd);
                    return b;
                }
                private function drawBox(_x:Number,_y:Number,_w:Number,_h:Number):b2Body
                {
                    var bd:b2BodyDef = new b2BodyDef();
                    bd.position.Set(_x / scale, _y / scale);
                    var ps:b2PolygonShape = new b2PolygonShape();
                    ps.SetAsBox(_w / 2 / scale, _h / 2 / scale);
                    var fd:b2FixtureDef = new b2FixtureDef();
                    fd.shape = ps;
                    var b:b2Body = world.CreateBody(bd);
                    b.CreateFixture(fd);
                    return b;
                }
            }   
        }

Upvotes: 0

Views: 76

Answers (1)

Marty
Marty

Reputation: 39456

You can add linear damping to the body:

b1.SetLinearDamping(3);

Upvotes: 1

Related Questions