cocos2dx 碰撞检测

//用平台封装的函数进行碰撞检测 

//将砖块精灵放入一个数组(容器)之中然后一 一进行碰撞检测

bool MyMap::isCollideWithHeroX()

{

 

 for(auto x:Road)

 {

  if(x->boundingBox().intersectsRect(myHero->getSprite()->boundingBox())) //boundingBox().intersectsRect()这个函数获取的是精灵的标准矩形,如果要更精确的检测可以自己获取一个不规则多边形进行检测

   {

    return  true;

  }

 }

 return false;

 }

//网上学习

检测矩形与矩形碰撞的函数:

bool intersectsRect(const CCRect &rect) 使用方法: rect1.intersectsRect(rect2); 
检测点与矩形碰撞的函数: bool containsPoint(const CCPoint &point) 使用方法: rect.containsPoint(point); 
下面重点说矩形与圆形的碰撞检测: PS:先说一下cocos2d-x中的矩形Rect,Rect的构造有四个参数,x, y, width, height,如下图所示 

//

//  CGCircle.h

//  HelloCpp

//

//  Created by Mike on 14-1-16.

//

//

 

#ifndef __HelloCpp__CGCircle__

#define __HelloCpp__CGCircle__

 

#include <iostream>

#include "cocos2d.h"

using namespace std;

using namespace cocos2d;

 

class CGCircle

{

private:

    //float _radius; //半径

    //Point _position; //圆心

     

public:

    CC_SYNTHESIZE(float, _radius, Radius);

    CC_SYNTHESIZE(Point, _position, Position);

     

    CGCircle(float radius, Point position): _radius(radius), _position(position){};

     

    bool isContainRect(Rect rect);

};

 

#endif /* defined(__HelloCpp__CGCircle__) */</iostream>



//  CGCircle.cpp

//  HelloCpp

//

//  Created by Mike on 14-1-16.

//

//

 

#include "CGCircle.h"

 

 

bool CGCircle::isContainRect(Rect rect)

{

    bool _isContian = false;

    Point orign = _position;  //圆心,新建坐标系的原点

     

    //圆的外切正方形  Rect(x, y, width, heigth) x,y是原点,左下顶点

    Rect square = Rect::Rect(_position.x - _radius, _position.y - _radius,

                             2 * _radius, 2 * _radius);

     

    do {

        Point rectVertex0 = rect.origin; //左下

        Point rectVertex1 = rect.origin + Point(rect.size.width, 0); //右下

        Point rectVertex2 = rect.origin + Point(rect.size.width, rect.size.height); //右上

        Point rectVertex3 = rect.origin + Point(0, rect.size.height); //左上

        Point pos[] = {rectVertex0, rectVertex1, rectVertex2, rectVertex3};

         

        if ((pos[1].x - orign.x) * (pos[3].x - orign.x) > 0 &&

            (pos[1].y - orign.y) * (pos[3].y - orign.y) > 0)

        {

            //右下顶点和左上顶点在同一象限,说明整个矩形位于一个象限内 若各顶点至少有一个在圆内则相交

            for (int i = 0; i < 4; i++) {

                //判断矩形四个顶点是否至少有一个位于圆内

                if (((pos[i].x - orign.x) * (pos[i].x - orign.x)

                     + (pos[i].y - orign.y) * (pos[i].y - orign.y))

                    <= _radius * _radius)

                {

                    goto next;

                }

            }

        }

         

        if (square.intersectsRect(rect)) {

            goto next;

        }

         

        for (int i = 0; i < 4; i++) {

            if (pos[i] == orign) {

                goto next;

            }

        }

         

        break;

         

    next:

        _isContian = true;

    while (0);

     

    return _isContian;

}



Size visibleSize = Director::getInstance()->getVisibleSize();

 

    auto circleS = Sprite::create("circle.png");

    circleS->setPosition(Point(visibleSize.width/2, visibleSize.height/2));

    this->addChild(circleS, 0);

     

    auto rectS = Sprite::create("rect.png");

    rectS->setPosition(Point(circleS->getPositionX() - 100, circleS->getPositionY()));

    this->addChild(rectS, 0);

     

    CGCircle circle0  = CGCircle(circleS->getContentSize().width/2, circleS->getPosition());

    Rect rect0 = Rect(rectS->getPositionX() - rectS->getContentSize().width/2,

                      rectS->getPositionY() - rectS->getContentSize().height/2,

                      rectS->getContentSize().width, rectS->getContentSize().height);

    if (circle0.isContainRect(rect0)) {

        CCLOG("circleS与rectS碰撞了");

    }

    else CCLOG("circleS与rectS未碰撞");

     

     

    float radius = 30.0;

    Point position = Point(300300);

    CGCircle circle = CGCircle(radius, position);

     

    Rect rect1 = Rect(2802901030);

    if (circle.isContainRect(rect1))

    {

        CCLOG("矩形1与圆碰撞了");

    }

     

    Rect rect2 = Rect(2102901030);

    if (! circle.isContainRect(rect2))

    {

        CCLOG("矩形2与圆未碰撞");

    }



\\

你可能感兴趣的:(return,检测,false,精灵,网上)