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

  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. 简单写入excel

    import pymysql,xlwt def to_excel(table_name): host, user, passwd, db = '127.0.0.1', 'root', '123', ' ...

  2. 如何用CSS定义一个动画?

    <style type="text/css"> div{ width:100px;height: 100px; animation: carton 5s; backgr ...

  3. 高级定时器-setTimeout()、setInterval()、链式setTimeout()

    使用 setTimeout()和 setInterval()创建的定时器可以用于实现有趣且有用的功能.执行时机是不能保证的,因为在页面的生命周期中,不同时间可能有其他代码在控制 JavaScript ...

  4. 2019-9-2-git镜像仓库

    title author date CreateTime categories git镜像仓库 lindexi 2019-09-02 12:57:37 +0800 2018-2-13 17:23:3 ...

  5. MySQL语句优化方法(简单版)

    基础回顾: sql语句是怎么样运行的? 一般来说,客户端发送sql语句到数据库服务器——数据库服务器进行运算并返回结果——客户端显示sql语句运行结果. 在本地运行时以workbench为例,客户端为 ...

  6. 64-基于TMS320C6455、XC5VSX95T 的6U CPCI无线通信处理平台

    基于TMS320C6455.XC5VSX95T 的6U CPCI无线通信处理平台   1. 板卡概述 本板卡由我公司自主研发,基于CPCI架构,符合PICMG2.0 D3.0标准,包含双TI TMS3 ...

  7. 使用eclipse创建Spring Boot项目

    环境介绍 1.jdk1.8 2.eclipse 3.maven 3.5.0 创建项目 eclectic 左上角 file -> new -> maven project 出现下图默认就好, ...

  8. ltp-ddt realtime_cpuload_10p 涉及的cpuloadgen交叉编译及安装

    1.下载源码 https://github.com/ptitiano/cpuloadgen/archive/v0.94.tar.gz 解压 tar -zxvf cpuloadgen-0.94.tar. ...

  9. ForkJoin简单示例

    import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import jav ...

  10. .net core 控制台程序生成EXE

    在项目上右键编辑xxx.csproj,添加一行 <RuntimeIdentifier>win7-x64</RuntimeIdentifier>