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 ...
随机推荐
- C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- idea 配置maven web项目
文章转自:https://www.cnblogs.com/weiqingfeng/p/9494914.html 步骤一:首先先创建一个project,上次我说过了创建一个project就是一个工作空间 ...
- C# PDF文件转图片
参考:https://blog.csdn.net/lai124793549/article/details/53392281 https://www.cnblogs.com/xiewei123/p/1 ...
- pyspider启动错误解决(Python 3.7)
问题一 安装好pyspider之后,在启动的时候,报出上图错误. 原因 async和await从 python3.7 开始已经加入保留关键字中. 参考: What’s New In Python 3. ...
- es5和es6中的this指向问题
const test ={ id:2, a:function(){ var a_this=this; setTimeout(function(){ console.log('a:',this,a_th ...
- Linux学习笔记4-CentOS7中redis3.2.9安装教程
redis下载地址:http://www.redis.cn/download.html 1.将下载过来的redis-3.2.9.tar.gz文件复制到/usr/local文件夹下 2.tar xzf ...
- 关于WTSAPI32
一般在windows编程都是用用从ntdll导出的Native API,现在看到一点COM编程或者其他的一些不常用的接口函数总觉得蛮有意思,准备以后多积累一下. 先简单总结WTSAPI32.以下实在W ...
- Sass-Opacity函数-rgba()函数
在前面介绍 RGB 函数一节中,还记得吗?有一个 rgba() 函数可以创建一个颜色,同时还可以对颜色修改其透明度.其可以接受两个参数,第一个参数为颜色,第二个参数是你需要设置的颜色透明值. > ...
- gulp为css,js添加版本号
由于cdn缓存,更改样式后会有一段时间不生效,解决方法就是给css,js加上版本号效果如下: 1.安装gulp插件 npm install --save-dev gulp-rev (version:9 ...
- hive之视图和索引
一.视图 1.视图定义 视图其实是一个虚表,视图可以允许保存一个查询,并像对待表一样对这个查询进行操作,视图是一个逻辑结构,并不会存储数据. 2.视图的创建 通过创建视图来限制数据访问可以用来保护信息 ...