Cocos2d Box2D之简介
| 版权声明:本文为博主原创文章,未经博主允许不得转载。
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之简介的更多相关文章
- Cocos2d Box2D之碰撞检测
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Box2D中碰撞事件由b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象 ...
- Cocos2d Box2D之浮动刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_kinematicBody 运动学物体在模拟环境中根据自身的速度进行移动.运动学物体自身不受力的作用.虽然用户可以手动移动它,但是通 ...
- Cocos2d Box2D之静态刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_staticBody 在模拟环境下静态物体是不会移动的,就好像有无限大的质量.在Box2D的内部会将质量至反,存储为零.静态物体也可 ...
- Cocos2d Box2D之动态刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_dynamicBody 动态物体可以进行全模拟.用户可以用手手动移动动态刚体,也可以由动态刚体自己受力而自运动.动态物体可以和任何物 ...
- 物理引擎简介——Cocos2d-x学习历程(十三)
Box2D引擎简介 Box2D是与Cocos2d-x一起发布的一套开源物理引擎,也是Cocos2d-x游戏需要使用物理引擎时的首选.二者同样提供C++开发接口,所使用的坐标系也一致,因此Box2D与C ...
- 《Cocos2d-x实战 工具卷》上线了
感谢大家一直以来的支持! 各大商店均开始销售:京东:http://item.jd.com/11659696.html当当:http://product.dangdang.com/23659809.ht ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- 万事开头难——Cocos2d-x学习历程(一)
万事开头难,不知该从哪里开始,不过既然要学习一样新东西,那就从了解它开始吧... Cocos2d-x是一个通用平面游戏引擎,基于一个同样十分著名的游戏引擎Cocos2d-iPhone设计,Cocos2 ...
- 我常用的iphone开发学习网站[原创]
引用地址:http://www.cnblogs.com/fuleying/archive/2011/08/13/2137032.html Google 翻译 Box2d 托德的Box2D的教程! Bo ...
随机推荐
- Codeforces - 1194C - From S To T - 子序列 - 排序
https://codeforces.com/contest/1194/problem/C 好像没什么好说的,要能构造s必须是t的子序列,并且相差的字符集合d是p的子集. 用双指针法求两遍子序列就可以 ...
- jsp:useBean的属性class值一直报错的问题
先附上代码: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...
- git-vi
VI命令可以说是Unix/Linux世界里最常用的编辑文件的命令了,但是它的命令集太多,所以要想精通他,也是一件很不容易的事情,除了专业SA,对于我们开发人员而已只需要掌握一些最最常见的用法应该就可以 ...
- Ajax爬取百度图片
目标网址 分析网址:http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2& ...
- 【ASE高级软件工程】Alpha 阶段 backend组 scrum1 记录
本次会议于11月4日,19:10 在微软北京西二号楼13478召开,持续40分钟. 与会人员:Haifeng Chen, Zhikai Chen, Hao Wang, Jia Ning 请假: Xin ...
- RabbitMQ数据同步一致性解决方案
1.概述 我们知道在使用RabbitMQ时,生产者将消息发布出去之后,消息是否顺利到达broker代理服务器呢?默认情况下发布操作没有任何信息返回给生产者,也就是生产者是不知道消息有没有顺利到达bro ...
- OkHttp源码剥离导入到eclipse中
1.里面有两个类关于Android版本的我稍微修改过了,没有用的. 2.可以直接导入eclipse中,maven里面的jar包可能还有不需要的冗余,可以自己去剔除. https://github.co ...
- 手写Mybatis,还需要后面调整下
参考博客 https://blog.csdn.net/Kurozaki_Kun/article/details/81482212 个人理解 读取Mybatis配置文件 数据库连接信息 读取Mapper ...
- AbstractQueuedSynchronizer简单使用
AQS是JUC中很多同步组件的构建基础,简单来讲,它内部实现主要是状态变量state和一个FIFO队列来完成,同步队列的头结点是当前获取到同步状态的结点,获取同步状态state失败的线程,会被构造成一 ...
- HIbernate 查询拼接参数
public List<TrailTestModel> findByEid(List<String> trailids, String eid) { // TODO Auto- ...