Cocos2d-x碰撞检测原理与英雄要打死怪物--之游戏开发《赵云要格斗》(7)

这里是Evankaka的博客,欢迎大家前面讨论与交流~~~~~~

        转载请注明出处http://blog.csdn.net/evankaka/article/details/42689689

        本文将详细讲述cocos2dx中英雄与怪物的碰撞检测原理,其实就是精灵和精灵碰撞检测哈。本文主要从矩形碰撞入手,自己编写了一个矩形碰撞检测的函数,并且在游戏中来进行应用。另一方面,当英雄出动攻击后,如果英雄和怪物碰撞到的话,怪物就要掉血,并且当怪物血量为0时,怪物死亡,死亡之前它还会倒在地上闪烁几下。下面,开始吧

cocos2d-x版本:2.2.5

工程环境:windows7+VS2010

打开方式:将工程放在cocos2d-x安装目录下的project文件夹下用VS打开


先来看看效果:



目录

一、精灵碰撞检测原理

二、自定义碰撞检测函数

三、英雄要打死怪物

四、思路总结


一、精灵碰撞检测原理

碰撞检测网上有很多人在讲,但是一般都只讲怎么用,也都没具体的讲讲原理,自己下来就摸索了下,发现其实这个确实很简单。

首先,我们来看看两个矩形,我们定义如下两个矩形,矩形1:红色;矩形2:黑色

 

如果我们把它们所有的不碰撞的情形列出来,那么其它的不就是碰撞的么,想到这一点,我就从这个出发,然后它们不碰撞的情形我们可以分为四种

矩形1:红色;矩形2:黑色

1.矩形1在矩形2左方,两者无碰撞



成立条件:x1+w1*0.5<x2-w2*0.5

2.矩形1在矩形2右方,两者无碰撞


成立条件::x1-w1*0.5>x2+w2*0.5


3.矩形1在矩形2下方,两者无碰撞


成立条件::y1+h1*0.5<y2-h2*0.5

4.矩形1在矩形2上方,两者无碰撞


成立条件:y1-h1*0.5>y2+h2*0.5

上面四种就是所有的不碰撞的情况了,然后我们弄个判断,依次检测上面四种情形,一旦发现有一种情况成立,就返回无碰撞,如果四种情况都不成立,那恭喜你了,碰撞成功了!

二、自定义碰撞检测函数

碰撞检测对于精灵类可以用

sprite1->boundingBox().intersectsRect(sprite1->boundingBox())

只不过我这个游戏中的英雄和怪物都是自己定义的类,所以直接调用上面的函数就出点儿问题,所以自己就把前面碰撞检测的原理写了个函数,可以直接调用了,不用管你是什么对像。

首先,在用到碰撞检测的地方#include "HelloWorldScene.h"

定义函数

//矩形碰撞检测
bool isRectCollision (CCRect rect1, CCRect rect2);


然后在其实现函数里HelloWorldScene.cpp里:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ///碰撞检测  
  2. bool HelloWorld::isRectCollision (CCRect rect1, CCRect rect2)  
  3. {  
  4.     float x1 = rect1.origin.x;//矩形1中心点的横坐标  
  5.     float y1 = rect1.origin.y;//矩形1中心点的纵坐标  
  6.     float w1 = rect1.size.width;//矩形1的宽度  
  7.     float h1 = rect1.size.height;//矩形1的高度  
  8.     float x2 = rect2.origin.x;  
  9.     float y2 = rect2.origin.y;  
  10.     float w2 = rect2.size.width;  
  11.     float h2 = rect2.size.height;  
  12.   
  13.     if (x1+w1*0.5<x2-w2*0.5)    
  14.         return false;//矩形1在矩形2左方,两者无碰撞  
  15.     else if (x1-w1*0.5>x2+w2*0.5)  
  16.         return false;//矩形1在矩形2右方,两者无碰撞  
  17.     else if (y1+h1*0.5<y2-h2*0.5)  
  18.         return false;//矩形1在矩形2下方,两者无碰撞  
  19.     else if (y1-h1*0.5>y2+h2*0.5)  
  20.         return false;//矩形1在矩形2上方,两者无碰撞  
  21.   
  22.     return true;  
  23. }  
这个代码的原理就是我们上面所讲的东西,很简单吧!

三、英雄要打死怪物

现在我们要调用二中的函数,我们先来看看英雄和怪物的碰撞范围吧


   (我把背景弄透明了,实际是这样的)

(我把背景弄透明了,实际是这样的)

所以,这里要注意下。这里就是要小心,最好不要把整个图片的宽度和高度都包含进去;

碰撞检测的一个简单流程




然后就是实现了啦~

void HelloWorld::update(float delta)函数中添加

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if(hero->IsAttack)//英雄正在攻击  
  2.     {  
  3.       if(!monster1->Isdead)//怪物还没死  
  4.       {  
  5.         if(abs(hero->getPositionY()-monster1->getPositionY())<30)//怪物和英雄应该在一个差不多的水平高度上,攻击才有效  
  6.         {  
  7.             //检测是否碰撞到怪物,这里要注意要减去一些边框值  
  8.           if (this->isRectCollision(CCRectMake(hero->getPositionX(), hero->getPositionY(),hero->GetSprite()->getContentSize().width-70, hero->GetSprite()->getContentSize().height-30), CCRectMake(monster1->getPositionX(), monster1->getPositionY(), monster1->GetSprite()->getContentSize().width-30,monster1->GetSprite()->getContentSize().height-20)))   
  9.              {  
  10.                 monster1->HurtAnimation("monster_hurt",2,monster1->MonsterDirecton);//受伤  
  11.              }  
  12.          }  
  13.       }  
  14.     }  


   好了,这里得来讲讲怪物受伤死亡动画了了

接着上一篇讲的Monster.h增加函数

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //受伤动画  
  2. void HurtAnimation(const char *name_each,const unsigned int num,bool run_directon);  
  3. //受伤动画结束  
  4. void HurtEnd();  
  5. //判断是否在受伤动画  
  6. bool IsHurt;  
  7.   
  8. //死亡动画  
  9. void DeadAnimation(const char *name_each,const unsigned int num,bool run_directon);  
  10. //死亡动画结束  
  11. void DeadEnd();  
  12. //判断是否死亡  
  13. bool Isdead;  
  14.   
  15. //怪物死亡闪烁结束  
  16. void BlinkEnd();  
然后在实现函数Monster.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //受伤动画  
  2. void Monster::HurtAnimation(const char *name_each,const unsigned int num,bool run_directon)  
  3. {  
  4.     if(IsHurt||Isdead)  
  5.         return;  
  6.     //受伤优先  
  7.     if(IsRunning||IsAttack)  
  8.     {  
  9.         m_MonsterSprite->stopAllActions();//当前精灵停止所有动画  
  10.        //恢复精灵原来的初始化贴图   
  11.       this->removeChild(m_MonsterSprite,TRUE);//把原来的精灵删除掉  
  12.       m_MonsterSprite=CCSprite::create(Monster_name);//恢复精灵原来的贴图样子  
  13.       m_MonsterSprite->setFlipX(MonsterDirecton);  
  14.       this->addChild(m_MonsterSprite);  
  15.       IsRunning=false;  
  16.       IsAttack=false;  
  17.     }  
  18.       
  19.   
  20.     CCAnimation* animation = CCAnimation::create();    
  21.     forint i=1;i<=num;i++)    
  22.     {    
  23.         char szName[100] = {0};    
  24.         sprintf(szName,"%s%d.png",name_each,i);    
  25.         animation->addSpriteFrameWithFileName(szName); //加载动画的帧    
  26.     }    
  27.     animation->setDelayPerUnit(2.8f/14.0f);    
  28.     animation->setRestoreOriginalFrame(true);    
  29.     animation->setLoops(1); //动画循环1次    
  30.     //将动画包装成一个动作  
  31.     CCAnimate* act=CCAnimate::create(animation);  
  32.     //创建回调动作,受伤动画结束调用HurtEnd()  
  33.     CCCallFunc* callFunc=CCCallFunc::create(this,callfunc_selector(Monster::HurtEnd));  
  34.     //创建连续动作  
  35.     CCActionInterval* hurtackact=CCSequence::create(act,callFunc,NULL);  
  36.   
  37.     m_MonsterSprite->runAction(hurtackact);    
  38.     IsHurt=true;  
  39.   
  40.   
  41. }  
  42. //受伤动画结束  
  43. void Monster::HurtEnd()  
  44. {  
  45.     IsHurt=false;  
  46.     Monster_xue->setCurrentProgress(Monster_xue->getCurrentProgress()-10);  
  47.     if(Monster_xue->getCurrentProgress()==0)  
  48.     {  
  49.         //播放怪物死亡动画  
  50.         DeadAnimation("monster_dead",2,MonsterDirecton);  
  51.     }     
  52. }  
  53. //死亡动画  
  54. void Monster::DeadAnimation(const char *name_each,const unsigned int num,bool run_directon)  
  55. {  
  56.     Isdead=true;  
  57.     CCAnimation* animation = CCAnimation::create();    
  58.     forint i=1;i<=num;i++)    
  59.     {    
  60.         char szName[100] = {0};    
  61.         sprintf(szName,"%s%d.png",name_each,i);    
  62.         animation->addSpriteFrameWithFileName(szName); //加载动画的帧    
  63.     }    
  64.     animation->setDelayPerUnit(2.8f/14.0f);    
  65.     animation->setRestoreOriginalFrame(true);    
  66.     animation->setLoops(1); //动画循环1次    
  67.     //将动画包装成一个动作  
  68.     CCAnimate* act=CCAnimate::create(animation);  
  69.     //创建回调动作,死亡结束后调用deadact()  
  70.     CCCallFunc* callFunc=CCCallFunc::create(this,callfunc_selector(Monster::DeadEnd));  
  71.     //创建连续动作  
  72.     CCActionInterval* deadact=CCSequence::create(act,callFunc,NULL);  
  73.     m_MonsterSprite->runAction(deadact);    
  74.   
  75. }  
  76. //死亡动画结束  
  77. void Monster::DeadEnd()  
  78. {  
  79.     //恢复死亡的样子  
  80.     this->removeChild(m_MonsterSprite,TRUE);//把原来的精灵删除掉  
  81.     m_MonsterSprite=CCSprite::create("monster_dead2.png");//恢复死亡的样子  
  82.     m_MonsterSprite->setFlipX(MonsterDirecton);  
  83.     this->addChild(m_MonsterSprite);  
  84.     //存在血条  
  85.     if(Monster_xue!=NULL)  
  86.     {  
  87.         if(MonsterDirecton)//因为怪物中心不在图片中心,所以只能根据怪物的脸朝向,设置血条的横坐标  
  88.             Monster_xue->setPosition(ccp(m_MonsterSprite->getPositionX()+60, m_MonsterSprite->getPositionY()));//设置在怪物上头    
  89.         else  
  90.             Monster_xue->setPosition(ccp(m_MonsterSprite->getPositionX()-60, m_MonsterSprite->getPositionY()));   
  91.     }  
  92.     //怪物闪两下再死亡  
  93.     CCBlink* blinkact=CCBlink::create(3,6);//3是持续时间,6是闪的次数  
  94.           
  95.     //创建回调动作,闪烁结束后调用BlinkEnd()  
  96.     CCCallFunc* callFunc=CCCallFunc::create(this,callfunc_selector(Monster::BlinkEnd));  
  97.     //创建连续动作  
  98.     CCActionInterval* deadact=CCSequence::create(blinkact,callFunc,NULL);  
  99.     m_MonsterSprite->runAction(deadact);  
  100.   
  101. }  
  102. //闪烁结束  
  103. void Monster::BlinkEnd()  
  104. {  
  105. this->removeAllChildren();//把怪物和血条都删除掉;  
  106. }  

怪物死亡的一个过程,在每次受伤掉血后,立马检测当前血量,如果血量为0,马上播放死亡动画,接着再播放闪烁动画,然后就可以把怪物删除掉了啦~~就这么简单

效果:

1、怪物在巡逻,这时攻击没有检测到碰撞

 

2、英雄在攻击,检测到碰撞,怪物受伤并掉血



3、怪物血量为0,怪物死亡,并播放闪烁动画


四、思路总结

      这里碰撞检测我是反其它道而行,把所有不碰撞的可能都列出来,其它的不就是碰撞的了么?然后再来自己编程,另一方面,怪物就是受伤、死亡的动画,以及闪烁,这些都是很基础的,基本上都是相同的函数,只要用一次你就会了。就是这里要记得这是按顺序的动作,要记得这点就行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值