• 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定义:

     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

  • 相关阅读:
    用正则表达式简单加密(C#为例)
    新浪微博error:redirect_uri_mismatch的解决方法 [
    UITableView延伸:点击cell关闭键盘,加载不同cell,监听里面的textfeild内容改变
    iossharesdk微信登录出错
    关于IOS项目QQ空间授权提示安装最新版本的QQ的解决方法!
    如何解决 错误code signing is required for product type 'xxxxx' in SDK 'iOS 8.2'
    UITableView加载几种不同的cell
    iOS学习小结(一)
    开源中国+soucetree
    获取本机ip地址
  • 原文地址:https://www.cnblogs.com/geore/p/5799801.html
Copyright © 2020-2023  润新知