Here is Evankaka's blog, Welcome to discuss and communicate with you before ~~~~~~
Reprint Please specify the source http://blog.csdn.net/evankaka/article/details/42689689
This article will describe in detail the collision detection principle of heroes and monsters in Cocos2dx, which is actually the clash detection of elves and Elves ha. In this paper, we mainly start with the collision of rectangle, write a function of collision detection, and apply it in the game. On the other hand, when the hero strikes, if the hero and the monster collide, the monster will drop the blood, and when the monster's blood is 0 o'clock, the monster died, before death it will fall on the ground flashing a few. Now, let's go.
Cocos2d-x version: 2.2.5
Engineering Environment: WINDOWS7+VS2010
Open by: Put the project under the project folder in the Cocos2d-x installation directory with VS Open
SOURCE Free Download
First look at the effect:
Catalogue
First, the Spirit Collision detection principle
Second, custom collision detection function
Three, heroes to kill monsters
Iv. Summary of Ideas
first, the Spirit Collision detection principle
Collision Detection Online There are many people speaking, but generally only talk about how to use, also did not specifically talk about the principle, oneself down on the next, found that actually this is really simple.
First, let's take a look at two rectangles, we define two rectangles, 1: Red, Rectangle 2: Black
If we list all of their non-collision situations, then the other is not collision, think of this, I start from this, and then they do not collide with the situation we can be divided into four kinds
Rectangle 1: red; Rectangle 2: Black
1. Rectangle 1 to the left of the rectangle 2, the two do not collide
Conditions of Incorporation: x1+w1*0.5<x2-w2*0.5
2. Rectangle 1 to the right of the rectangle 2, the two do not collide
Conditions of Incorporation:: x1-w1*0.5>x2+w2*0.5
3. Rectangle 1 below the rectangle 2, no collision
Conditions of Incorporation:: y1+h1*0.5<y2-h2*0.5
4. Rectangle 1 above the rectangle 2, no collision
Conditions of Incorporation: y1-h1*0.5>y2+h2*0.5
The above four kinds is all the non-collision situation, and then we make a judgment, in turn, detect the above four cases, once found that there is a situation, return no collision, if four cases are not established, then congratulations, the collision succeeded. Second, custom collision detection function
Collision detection can be used for Elf classes
Sprite1->boundingbox (). Intersectsrect (Sprite1->boundingbox ())
But I this game of heroes and monsters are their own definition of the class, so directly call the above function is a bit of a problem, so I put the previous collision detection principle write a function, you can call directly, do not care what you are to the image.
First, where collision detection is used, include "HelloWorldScene.h"
Defining functions
Rectangular Collision Detection
BOOL Isrectcollision (Ccrect rect1, Ccrect rect2);
Then in its implementation function HelloWorldScene.cpp:
Collision Detection
BOOL Helloworld::isrectcollision (ccrect rect1, Ccrect rect2)
{
float x1 = rect1.origin.x;// Rectangle 1 Center Point of the horizontal axis
float y1 = rect1.origin.y;//Rectangle 1 center Point of the ordinate
float w1 = rect1.size.width;//Rectangle 1 width
float h1 = rect1.size.height;//height of rectangle 1
float x2 = rect2.origin.x;
float y2 = rect2.origin.y;
float W2 = rect2.size.width;
float H2 = rect2.size.height;
if (x1+w1*0.5<x2-w2*0.5)
return false;//Rectangle 1 to the left of the rectangle 2, the two do not collide
else if (x1-w1*0.5>x2+w2*0.5)
return false;//Rectangle 1 to the right of the rectangle 2, the two do not collide
else if (y1+h1*0.5<y2-h2*0.5)
return false;//rectangle 1 below the rectangle 2, the two do not collide
else if ( y1-h1*0.5>y2+h2*0.5)
return false;//Rectangle 1 above Rectangle 2, both without collision
return true;
}
The principle of this code is what we said above, very simple.
Three, heroes to kill monsters
Now we're going to call the two functions, let's take a look at the collision range of heroes and monsters.
(I've made the background transparent, that's what it really is)
(I've made the background transparent, that's what it really is)
So, be careful here. Here is to be careful, it is best not to the whole picture width and height are included;
A simple process for collision detection
And then it's realized.
The Void Helloworld::update (float delta) function is added
if (hero->isattack)//hero is attacking
{
if (!monster1->isdead)//Monsters are not dead
{
if (ABS (Hero->getpositiony ()-monster1->getpositiony ()) <30)//Monsters and heroes should be in a similar level of height, the attack
is effective {
//Detect collision to the monster, here to be careful to subtract some of the border value
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))
{
monster1->hurtanimation ("Monster_hurt", 2, Monster1->monsterdirecton);//Injured}}}
}
Okay, here's a story about the monster injury, the death animation.
And then the previous Monster.h add function
Injured animation
void hurtanimation (const char *name_each,const unsigned int num,bool run_directon);
Injured animation end
void Hurtend ();
Judging whether the injured animated
bool Ishurt;
Death animation
void deadanimation (const char *name_each,const unsigned int num,bool run_directon);
Death animation end
void Deadend ();
Judge whether death
bool Isdead;
Monster death blink end
void Blinkend ();
Then in the implementation function Monster.cpp
Injured animation void monster::hurtanimation (const char *name_each,const unsigned int num,bool run_directon) {if (ishurt| |
Isdead) return; Injury priority if (Isrunning| | Isattack) {m_monstersprite->stopallactions ();//The current sprite stops all animation//restore sprites from the original initialization map This->removechild (M_MONSTERSPRI Te,true);//Remove the original sprite m_monstersprite=ccsprite::create (monster_name);//Restore the original image of the sprite m_monstersprite->setflipx (
Monsterdirecton);
This->addchild (M_monstersprite);
Isrunning=false;
Isattack=false;
} ccanimation* animation = Ccanimation::create ();
for (int i=1;i<=num;i++) {char szname[100] = {0};
sprintf (SzName, "%s%d.png", name_each,i); Animation->addspriteframewithfilename (SzName);
Load the animated frame} animation->setdelayperunit (2.8f/14.0f);
Animation->setrestoreoriginalframe (TRUE); Animation->setloops (1);
Animation Loop 1 times//wrap the animation into an action ccanimate* act=ccanimate::create (animation); Create callback action, end of injury animation call Hurtend () cccallfunc* callfunc=cccallfunc::create (this,callfunc_seLector (Monster::hurtend));
Create a continuous action ccactioninterval* hurtackact=ccsequence::create (Act,callfunc,null);
M_monstersprite->runaction (hurtackact);
Ishurt=true;
}//Injured animation end void Monster::hurtend () {ishurt=false;
Monster_xue->setcurrentprogress (Monster_xue->getcurrentprogress ()-10);
if (monster_xue->getcurrentprogress () ==0) {//play Monster Death animation deadanimation ("Monster_dead", 2,monsterdirecton);
}}//Death animation void Monster::D eadanimation (const char *name_each,const unsigned int num,bool run_directon) {isdead=true;
ccanimation* animation = Ccanimation::create ();
for (int i=1;i<=num;i++) {char szname[100] = {0};
sprintf (SzName, "%s%d.png", name_each,i); Animation->addspriteframewithfilename (SzName);
Load the animated frame} animation->setdelayperunit (2.8f/14.0f);
Animation->setrestoreoriginalframe (TRUE); Animation->setloops (1);
Animation Loop 1 times//wrap the animation into an action ccanimate* act=ccanimate::create (animation); Create a callback action, call Deadact () cccallfunc* Callfunc after the end of death=cccallfunc::create (This,callfunc_selector (Monster::D eadend));
Create a continuous action ccactioninterval* deadact=ccsequence::create (Act,callfunc,null);
M_monstersprite->runaction (deadact); }//Death animation ends void Monster::D eadend () {//Restores the appearance of Death This->removechild (m_monstersprite,true);//Remove the original sprite M_monstersprite
=ccsprite::create ("Monster_dead2.png");//restore the appearance of Death m_monstersprite->setflipx (Monsterdirecton);
This->addchild (M_monstersprite); There is a blood bar if (monster_xue!=null) {if (Monsterdirecton)//Because the Monster Center is not in the center of the picture, so it can only be based on the face of the monster, set the horizontal axis of the blood bar monster_xue->setposition (CCP (M_monstersprite->getpositionx () +60, M_monstersprite->getpositiony ()));//set at the top of the monster else monster_xue->
SetPosition (CCP (M_monstersprite->getpositionx () -60, M_monstersprite->getpositiony ())); }//Monster flashes twice to Die ccblink* blinkact=ccblink::create (3,6);//3 is duration, 6 is the number of flashes//create callback action, after flashing end call Blinkend () cccallfunc* Callfunc
=cccallfunc::create (This,callfunc_selector (monster::blinkend)); Create a continuous action ccactioninterval* DEADACT=CCSEQuence::create (Blinkact,callfunc,null);
M_monstersprite->runaction (deadact); }//flashing end void Monster::blinkend () {This->removeallchildren ();//Remove monsters and blood strips;}
Monster death of a process, in each injury after the blood, immediately detect the current amount of blood, if the blood volume is 0, immediately play death animation, and then play the flashing animation, and then you can remove the monster off ~ ~ it is so simple
Effect:
1, the Monster in Patrol, when the attack did not detect a collision
2, Heroes in attack, detected a collision, the monster injured and lost blood
3, Monster Blood volume of 0, Monster death, and play flashing animation
Iv. Summary of Ideas
Here Collision detection I am anti-other road, and all the possible non-collision is listed, the other is not the collision of it. And then to do their own programming, on the other hand, the monster is injured, the animation of death, as well as flashing, these are very basic, basically the same function, just once you will. It's here to remember that this is a sequential action, just remember that.