|   版权声明:本文为博主原创文章,未经博主允许不得转载。

  Box2D是一个用于模拟2D刚体物体的C++引擎。Box2D集成了大量的物理力学和运动学的计算,并将物理模拟过程封装到类对象中,将对物体的操作,以简单友好的接口提供给开发者。我们只需要调用引擎中相应的对象或函数,就可以模拟现实生活中的加速、减速、抛物线运动、万有引力、碰撞反弹等等各种真实的物理运动。

  Box2D中的名词:

  >>世界(World)

  传说世界本是一片混沌,自从盘古开天辟地之后,盘古神斧砍下出了天和地,形成了真正的世界,而后形成才有了山川、河流、花草、树木。Box2D也一样,现在Box2D在我们的脑中也是混沌的一片,我们要创建物体或进行物理模拟之前,首先也是先创建物理世界。

  在Box2D中用b2World类来表示世界。它是Box2D的一个核心类之一,集成了Box2D对所有对象的创建、删除、碰撞模拟的相关接口。

  >>刚体(b2Body)

  生活中我们看到的任何物体都可以用东西来描述,飞的小鸟,马路上行驶的汽车,等等。“东西”这个词在Box2D的字典中叫做“刚体”(b2Body),其实刚体也就是物理世界中的物体。b2Body是Box2D的核心类,是学习Box2D的基础,也是重中之重。b2Body用来模拟现实物理世界中的所有物体。Box2D中的任何碰撞、反弹、运动轨迹等各种物理现象模拟和数据计算都是基于刚体实现的,所以刚体b2Body所包含的信息有很多,如物体的坐标、角度、受力大小、速度、质量等大量的信息。

Box2D引擎中b2Body定义:

 /// The body type.
/// static: zero mass, zero velocity, may be manually moved
/// kinematic: zero mass, non-zero velocity set by user, moved by solver
/// dynamic: positive mass, non-zero velocity determined by forces, moved by solver
enum b2BodyType
{
b2_staticBody = , //静止Body
b2_kinematicBody, //浮动Body
b2_dynamicBody //动态Body
// TODO_ERIN
//b2_bulletBody,
}; /// A body definition holds all the data needed to construct a rigid body.
/// You can safely re-use body definitions. Shapes are added to a body after construction.
struct b2BodyDef
{
/// This constructor sets the body definition default values.
b2BodyDef()
{
userData = NULL;
position.Set(0.0f, 0.0f); //位置
angle = 0.0f; //弧度
linearVelocity.Set(0.0f, 0.0f); //直线速度设置
angularVelocity = 0.0f;
linearDamping = 0.0f; //直线阻尼
angularDamping = 0.0f;
allowSleep = true;
awake = true;
fixedRotation = false; //角度
bullet = false;
type = b2_staticBody;
active = true;
gravityScale = 1.0f;
} /// The body type: static, kinematic, or dynamic.
/// Note: if a dynamic body would have zero mass, the mass is set to one. ///如果一个动态的身体将有零质量,质量被设置为一。
b2BodyType type; /// The world position of the body. Avoid creating bodies at the origin
/// since this can lead to many overlapping shapes. /// 刚体在物理世界中的位置
b2Vec2 position; /// The world angle of the body in radians.
float32 angle; /// The linear velocity of the body's origin in world co-ordinates.
b2Vec2 linearVelocity; /// The angular velocity of the body.
float32 angularVelocity; /// Linear damping is use to reduce the linear velocity. The damping parameter
/// can be larger than 1.0f but the damping effect becomes sensitive to the
/// time step when the damping parameter is large.
float32 linearDamping; /// Angular damping is use to reduce the angular velocity. The damping parameter
/// can be larger than 1.0f but the damping effect becomes sensitive to the
/// time step when the damping parameter is large.
float32 angularDamping; /// Set this flag to false if this body should never fall asleep. Note that
/// this increases CPU usage.
bool allowSleep; /// Is this body initially awake or sleeping?
bool awake; /// Should this body be prevented from rotating? Useful for characters.
bool fixedRotation; /// Is this a fast moving body that should be prevented from tunneling through
/// other moving bodies? Note that all bodies are prevented from tunneling through
/// kinematic and static bodies. This setting is only considered on dynamic bodies.
/// @warning You should use this flag sparingly since it increases processing time.
bool bullet; /// Does this body start out active?
bool active; /// Use this to store application specific body data.
void* userData; /// Scale the gravity applied to this body.
float32 gravityScale;
};

  >>夹具(Fixture)

  Fixture在Box2D中是一种夹具,主要作用是用来定义刚体所固有的一些属性,并保存在b2Fixture对象中。现实中通常是物体材料特性相关的一些属性,如刚体的密度、摩擦系数等属性都是由b2FixtureDef保存的。

Box2D中b2FixtureDef结构体定义:

 /// A fixture definition is used to create a fixture. This class defines an
/// abstract fixture definition. You can reuse fixture definitions safely.
struct b2FixtureDef
{
/// The constructor sets the default fixture definition values.
b2FixtureDef()
{
shape = NULL;
userData = NULL;
friction = 0.2f;
restitution = 0.0f;
density = 0.0f;
isSensor = false;
} /// The shape, this must be set. The shape will be cloned, so you
/// can create the shape on the stack.
const b2Shape* shape; /// Use this to store application specific fixture data.
void* userData; /// The friction coefficient, usually in the range [0,1].
float32 friction; /// The restitution (elasticity) usually in the range [0,1].
float32 restitution; /// The density, usually in kg/m^2.
float32 density; /// A sensor shape collects contact information but never generates a collision
/// response.
bool isSensor; /// Contact filtering data.
b2Filter filter;
};

  >>形状(Shape)

  形状是一个b2Shape类型的对象,实现了刚体的具体形状,Box2D将基于这个形状进行精确的物理碰撞模拟。实际上,b2Shape只是一个抽象的父类,没有实际创建形状的过程。在实际开发过程中,b2FixtureDef.shape的属性值都是b2CircleShape、b2PolygonShape等b2Shape的子类对象。

Box2D中Shape的定义:

 /// A shape is used for collision detection. You can create a shape however you like.
/// Shapes used for simulation in b2World are created automatically when a b2Fixture
/// is created. Shapes may encapsulate a one or more child shapes. class b2Shape
{
public: enum Type
{
e_circle = , //圆形
e_edge = , //边界
e_polygon = , //自定义
e_chain = ,
e_typeCount =
};
//余下部分省略
};

Cocos2d Box2D之简介的更多相关文章

  1. Cocos2d Box2D之碰撞检测

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 在Box2D中碰撞事件由b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象 ...

  2. Cocos2d Box2D之浮动刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_kinematicBody 运动学物体在模拟环境中根据自身的速度进行移动.运动学物体自身不受力的作用.虽然用户可以手动移动它,但是通 ...

  3. Cocos2d Box2D之静态刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_staticBody 在模拟环境下静态物体是不会移动的,就好像有无限大的质量.在Box2D的内部会将质量至反,存储为零.静态物体也可 ...

  4. Cocos2d Box2D之动态刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_dynamicBody 动态物体可以进行全模拟.用户可以用手手动移动动态刚体,也可以由动态刚体自己受力而自运动.动态物体可以和任何物 ...

  5. 物理引擎简介——Cocos2d-x学习历程(十三)

    Box2D引擎简介 Box2D是与Cocos2d-x一起发布的一套开源物理引擎,也是Cocos2d-x游戏需要使用物理引擎时的首选.二者同样提供C++开发接口,所使用的坐标系也一致,因此Box2D与C ...

  6. 《Cocos2d-x实战 工具卷》上线了

    感谢大家一直以来的支持! 各大商店均开始销售:京东:http://item.jd.com/11659696.html当当:http://product.dangdang.com/23659809.ht ...

  7. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  8. 万事开头难——Cocos2d-x学习历程(一)

    万事开头难,不知该从哪里开始,不过既然要学习一样新东西,那就从了解它开始吧... Cocos2d-x是一个通用平面游戏引擎,基于一个同样十分著名的游戏引擎Cocos2d-iPhone设计,Cocos2 ...

  9. 我常用的iphone开发学习网站[原创]

    引用地址:http://www.cnblogs.com/fuleying/archive/2011/08/13/2137032.html Google 翻译 Box2d 托德的Box2D的教程! Bo ...

随机推荐

  1. Emacs中的前进后退jump-tree

    Emacs中的前进后退jump-tree */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #8394 ...

  2. python学习第十五天字典的创建及增删改查操作方法

    字典是python比较常见的数据类型,跟列表一样,比如的字典的创建,字典的常见的操作的方法,增加,删除,修改,查找等方法,字典的一共的数据方法为 keys()  values() fromkeys() ...

  3. qt 如何注册自定义类型?

    如何声明自定义类型 如果仅仅在 QVariant 中使用,则仅需要使用 Q_DECLARE_METATYPE 宏进行声明即可. class Custom_ : public QObject { Q_O ...

  4. Spring学习笔记(14)——SpEL

    是什么 Spring表达式语言全称为"Spring Expression Language",缩写为"SpEL",类似于Struts2x中使用的OGNL表达式语 ...

  5. Android禁止输入表情符号

    在我们Android开发的项目中,难免有要求在输入框中禁止输入表情,所以呢,写了一个输入框禁止输入表情的demo,供大伙参考 效果图 如图显示,如果用户输入了表情字符,会提示. EmojiFilter ...

  6. C#使用Process启动exe程序,不弹出控制台窗口的方法

    背景:使用wkhtmltopdf工具将html转换成pdf时,这个工具在进行转换时会弹出命令行窗口显示转换过程,但是在项目运行时弹出服务器突然弹出控制台窗口会很奇怪,尤其是当转换多个时.解决这个问题 ...

  7. 8VC Venture Cup 2017 - Elimination Round - B

    题目链接:http://codeforces.com/contest/755/problem/B 题意:给定PolandBall 和EnemyBall 这2个人要说的单词,然后每一回合轮到的人要说一个 ...

  8. 字符串函数-unquote()函数

    字符串函数顾名思意是用来处理字符串的函数.Sass 的字符串函数主要包括两个函数: unquote($string):删除字符串中的引号: quote($string):给字符串添加引号. 1.unq ...

  9. Ehcahe独立使用

    <?xml version="1.0" encoding="utf-8"?><ehcache xmlns:xsi="http://w ...

  10. vue星级评分组件

    <template> <div class="Rating-gray"> <i v-for="(item,index) in itemCla ...