cocos2d-x 3.0学习-使用Physicals创建物理世界

一、创建物理世界

在cocos2d-x 3.0版本中,封装了方便模拟物理世界操作的一系列physicals类,在这之前一般是将box2d或者chipmunk集成到cocos2d-x中来。而现在,就方便多了,到底多简单?看看就知道。接下来就来实现一个简单的物理世界吧。

二、简单的例子 - hello Physicals world

首先创建一个场景:

PhyScene.h

  
  
  1. #ifndef __PHY_SCENE_H__
  2. #define __PHY_SCENE_H__
  3.  
  4.  
  5. #include "cocos2d.h"
  6.  
  7. using namespace cocos2d;
  8.  
  9. class PhySceneDemo:public Layer{
  10. public:
  11. virtual bool init();
  12. static Scene* scene();
  13. CREATE_FUNC(PhySceneDemo);
  14. void setPhyWorld(PhysicsWorld *phyworld){this->m_world=phyworld;}
  15. void PhySceneDemo::onTouchesEnded(const std::vector<Touch*>& touches, Event *event);
  16.  
  17. private:
  18. PhysicsWorld *m_world;
  19.  
  20. };
  21.  
  22. #endif

PhyScene.cpp

  
  
  1. #include "PhyScene.h"
  2.  
  3. Scene* PhySceneDemo::scene(){
  4. auto scene = Scene::createWithPhysics();
  5. auto layer = PhySceneDemo::create();
  6. layer->setPhyWorld(scene->getPhysicsWorld());
  7. scene->addChild(layer);
  8. return scene;
  9. }
  10.  
  11. bool PhySceneDemo::init(){
  12.  
  13. if(!Layer::init()){
  14. return false;
  15. }
  16.  
  17. Size visiableSize = Director::getInstance()->getVisibleSize();
  18. Point point = Director::getInstance()->getVisibleOrigin();
  19. edge = Sprite::create();
  20.  
  21. auto body = PhysicsBody::createEdgeBox(visiableSize,PHYSICSBODY_MATERIAL_DEFAULT,3);
  22. edge->setPosition(Point(visiableSize.width/2,visiableSize.height/2));
  23. edge->setPhysicsBody(body);
  24. this->addChild(edge);
  25. edge->setTag(0);
  26. this->setTouchEnabled(true);
  27.  
  28. return true;
  29. }
  30.  
  31. void PhySceneDemo::onTouchesEnded(const std::vector<Touch*>& touches, Event *event){
  32. for(Touch *touch:touches){
  33. auto location = touch->getLocation();
  34. Sprite *sprite = Sprite::create("hero_00.png");
  35. sprite->setPosition(location);
  36. sprite->setTag(1);
  37. auto sbody = PhysicsBody::createBox(Size(31,32));
  38. sprite->setPhysicsBody(sbody);
  39.  
  40. this->addChild(sprite);
  41. }
  42.  
  43. }

最后修改AppDelegate.cpp文件,将启动Scene设置成PhySceneDemo:

  
  
  1. auto scene = PhySceneDemo::scene();
  2. // run
  3. director->runWithScene(scene);

通过以上代码便完成了具有物理效果的一个物理场景,是不是非常简单,最后看下运行效果,然后我们再解释关键代码的意思。 

三、关键代码讲解

相信大家都看到上图的效果了,那么现在来具体讲解下前面程序中,是如何创建一个物理世界的。 首先还是创建了一个场景叫做PhyScene.h的头文件,其中大部分与普通场景的创建类似,比如自带的HelloWorld场景。只不过新增了一个PhysicsWorld *m_world 的私有成员变量和一个void setPhyWorld(PhysicsWorld *phyworld){this->m_world=phyworld;}成员函数。由于PhySceneDemo类其实是一个继承Layer的类,本身是没有PhysicsWorld的,PhysicsWorld是在Scene类中的,上面新增的这两个就是将Scene的PhysicsWorld传递到Layer中,是得Layer具有物理世界的性质。具体传递可以看PhyScene.cpp的scene成员函数的实现,里面有

  
  
  1. layer->setPhyWorld(scene->getPhysicsWorld());

这样,Layer中就有了PhysicsWorld,然后可以让添加到Layer中的Sprite等节点具有物理特效。具体来看init实现中的:

  
  
  1. edge = Sprite::create();
  2. auto body = PhysicsBody::createEdgeBox(visiableSize,PHYSICSBODY_MATERIAL_DEFAULT,3);
  3. edge->setPosition(Point(visiableSize.width/2,visiableSize.height/2));
  4. edge->setPhysicsBody(body);
  5. this->addChild(edge);

关键创建一个PhysicsBody对象,但是我们看到这里是用createEdgeBox创建的。而后面的onTouchesEnded函数中的又是使用createBox函数创建的,其实还有更多的创建方式。虽然都是返回PhysicsBody对象,但是效果不一样。举例说明前者,我们进入createEdgeBox函数实现可以看到:

  
  
  1. PhysicsBody* body = new PhysicsBody();
  2. if (body && body->init())
  3. {
  4. body->addShape(PhysicsShapeEdgeBox::create(size, material, border, offset));
  5. body->_dynamic = false;
  6. body->autorelease();
  7. return body;
  8. }

其实在创建PhysicsBody对象的同时,会给他赋予Shape属性和其它一些属性,这里是一个PhysicsShapeEdgeBox,从名称可以看到是一个边缘盒子模型(物理世界的边缘)。最后设置精灵对象的PhysicsBody属性为之前的body对象,这样这个精灵就成为了一个物理世界的精灵。

总结

简单介绍了如何使用texturepacker并在cocos2d-x中创建精灵动画。

##文档信息

 2014-06-15 11:50:00

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值