接上一節內容:cocos2dx - 生成怪物及AI
本節主要講如何通過創建簡單的矩形區域來造成傷害
在小游戲中簡單的碰撞需求應用box2d等引擎會顯得過於臃腫復雜,且功能不是根據需求定制,還要封裝,為此本節講述一下如何自己實現簡單的碰撞,來達到傷害效果。
這里先看下效果圖:
一、定義小組類別
定義如下:
// 組別mask enum enGroupMask { GROUP_NONE = 0x0000, GROUP_PLAYER = 0x0001, GROUP_MONSTER = 0x0002, };
用二進制位來定義小組,方便后面判斷用位快速判斷。
二、定義CBody類用於碰撞實現
代碼如下:

#ifndef __CBody_H__ #define __CBody_H__ #include "IGameDef.h" #include "cocos2d.h" #define DRAW_BODY 1 #ifdef DRAW_BODY #include "GLES-Render.h" #endif USING_NS_CC; class CBody : public Node { public: // implement the "static create()" method manually CREATE_FUNC(CBody); virtual bool init(); void SetRect(float x, float y, float w, float h){ m_cRect = Rect(x,y,w,h); } void SetGroupMask(enGroupMask nGroupMask){ m_nGroupMask = nGroupMask; } enGroupMask GetGroupMask() const{ return m_nGroupMask; } enGroupMask CanContactGroup() const; //判斷是否可以碰撞該類別 bool IsGroupCanContact(enGroupMask nGroupMask) const; const Rect GetRect() const; //判斷矩形是否交叉 bool IntersectsRect(const Rect& rect) const; void SetContactCallback(const std::function<void(CBody*)>& callback){ m_constactFunc = callback; } // 碰撞回調函數 void OnContact(CBody* pBody); // 繪制矩形區域 #ifdef DRAW_BODY virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags); virtual void onDraw(const Mat4 &transform, uint32_t flags); #endif private: CBody(); ~CBody(); #ifdef DRAW_BODY CustomCommand m_pCustomCommand; b2Draw* m_debugDraw; #endif Rect m_cRect; std::function<void(CBody*)> m_constactFunc; enGroupMask m_nGroupMask; }; #endif __CBody_H__
關鍵實現有以下幾個:
1、用位快速判斷是否可以產生碰撞
bool CBody::IsGroupCanContact(enGroupMask nGroupMask) const { return nGroupMask&CanContactGroup(); }
2、獲取矩形實際的坐標,利用cocos2dx矩形相交判斷方法,判斷重疊
const Rect CBody::GetRect() const { const Vec2& pt =this->convertToWorldSpace(this->getPosition()); return Rect(pt.x + m_cRect.origin.x, pt.y + m_cRect.origin.y, m_cRect.size.width, m_cRect.size.height); }
bool CBody::IntersectsRect(const Rect& rect) const { return GetRect().intersectsRect(rect); }
3、將創建的CBody類添加到管理中vector的實例m_vBody管理
CBody::~CBody() { CBattleMgr::getInstance()->EreaseBody(this); #ifdef DRAW_BODY if (m_debugDraw) { delete m_debugDraw; m_debugDraw = NULL; } #endif } bool CBody::init() { #ifdef DRAW_BODY m_debugDraw = new GLESDebugDraw(1.0); #endif CBattleMgr::getInstance()->InsertBody(this); return true; }
同時每幀在管理類中的update進行碰撞判斷如下:
void CBattleMgr::update(float dt) { while (m_vBody.size()>0) { size_t nLast = m_vBody.size() - 1; CBody* pBody = m_vBody[nLast]; m_vBody.pop_back(); if (pBody) { m_vSwapBody.push_back(pBody); for (size_t i = 0; i < nLast; ++i) { CBody* pTempBody = m_vBody[i]; if (pTempBody ) { bool hurt1 = pTempBody->IsGroupCanContact(pBody->GetGroupMask()); bool hurt2 = pBody->IsGroupCanContact(pTempBody->GetGroupMask()); if ((hurt1 || hurt2)&&pTempBody->IntersectsRect(pBody->GetRect())) { if (hurt1) { pTempBody->OnContact(pBody); } if (hurt2) { pBody->OnContact(pTempBody); } } } } } } m_vSwapBody.swap(m_vBody); }
上述代碼通過2個vector管理CBody,在update時進行一次判斷碰撞並調用OnContact方法。
4、為了方便顯示上圖的灰色矩形框需要用以下的類進行顯示圖形

/* * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com * * iPhone port by Simon Oliver - http://www.simonoliver.com - http://www.handcircus.com * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be * appreciated but is not required. * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. */ #ifndef RENDER_H #define RENDER_H #include "Box2D/Box2D.h" #include "cocos2d.h" struct b2AABB; // This class implements debug drawing callbacks that are invoked // inside b2World::Step. class GLESDebugDraw : public b2Draw { float32 mRatio; cocos2d::GLProgram* mShaderProgram; GLint mColorLocation; void initShader( void ); public: GLESDebugDraw(); GLESDebugDraw( float32 ratio ); virtual void DrawPolygon(const b2Vec2* vertices, int vertexCount, const b2Color& color); virtual void DrawSolidPolygon(const b2Vec2* vertices, int vertexCount, const b2Color& color); virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color); virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color); virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color); virtual void DrawTransform(const b2Transform& xf); virtual void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color); virtual void DrawString(int x, int y, const char* string, ...); virtual void DrawAABB(b2AABB* aabb, const b2Color& color); }; #endif /* * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com * * iPhone port by Simon Oliver - http://www.simonoliver.com - http://www.handcircus.com * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be * appreciated but is not required. * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. */ #include "GLES-Render.h" #include "cocos2d.h" #include <stdio.h> #include <stdarg.h> #include <string.h> USING_NS_CC; GLESDebugDraw::GLESDebugDraw() : mRatio( 1.0f ) { this->initShader(); } GLESDebugDraw::GLESDebugDraw( float32 ratio ) : mRatio( ratio ) { this->initShader(); } void GLESDebugDraw::initShader( void ) { mShaderProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR); mColorLocation = glGetUniformLocation( mShaderProgram->getProgram(), "u_color"); } void GLESDebugDraw::DrawPolygon(const b2Vec2* old_vertices, int vertexCount, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); b2Vec2* vertices = new b2Vec2[vertexCount]; for( int i=0;i<vertexCount;i++) { vertices[i] = old_vertices[i]; vertices[i] *= mRatio; } mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,vertexCount); CHECK_GL_ERROR_DEBUG(); delete[] vertices; } void GLESDebugDraw::DrawSolidPolygon(const b2Vec2* old_vertices, int vertexCount, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); b2Vec2* vertices = new b2Vec2[vertexCount]; for( int i=0;i<vertexCount;i++) { vertices[i] = old_vertices[i]; vertices[i] *= mRatio; } mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r*0.5f, color.g*0.5f, color.b*0.5f, 0.5f); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount); mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(2,vertexCount*2); CHECK_GL_ERROR_DEBUG(); delete[] vertices; } void GLESDebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); const float32 k_segments = 16.0f; int vertexCount=16; const float32 k_increment = 2.0f * b2_pi / k_segments; float32 theta = 0.0f; GLfloat* glVertices = new GLfloat[vertexCount*2]; for (int i = 0; i < k_segments; ++i) { b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta)); glVertices[i*2]=v.x * mRatio; glVertices[i*2+1]=v.y * mRatio; theta += k_increment; } mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, glVertices); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,vertexCount); CHECK_GL_ERROR_DEBUG(); delete[] glVertices; } void GLESDebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); const float32 k_segments = 16.0f; int vertexCount=16; const float32 k_increment = 2.0f * b2_pi / k_segments; float32 theta = 0.0f; GLfloat* glVertices = new GLfloat[vertexCount*2]; for (int i = 0; i < k_segments; ++i) { b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta)); glVertices[i*2]=v.x * mRatio; glVertices[i*2+1]=v.y * mRatio; theta += k_increment; } mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r*0.5f, color.g*0.5f, color.b*0.5f, 0.5f); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, glVertices); glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount); mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); // Draw the axis line DrawSegment(center,center+radius*axis,color); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(2,vertexCount*2); CHECK_GL_ERROR_DEBUG(); delete[] glVertices; } void GLESDebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); GLfloat glVertices[] = { p1.x * mRatio, p1.y * mRatio, p2.x * mRatio, p2.y * mRatio }; glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, glVertices); glDrawArrays(GL_LINES, 0, 2); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2); CHECK_GL_ERROR_DEBUG(); } void GLESDebugDraw::DrawTransform(const b2Transform& xf) { b2Vec2 p1 = xf.p, p2; const float32 k_axisScale = 0.4f; p2 = p1 + k_axisScale * xf.q.GetXAxis(); DrawSegment(p1, p2, b2Color(1,0,0)); p2 = p1 + k_axisScale * xf.q.GetYAxis(); DrawSegment(p1,p2,b2Color(0,1,0)); } void GLESDebugDraw::DrawPoint(const b2Vec2& p, float32 size, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); // glPointSize(size); GLfloat glVertices[] = { p.x * mRatio, p.y * mRatio }; glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, glVertices); glDrawArrays(GL_POINTS, 0, 1); // glPointSize(1.0f); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,1); CHECK_GL_ERROR_DEBUG(); } void GLESDebugDraw::DrawString(int x, int y, const char *string, ...) { // NSLog(@"DrawString: unsupported: %s", string); //printf(string); /* Unsupported as yet. Could replace with bitmap font renderer at a later date */ } void GLESDebugDraw::DrawAABB(b2AABB* aabb, const b2Color& color) { mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); GLfloat glVertices[] = { aabb->lowerBound.x * mRatio, aabb->lowerBound.y * mRatio, aabb->upperBound.x * mRatio, aabb->lowerBound.y * mRatio, aabb->upperBound.x * mRatio, aabb->upperBound.y * mRatio, aabb->lowerBound.x * mRatio, aabb->upperBound.y * mRatio }; glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, glVertices); glDrawArrays(GL_LINE_LOOP, 0, 4); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4); CHECK_GL_ERROR_DEBUG(); }
然后在CBody的draw中做如下處理進行繪制
#ifdef DRAW_BODY void CBody::onDraw(const Mat4 &transform, uint32_t flags) { if (m_debugDraw) { b2Vec2 vs[4]; const Rect rect = GetRect(); vs[0].Set(rect.origin.x, rect.origin.y); vs[1].Set(rect.origin.x + rect.size.width, rect.origin.y); vs[2].Set(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); vs[3].Set(rect.origin.x, rect.origin.y + rect.size.height); m_debugDraw->DrawSolidPolygon(vs, 4, b2Color(0.6f, 0.6f, 0.6f)); } } void CBody::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) { //_globalZOrder m_pCustomCommand.init(30); m_pCustomCommand.func = CC_CALLBACK_0(CBody::onDraw, this, transform, flags); renderer->addCommand(&m_pCustomCommand); } #endif
5、最后在Monster等實體類init中添加該CBody的實例,即可為其添加碰撞形狀。
如下:
m_pBody = CBody::create(); m_pBody->SetGroupMask(enGroupMask::GROUP_MONSTER); m_pBody->SetRect(-100, -100, 200, 200); this->addChild(m_pBody);
三、定義CDamage用於創建傷害
主要實現以下方法:
#ifndef __CDamage_H__ #define __CDamage_H__ #include "IGameDef.h" #include "Body.h" #include <set> USING_NS_CC; class CDamage : public Node { public: // implement the "static create()" method manually CREATE_FUNC(CDamage); virtual bool init(); void update(float dt); void SetRect(float x, float y, float w, float h); void SetGroupMask(enGroupMask nGroupMask); void OnContact(CBody* pBody); private: CDamage(); ~CDamage(); int m_nDieTime; // 傷害區域消失時間 CBody *m_pBody; std::set<CBody*> m_sContact; }; #endif __CDamage_H__
關鍵點:
1、m_nDieTime在update中實現控制傷害區域存在時間
2、m_sContact控制一個CDamage實例對每個實體僅造成一次傷害。
在怪物等攻擊動作中添加事件並在事件回調中添加如下代碼創建傷害
void CEntity::ActionEvent(Frame *frame) { cocostudio::timeline::EventFrame* evnt = dynamic_cast<cocostudio::timeline::EventFrame*>(frame); if (!evnt) return; std::string str = evnt->getEvent(); if (str == "attack") { auto damage = CDamage::create(); damage->SetRect(-100, -100, 100, 200); damage->SetGroupMask(m_nGroupMask); if (this->getScaleX()<0) { damage->setPositionX(-100); } this->addChild(damage); } }
添加事件方法,即在特定幀添加以下的幀事件
至此,游戲的基本元素已經都有了。
剩下的自己可以進行擴展了,如:血量為0時,播放死亡動畫,並顯示失敗等。
在怪物死亡創建一個新的怪物: