首页 > 代码库 > 瘸腿蛤蟆笔记42-cocos2d-x-3.2 Box2d物理引擎Motor Joint

瘸腿蛤蟆笔记42-cocos2d-x-3.2 Box2d物理引擎Motor Joint

瘸腿蛤蟆原创笔记,欢迎转载,转载请标明出处:

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

上篇回顾

本篇名言:"战士旅行者"而言,选择其实不是去选择,而是优雅地接受"无限"的邀请。[唐望]

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

        

理论介绍

         本次的理论知识, 咱们回顾下MotorJoint关节即可。

         MotorJoint关节是通过设置位置和旋转偏移来实现物体运行的。我可以设置最大的力和扭矩来使Body达到目标位置。如果物体被停住了,那么接触力按比例达到最大力和力矩。

 

具体步骤

        

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

         RayCastNew()

         {      

                   b2Vec2 gravity;

                   gravity.Set(0.0f,0.0f);

                   m_world= new b2World(gravity);

                   m_mouseJoint= nullptr;

                   b2BodyDef bodyDef;               

                   m_groundBody= m_world->CreateBody(&bodyDef);

                   b2Body* ground = NULL;

                   {

                            b2BodyDef bd;

                            ground= m_world->CreateBody(&bd);

                            b2EdgeShape shape;

                            shape.Set(b2Vec2(-20.0f, 0.0f),b2Vec2(20.0f, 0.0f));

                            b2FixtureDef fd;

                            fd.shape= &shape;

                            ground->CreateFixture(&fd);

                   }

 

                   {

                            b2BodyDef bd;

                            bd.type= b2_dynamicBody;

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

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

                            b2PolygonShape shape;

                            shape.SetAsBox(2.0f,0.5f);

                            b2FixtureDef fd;

                            fd.shape= &shape;

                            fd.friction= 0.6f;

                            fd.density= 2.0f;

                            body->CreateFixture(&fd);

                            b2MotorJointDef mjd;

                            mjd.Initialize(ground,body);

                            mjd.maxForce= 1000.0f;

                            mjd.maxTorque= 1000.0f;

                            m_joint= (b2MotorJoint*)m_world->CreateJoint(&mjd);

                   }

                   GLESDebugDraw * _debugDraw =newGLESDebugDraw(1.0);

                   m_world->SetDebugDraw(_debugDraw);

                   uint32 flags = 0;

                   flags+=b2Draw::e_shapeBit;

                   flags+= b2Draw::e_jointBit;

                   _debugDraw->SetFlags(flags);

                   m_world->DrawDebugData();

         }

 

代码解释       

1、 构造函数介绍

其他部分,蛤蟆就不一一记录了,因为和自由落体代码是一致的。

         RayCastNew()

         {      

                   b2Vec2 gravity;

                   gravity.Set(0.0f,0.0f);

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

                   m_mouseJoint= nullptr; //鼠标关节变量,初始化为0

                   b2BodyDef bodyDef;//Body定义变量            

                   m_groundBody= m_world->CreateBody(&bodyDef); //创建一个默认Body

                   b2Body* ground = NULL;//地面 Body

                   {

                            b2BodyDef bd;// Body定义变量

                            ground= m_world->CreateBody(&bd);//创建一个默认的地面

                            b2EdgeShape shape;//定义一个边界shape

                            shape.Set(b2Vec2(-20.0f, 0.0f),b2Vec2(20.0f, 0.0f));//设置shape为一个直线

                            b2FixtureDef fd;//定义一个Fixture

                            fd.shape= &shape;//设置fixture的shape为刚才的边界shape.

                            ground->CreateFixture(&fd);//通过Fixture关联底面和shape.

                   }

                   {

                            b2BodyDef bd; // Body定义变量

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

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

                            b2Body* body = m_world->CreateBody(&bd);//创建一个动态的Body

 

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

                            shape.SetAsBox(2.0f,0.5f);//设置shape的形状

                            b2FixtureDef fd;//定义Fixture 变量

                            fd.shape= &shape;//定义Fixture变量中的shape

                            fd.friction= 0.6f;//定义摩擦力

                            fd.density= 2.0f;//定义密度

                            body->CreateFixture(&fd);//通过Fixture绑定body和shape

 

                            b2MotorJointDef mjd;//定义一个MotorJoint

                            mjd.Initialize(ground,body);//初始化这个Joint为groud和boyd

                            mjd.maxForce= 1000.0f;//设置最大力为1000

                            mjd.maxTorque= 1000.0f;//设置最大力矩为1000

                            m_joint= (b2MotorJoint*)m_world->CreateJoint(&mjd);//通过定义创建MotroJoint

                   }

 

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

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

                   uint32 flags = 0;

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

                   flags+= b2Draw::e_jointBit;

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

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

 

         }

瘸腿蛤蟆笔记42-cocos2d-x-3.2 Box2d物理引擎Motor Joint