Box2D中ApplyForce( )和ApplyImpulse( ) 以及SetLinear...

先看API文档

  • ApplyForce()method

public function ApplyForce(force:b2Vec2, point:b2Vec2):void

Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity.This wakes up the body.

Parameters
force:b2Vec2 — the world force vector, usually in Newtons (N).                 单位:牛顿
point:b2Vec2 — the world position of the point of application.

  • ApplyImpulse()method

public function ApplyImpulse(impulse:b2Vec2, point:b2Vec2):void

Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass.This wakes up the body.

Parameters
impulse:b2Vec2 — the world impulse vector, usually in N-seconds or kg-m/s.       单位:牛顿·秒    或者  千克·米/秒
point:b2Vec2 — the world position of the point of application.

  • SetLinearVelocity()method

public function SetLinearVelocity(v:b2Vec2):void

Set the linear velocity of the center of mass.

物体在休眠状态下,这个函数不起作用。因此先要唤醒物体,才能使用。

Parameters
v:b2Vec2 — the new linear velocity of the center of mass.

我该使用那个外力?

1.当物体静止不动的时候

这个静止不动包括了2种情况:一个是静止在地面 一个是上升到最高阶段即将下落的那一刻(速度为0)

84767D733B6C1F3EF9962842B1E996FB_500_364.jpg

2.当物体处于上升阶段的时候

C20D21A1F15A346BA247A45057BCC0CE_500_345.jpg

3.当物体处于下降阶段的时候

6B978D6C2B13F0E204DA39BA39316F4A_500_345.jpg

源码:

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.b2Fixture;
    import Box2D.Dynamics.b2FixtureDef;
    import Box2D.Dynamics.b2World;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
    /**
     * ...
     * @author Childhood
     */
    public class Main extends Sprite 
    {
        private var world:b2World = new b2World(new b2Vec2(0, 10), true);
        private var worldScale:uint = 30;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        public function Main():void
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            getStarted();
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        private function getStarted():void
        {
            debugDraw();
            drawBox(0,800,1600,100,false,"ground");
            drawBox(100,500,100,100,true,"left");
            drawBox(400,500,10,10,true,"middle");
            drawBox(700,500,10,10,true,"right");
            addEventListener(Event.ENTER_FRAME, update);
            stage.addEventListener(MouseEvent.CLICK, applyForces);
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        private function drawBox(px:Number,py:Number,width:Number,height:Number,d:Boolean,userData:*):void
        {
            var boxBodyDef:b2BodyDef = new b2BodyDef();
            boxBodyDef.position.Set(px / worldScale, py / worldScale);
            if (d) {
                boxBodyDef.type = b2Body.b2_dynamicBody;
            }
            var boxBody:b2Body = world.CreateBody(boxBodyDef);
            boxBody.SetUserData(userData);
            var boxShape:b2PolygonShape = new b2PolygonShape();
            boxShape.SetAsBox(width / 2 / worldScale, height / 2 / worldScale);
            var boxFixtureDef:b2FixtureDef = new b2FixtureDef();
            boxFixtureDef.shape = boxShape;
            var boxFixture:b2Fixture = boxBody.CreateFixture(boxFixtureDef);
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        private function applyForces(e:MouseEvent):void
        {
            var force:b2Vec2;
            //Makes a for loop scanning all the bodys in the World
            for (var worldBody:b2Body = world.GetBodyList(); worldBody; worldBody = worldBody.GetNext()) {
                switch(worldBody.GetUserData()) {
                case "left":
                    force = new b2Vec2(0, -480);
                    //worldBody.ApplyForce(force, worldBody.GetWorldCenter());
                    worldBody.ApplyForce(force, worldBody.GetLocalPoint(new b2Vec2(15,15)));
                    break;
                case "middle":
                    force = new b2Vec2(0, -16);
                    worldBody.ApplyImpulse(force, worldBody.GetWorldCenter());
                    break;
                case "right":
                    force = new b2Vec2(0, -16);
                    worldBody.SetAwake(true);
                    worldBody.SetLinearVelocity(force);
                    break;
                }
            }
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        private function update(e:Event):void
        {
            world.Step(1 / 30, 10, 10);
            world.ClearForces();
            world.DrawDebugData();
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        private function debugDraw():void
        {
            var debugDraw:b2DebugDraw = new b2DebugDraw();
            var debugSprite:Sprite = new Sprite();
            addChild(debugSprite);
            debugDraw.SetSprite(debugSprite);
            debugDraw.SetDrawScale(worldScale);
            debugDraw.SetFillAlpha(0.8);
            debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
            world.SetDebugDraw(debugDraw);
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
}

源码及其效果下载:

http://115.com/file/an83qkmw#ApplyForce&ApplyImpulse.rar

大家可以在以上3种情况下点击舞台,体会下3者的不同之处。

这里的ApplyForce()中的-480 和ApplyImpulse()中的-16是什么关系呢?

为什么这样子左边的箱子和中间的箱子分别给定这数值后运动保持一致呢?

  • 大家记得物理中的p=F*t 吧,动量=力*施加的时间,world的时间步是1/30s,-480*(1/30)=-16,-480是力,单位是牛顿,-16是动量,单位是牛顿*秒。所以左箱子和中间的箱子其实给定的冲量是一样的。不妨把时间步改成1/40,那么-480*(1/40)=-12,那我敢肯定当点击舞台的时候,左边的箱子给定的冲量12小于中间的箱子的冲量16(负号仅代表向上),左边的箱子没有中间的箱子蹦的高。

  • ApplyImpulse可以立即改变物体的速度,而ApplyForce施加的力需要world的时间步来改变物体的速度。(不知道这么理解对不对?)

  • SetLinearVelocity()每次使用的时候会把原来的动量清0然后加上给定的冲量。ApplyForce和ApplyImpulse会在原来的动量上进行加减运算。

以上看法可能有些出入,如果有误,希望朋友留言勘误。非常感谢!(童年) 

转载于:https://my.oschina.net/childhood123/blog/80247

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值