Merge branch 'master' into develop

This commit is contained in:
yinkaile 2013-07-16 09:37:56 +08:00
commit f6c59928b2
25 changed files with 590 additions and 716 deletions

View File

@ -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<CCBone*>(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<CCBone*>(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

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -313,10 +313,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, keyFrameData->sound);

View File

@ -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
}

View File

@ -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:

View File

@ -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)
{

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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 <stdio.h>
#include <stdarg.h>
#include <string.h>
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;i<vertexCount;i++)
{
vertices[i] = old_vertices[i];
vertices[i] *= mRatio;
}
mShaderProgram->setUniformLocationWith4f(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;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(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

View File

@ -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

View File

@ -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; i<num; i++)
{
verts[num-1-i].x = vs[i]->x;
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

View File

@ -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

View File

@ -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> 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<Contact>::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

View File

@ -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 <list>
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<CCBone *, CCBone *> BoneColliderSignal;
};
NS_CC_EXT_END
#endif/*__CCPHYSICSWORLD_H__*/

View File

@ -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);
}

View File

@ -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

View File

@ -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"

View File

@ -104,13 +104,11 @@
<ClCompile Include="..\CCArmature\display\CCDisplayManager.cpp" />
<ClCompile Include="..\CCArmature\display\CCSkin.cpp" />
<ClCompile Include="..\CCArmature\external_tool\CCTexture2DMutable.cpp" />
<ClCompile Include="..\CCArmature\external_tool\GLES-Render.cpp" />
<ClCompile Include="..\CCArmature\external_tool\Json\CSContentJsonDictionary.cpp" />
<ClCompile Include="..\CCArmature\external_tool\Json\lib_json\json_reader.cpp" />
<ClCompile Include="..\CCArmature\external_tool\Json\lib_json\json_value.cpp" />
<ClCompile Include="..\CCArmature\external_tool\Json\lib_json\json_writer.cpp" />
<ClCompile Include="..\CCArmature\physics\CCColliderDetector.cpp" />
<ClCompile Include="..\CCArmature\physics\CCPhysicsWorld.cpp" />
<ClCompile Include="..\CCArmature\utils\CCArmatureDataManager.cpp" />
<ClCompile Include="..\CCArmature\utils\CCDataReaderHelper.cpp" />
<ClCompile Include="..\CCArmature\utils\CCSpriteFrameCacheHelper.cpp" />
@ -205,7 +203,6 @@
<ClInclude Include="..\CCArmature\display\CCDisplayManager.h" />
<ClInclude Include="..\CCArmature\display\CCSkin.h" />
<ClInclude Include="..\CCArmature\external_tool\CCTexture2DMutable.h" />
<ClInclude Include="..\CCArmature\external_tool\GLES-Render.h" />
<ClInclude Include="..\CCArmature\external_tool\Json\CSContentJsonDictionary.h" />
<ClInclude Include="..\CCArmature\external_tool\Json\lib_json\autolink.h" />
<ClInclude Include="..\CCArmature\external_tool\Json\lib_json\config.h" />
@ -219,7 +216,6 @@
<ClInclude Include="..\CCArmature\external_tool\Json\lib_json\writer.h" />
<ClInclude Include="..\CCArmature\external_tool\sigslot.h" />
<ClInclude Include="..\CCArmature\physics\CCColliderDetector.h" />
<ClInclude Include="..\CCArmature\physics\CCPhysicsWorld.h" />
<ClInclude Include="..\CCArmature\utils\CCArmatureDataManager.h" />
<ClInclude Include="..\CCArmature\utils\CCArmatureDefine.h" />
<ClInclude Include="..\CCArmature\utils\CCConstValue.h" />

View File

@ -255,18 +255,12 @@
<ClCompile Include="..\CCArmature\physics\CCColliderDetector.cpp">
<Filter>CCArmature\physics</Filter>
</ClCompile>
<ClCompile Include="..\CCArmature\physics\CCPhysicsWorld.cpp">
<Filter>CCArmature\physics</Filter>
</ClCompile>
<ClCompile Include="..\CCArmature\CCArmature.cpp">
<Filter>CCArmature</Filter>
</ClCompile>
<ClCompile Include="..\CCArmature\CCBone.cpp">
<Filter>CCArmature</Filter>
</ClCompile>
<ClCompile Include="..\CCArmature\external_tool\GLES-Render.cpp">
<Filter>CCArmature\external_tool</Filter>
</ClCompile>
<ClCompile Include="..\CCArmature\external_tool\Json\CSContentJsonDictionary.cpp">
<Filter>CCArmature\external_tool\Json</Filter>
</ClCompile>
@ -589,18 +583,12 @@
<ClInclude Include="..\CCArmature\physics\CCColliderDetector.h">
<Filter>CCArmature\physics</Filter>
</ClInclude>
<ClInclude Include="..\CCArmature\physics\CCPhysicsWorld.h">
<Filter>CCArmature\physics</Filter>
</ClInclude>
<ClInclude Include="..\CCArmature\CCArmature.h">
<Filter>CCArmature</Filter>
</ClInclude>
<ClInclude Include="..\CCArmature\CCBone.h">
<Filter>CCArmature</Filter>
</ClInclude>
<ClInclude Include="..\CCArmature\external_tool\GLES-Render.h">
<Filter>CCArmature\external_tool</Filter>
</ClInclude>
<ClInclude Include="..\CCArmature\external_tool\Json\CSContentJsonDictionary.h">
<Filter>CCArmature\external_tool\Json</Filter>
</ClInclude>

View File

@ -1,6 +1,7 @@
#include "ArmatureScene.h"
#include "../../testResource.h"
using namespace cocos2d;
using namespace cocos2d::extension;
@ -32,8 +33,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,7 +99,7 @@ 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");
@ -513,15 +514,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> 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 +641,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<Contact>::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();

View File

@ -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:

View File

@ -1 +1 @@
db891b84393bfe4b806f46bbf9c2879fa9438608
2b61d3b005b4076c5ff22a78a00822c0f0dc0875