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

童年伊梦

• 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.当物体处于上升阶段的时候

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

``````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();
}

private function init(e:Event = null):void
{
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");
}

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();
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

• 大家记得物理中的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会在原来的动量上进行加减运算。

### 童年伊梦

Visual Studio 2012下Box2D开发环境设置

Cocos2d-x 3.x默认情况下采用的物理引擎是Chipmunk，如果我们要使用Box2D引擎，需要进行一些设置和调整，而且不同的开发平台下这些设置也有所不同。由于本书在此之前介绍的都是基于微软的Vis...

2014/10/07
0
0
【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法

junwong
2012/03/02
1K
0

2011/09/07
2.1K
0

2014/11/25
0
0

2015/11/21
0
0

spinachgit
16分钟前
1
0

18分钟前
4
0
MySQL-入门（二）

29分钟前
1
0
BAM转VCF的方法对比

1 使用GATK HaplotypeCaller #java -jar gatk.jar HaplotypeCaller --native-pair-hmm-threads 4 -R xx.fa -I xx.bam -O xx.vcf --native-pair-hmm-threads用来设置多线程，默认为4线程 2 sa......

37分钟前
0
0

Owen_Jia
39分钟前
1
0