//用平臺封裝的函數進行碰撞檢測
//將磚塊精靈放入一個數組(容器)之中然後一 一進行碰撞檢測
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(
300
,
300
);
CGCircle circle = CGCircle(radius, position);
Rect rect1 = Rect(
280
,
290
,
10
,
30
);
if
(circle.isContainRect(rect1))
{
CCLOG(
"矩形1與圓碰撞了"
);
}
Rect rect2 = Rect(
210
,
290
,
10
,
30
);
if
(! circle.isContainRect(rect2))
{
CCLOG(
"矩形2與圓未碰撞"
);
}