cocos2dx 碰撞检测

前端之家收集整理的这篇文章主要介绍了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;

classCGCircle

{

private:

//float _radius; //半径

//Point _position; //圆心

publicCC_SYNTHESIZE(float,_radius,Radius);

CC_SYNTHESIZE(Point,_position,Position);

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

bool isContainRect(Rect rect);

};

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



// CGCircle.cpp

"CGCircle.h"

bool CGCircle::isContainRect(Rect rect)

bool _isContian =false;

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

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

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

2* _radius,* _radius);

doPoint rectVertex0 = rect.origin;//左下

Point rectVertex1 = rect.origin + Point(rect.size.width,0);background-color:inherit;">0);//右下

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

Point rectVertex3 = rect.origin + Point(//左上

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

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

(pos[].y - orign.y) * (pos[].y - orign.y) >)

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

for(inti =; i <4; i++) {

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

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

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

<= _radius * _radius)

gotonext;

}

(square.intersectsRect(rect)) {

(pos[i] == orign) {

breaknext:

_isContian =true}while);

return_isContian;



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

auto circleS = Sprite::create("circle.png"circleS->setPosition(Point(visibleSize.width/));

this->addChild(circleS,monospace;font-size:1em;line-height:1.1em;background-color:inherit;">auto rectS = Sprite::create("rect.png"rectS->setPosition(Point(circleS->getPositionX() -100->addChild(rectS,monospace;font-size:1em;line-height:1.1em;background-color:inherit;">CGCircle circle0 = CGCircle(circleS->getContentSize().width/Rect rect0 = Rect(rectS->getPositionX() - rectS->getContentSize().width/rectS->getPositionY() - rectS->getContentSize().height/rectS->getContentSize().width,rectS->getContentSize().height);

(circle0.isContainRect(rect0)) {

CCLOG("circleS与rectS碰撞了"else"circleS与rectS未碰撞"radius =30.0Point position = Point(300CGCircle circle = CGCircle(radius,position);

Rect rect1 = Rect(2802901030(circle.isContainRect(rect1))

"矩形1与圆碰撞了"Rect rect2 = Rect(210(! circle.isContainRect(rect2))

"矩形2与圆未碰撞"

\

\

原文链接:/cocos2dx/343739.html

猜你在找的Cocos2d-x相关文章