From 619a8810fe435024524f86cd3ae6475c317b4cdb Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Sat, 13 Jul 2013 19:14:30 +0800 Subject: [PATCH] Change Physics detector --- extensions/CCArmature/CCArmature.cpp | 61 ++++ extensions/CCArmature/CCArmature.h | 9 + extensions/CCArmature/CCBone.cpp | 10 + extensions/CCArmature/CCBone.h | 2 + extensions/CCArmature/animation/CCTween.cpp | 7 +- .../display/CCDecorativeDisplay.cpp | 4 +- .../CCArmature/display/CCDecorativeDisplay.h | 4 +- .../CCArmature/display/CCDisplayFactory.cpp | 4 +- .../CCArmature/display/CCDisplayManager.cpp | 9 +- extensions/CCArmature/display/CCSkin.cpp | 6 + extensions/CCArmature/display/CCSkin.h | 2 + .../CCArmature/external_tool/GLES-Render.cpp | 254 ----------------- .../CCArmature/external_tool/GLES-Render.h | 67 ----- .../CCArmature/physics/CCColliderDetector.cpp | 240 +++++++++++----- .../CCArmature/physics/CCColliderDetector.h | 56 ++-- .../CCArmature/physics/CCPhysicsWorld.cpp | 165 ----------- .../CCArmature/physics/CCPhysicsWorld.h | 80 ------ .../utils/CCArmatureDataManager.cpp | 3 - .../CCArmature/utils/CCArmatureDefine.h | 9 +- extensions/cocos-ext.h | 1 - extensions/proj.win32/libExtensions.vcxproj | 4 - .../proj.win32/libExtensions.vcxproj.filters | 12 - .../ArmatureTest/ArmatureScene.cpp | 262 ++++++++++++++++-- .../ArmatureTest/ArmatureScene.h | 36 ++- .../armature/Cowboy0.png.REMOVED.git-id | 2 +- 25 files changed, 591 insertions(+), 718 deletions(-) delete mode 100644 extensions/CCArmature/external_tool/GLES-Render.cpp delete mode 100644 extensions/CCArmature/external_tool/GLES-Render.h delete mode 100644 extensions/CCArmature/physics/CCPhysicsWorld.cpp delete mode 100644 extensions/CCArmature/physics/CCPhysicsWorld.h diff --git a/extensions/CCArmature/CCArmature.cpp b/extensions/CCArmature/CCArmature.cpp index 20222afe01..b5e659d76f 100644 --- a/extensions/CCArmature/CCArmature.cpp +++ b/extensions/CCArmature/CCArmature.cpp @@ -587,4 +587,65 @@ CCBone *CCArmature::getBoneAtPoint(float x, float y) return NULL; } +#if ENABLE_PHYSICS_BOX2D_DETECT +b2Body *CCArmature::getB2Body() +{ + return m_pB2Body; +} + +void CCArmature::setB2Body(b2Body *body) +{ + m_pB2Body = body; + + CCObject *object = NULL; + CCARRAY_FOREACH(m_pChildren, object) + { + if (CCBone *bone = dynamic_cast(object)) + { + CCArray *displayList = bone->getDisplayManager()->getDecorativeDisplayList(); + + CCObject *displayObject = NULL; + CCARRAY_FOREACH(displayList, displayObject) + { + CCColliderDetector *detector = ((CCDecorativeDisplay*)displayObject)->getColliderDetector(); + if (detector != NULL) + { + detector->setB2Body(m_pB2Body); + } + } + } + } +} +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT +cpBody *CCArmature::getCPBody() +{ + return m_pCPBody; +} + +void CCArmature::setCPBody(cpBody *body) +{ + m_pCPBody = body; + + CCObject *object = NULL; + CCARRAY_FOREACH(m_pChildren, object) + { + if (CCBone *bone = dynamic_cast(object)) + { + CCArray *displayList = bone->getDisplayManager()->getDecorativeDisplayList(); + + CCObject *displayObject = NULL; + CCARRAY_FOREACH(displayList, displayObject) + { + CCColliderDetector *detector = ((CCDecorativeDisplay*)displayObject)->getColliderDetector(); + if (detector != NULL) + { + detector->setCPBody(m_pCPBody); + } + } + } + } +} +#endif + + NS_CC_EXT_END diff --git a/extensions/CCArmature/CCArmature.h b/extensions/CCArmature/CCArmature.h index b015f16ee8..0ff843bcb1 100644 --- a/extensions/CCArmature/CCArmature.h +++ b/extensions/CCArmature/CCArmature.h @@ -30,6 +30,9 @@ THE SOFTWARE. #include "display/CCBatchNode.h" #include "animation/CCArmatureAnimation.h" +class b2Body; +struct cpBody; + NS_CC_EXT_BEGIN class CCArmature : public CCNodeRGBA, public CCBlendProtocol @@ -154,6 +157,12 @@ protected: CCPoint m_pOffsetPoint; CCArmatureAnimation *m_pAnimation; + +#if ENABLE_PHYSICS_BOX2D_DETECT + CC_PROPERTY(b2Body*, m_pB2Body, B2Body); +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + CC_PROPERTY(cpBody*, m_pCPBody, CPBody); +#endif }; NS_CC_EXT_END diff --git a/extensions/CCArmature/CCBone.cpp b/extensions/CCArmature/CCBone.cpp index aa2e11a1c6..e93c761eac 100644 --- a/extensions/CCArmature/CCBone.cpp +++ b/extensions/CCArmature/CCBone.cpp @@ -320,6 +320,16 @@ CCAffineTransform CCBone::nodeToArmatureTransform() return m_tWorldTransform; } +CCAffineTransform CCBone::nodeToWorldTransform() +{ + return CCAffineTransformConcat(m_tWorldTransform, m_pArmature->nodeToWorldTransform()); +} + +CCNode *CCBone::getDisplayRenderNode() +{ + return m_pDisplayManager->getDisplayRenderNode(); +} + void CCBone::addDisplay(CCDisplayData *displayData, int index) { m_pDisplayManager->addDisplay(displayData, index); diff --git a/extensions/CCArmature/CCBone.h b/extensions/CCArmature/CCBone.h index 719726ae05..8cf83a2ace 100644 --- a/extensions/CCArmature/CCBone.h +++ b/extensions/CCArmature/CCBone.h @@ -137,7 +137,9 @@ public: virtual bool isTransformDirty(); virtual CCAffineTransform nodeToArmatureTransform(); + virtual CCAffineTransform nodeToWorldTransform(); + CCNode *getDisplayRenderNode(); public: /* * The origin state of the CCBone. Display's state is effected by m_pBoneData, m_pNode, m_pTweenData diff --git a/extensions/CCArmature/animation/CCTween.cpp b/extensions/CCArmature/animation/CCTween.cpp index a43ff4ad24..4fc59f8961 100644 --- a/extensions/CCArmature/animation/CCTween.cpp +++ b/extensions/CCArmature/animation/CCTween.cpp @@ -217,8 +217,6 @@ void CCTween::updateHandler() // m_fCurrentFrame = (1 - m_pMovementBoneData->delay) * (float)m_iNextFrameIndex; m_fCurrentPercent = m_fCurrentFrame / m_iNextFrameIndex; - - } else { @@ -322,10 +320,7 @@ void CCTween::arriveKeyFrame(CCFrameData *keyFrameData) } } - if(keyFrameData->m_strEvent.length() != 0) - { - m_pAnimation->FrameEventSignal.emit(m_pBone, keyFrameData->m_strEvent.c_str()); - } + // if(keyFrameData->m_strSound.length() != 0) // { // //soundManager.dispatchEventWith(Event.SOUND_FRAME, m_pCurrentKeyFrame->sound); diff --git a/extensions/CCArmature/display/CCDecorativeDisplay.cpp b/extensions/CCArmature/display/CCDecorativeDisplay.cpp index 6e9bfa3acf..6783e84265 100644 --- a/extensions/CCArmature/display/CCDecorativeDisplay.cpp +++ b/extensions/CCArmature/display/CCDecorativeDisplay.cpp @@ -44,7 +44,7 @@ CCDecorativeDisplay::CCDecorativeDisplay() , m_pDisplayData(NULL) { -#if ENABLE_PHYSICS_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT m_pColliderDetector = NULL; #endif } @@ -55,7 +55,7 @@ CCDecorativeDisplay::~CCDecorativeDisplay(void) CC_SAFE_RELEASE_NULL(m_pDisplayData); CC_SAFE_RELEASE_NULL(m_pDisplay); -#if ENABLE_PHYSICS_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT CC_SAFE_RELEASE_NULL(m_pColliderDetector); #endif } diff --git a/extensions/CCArmature/display/CCDecorativeDisplay.h b/extensions/CCArmature/display/CCDecorativeDisplay.h index c8ee8aecb6..19b558f141 100644 --- a/extensions/CCArmature/display/CCDecorativeDisplay.h +++ b/extensions/CCArmature/display/CCDecorativeDisplay.h @@ -31,7 +31,7 @@ THE SOFTWARE. #include "../external_tool/sigslot.h" -#if ENABLE_PHYSICS_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT #include "../physics/CCColliderDetector.h" #endif @@ -52,7 +52,7 @@ protected: CC_SYNTHESIZE_RETAIN(CCNode *, m_pDisplay, Display); CC_SYNTHESIZE_RETAIN(CCDisplayData *, m_pDisplayData, DisplayData); -#if ENABLE_PHYSICS_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT CC_SYNTHESIZE_RETAIN(CCColliderDetector *, m_pColliderDetector, ColliderDetector); #endif public: diff --git a/extensions/CCArmature/display/CCDisplayFactory.cpp b/extensions/CCArmature/display/CCDisplayFactory.cpp index a8ce6d6336..d92935e74c 100644 --- a/extensions/CCArmature/display/CCDisplayFactory.cpp +++ b/extensions/CCArmature/display/CCDisplayFactory.cpp @@ -72,7 +72,7 @@ void CCDisplayFactory::updateDisplay(CCBone *bone, CCDecorativeDisplay *decoDisp { CS_RETURN_IF(!decoDisplay); -#if ENABLE_PHYSICS_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT if (dirty) { CCColliderDetector *detector = decoDisplay->getColliderDetector(); @@ -167,7 +167,7 @@ void CCDisplayFactory::createSpriteDisplay(CCBone *bone, CCDecorativeDisplay *de decoDisplay->setDisplay(skin); -#if ENABLE_PHYSICS_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT if (textureData && textureData->contourDataList.count() > 0) { diff --git a/extensions/CCArmature/display/CCDisplayManager.cpp b/extensions/CCArmature/display/CCDisplayManager.cpp index 09d37f8ae4..d39d8e7b68 100644 --- a/extensions/CCArmature/display/CCDisplayManager.cpp +++ b/extensions/CCArmature/display/CCDisplayManager.cpp @@ -145,6 +145,11 @@ void CCDisplayManager::removeDisplay(int index) } } +CCArray *CCDisplayManager::getDecorativeDisplayList() +{ + return m_pDecoDisplayList; +} + void CCDisplayManager::changeDisplayByIndex(int index, bool force) { CCAssert( (m_pDecoDisplayList ? index < (int)m_pDecoDisplayList->count() : true), "the _index value is out of range"); @@ -177,7 +182,7 @@ void CCDisplayManager::changeDisplayByIndex(int index, bool force) void CCDisplayManager::setCurrentDecorativeDisplay(CCDecorativeDisplay *decoDisplay) { -#if ENABLE_PHYSICS_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT if (m_pCurrentDecoDisplay && m_pCurrentDecoDisplay->getColliderDetector()) { m_pCurrentDecoDisplay->getColliderDetector()->setActive(false); @@ -186,7 +191,7 @@ void CCDisplayManager::setCurrentDecorativeDisplay(CCDecorativeDisplay *decoDisp m_pCurrentDecoDisplay = decoDisplay; -#if ENABLE_PHYSICS_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT if (m_pCurrentDecoDisplay && m_pCurrentDecoDisplay->getColliderDetector()) { m_pCurrentDecoDisplay->getColliderDetector()->setActive(true); diff --git a/extensions/CCArmature/display/CCSkin.cpp b/extensions/CCArmature/display/CCSkin.cpp index 967cbab6ae..cba1d8bc92 100644 --- a/extensions/CCArmature/display/CCSkin.cpp +++ b/extensions/CCArmature/display/CCSkin.cpp @@ -24,6 +24,7 @@ THE SOFTWARE. #include "CCSkin.h" #include "../utils/CCTransformHelp.h" +#include "../CCArmature.h" NS_CC_EXT_BEGIN @@ -149,4 +150,9 @@ void CCSkin::updateTransform() } } +CCAffineTransform CCSkin::nodeToWorldTransform() +{ + return CCAffineTransformConcat(m_sTransform, m_pBone->getArmature()->nodeToWorldTransform()); +} + NS_CC_EXT_END diff --git a/extensions/CCArmature/display/CCSkin.h b/extensions/CCArmature/display/CCSkin.h index 7dcbd112b2..801201ebff 100644 --- a/extensions/CCArmature/display/CCSkin.h +++ b/extensions/CCArmature/display/CCSkin.h @@ -42,6 +42,8 @@ public: void updateArmatureTransform(); void updateTransform(); + CCAffineTransform nodeToWorldTransform(); + CC_PROPERTY_PASS_BY_REF(CCBaseData, m_sSkinData, SkinData); CC_SYNTHESIZE(CCBone *, m_pBone, Bone); diff --git a/extensions/CCArmature/external_tool/GLES-Render.cpp b/extensions/CCArmature/external_tool/GLES-Render.cpp deleted file mode 100644 index 9d9989b7ba..0000000000 --- a/extensions/CCArmature/external_tool/GLES-Render.cpp +++ /dev/null @@ -1,254 +0,0 @@ -/* - * 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 -#include -#include - -NS_CC_EXT_BEGIN - -USING_NS_CC; - -GLESDebugDraw::GLESDebugDraw() - : mRatio( 1.0f ) -{ - this->initShader(); -} - -GLESDebugDraw::GLESDebugDraw( float32 ratio ) - : mRatio( ratio ) -{ - this->initShader(); -} - -void GLESDebugDraw::initShader( void ) -{ - mShaderProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_Position_uColor); - - 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;isetUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1); - - glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); - glDrawArrays(GL_LINE_LOOP, 0, vertexCount); - - CC_INCREMENT_GL_DRAWS(1); - - 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;isetUniformLocationWith4f(mColorLocation, color.r*0.5f, color.g*0.5f, color.b*0.5f, 0.5f); - - glVertexAttribPointer(kCCVertexAttrib_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_DRAWS(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(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, glVertices); - - glDrawArrays(GL_LINE_LOOP, 0, vertexCount); - - CC_INCREMENT_GL_DRAWS(1); - - 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(kCCVertexAttrib_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_DRAWS(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(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, glVertices); - - glDrawArrays(GL_LINES, 0, 2); - - CC_INCREMENT_GL_DRAWS(1); - - 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(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, glVertices); - - glDrawArrays(GL_POINTS, 0, 1); - // glPointSize(1.0f); - - CC_INCREMENT_GL_DRAWS(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(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, glVertices); - glDrawArrays(GL_LINE_LOOP, 0, 8); - - CC_INCREMENT_GL_DRAWS(1); - - CHECK_GL_ERROR_DEBUG(); - -} - -NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CCArmature/external_tool/GLES-Render.h b/extensions/CCArmature/external_tool/GLES-Render.h deleted file mode 100644 index 5f65348b8c..0000000000 --- a/extensions/CCArmature/external_tool/GLES-Render.h +++ /dev/null @@ -1,67 +0,0 @@ -/* -* 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" -#include "ExtensionMacros.h" - -struct b2AABB; - -NS_CC_EXT_BEGIN - -// This class implements debug drawing callbacks that are invoked -// inside b2World::Step. -class GLESDebugDraw : public b2Draw -{ - float32 mRatio; - cocos2d::CCGLProgram* 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); -}; - -NS_CC_EXT_END - -#endif diff --git a/extensions/CCArmature/physics/CCColliderDetector.cpp b/extensions/CCArmature/physics/CCColliderDetector.cpp index b88ec65527..3b0c42d472 100644 --- a/extensions/CCArmature/physics/CCColliderDetector.cpp +++ b/extensions/CCArmature/physics/CCColliderDetector.cpp @@ -23,9 +23,15 @@ THE SOFTWARE. ****************************************************************************/ #include "CCColliderDetector.h" -#include "CCPhysicsWorld.h" #include "../CCBone.h" +#include "../utils/CCTransformHelp.h" + +#if ENABLE_PHYSICS_BOX2D_DETECT #include "Box2D/Box2D.h" +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT +#include "chipmunk.h" +#endif + NS_CC_EXT_BEGIN @@ -56,20 +62,15 @@ CCColliderDetector *CCColliderDetector::create(CCBone *bone) CCColliderDetector::CCColliderDetector() : m_pColliderBodyList(NULL) { +#if ENABLE_PHYSICS_BOX2D_DETECT + m_pB2Body = NULL; +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + m_pCPBody = NULL; +#endif } CCColliderDetector::~CCColliderDetector() { - CCObject *object = NULL; - CCARRAY_FOREACH(m_pColliderBodyList, object) - { - ColliderBody *colliderBody = (ColliderBody *)object; - - b2Body *body = colliderBody->getB2Body(); - CCPhysicsWorld::sharedPhysicsWorld()->getNoGravityWorld()->DestroyBody(body); - } - - m_pColliderBodyList->removeAllObjects(); CC_SAFE_DELETE(m_pColliderBodyList); } @@ -93,38 +94,7 @@ bool CCColliderDetector::init(CCBone *bone) void CCColliderDetector::addContourData(CCContourData *contourData) { - const CCArray *array = &contourData->vertexList; - CCObject *object = NULL; - - b2Vec2 *b2bv = new b2Vec2[contourData->vertexList.count()]; - - int i = 0; - CCARRAY_FOREACH(array, object) - { - CCContourVertex2 *v = (CCContourVertex2 *)object; - b2bv[i].Set(v->x / PT_RATIO, v->y / PT_RATIO); - i++; - } - - b2PolygonShape polygon; - polygon.Set(b2bv, contourData->vertexList.count()); - - CC_SAFE_DELETE(b2bv); - - b2FixtureDef fixtureDef; - fixtureDef.shape = &polygon; - fixtureDef.density = 10.0f; - fixtureDef.isSensor = true; - - b2BodyDef bodyDef; - bodyDef.type = b2_dynamicBody; - bodyDef.position = b2Vec2(0.0f, 0.0f); - bodyDef.userData = m_pBone; - - b2Body *body = CCPhysicsWorld::sharedPhysicsWorld()->getNoGravityWorld()->CreateBody(&bodyDef); - body->CreateFixture(&fixtureDef); - - ColliderBody *colliderBody = new ColliderBody(body, contourData); + ColliderBody *colliderBody = new ColliderBody(contourData); m_pColliderBodyList->addObject(colliderBody); colliderBody->release(); } @@ -148,26 +118,35 @@ void CCColliderDetector::removeAll() m_pColliderBodyList->removeAllObjects(); } -void CCColliderDetector::setColliderFilter(b2Filter &filter) -{ - CCObject *object = NULL; - CCARRAY_FOREACH(m_pColliderBodyList, object) - { - ColliderBody *colliderBody = (ColliderBody *)object; - colliderBody->getB2Body()->GetFixtureList()->SetFilterData(filter); - } -} void CCColliderDetector::setActive(bool active) { - CCObject *object = NULL; - CCARRAY_FOREACH(m_pColliderBodyList, object) - { - ColliderBody *colliderBody = (ColliderBody *)object; - colliderBody->getB2Body()->SetActive(active); - } +#if ENABLE_PHYSICS_BOX2D_DETECT + if (m_pB2Body) + { + m_pB2Body->SetActive(active); + } +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + if (m_pCPBody) + { + if (active) + { + cpBodyActivate(m_pCPBody); + } + else + { + cpBodySleep(m_pCPBody); + } + } +#endif } +CCArray *CCColliderDetector::getColliderBodyList() +{ + return m_pColliderBodyList; +} + + CCPoint helpPoint; void CCColliderDetector::updateTransform(CCAffineTransform &t) @@ -176,31 +155,144 @@ void CCColliderDetector::updateTransform(CCAffineTransform &t) CCARRAY_FOREACH(m_pColliderBodyList, object) { ColliderBody *colliderBody = (ColliderBody *)object; - CCContourData *contourData = colliderBody->getContourData(); - b2Body *body = colliderBody->getB2Body(); - b2PolygonShape *shape = (b2PolygonShape *)body->GetFixtureList()->GetShape(); +#if ENABLE_PHYSICS_BOX2D_DETECT + b2PolygonShape *shape = NULL; + if (m_pB2Body != NULL) + { + shape = (b2PolygonShape *)colliderBody->getB2Fixture()->GetShape(); + } +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + cpPolyShape *shape = NULL; + if (m_pCPBody != NULL) + { + shape = (cpPolyShape *)colliderBody->getShape(); + } +#endif - //! update every vertex - const CCArray *array = &contourData->vertexList; - CCObject *object = NULL; - int i = 0; - CCARRAY_FOREACH(array, object) - { - CCContourVertex2 *cv = (CCContourVertex2 *)object; - b2Vec2 &bv = shape->m_vertices[i]; + //! update every vertex + const CCArray *array = &contourData->vertexList; + CCObject *object = NULL; + int i = 0; + CCARRAY_FOREACH(array, object) + { + CCContourVertex2 *cv = (CCContourVertex2 *)object; - helpPoint.setPoint(cv->x, cv->y); - helpPoint = CCPointApplyAffineTransform(helpPoint, t); + helpPoint.setPoint(cv->x, cv->y); + helpPoint = CCPointApplyAffineTransform(helpPoint, t); - bv.Set(helpPoint.x / PT_RATIO, helpPoint.y / PT_RATIO); +#if ENABLE_PHYSICS_BOX2D_DETECT + if (shape != NULL) + { + b2Vec2 &bv = shape->m_vertices[i]; + bv.Set(helpPoint.x / PT_RATIO, helpPoint.y / PT_RATIO); + } +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + if (shape != NULL) + { + cpVect v ; + v.x = helpPoint.x; + v.y = helpPoint.y; + shape->tVerts[i] = shape->verts[i] = v; - i++; - } + cpVect b = shape->verts[(i+1)%shape->numVerts]; + cpVect n = cpvnormalize(cpvperp(cpvsub(b, shape->verts[i]))); + + shape->planes[i].n = n; + shape->planes[i].d = cpvdot(n, shape->verts[i]); + } +#endif + i++; + } } } +#if ENABLE_PHYSICS_BOX2D_DETECT + +void CCColliderDetector::setB2Body(b2Body *pBody) +{ + m_pB2Body = pBody; + m_pB2Body->SetUserData(m_pBone); + + CCObject *object = NULL; + CCARRAY_FOREACH(m_pColliderBodyList, object) + { + ColliderBody *colliderBody = (ColliderBody *)object; + + CCContourData *contourData = colliderBody->getContourData(); + const CCArray *array = &contourData->vertexList; + CCObject *object = NULL; + + b2Vec2 *b2bv = new b2Vec2[contourData->vertexList.count()]; + + int i = 0; + CCARRAY_FOREACH(array, object) + { + CCContourVertex2 *v = (CCContourVertex2 *)object; + b2bv[i].Set(v->x / PT_RATIO, v->y / PT_RATIO); + i++; + } + + b2PolygonShape polygon; + polygon.Set(b2bv, contourData->vertexList.count()); + + CC_SAFE_DELETE(b2bv); + + b2FixtureDef fixtureDef; + fixtureDef.shape = &polygon; + fixtureDef.isSensor = true; + + b2Fixture *fixture = m_pB2Body->CreateFixture(&fixtureDef); + colliderBody->setB2Fixture(fixture); + } +} + +b2Body *CCColliderDetector::getB2Body() +{ + return m_pB2Body; +} + +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT +void CCColliderDetector::setCPBody(cpBody *pBody) +{ + m_pCPBody = pBody; + + CCObject *object = NULL; + CCARRAY_FOREACH(m_pColliderBodyList, object) + { + ColliderBody *colliderBody = (ColliderBody *)object; + + CCContourData *contourData = colliderBody->getContourData(); + const CCArray *array = &contourData->vertexList; + CCObject *object = NULL; + + int num = contourData->vertexList.count(); + CCContourVertex2 **vs = (CCContourVertex2 **)contourData->vertexList.data->arr; + cpVect *verts = new cpVect[num]; + for (int i=0; ix; + verts[num-1-i].y = vs[i]->y; + } + + cpShape* shape = cpPolyShapeNew(m_pCPBody, num, verts, cpvzero); + shape->sensor = true; + shape->data = m_pBone; + cpSpaceAddShape(m_pCPBody->space_private, shape); + + colliderBody->setShape(shape); + + delete []verts; + } +} + +cpBody *CCColliderDetector::getCPBody() +{ + return m_pCPBody; +} + +#endif NS_CC_EXT_END diff --git a/extensions/CCArmature/physics/CCColliderDetector.h b/extensions/CCArmature/physics/CCColliderDetector.h index eaff42ece5..3b66511c92 100644 --- a/extensions/CCArmature/physics/CCColliderDetector.h +++ b/extensions/CCArmature/physics/CCColliderDetector.h @@ -28,8 +28,16 @@ THE SOFTWARE. #include "../utils/CCArmatureDefine.h" #include "../datas/CCDatas.h" +#ifndef PT_RATIO +#define PT_RATIO 32 +#endif + + class b2Body; -struct b2Filter; +class b2Fixture; + +struct cpBody; +struct cpShape; NS_CC_EXT_BEGIN @@ -38,24 +46,36 @@ class CCBone; class ColliderBody : public CCObject { public: - ColliderBody(b2Body *b2b, CCContourData *contourData) - :m_pB2b(NULL) - ,m_pContourData(NULL) +#if ENABLE_PHYSICS_BOX2D_DETECT + ColliderBody(CCContourData *contourData) + : m_pFixture(NULL) + , m_pContourData(contourData) { - this->m_pB2b = b2b; - this->m_pContourData = contourData; CC_SAFE_RETAIN(m_pContourData); } +private: + CC_SYNTHESIZE(b2Fixture*, m_pFixture, B2Fixture) +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + + ColliderBody(CCContourData *contourData) + : m_pShape(NULL) + , m_pContourData(contourData) + { + CC_SAFE_RETAIN(m_pContourData); + } +private: + CC_SYNTHESIZE(cpShape*, m_pShape, Shape) + +#endif + + +public: ~ColliderBody() { CC_SAFE_RELEASE(m_pContourData); } - inline b2Body *getB2Body() - { - return m_pB2b; - } inline CCContourData *getContourData() { @@ -63,7 +83,6 @@ public: } private: - b2Body *m_pB2b; CCContourData *m_pContourData; }; @@ -90,14 +109,19 @@ public: void updateTransform(CCAffineTransform &t); - void setColliderFilter(b2Filter &filter); + void setActive(bool active); - void setActive(bool active); -private: - CCArray *m_pColliderBodyList; - + CCArray *getColliderBodyList(); + +protected: + CCArray *m_pColliderBodyList; CC_SYNTHESIZE(CCBone*, m_pBone, Bone); +#if ENABLE_PHYSICS_BOX2D_DETECT + CC_PROPERTY(b2Body*, m_pB2Body, B2Body); +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + CC_PROPERTY(cpBody*, m_pCPBody, CPBody); +#endif }; NS_CC_EXT_END diff --git a/extensions/CCArmature/physics/CCPhysicsWorld.cpp b/extensions/CCArmature/physics/CCPhysicsWorld.cpp deleted file mode 100644 index 8472f7130d..0000000000 --- a/extensions/CCArmature/physics/CCPhysicsWorld.cpp +++ /dev/null @@ -1,165 +0,0 @@ -/**************************************************************************** -Copyright (c) 2013 cocos2d-x.org - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - - -#include "CCPhysicsWorld.h" -#include "../utils/CCArmatureDefine.h" -#include "Box2D/Box2D.h" -#include "../external_tool/GLES-Render.h" - -NS_CC_EXT_BEGIN - - -class Contact -{ -public: - b2Fixture *fixtureA; - b2Fixture *fixtureB; -}; - -class ContactListener : public b2ContactListener -{ - //! Callbacks for derived classes. - virtual void BeginContact(b2Contact *contact) - { - if (contact) - { - Contact c; - c.fixtureA = contact->GetFixtureA(); - c.fixtureB = contact->GetFixtureB(); - - contact_list.push_back(c); - } - B2_NOT_USED(contact); - } - virtual void EndContact(b2Contact *contact) - { - contact_list.clear(); - B2_NOT_USED(contact); - } - virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) - { - B2_NOT_USED(contact); - B2_NOT_USED(oldManifold); - } - virtual void PostSolve(const b2Contact *contact, const b2ContactImpulse *impulse) - { - B2_NOT_USED(contact); - B2_NOT_USED(impulse); - } - -public: - std::list contact_list; -}; - - - -CCPhysicsWorld *CCPhysicsWorld::s_PhysicsWorld = NULL; - - -CCPhysicsWorld *CCPhysicsWorld::sharedPhysicsWorld() -{ - if (s_PhysicsWorld == NULL) - { - s_PhysicsWorld = new CCPhysicsWorld(); - s_PhysicsWorld->initNoGravityWorld(); - } - - return s_PhysicsWorld; -} - -void CCPhysicsWorld::purgePhysicsWorld() -{ - delete s_PhysicsWorld; - s_PhysicsWorld = NULL; -} - -CCPhysicsWorld::CCPhysicsWorld() - : m_pNoGravityWorld(NULL) - , m_pDebugDraw(NULL) -{ -} - -CCPhysicsWorld::~CCPhysicsWorld() -{ - CC_SAFE_DELETE(m_pDebugDraw); - CC_SAFE_DELETE(m_pNoGravityWorld); - CC_SAFE_DELETE(m_pContactListener); -} - -void CCPhysicsWorld::initNoGravityWorld() -{ - b2Vec2 noGravity(0, 0); - - m_pNoGravityWorld = new b2World(noGravity); - m_pNoGravityWorld->SetAllowSleeping(true); - - m_pContactListener = new ContactListener(); - m_pNoGravityWorld->SetContactListener(m_pContactListener); - - -#if ENABLE_PHYSICS_DEBUG - m_pDebugDraw = new GLESDebugDraw( PT_RATIO ); - m_pNoGravityWorld->SetDebugDraw(m_pDebugDraw); - - uint32 flags = 0; - flags += b2Draw::e_shapeBit; - // flags += b2Draw::e_jointBit; - // flags += b2Draw::e_aabbBit; - // flags += b2Draw::e_pairBit; - // flags += b2Draw::e_centerOfMassBit; - m_pDebugDraw->SetFlags(flags); -#endif -} - -b2World *CCPhysicsWorld::getNoGravityWorld() -{ - return m_pNoGravityWorld; -} - -void CCPhysicsWorld::update(float dt) -{ - m_pNoGravityWorld->Step(dt, 0, 0); - - for (std::list::iterator it = m_pContactListener->contact_list.begin(); it != m_pContactListener->contact_list.end(); ++it) - { - Contact &contact = *it; - - b2Body *b2a = contact.fixtureA->GetBody(); - b2Body *b2b = contact.fixtureB->GetBody(); - - CCBone *ba = (CCBone *)b2a->GetUserData(); - CCBone *bb = (CCBone *)b2b->GetUserData(); - - BoneColliderSignal.emit(ba, bb); - } - -} - -void CCPhysicsWorld::drawDebug() -{ - m_pNoGravityWorld->DrawDebugData(); -} - -NS_CC_EXT_END diff --git a/extensions/CCArmature/physics/CCPhysicsWorld.h b/extensions/CCArmature/physics/CCPhysicsWorld.h deleted file mode 100644 index 60d94b7e04..0000000000 --- a/extensions/CCArmature/physics/CCPhysicsWorld.h +++ /dev/null @@ -1,80 +0,0 @@ -/**************************************************************************** -Copyright (c) 2013 cocos2d-x.org - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#ifndef __CCPHYSICSWORLD_H__ -#define __CCPHYSICSWORLD_H__ - -#include "../utils/CCArmatureDefine.h" -#include "../CCBone.h" -#include "../external_tool/sigslot.h" - -#include -using std::list; - -#ifndef PT_RATIO -#define PT_RATIO 32 -#endif - -struct b2Manifold; -struct b2ContactImpulse; -class b2Fixture; -class b2Contact; -class b2World; - -NS_CC_EXT_BEGIN - -class ContactListener; -class GLESDebugDraw; - -class CCPhysicsWorld -{ -public: - static CCPhysicsWorld *sharedPhysicsWorld(); - static void purgePhysicsWorld(); - - void initNoGravityWorld(); -private: - CCPhysicsWorld(); - ~CCPhysicsWorld(); - -private: - static CCPhysicsWorld *s_PhysicsWorld; - - b2World *m_pNoGravityWorld; - - ContactListener *m_pContactListener; - - GLESDebugDraw *m_pDebugDraw; -public: - void update(float dt); - void drawDebug(); - - b2World *getNoGravityWorld(); - - sigslot::signal2 BoneColliderSignal; -}; - -NS_CC_EXT_END - -#endif/*__CCPHYSICSWORLD_H__*/ diff --git a/extensions/CCArmature/utils/CCArmatureDataManager.cpp b/extensions/CCArmature/utils/CCArmatureDataManager.cpp index cc12ad27d9..9d1bc62e5f 100644 --- a/extensions/CCArmature/utils/CCArmatureDataManager.cpp +++ b/extensions/CCArmature/utils/CCArmatureDataManager.cpp @@ -27,7 +27,6 @@ THE SOFTWARE. #include "CCTransformHelp.h" #include "CCDataReaderHelper.h" #include "CCSpriteFrameCacheHelper.h" -#include "../physics/CCPhysicsWorld.h" NS_CC_EXT_BEGIN @@ -67,8 +66,6 @@ CCArmatureDataManager::~CCArmatureDataManager(void) void CCArmatureDataManager::purgeArmatureSystem() { CCSpriteFrameCacheHelper::purgeSpriteFrameCacheHelper(); - CCPhysicsWorld::purgePhysicsWorld(); - CC_SAFE_RELEASE_NULL(s_sharedArmatureDataManager); } diff --git a/extensions/CCArmature/utils/CCArmatureDefine.h b/extensions/CCArmature/utils/CCArmatureDefine.h index 1e1eac7aff..79812fc43a 100644 --- a/extensions/CCArmature/utils/CCArmatureDefine.h +++ b/extensions/CCArmature/utils/CCArmatureDefine.h @@ -31,15 +31,14 @@ THE SOFTWARE. #include "ExtensionMacros.h" -#ifndef ENABLE_PHYSICS_DEBUG -#define ENABLE_PHYSICS_DEBUG 1 +#ifndef ENABLE_PHYSICS_BOX2D_DETECT +#define ENABLE_PHYSICS_BOX2D_DETECT 0 #endif -#ifndef ENABLE_PHYSICS_DETECT -#define ENABLE_PHYSICS_DETECT 1 +#ifndef ENABLE_PHYSICS_CHIPMUNK_DETECT +#define ENABLE_PHYSICS_CHIPMUNK_DETECT 1 #endif - #define MAX_VERTEXZ_VALUE 5000000.0f #define ARMATURE_MAX_CHILD 50.0f #define ARMATURE_MAX_ZORDER 100 diff --git a/extensions/cocos-ext.h b/extensions/cocos-ext.h index 8d6171d9c9..c8c2991fd8 100644 --- a/extensions/cocos-ext.h +++ b/extensions/cocos-ext.h @@ -50,7 +50,6 @@ #include "CCArmature/display/CCDisplayManager.h" #include "CCArmature/display/CCSkin.h" #include "CCArmature/physics/CCColliderDetector.h" -#include "CCArmature/physics/CCPhysicsWorld.h" #include "CCArmature/utils/CCArmatureDataManager.h" #include "CCArmature/utils/CCConstValue.h" #include "CCArmature/utils/CCDataReaderHelper.h" diff --git a/extensions/proj.win32/libExtensions.vcxproj b/extensions/proj.win32/libExtensions.vcxproj index 03d8f2d893..d92b44486a 100644 --- a/extensions/proj.win32/libExtensions.vcxproj +++ b/extensions/proj.win32/libExtensions.vcxproj @@ -104,13 +104,11 @@ - - @@ -205,7 +203,6 @@ - @@ -219,7 +216,6 @@ - diff --git a/extensions/proj.win32/libExtensions.vcxproj.filters b/extensions/proj.win32/libExtensions.vcxproj.filters index 52b9d2b51e..43f706349c 100644 --- a/extensions/proj.win32/libExtensions.vcxproj.filters +++ b/extensions/proj.win32/libExtensions.vcxproj.filters @@ -255,18 +255,12 @@ CCArmature\physics - - CCArmature\physics - CCArmature CCArmature - - CCArmature\external_tool - CCArmature\external_tool\Json @@ -589,18 +583,12 @@ CCArmature\physics - - CCArmature\physics - CCArmature CCArmature - - CCArmature\external_tool - CCArmature\external_tool\Json diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp index e32d4a6792..ad97c8da9b 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp @@ -1,5 +1,7 @@ #include "ArmatureScene.h" #include "../../testResource.h" +#include "D:/Program Files/Visual Leak Detector\include/vld.h" + using namespace cocos2d; using namespace cocos2d::extension; @@ -32,8 +34,8 @@ CCLayer *CreateLayer(int index) pLayer = new TestParticleDisplay(); break; case TEST_USE_DIFFERENT_PICTURE: pLayer = new TestUseMutiplePicture(); break; - case TEST_BOX2D_DETECTOR: - pLayer = new TestBox2DDetector(); break; + case TEST_BCOLLIDER_DETECTOR: + pLayer = new TestColliderDetector(); break; case TEST_BOUDINGBOX: pLayer = new TestBoundingBox(); break; case TEST_ANCHORPOINT: @@ -98,13 +100,13 @@ ArmatureTestScene::ArmatureTestScene(bool bPortrait) void ArmatureTestScene::runThisTest() { CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/TestBone0.png", "armature/TestBone0.plist", "armature/TestBone.json"); - CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/Cowboy0.png", "armature/Cowboy0.plist", "armature/Cowboy.json"); + CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/Cowboy0.png", "armature/Cowboy0.plist", "armature/Cowboy.ExportJson"); CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/knight.png", "armature/knight.plist", "armature/knight.xml"); CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/weapon.png", "armature/weapon.plist", "armature/weapon.xml"); CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/robot.png", "armature/robot.plist", "armature/robot.xml"); CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/cyborg.png", "armature/cyborg.plist", "armature/cyborg.xml"); CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/Dragon.png", "armature/Dragon.plist", "armature/Dragon.xml"); - + s_nActionIdx = -1; addChild(NextTest()); @@ -513,15 +515,121 @@ void TestUseMutiplePicture::registerWithTouchDispatcher() -void TestBox2DDetector::onEnter() + + + +#if ENABLE_PHYSICS_BOX2D_DETECT + +class Contact +{ +public: + b2Fixture *fixtureA; + b2Fixture *fixtureB; +}; + +class ContactListener : public b2ContactListener +{ + //! Callbacks for derived classes. + virtual void BeginContact(b2Contact *contact) + { + if (contact) + { + Contact c; + c.fixtureA = contact->GetFixtureA(); + c.fixtureB = contact->GetFixtureB(); + + contact_list.push_back(c); + } + B2_NOT_USED(contact); + } + virtual void EndContact(b2Contact *contact) + { + contact_list.clear(); + B2_NOT_USED(contact); + } + virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) + { + B2_NOT_USED(contact); + B2_NOT_USED(oldManifold); + } + virtual void PostSolve(const b2Contact *contact, const b2ContactImpulse *impulse) + { + B2_NOT_USED(contact); + B2_NOT_USED(impulse); + } + +public: + std::list contact_list; +}; + +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + +enum ColliderType +{ + eBulletTag, + eEnemyTag +}; + + +int TestColliderDetector::beginHit(cpArbiter *arb, cpSpace *space, void *unused) +{ + CP_ARBITER_GET_SHAPES(arb, a, b); + + CCBone *bone = (CCBone*)a->data; + bone->getArmature()->setVisible(false); + + return 0; +} + +void TestColliderDetector::endHit(cpArbiter *arb, cpSpace *space, void *unused) +{ + CP_ARBITER_GET_SHAPES(arb, a, b); + + CCBone *bone = (CCBone*)a->data; + bone->getArmature()->setVisible(true); +} + +void TestColliderDetector::destroyCPBody(cpBody *body) +{ + cpShape *shape = body->shapeList_private; + while(shape) + { + cpShape *temp = shape->next_private; + + cpSpaceRemoveShape(space, shape); + cpShapeFree(shape); + + shape = temp; + } + + cpSpaceRemoveBody(space, body); + cpBodyFree(body); +} +#endif + +TestColliderDetector::~TestColliderDetector() +{ +#if ENABLE_PHYSICS_BOX2D_DETECT + CC_SAFE_DELETE(world); + CC_SAFE_DELETE(listener); + CC_SAFE_DELETE(debugDraw); +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + destroyCPBody(armature2->getCPBody()); + destroyCPBody(bullet->getCPBody()); + + cpSpaceFree(space); +#endif +} + +void TestColliderDetector::onEnter() { ArmatureTestLayer::onEnter(); scheduleUpdate(); armature = cocos2d::extension::CCArmature::create("Cowboy"); - armature->getAnimation()->play("Fire"); - armature->getAnimation()->setAnimationScale(0.1f); + armature->getAnimation()->play("FireWithoutBullet"); + armature->getAnimation()->setAnimationScale(0.2f); armature->setScaleX(-0.2f); armature->setScaleY(0.2f); armature->setPosition(ccp(VisibleRect::left().x + 70, VisibleRect::left().y)); @@ -534,36 +642,152 @@ void TestBox2DDetector::onEnter() armature2->setPosition(ccp(VisibleRect::right().x - 30, VisibleRect::left().y)); addChild(armature2); - CCPhysicsWorld::sharedPhysicsWorld()->BoneColliderSignal.connect(this, &TestBox2DDetector::onHit); + bullet = CCPhysicsSprite::createWithSpriteFrameName("25.png"); + addChild(bullet); + + initWorld(); } -std::string TestBox2DDetector::title() +std::string TestColliderDetector::title() { return "Test Box2D Detector"; } -void TestBox2DDetector::draw() +void TestColliderDetector::draw() { +#if ENABLE_PHYSICS_BOX2D_DETECT ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); - kmGLPushMatrix(); - - CCPhysicsWorld::sharedPhysicsWorld()->drawDebug(); - + world->DrawDebugData(); kmGLPopMatrix(); - +#endif } -void TestBox2DDetector::update(float delta) +void TestColliderDetector::update(float delta) { +#if ENABLE_PHYSICS_BOX2D_DETECT armature2->setVisible(true); - CCPhysicsWorld::sharedPhysicsWorld()->update(delta); +#endif + + if (armature->getAnimation()->getCurrentFrameIndex() == 9) + { + CCPoint p = armature->getBone("Layer126")->getDisplayRenderNode()->convertToWorldSpaceAR(ccp(0, 0)); + bullet->setPosition(ccp(p.x + 60, p.y)); + + bullet->stopAllActions(); + bullet->runAction(CCMoveBy::create(1.5f, ccp(350, 0))); + } + +#if ENABLE_PHYSICS_BOX2D_DETECT + world->Step(delta, 0, 0); + + for (std::list::iterator it = listener->contact_list.begin(); it != listener->contact_list.end(); ++it) + { + Contact &contact = *it; + + b2Body *b2a = contact.fixtureA->GetBody(); + b2Body *b2b = contact.fixtureB->GetBody(); + + CCBone *ba = (CCBone *)b2a->GetUserData(); + CCBone *bb = (CCBone *)b2b->GetUserData(); + + bb->getArmature()->setVisible(false); + } +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + cpSpaceStep(space, delta); +#endif } -void TestBox2DDetector::onHit(cocos2d::extension::CCBone *bone, cocos2d::extension::CCBone *bone2) +void TestColliderDetector::initWorld() { - armature2->setVisible(false); +#if ENABLE_PHYSICS_BOX2D_DETECT + b2Vec2 noGravity(0, 0); + + world = new b2World(noGravity); + world->SetAllowSleeping(true); + + listener = new ContactListener(); + world->SetContactListener(listener); + + debugDraw = new GLESDebugDraw( PT_RATIO ); + world->SetDebugDraw(debugDraw); + + uint32 flags = 0; + flags += b2Draw::e_shapeBit; + // flags += b2Draw::e_jointBit; + // flags += b2Draw::e_aabbBit; + // flags += b2Draw::e_pairBit; + // flags += b2Draw::e_centerOfMassBit; + debugDraw->SetFlags(flags); + + + // Define the dynamic body. + //Set up a 1m squared box in the physics world + b2BodyDef bodyDef; + bodyDef.type = b2_dynamicBody; + + b2Body *body = world->CreateBody(&bodyDef); + + // Define another box shape for our dynamic body. + b2PolygonShape dynamicBox; + dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box + + // Define the dynamic body fixture. + b2FixtureDef fixtureDef; + fixtureDef.shape = &dynamicBox; + fixtureDef.isSensor = true; + body->CreateFixture(&fixtureDef); + + + bullet->setB2Body(body); + bullet->setPTMRatio(PT_RATIO); + bullet->setPosition( ccp( -100, -100) ); + + body = world->CreateBody(&bodyDef); + armature2->setB2Body(body); + +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + space = cpSpaceNew(); + space->gravity = cpv(0, 0); + + // Physics debug layer + CCPhysicsDebugNode *debugLayer = CCPhysicsDebugNode::create(space); + this->addChild(debugLayer, INT_MAX); + + CCSize size = bullet->getContentSize(); + + int num = 4; + cpVect verts[] = { + cpv(-size.width/2,-size.height/2), + cpv(-size.width/2,size.height/2), + cpv(size.width/2,size.height/2), + cpv(size.width/2,-size.height/2), + }; + + cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)); + cpSpaceAddBody(space, body); + + cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero); + shape->collision_type = eBulletTag; + cpSpaceAddShape(space, shape); + + bullet->setCPBody(body); + + body = cpBodyNew(INFINITY, INFINITY); + cpSpaceAddBody(space, body); + armature2->setCPBody(body); + + shape = body->shapeList_private; + while(shape){ + cpShape *next = shape->next_private; + shape->collision_type = eEnemyTag; + shape = next; + } + + cpSpaceAddCollisionHandler(space, eEnemyTag, eBulletTag, beginHit, NULL, NULL, endHit, NULL); +#endif } + void TestBoundingBox::onEnter() { ArmatureTestLayer::onEnter(); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.h index a998d1f990..67576a3746 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.h @@ -6,6 +6,13 @@ #include "../../VisibleRect.h" #include "../../testBasic.h" +#if ENABLE_PHYSICS_BOX2D_DETECT +#include "../../Box2DTestBed/GLES-Render.h" +#include "Box2D/Box2D.h" +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT +#include "chipmunk.h" +#endif + class ArmatureTestScene : public TestScene { public: @@ -26,7 +33,7 @@ enum { TEST_ANIMATION_EVENT, TEST_PARTICLE_DISPLAY, TEST_USE_DIFFERENT_PICTURE, - TEST_BOX2D_DETECTOR, + TEST_BCOLLIDER_DETECTOR, TEST_BOUDINGBOX, TEST_ANCHORPOINT, TEST_ARMATURE_NESTING, @@ -141,20 +148,43 @@ class TestParticleDisplay : public ArmatureTestLayer cocos2d::extension::CCArmature *armature; }; -class TestBox2DDetector : public ArmatureTestLayer, public sigslot::has_slots<> +#if ENABLE_PHYSICS_BOX2D_DETECT +class ContactListener; +#endif + +class TestColliderDetector : public ArmatureTestLayer, public sigslot::has_slots<> { public: + ~TestColliderDetector(); + virtual void onEnter(); virtual std::string title(); virtual void draw(); virtual void update(float delta); - void onHit(cocos2d::extension::CCBone *bone, cocos2d::extension::CCBone *bone2); + void initWorld(); cocos2d::extension::CCArmature *armature; cocos2d::extension::CCArmature *armature2; + + cocos2d::extension::CCPhysicsSprite *bullet; + +#if ENABLE_PHYSICS_BOX2D_DETECT + b2World *world; + ContactListener *listener; + GLESDebugDraw *debugDraw; +#elif ENABLE_PHYSICS_CHIPMUNK_DETECT + cpSpace *space; + + static int beginHit(cpArbiter *arb, cpSpace *space, void *unused); + static void endHit(cpArbiter *arb, cpSpace *space, void *unused); + + void destroyCPBody(cpBody *body); +#endif }; + + class TestBoundingBox : public ArmatureTestLayer { public: diff --git a/samples/Cpp/TestCpp/Resources/armature/Cowboy0.png.REMOVED.git-id b/samples/Cpp/TestCpp/Resources/armature/Cowboy0.png.REMOVED.git-id index 9bac07216b..d2c12af0e0 100644 --- a/samples/Cpp/TestCpp/Resources/armature/Cowboy0.png.REMOVED.git-id +++ b/samples/Cpp/TestCpp/Resources/armature/Cowboy0.png.REMOVED.git-id @@ -1 +1 @@ -db891b84393bfe4b806f46bbf9c2879fa9438608 \ No newline at end of file +2b61d3b005b4076c5ff22a78a00822c0f0dc0875 \ No newline at end of file