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 ...
随机推荐
- 小B的询问(题解)(莫队)
小B的询问(题解)(莫队) Junlier良心莫队 题目 luoguP2709 小B的询问 code #include<bits/stdc++.h> #define lst long lo ...
- seaborn教程1——风格选择
原文链接:https://segmentfault.com/a/1190000014915873 Seaborn学习大纲 seaborn的学习内容主要包含以下几个部分: 风格管理 绘图风格设置 颜色风 ...
- 记录混合APP开发遇到的坑!!
1.在IOS中给body绑定click事件会失效 2.在IOS中<div contenteditable="true"></div>中点击时可以弹出键盘但是 ...
- Oracle安装client客户端报错Environment variable: "PATH"
安装时出行这个错误 Environment variable: "PATH" 解决方法 1.找到你的安装包里的这个路径下的这两个文件 2.用文本方式打开 将里两个文件面所有的102 ...
- 【记录】eclipse jar包看不了源码
第一步:下载JAD . jad官方地址的官方下载地址是: http://www.softpedia.com/get/Programming/Debuggers-Decompilers-Dissasem ...
- 工欲善其事,必先利其器——React Native的 IDE
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yayayaya20122012/article/details/51119801之前的文章中,我们已 ...
- HTML5:Canvas-基本用法
<canvas id="tutorial" width="150" height="150"></canvas> & ...
- vue Echarts自适应浏览器窗口大小
<template> <div class="chinaecharts"> <div id="mapChart" ref=&quo ...
- C#_winform登陆框验证码的实现
验证码技术已愈来愈成熟,从最初的数字.字母.字符.汉字已经到目前的语言,其应用也甚广,之前大多数只有在网站上可以看到,现在在一些客户端软件也经常可见(比如证券相关软件).之前做的一个基于 C# 客户端 ...
- vue多个input绑定一个数组变量问题
对于data中声明的一个数组变量arr=[],在绑定时候可以如下: <div style="margin-top: 10px;margin-left: 40px;"> ...