瘸腿蛤蟆笔记41-cocos2d-x-3.2 Box2d物理引擎 持续施加力

瘸腿蛤蟆原创笔记,欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/39189821

源码下载: http://download.csdn.net/detail/notbaron/7893281

上篇回顾

本篇名言:我们的生活似乎在代替我们过日子,生活本身具有的奇异冲力,把我们带得晕头转向;到最后,我们会感觉对生命一点选择也没有,丝毫无法作主。 [索甲仁波切]

上篇中,蛤蟆学习了Box2d物理引擎中的自由落体实现,接下去蛤蟆继续学习其他的Box2d引擎的使用。这次我们使用Box2d物理引擎来学习持续施加力   。

        

理论介绍

         本次的理论知识基本没有。咱就跳过吧。

 

具体步骤

        

1、修改RayCastNew的构造函数如下

RayCastNew()

         {

 

                            b2Vec2 gravity;

                            gravity.Set(0.0f,0.0f);

                            m_world= new b2World(gravity);

                            b2BodyDef bd;

                            bd.position.Set(0.0f,0.0f);

                            b2Body* body = m_world->CreateBody(&bd);

 

                            b2EdgeShape edge;

 

                            edge.Set(b2Vec2(-10.0f, 0.0f),b2Vec2(10.0f, 0.0f));

                            body->CreateFixture(&edge,0.0f);

 

                            b2PolygonShape shape;

                            shape.SetAsBox(0.2f,1.0f, b2Vec2(0.5f, 1.0f), 0.0f);

                            body->CreateFixture(&shape,0.0f);

                   }

                   {

                            b2BodyDef bd;

                            bd.type= b2_dynamicBody;

                            bd.position.Set(0.0f,20.0f);

                            //bd.angle = 0.1f;

 

                            b2PolygonShape shape;

                            shape.SetAsBox(2.0f,0.1f);

 

                            m_body= m_world->CreateBody(&bd);

                            m_body->CreateFixture(&shape,1.0f);

                            m_angularVelocity =10.0f; //这个角速度可以手动改变

                            m_body->SetLinearVelocity(b2Vec2(0.0f, -100.0f));

                            m_body->SetAngularVelocity(m_angularVelocity);

                   }

 

                   GLESDebugDraw * _debugDraw =newGLESDebugDraw(1.0);//定义GLESDebugDraw类,用于画底面

                   m_world->SetDebugDraw(_debugDraw);//注册GLESDebugDraw

                   uint32 flags = 0;

                   flags+=b2Draw::e_shapeBit;//设置需要画的内容           

 

                   flags+= b2Draw::e_jointBit;

                   //flags += b2Draw::e_centerOfMassBit;

 

                   m_debugDraw.SetFlags(flags);

 

                   _debugDraw->SetFlags(flags);//加载需要画内容的位

                   m_world->DrawDebugData();//画出地面Body.

         }

 

代码解释

        

1、构造函数介绍

RayCastNew()

         {

                            b2Vec2 gravity;

                            gravity.Set(0.0f,0.0f);

                            m_world= new b2World(gravity); //通过一个重力向量,创建物理世界

                            b2BodyDef bd; //创建BODY定义的变量

                            bd.position.Set(0.0f,0.0f);//设置Body的位置

                            b2Body* body = m_world->CreateBody(&bd);//通过bd变量,创建一个body,这是一个静态的body.

                            b2EdgeShape edge; //定义一个临界的shape

 

                            edge.Set(b2Vec2(-10.0f, 0.0f),b2Vec2(10.0f, 0.0f)); //设置Shape为一条直线。

                            body->CreateFixture(&edge,0.0f);//绑定Body和shape

 

                            b2PolygonShape shape; //定义一个多边形的shape

                            shape.SetAsBox(0.2f,1.0f, b2Vec2(0.5f, 1.0f), 0.0f); //设置shape为长方形,长宽为2和1,中心为(0.5f,1.0f).

                            body->CreateFixture(&shape,0.0f); //绑定Body和shape

                   }

 

                   {

                            b2BodyDef bd; //创建BODY定义的变量

                            bd.type= b2_dynamicBody;//定义Body为动态变量

                            bd.position.Set(0.0f,20.0f); //设置Body的位置

                            //bd.angle = 0.1f;

 

                            b2PolygonShape shape; //定义一个多边形的shape

                            shape.SetAsBox(2.0f,0.1f); //设置shape为长方形,长宽为2和0.1

                            m_body= m_world->CreateBody(&bd); //通过bd变量,创建一个body

                            m_body->CreateFixture(&shape,1.0f); //绑定Body和shape

 

                           

                            m_angularVelocity = 10.0f; //设定浮点变量值,就是角速度

                            m_body->SetLinearVelocity(b2Vec2(0.0f, -100.0f)); //设定线速度值

                            m_body->SetAngularVelocity(m_angularVelocity); //通过浮点变量,设定角速度值

                   }

                   GLESDebugDraw * _debugDraw =newGLESDebugDraw(1.0);//定义GLESDebugDraw类,用于画底面

                   m_world->SetDebugDraw(_debugDraw);//注册GLESDebugDraw

                   uint32 flags = 0;

                   flags+=b2Draw::e_shapeBit;//设置需要画的内容           

                   flags+= b2Draw::e_jointBit;

                   //flags += b2Draw::e_centerOfMassBit;

                   m_debugDraw.SetFlags(flags);

                   _debugDraw->SetFlags(flags);//加载需要画内容的位

                   m_world->DrawDebugData();//画出地面Body.

         }

你可能感兴趣的:(瘸腿蛤蟆笔记41-cocos2d-x-3.2 Box2d物理引擎 持续施加力)