圆形碰撞

判断两个圆的圆心距与其半径和,当两个圆的圆心距小于两圆的半径和时,表示发生了碰撞,反之则没有     void onTouchesBegan(const std::vector<Touch*>& touches, Event  *event);
   
void onTouchesMoved(const std::vector<Touch*>& touches, Event  *event);
   
void onTouchesEnded(const std::vector<Touch*>& touches, Event  *event);
private:
    Size size;

   
bool isCollision(Sprite *sp);//检测两个矩形是否碰撞
   
void ChangeLabelContent(bool isCollision);//改变文本内容
   
//
    bool isCircleCollision(Point pos1,float radius1,Point pos2,float radius2);



size = Director::getInstance()->getWinSize();
   
auto sp1 = Sprite::create("r1.png");
    sp1->
setPosition(size/2);
   
//Sprite::addChild(Node *child, int zOrder, int tag)
   
addChild(sp1,0,1);
   
   
auto sp2 = Sprite::create("r2.png");
    sp2->
setPosition(Vec2(size.width*0.8, size.height/2));
   
addChild(sp2,0,2);
   
   
//添加显示碰撞信息
   
auto label = Label::createWithTTF("no collision", "fonts/Marker Felt.ttf", 25);
    label->
setPosition(Vec2(size.width/2, size.height*0.8));
   
addChild(label,1,3);
   
   
   
   
auto listener = EventListenerTouchAllAtOnce::create();
   
   
    listener->
onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
    listener->
onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
    listener->
onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
   
   
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
   
   
return true;
}



void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event  *event){
   
auto touch = static_cast<Touch*>(touches[0]);
   
auto point = touch->getLocation();
 
   
auto sp1 = (Sprite*)this->getChildByTag(1);
    sp1->
setPosition(point);
  
   
ChangeLabelContent(isCollision(sp1));
   

}


void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event  *event){

   
auto touch = static_cast<Touch*>(touches[0]);
   
auto point = touch->getLocation();
   
   
auto sp1 = (Sprite*)this->getChildByTag(1);
    sp1->
setPosition(point);
   
   
ChangeLabelContent(isCollision(sp1));


}

void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event  *event){

}

bool HelloWorld::isCollision(Sprite *sp){
   
bool flag = false;
   
auto sp2 = (Sprite*)this->getChildByTag(2);
   
// 重点
    flag =
isCircleCollision(sp->getPosition(), sp->getContentSize().width/2, sp2->getPosition(), sp2->getContentSize().width/2);



   
return flag;
}

void HelloWorld::ChangeLabelContent(bool isCollision){
   
   
auto label = (Label*)this->getChildByTag(3);
   
if (isCollision) {
        label->
setString("Collision!");
    }else label->setString("no collision!");
}



bool HelloWorld::isCircleCollision(Point pos1,float radius1,Point pos2,float radius2){
   
// 重点
   
if (pos1.getDistance(pos2)>radius1+radius2) {
       
return false;
    }

   
return true;
}


    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值