COCOS2DX,cocostudio::ColliderDetector 简单介绍 骨骼动画绑定碰撞区域进行碰撞检测

COCOS2DX,cocostudio::ColliderDetector 简单介绍 骨骼动画绑定碰撞区域进行碰撞检测,第1张

概述/ #ifndef __CCCOLLIDERDETECTOR_H__ #define __CCCOLLIDERDETECTOR_H__ #include "cocostudio/CCArmatureDefine.h" #include "cocostudio/CCDatas.h" #ifndef PT_RATIO #define PT_RATIO 32 #endif #if ENA
/

#ifndef __CCColLIDERDETECTOR_H__

#define __CCColLIDERDETECTOR_H__

#include "cocostudio/CCArmaturedefine.h"

#include "cocostudio/Ccdatas.h"

#ifndef PT_RATIO

#define PT_RATIO 32

#endif

#if ENABLE_PHYSICS_CHIPMUNK_DETECT

#include "chipmunk.h"

#elif ENABLE_PHYSICS_Box2D_DETECT

#include "Box2D/Box2D.h"

#endif

namespace cocostudio {

class Bone;

/**

* @Js NA

* @lua NA

*/

class CollIDerFilter

{

public:

virtual ~CollIDerFilter() { }

#if ENABLE_PHYSICS_Box2D_DETECT

public:

CollIDerFilter(uint16 categoryBits = 0x0001,uint16 maskBits = 0xFFFF,int16 groupIndex = 0);

voID updateShape(b2Fixture *fixture);

virtual voID setcategoryBits(uint16 categoryBits) { _categoryBits = categoryBits; }

virtual uint16 getcategoryBits() const { return _categoryBits; }

virtual voID setMaskBits(uint16 maskBits) { _maskBits = maskBits; }

virtual uint16 getMaskBits() const { return _maskBits; }

virtual voID setGroupIndex(int16 groupIndex) { _groupIndex = groupIndex; }

virtual int16 getGroupIndex() const { return _groupIndex; }

protected:

uint16 _categoryBits;

uint16 _maskBits;

int16 _groupIndex;

#elif ENABLE_PHYSICS_CHIPMUNK_DETECT

public:

CollIDerFilter(cpCollisionType collisionType = 0,cpGroup group = 0);

voID updateShape(cpshape *shape);

virtual voID setCollisionType(cpCollisionType collisionType) { _collisionType = collisionType; }

virtual cpCollisionType getCollisionType() const { return _collisionType; }

virtual voID setGroup(cpGroup group) { _group = group; }

virtual cpGroup getGroup() const { return _group; }

protected:

cpCollisionType _collisionType;

cpGroup _group;

#endif

};

//碰撞class CollIDerBody : public cocos2d::Ref{public:  CollIDerBody(ContourData *contourData);  ~CollIDerBody();  inline ContourData *getContourData() { return _contourData; }#if ENABLE_PHYSICS_Box2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT   voID setCollIDerFilter(CollIDerFilter *filter);  CollIDerFilter *getCollIDerFilter();#endif#if ENABLE_PHYSICS_Box2D_DETECT  virtual voID setB2Fixture(b2Fixture *fixture) { _fixture = fixture; }  virtual b2Fixture *getB2Fixture() const { return _fixture; }#elif ENABLE_PHYSICS_CHIPMUNK_DETECT  virtual voID setShape(cpshape *shape) { _shape = shape; }  virtual cpshape *getShape() const { return _shape; }#elif ENABLE_PHYSICS_SAVE_CALculaTED_VERTEX  virtual const std::vector<cocos2d::Point> &getCalculatedVertexList() const { return _calculatedVertexList; }#endifprivate:#if ENABLE_PHYSICS_Box2D_DETECT  b2Fixture *_fixture;  CollIDerFilter *_filter;#elif ENABLE_PHYSICS_CHIPMUNK_DETECT  cpshape *_shape;  CollIDerFilter *_filter;#elif ENABLE_PHYSICS_SAVE_CALculaTED_VERTEX  vector<cocos2d::Point> _calculatedVertexList;//所有顶点#endif  ContourData *_contourData;//轮廓数据  frIEnd class CollIDerDetector;};/* *  @brIEf  ContourSprite used to draw the contour of the display *  @Js NA *  @lua NA */class CollIDerDetector : public cocos2d::Ref{public:  static CollIDerDetector *create();  static CollIDerDetector *create(Bone *bone);public:  /**  * @Js ctor  */  CollIDerDetector();  /**  * @Js NA  * @lua NA  */  ~CollIDerDetector(voID);  virtual bool init();  virtual bool init(Bone *bone);  voID addContourData(ContourData *contourData);//手动添加 一个 轮廓顶点信息数据  voID addContourDataList(cocos2d::Vector<ContourData*> &contourDataList);//手动添加 一组 轮廓顶点信息数据  voID removeContourData(ContourData *contourData);//手动移除 一个 轮廓顶点信息数据  voID removeAll();//手动添加 所有 轮廓顶点信息数据  voID updatetransform(kmMat4 &t);//旋转更新  voID setActive(bool active);//设置激活状态  bool getActive();  const cocos2d::Vector<CollIDerBody*>& getCollIDerBodyList();//得到所有轮廓框信息#if ENABLE_PHYSICS_Box2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT   virtual voID setCollIDerFilter(CollIDerFilter *filter);  virtual CollIDerFilter *getCollIDerFilter();#endif  virtual voID setBone(Bone *bone) { _bone = bone; }  virtual Bone *getBone() const { return _bone; }#if ENABLE_PHYSICS_Box2D_DETECT  virtual voID setbody(b2Body *body);  virtual b2Body *getbody() const;#elif ENABLE_PHYSICS_CHIPMUNK_DETECT  virtual voID setbody(cpBody *body);  virtual cpBody *getbody() const;#endif protected:  cocos2d::Vector<CollIDerBody*> _collIDerBodyList;//碰撞body  Bone *_bone;//骨骼#if ENABLE_PHYSICS_Box2D_DETECT  b2Body *_body;  CollIDerFilter *_filter;#elif ENABLE_PHYSICS_CHIPMUNK_DETECT  cpBody *_body;  CollIDerFilter *_filter;#endifprotected:  bool _active;//是否激活};}#endif /*__CCColLIDERDETECTOR_H__*/
class TestCollIDerDetector : public ArmatureTestLayer{public:  ~TestCollIDerDetector();    virtual voID onEnter() overrIDe;  virtual std::string Title() const overrIDe;  virtual voID update(float delta);  virtual voID draw(Renderer *renderer,const kmMat4 &transform,bool transformUpdated) overrIDe;  voID onDraw(const kmMat4 &transform,bool transformUpdated);    voID onFrameEvent(cocostudio::Bone *bone,const std::string& evt,int originFrameIndex,int currentFrameIndex);    voID initWorld() {};  cocostudio::Armature *armature;  cocostudio::Armature *armature2;    CustomCommand _customCommand; //new render needed this for drawing primitives  cocos2d::Sprite *bullet;};
voID TestCollIDerDetector::update(float delta){  armature2->setVisible(true);////	CCNode中两个个方法方法://	getContentSize用来获得节点原始大小。返回CGSize类型//	getBoundingBox获得节点当前大小,即如果经过缩放那么就是缩放后的大小。返回CGRect类型。//	有个问题最近才遇到,父精灵进行缩放处理,会对子精灵进行标记(boolean值),在实际绘制过程中会影响子精灵显示大小,但并不改变子精灵的getBoundingBox所获得的值,也就是只有直接setscale才会影响getBoundingBox数值。//	CCSprite中有个方法://	getTextureRect返回精灵纹理大小,返回CGRect类型,并且是原始纹理大小,无关缩放。在一般情况下和getContentSize作用一样,但如果用TP处理过,还回值是实际纹理大小,留白部分会去除。这个在碰撞检测过程中经常用到。  Rect rect = bullet->getBoundingBox();  // This code is just telling how to get the vertex.  // For a more accurate collIDer detection,you need to implemente yourself.  const Map<std::string,Bone*>& map = armature2->getBoneDic();//得到骨骼字典  for(const auto& element : map)//遍历字典  {    Bone *bone = element.second;    CollIDerDetector *detector = bone->getCollIDerDetector();    if (!detector)//如否没有碰撞 跳过 继续遍历      continue;    //得到碰撞框列表    const cocos2d::Vector<CollIDerBody*>& bodyList = detector->getCollIDerBodyList();    for (const auto& object : bodyList)    {      //得到一个碰撞框      CollIDerBody *body = static_cast<CollIDerBody*>(object);      //一个碰撞框包含的所有顶点      const vector<Point> &vertexList = body->getCalculatedVertexList();      float minx = 0,miny = 0;       int length = (int)vertexList.size();//顶点个数      for (int i = 0; i<length; i++)      {        Point vertex = vertexList.at(i);        if (i == 0)        {          minx = maxx = vertex.x;//起点和终点 是 第一个点          miny = maxy = vertex.y;        }        else//遍历所有点 得到4个最值        {          minx = vertex.x < minx ? vertex.x : minx;          miny = vertex.y < miny ? vertex.y : miny;          maxx = vertex.x > maxx ? vertex.x : maxx;          maxy = vertex.y > maxy ? vertex.y : maxy;        }
}      Rect temp = Rect(minx,miny,maxx - minx,maxy - miny);//组成一个包含所有顶点的矩形      if (temp.intersectsRect(rect))      {        //子d矩形在碰撞区域内 碰撞框不可见        armature2->setVisible(false);      }    }  }}voID TestCollIDerDetector::draw(Renderer *renderer,bool transformUpdated){  _customCommand.init(_globalZOrder);  _customCommand.func = CC_CALLBACK_0(TestCollIDerDetector::onDraw,this,transform,transformUpdated);  renderer->addCommand(&_customCommand);}voID TestCollIDerDetector::onDraw(const kmMat4 &transform,bool transformUpdated){  kmGLPushmatrix();  kmGLLoadMatrix(&transform);    armature2->drawContour();    kmGLPopMatrix();}
TestCollIDerDetector::~TestCollIDerDetector(){}voID TestCollIDerDetector::onEnter(){  ArmatureTestLayer::onEnter();  scheduleUpdate();  armature = Armature::create("Cowboy");  armature->getAnimation()->play("FireWithoutBullet");  armature->getAnimation()->setSpeedScale(0.2f);  armature->setScaleX(-2f);  armature->setScaleY(2f);  armature->setposition(Point(VisibleRect::left().x + 70,VisibleRect::left().y));  /* * Set armature's frame event callback function * To disconnect this event,just setFrameEventCallFunc(nullptr); */  armature->getAnimation()->setFrameEventCallFunc(CC_CALLBACK_0(TestCollIDerDetector::onFrameEvent,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4));  addChild(armature);  armature2 = Armature::create("Cowboy");  armature2->getAnimation()->play("Walk");  armature2->setScaleX(-2f);  armature2->setScaleY(2f);  armature2->setposition(Point(VisibleRect::right().x - 60,VisibleRect::left().y));  addChild(armature2);#if ENABLE_PHYSICS_Box2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT  bullet = cocos2d::extension::Physicssprite::createWithSpriteFramename("25.png");#elif ENABLE_PHYSICS_SAVE_CALculaTED_VERTEX  bullet = Sprite::createWithSpriteFramename(#endif  addChild(bullet);  initWorld();}std::string TestCollIDerDetector::Title() const{  return "Test CollIDer Detector";}voID TestCollIDerDetector::onFrameEvent(Bone *bone,const std::string& evt,int currentFrameIndex){  cclOG("(%s) emit a frame event (%s) at frame index (%d).",bone->getname().c_str(),evt.c_str(),currentFrameIndex);  /* * originFrameIndex is the frame index editted in Action Editor * currentFrameIndex is the current index animation played to * frame event may be delay emit,so originFrameIndex may be different from currentFrameIndex. */  Point p = armature->getBone("Layer126")->getdisplayRenderNode()->convertToWorldspaceAR(Point(0));  bullet->setposition(Point(p.x + y));  bullet->stopAllActions();  bullet->runAction(CCMoveBy::create(1.5f,Point(350,153)">0)));}
参考原地址:http://www.tuicool.com/articles/bUz6BrM
总结

以上是内存溢出为你收集整理的COCOS2DX,cocostudio::ColliderDetector 简单介绍 骨骼动画绑定碰撞区域进行碰撞检测全部内容,希望文章能够帮你解决COCOS2DX,cocostudio::ColliderDetector 简单介绍 骨骼动画绑定碰撞区域进行碰撞检测所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1034982.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存