大佬教程收集整理的这篇文章主要介绍了基于像素的碰撞检测(移植到cocos2dx 3.x),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
CollisionDetection.h
// // CollisionDetection.h // Created by Mudit Jaju on 30/08/13. // // SINGLETON class for checking Pixel Based Collision Detection #ifndef __CollisionDetection__ #define __CollisionDetection__ #include <iostream> #include "cocos2d.h" class CollisionDetection { public: //Handle for getTing the Singleton Object static CollisionDetection* GeTinstance(); //Function signature for checking for collision detection spr1,spr2 are the concerned sprites //pp is bool,set to true if Pixel Perfection Collision is required. Else set to false //_rt is the secondary buffer used in our system bool areTheSpritesColliding(cocos2d::Sprite* spr1,cocos2d::Sprite* spr2,bool pp,cocos2d::renderTexture* _rt); private: static CollisionDetection* instance; CollisionDetection(); // Values below are all required for openGL shading cocos2d::GLProgram *glProgram; cocos2d::Color4B *buffer; int uniformColorRed; int uniformColorBlue; }; #endif /* defined(__CollisionDetection__) */
CollisionDetection.cpp
// // CollisionDetection.cpp // Created by Mudit Jaju on 30/08/13. // // SINGLETON class for checking Pixel Based Collision Detection #include "CollisionDetection.h" USING_NS_Cc; // Singleton Instance set to NULL initially CollisionDetection* CollisionDetection::instance = NULL; // Handle to get Singleton Instance CollisionDetection* CollisionDetection::GeTinstance() { if (instance == NULL) { instance = new CollisionDetection(); } return instance; } // Private Constructor being called from within the GeTinstance handle CollisionDetection::CollisionDetection() { // Code below to setup shaders for use in Cocos2d-x glProgram = new GLProgram(); glProgram->retain(); glProgram->initWithFilenames("SolidVertexShader.vsh","SolidColorShader.fsh"); glProgram->bindAttribLOCATIOn(GLProgram::ATTRIBUTE_NAME_POSITION,GLProgram::VERTEX_ATTRIB_POSITION); glProgram->bindAttribLOCATIOn(GLProgram::ATTRIBUTE_NAME_TEX_COORD,GLProgram::VERTEX_ATTRIB_TEX_COORDS); glProgram->link(); glProgram->updateUniforms(); glProgram->use(); uniformColorRed = glGetUniformLOCATIOn(glProgram->getProgram(),"u_color_red"); uniformColorBlue = glGetUniformLOCATIOn(glProgram->getProgram(),"u_color_blue"); // A large buffer created and re-used again and again to store glReadPixels data buffer = (Color4B *)malloc( sizeof(Color4B) * 10000 ); } bool CollisionDetection::areTheSpritesColliding(cocos2d::Sprite* spr1,RenderTexture* _rt) { bool isColliding = false; // Rectangle of thE intersecTing area if the sprites are colliding according to Bounding Box collision Rect intersection; // Bounding box of the Two concerned sprites being saved Rect r1 = spr1->getBoundingBox(); Rect r2 = spr2->getBoundingBox(); // Look for simple bounding box collision if (r1.intersectsRect(r2)) { // If we're not checking for pixel perfect collisions,return true if (!pp) { return true; } float tempX; float tempY; float tempWidth; float tempHeight; //OPTIMIZE FURTHER //CONSIDER THE CASE WHEN ONE BOUDNING BOX IS COMPLETELY INSIDE ANOTHER BOUNDING BOX! if (r1.getMaxX() > r2.getMinX()) { tempX = r2.getMinX(); tempWidth = r1.getMaxX() - r2.getMinX(); } else { tempX = r1.getMinX(); tempWidth = r2.getMaxX() - r1.getMinX(); } if (r2.getMaxY() < r1.getMaxY()) { tempY = r1.getMinY(); tempHeight = r2.getMaxY() - r1.getMinY(); } else { tempY = r2.getMinY(); tempHeight = r1.getMaxY() - r2.getMinY(); } // We make the rectangle for thE intersection area intersection = Rect(tempX * CC_CONTENT_SCALE_FACTOR(),tempY * CC_CONTENT_SCALE_FACTOR(),tempWidth * CC_CONTENT_SCALE_FACTOR(),tempHeight * CC_CONTENT_SCALE_FACTOR()); unsigned int x = intersection.origin.x; unsigned int y = intersection.origin.y; unsigned int w = intersection.size.width; unsigned int h = intersection.size.height; // @R_895_10586@l pixels whose values we will get using glReadPixels depends on the Height and Width of thE intersection area unsigned int numPixels = w * h; // SetTing the custom shader to be used spr1->setGLProgram(glProgram); spr2->setGLProgram(glProgram); glProgram->use(); // Clearing the Secondary Draw buffer of all previous values _rt->beginWithClear( 0,0); // The below two values are being used in the custom shaders to set the value of RED and BLUE colors to be used glUniform1i(uniformColorRed,255); glUniform1i(uniformColorBlue,0); BlendFunc blen_SRCAlpha_ONE={GL_SRC_ALPHA,GL_ONE}; BlendFunc blen_BLEND_SRC_BLEND_DST={CC_BLEND_SRC,CC_BLEND_DST}; // The blend function is important we don't want the pixel value of the RED color being over-written by the BLUE color. // We want both the colors at a single pixel and hence get a PINK color (so that we have both the RED and BLUE pixels) spr1->setBlendFunc(blen_SRCAlpha_ONE); // The visit() function draws the sprite in the _rt draw buffer its a Cocos2d-x function spr1->visit(); // SetTing the shader program BACk to the default shader being used by our game spr1->setGLProgram(ShaderCache::geTinstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVp)); // SetTing the default blender function being used by the game spr1->setBlendFunc(blen_BLEND_SRC_BLEND_DST); // SetTing new values for the same shader but for our second sprite as this time we want to have an all BLUE sprite glUniform1i(uniformColorRed,0); glUniform1i(uniformColorBlue,255); spr2->setBlendFunc(blen_SRCAlpha_ONE); spr2->visit(); spr2->setGLProgram(CCShaderCache::geTinstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVp)); spr2->setBlendFunc(blen_BLEND_SRC_BLEND_DST); // Get color values of intersection area glReadPixels(x,y,w,h,GL_RGBA,GL_UNSIGNED_BYTE,buffer); _rt->end(); // Read buffer unsigned int step = 1; for(unsigned int i=0; i<numPixels; i+=step) { Color4B color = buffer[i]; // Here we check if a single pixel has both RED and BLUE pixels if (color.r > 0 && color.b > 0) { isColliding = true; break; } } } return isColliding; }
#ifdef GL_ES precision lowp float; #endif varying vec2 v_texCoord; uniform sampler2D u_texture; uniform int u_color_red; uniform int u_color_blue; void main() { vec4 color = texture2D(u_texture,v_texCoord); gl_FragColor = vec4(u_color_red,u_color_blue,color.a); }
attribute vec4 a_position; attribute vec2 a_texCoord; attribute vec4 a_color; #ifdef GL_ES varying lowp vec4 v_fragmentColor; varying mediump vec2 v_texCoord; #else varying vec4 v_fragmentColor; varying vec2 v_texCoord; #endif void main() { gl_Position = CC_MVPMatrix * a_position; v_fragmentColor = a_color; v_texCoord = a_texCoord; }
1. Initialize the CCRenderTexture object
_rt = RenderTexture::create(visibleSize.width * 2,visibleSize.height * 2); _rt->setPosition(ccp(visibleSize.width,visibleSize.height)); _rt->retain(); _rt->setVisible(false);
if (CollisionDetection::GeTinstance()->areTheSpritesColliding(pSprite,pCurrentSpriteToDrag,true,_rt)) { //Code here }
以上是大佬教程为你收集整理的基于像素的碰撞检测(移植到cocos2dx 3.x)全部内容,希望文章能够帮你解决基于像素的碰撞检测(移植到cocos2dx 3.x)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。