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

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

  Box2D中的名词:

  >>世界(World)

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

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

  >>刚体(b2Body)

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

Box2D引擎中b2Body定义:

 1 /// The body type.
 2 /// static: zero mass, zero velocity, may be manually moved
 3 /// kinematic: zero mass, non-zero velocity set by user, moved by solver
 4 /// dynamic: positive mass, non-zero velocity determined by forces, moved by solver
 5 enum b2BodyType
 6 {
 7         b2_staticBody = 0,    //静止Body
 8         b2_kinematicBody,     //浮动Body
 9         b2_dynamicBody        //动态Body
10         // TODO_ERIN
11         //b2_bulletBody,
12 };
13
14 /// A body definition holds all the data needed to construct a rigid body.
15 /// You can safely re-use body definitions. Shapes are added to a body after construction.
16 struct b2BodyDef
17 {
18         /// This constructor sets the body definition default values.
19         b2BodyDef()
20         {
21                userData = NULL;
22                position.Set(0.0f, 0.0f);          //位置
23                angle = 0.0f;                      //弧度
24                linearVelocity.Set(0.0f, 0.0f);    //直线速度设置
25                angularVelocity = 0.0f;
26                linearDamping = 0.0f;              //直线阻尼
27                angularDamping = 0.0f;
28                allowSleep = true;
29                awake = true;
30                fixedRotation = false;             //角度
31                bullet = false;
32                type = b2_staticBody;
33                active = true;
34                gravityScale = 1.0f;
35         }
36
37         /// The body type: static, kinematic, or dynamic.
38         /// Note: if a dynamic body would have zero mass, the mass is set to one.
39
40     ///如果一个动态的身体将有零质量,质量被设置为一。
41         b2BodyType type;
42
43         /// The world position of the body. Avoid creating bodies at the origin
44         /// since this can lead to many overlapping shapes.
45
46     /// 刚体在物理世界中的位置
47         b2Vec2 position;
48
49         /// The world angle of the body in radians.
50         float32 angle;
51
52         /// The linear velocity of the body's origin in world co-ordinates.
53         b2Vec2 linearVelocity;
54
55         /// The angular velocity of the body.
56         float32 angularVelocity;
57
58         /// Linear damping is use to reduce the linear velocity. The damping parameter
59         /// can be larger than 1.0f but the damping effect becomes sensitive to the
60         /// time step when the damping parameter is large.
61         float32 linearDamping;
62
63         /// Angular damping is use to reduce the angular velocity. The damping parameter
64         /// can be larger than 1.0f but the damping effect becomes sensitive to the
65         /// time step when the damping parameter is large.
66         float32 angularDamping;
67
68         /// Set this flag to false if this body should never fall asleep. Note that
69         /// this increases CPU usage.
70         bool allowSleep;
71
72         /// Is this body initially awake or sleeping?
73         bool awake;
74
75         /// Should this body be prevented from rotating? Useful for characters.
76         bool fixedRotation;
77
78         /// Is this a fast moving body that should be prevented from tunneling through
79         /// other moving bodies? Note that all bodies are prevented from tunneling through
80         /// kinematic and static bodies. This setting is only considered on dynamic bodies.
81         /// @warning You should use this flag sparingly since it increases processing time.
82         bool bullet;
83
84         /// Does this body start out active?
85         bool active;
86
87         /// Use this to store application specific body data.
88         void* userData;
89
90         /// Scale the gravity applied to this body.
91         float32 gravityScale;
92 };

View Code

  >>夹具(Fixture)

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

Box2D中b2FixtureDef结构体定义:

 1 /// A fixture definition is used to create a fixture. This class defines an
 2 /// abstract fixture definition. You can reuse fixture definitions safely.
 3 struct b2FixtureDef
 4 {
 5         /// The constructor sets the default fixture definition values.
 6         b2FixtureDef()
 7         {
 8                shape = NULL;
 9                userData = NULL;
10                friction = 0.2f;
11                restitution = 0.0f;
12                density = 0.0f;
13                isSensor = false;
14         }
15
16         /// The shape, this must be set. The shape will be cloned, so you
17         /// can create the shape on the stack.
18         const b2Shape* shape;
19
20         /// Use this to store application specific fixture data.
21         void* userData;
22
23         /// The friction coefficient, usually in the range [0,1].
24         float32 friction;
25
26         /// The restitution (elasticity) usually in the range [0,1].
27         float32 restitution;
28
29         /// The density, usually in kg/m^2.
30         float32 density;
31
32         /// A sensor shape collects contact information but never generates a collision
33         /// response.
34         bool isSensor;
35
36         /// Contact filtering data.
37         b2Filter filter;
38 };

View Code

  >>形状(Shape)

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

Box2D中Shape的定义:

 1 /// A shape is used for collision detection. You can create a shape however you like.
 2 /// Shapes used for simulation in b2World are created automatically when a b2Fixture
 3 /// is created. Shapes may encapsulate a one or more child shapes.
 4
 5 class b2Shape
 6 {
 7 public:
 8
 9         enum Type
10         {
11                e_circle = 0,   //圆形
12                e_edge = 1,     //边界
13                e_polygon = 2,  //自定义
14                e_chain = 3,
15                e_typeCount = 4
16         };
17         //余下部分省略
18 };

View Code

转载于:https://www.cnblogs.com/geore/p/5799801.html

Cocos2d Box2D之简介相关推荐

  1. cocos2d, Box2D

    http://antkillerfarm.github.io/ 安装 cocos2d和cocos2d-x虽然都是一家的产品,但前者是用python写的,而后者是用C++写的. 我试着在windows下 ...

  2. Cocos2d Box2D之浮动刚体

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

  3. Cocos2d-x 结合Box2D开发Android游戏配置方法

    cocos2d 游戏引擎和 Box2D 物理引擎都是什么,干什么用的,这里我就不多废话了.这里主要讲的是基于 C++ 的 cocos2d-x 在 Android 系统上开发游戏时如何与 Box2D 物 ...

  4. Cocos2d-x高级开发教程:制作自己的《捕鱼达人》

    <Cocos2d-x高级开发教程:制作自己的<捕鱼达人>> 基本信息 作者: 火烈鸟网络科技 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115317964 ...

  5. cocos2d-x游戏开发基础与实战 经典视频教程

    cocos2d-x游戏开发基础与实战 经典视频教程 cocos2d-x游戏开发工资高吗? 精通C/C++,熟练掌握Cocos2d-x引擎及其Cocos2d-x引擎周边开发工具,了解游戏开发常用的工具和 ...

  6. 某网校之Cocos2d-x视频教程

    下载进群: 377215114 Cocos2d-x 高级开发教程 -制作自己的<捕鱼达人>! 本课程以<捕鱼达人>游戏为案例,全面系统地讲解了Cocos2d-x的功能与特性,  ...

  7. iOS游戏开发 几个有利工具

    2019独角兽企业重金招聘Python工程师标准>>> iOS游戏开发 几个有利工具 本文介绍的是iOS游戏开发 几个有利工具,为友们介绍几款开发工具,游戏爱好者记住了!先来看内容. ...

  8. [每日100问][2011-9-06]iphone开发笔记,今天你肿了么

    [url=http://www.buildapp.net/iphone/show.asp?id=5700]怎么让view保持不动,实现层次布局[/url] [url=http://www.builda ...

  9. iPhone 开发中心 论坛 与 视频

    苹果开发者联盟 - iPhone 开发中心  http://www.apple.com.cn/developer/iphone/  http://developer.apple.com/devcent ...

最新文章

  1. 作为程序员,要取得非凡成就需要记住的15件事。
  2. 【操作系统】实验二 作业调度模拟程序
  3. 工智能遇上银行反欺诈,到底能帮什么忙
  4. 想要成为JAVA高手的25个学习目标
  5. TinkPHP框架学习-01基本知识
  6. MoveIt简单编程
  7. 程序员需不需要数学知识?
  8. Figma常用快捷键(Mac版)
  9. windows10系统纯净版下载地址
  10. 计算机凭证打印格式设置,打印凭证怎么设置纸张
  11. Mybaits-Plus Invalid bound statement (not found) 问题
  12. matlab MinGW-w64 C/C++ Compiler 的配置(附百度云下载资源)
  13. python打开qq并登录_使用Python进行QQ批量登录的实例代码
  14. css3扭蛋机,微信小程序扭蛋抽奖机css3动画实现详解.pdf
  15. USB数据线厂家加工生产流程
  16. 【nacos】springboot @Value @NacosValue 使用时可能无效
  17. python实训名片管理程序_python实现名片管理系统
  18. leetcode No7. Reverse Integer
  19. Google账户设置
  20. 《孙子兵法》十三篇注译(3--作战篇)

热门文章

  1. java静态类_Java静态类
  2. angularjs绑定属性_AngularJS隔离范围绑定表达式教程
  3. Java中的Flyweight设计模式
  4. ejb 2.1 jboss_带有Eclipse IDE,EJB Project和JBoss 6.0 AS的JMS 1.1生产者和使用者示例
  5. 使用zk可以实现Master选举,实现原理是什么?
  6. Letter Combinations of a Phone Number
  7. VLOOKUP函数返回查询值左侧的数据
  8. ML————朴素贝叶斯原理和SKlearn相关库
  9. 详细解析RxAndroid的使用方式
  10. UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews