axmol/extensions/CocoStudio/Armature/physics/CCColliderDetector.cpp

397 lines
9.8 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/****************************************************************************
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 "CCColliderDetector.h"
#include "../CCBone.h"
2013-09-13 18:07:37 +08:00
#include "../utils/CCTransformHelp.h"
#if ENABLE_PHYSICS_BOX2D_DETECT
2013-06-07 10:52:32 +08:00
#include "Box2D/Box2D.h"
2013-09-13 18:07:37 +08:00
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
#include "chipmunk.h"
#endif
NS_CC_EXT_ARMATURE_BEGIN
#if ENABLE_PHYSICS_BOX2D_DETECT
ColliderBody::ColliderBody(ContourData *contourData)
2013-09-15 20:24:25 +08:00
: _fixture(NULL)
, _filter(NULL)
, _contourData(contourData)
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
CC_SAFE_RETAIN(_contourData);
2013-09-13 18:07:37 +08:00
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
ColliderBody::ColliderBody(ContourData *contourData)
2013-09-15 20:24:25 +08:00
: _shape(NULL)
, _contourData(contourData)
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
CC_SAFE_RETAIN(_contourData);
2013-09-13 18:07:37 +08:00
}
#endif
ColliderBody::~ColliderBody()
{
2013-09-15 20:24:25 +08:00
CC_SAFE_RELEASE(_contourData);
2013-09-13 18:07:37 +08:00
#if ENABLE_PHYSICS_BOX2D_DETECT
2013-09-15 20:24:25 +08:00
CC_SAFE_DELETE(_filter);
2013-09-13 18:07:37 +08:00
#endif
}
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
ColliderDetector *ColliderDetector::create()
2013-06-06 12:02:54 +08:00
{
ColliderDetector *pColliderDetector = new ColliderDetector();
2013-06-07 10:52:32 +08:00
if (pColliderDetector && pColliderDetector->init())
{
pColliderDetector->autorelease();
return pColliderDetector;
}
CC_SAFE_DELETE(pColliderDetector);
return NULL;
2013-06-06 12:02:54 +08:00
}
ColliderDetector *ColliderDetector::create(Bone *bone)
2013-06-06 12:02:54 +08:00
{
ColliderDetector *pColliderDetector = new ColliderDetector();
2013-06-07 10:52:32 +08:00
if (pColliderDetector && pColliderDetector->init(bone))
{
pColliderDetector->autorelease();
return pColliderDetector;
}
CC_SAFE_DELETE(pColliderDetector);
return NULL;
2013-06-06 12:02:54 +08:00
}
ColliderDetector::ColliderDetector()
2013-09-15 20:24:25 +08:00
: _colliderBodyList(NULL)
, _active(false)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_body = NULL;
2013-06-06 12:02:54 +08:00
}
ColliderDetector::~ColliderDetector()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_colliderBodyList->removeAllObjects();
CC_SAFE_DELETE(_colliderBodyList);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
bool ColliderDetector::init()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_colliderBodyList = Array::create();
2013-09-16 12:02:57 +08:00
CCASSERT(_colliderBodyList, "create _colliderBodyList failed!");
2013-09-15 20:24:25 +08:00
_colliderBodyList->retain();
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
return true;
}
bool ColliderDetector::init(Bone *bone)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
init();
setBone(bone);
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
return true;
2013-06-06 12:02:54 +08:00
}
void ColliderDetector::addContourData(ContourData *contourData)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
ColliderBody *colliderBody = new ColliderBody(contourData);
2013-09-15 20:24:25 +08:00
_colliderBodyList->addObject(colliderBody);
2013-09-13 18:07:37 +08:00
colliderBody->release();
}
2013-06-07 10:52:32 +08:00
void ColliderDetector::addContourDataList(Array *contourDataList)
2013-09-13 18:07:37 +08:00
{
Object *object = NULL;
CCARRAY_FOREACH(contourDataList, object)
2013-06-07 10:52:32 +08:00
{
addContourData((ContourData *)object);
2013-06-07 10:52:32 +08:00
}
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
void ColliderDetector::removeContourData(ContourData *contourData)
2013-09-13 18:07:37 +08:00
{
Object *object = NULL;
2013-09-15 20:24:25 +08:00
CCARRAY_FOREACH(_colliderBodyList, object)
2013-09-13 18:07:37 +08:00
{
ColliderBody *body = (ColliderBody*)object;
if (body && body->getContourData() == contourData)
{
2013-09-15 20:24:25 +08:00
_colliderBodyList->removeObject(body);
2013-09-13 18:07:37 +08:00
}
}
}
2013-06-07 10:52:32 +08:00
void ColliderDetector::removeAll()
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
_colliderBodyList->removeAllObjects();
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
void ColliderDetector::setActive(bool active)
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
if (_active == active)
2013-09-13 18:07:37 +08:00
{
return;
}
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_active = active;
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
#if ENABLE_PHYSICS_BOX2D_DETECT
2013-09-15 20:24:25 +08:00
if (_body)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
if (active)
{
2013-09-15 20:24:25 +08:00
setBody(_body);
2013-09-13 18:07:37 +08:00
}
else
{
Object *object = NULL;
2013-09-15 20:24:25 +08:00
CCARRAY_FOREACH(_colliderBodyList, object)
2013-09-13 18:07:37 +08:00
{
ColliderBody *colliderBody = (ColliderBody *)object;
b2Fixture *fixture = colliderBody->getB2Fixture();
b2Filter *filter = colliderBody->getB2Filter();
*filter = fixture->GetFilterData();
2013-09-15 20:24:25 +08:00
_body->DestroyFixture(fixture);
2013-09-13 18:07:37 +08:00
colliderBody->setB2Fixture(NULL);
}
}
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
2013-09-15 20:24:25 +08:00
if (_body)
2013-09-13 18:07:37 +08:00
{
Object *object = NULL;
2013-09-15 20:24:25 +08:00
if (_active)
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
CCARRAY_FOREACH(_colliderBodyList, object)
2013-09-13 18:07:37 +08:00
{
ColliderBody *colliderBody = (ColliderBody *)object;
cpShape *shape = colliderBody->getShape();
2013-09-15 20:24:25 +08:00
cpSpaceAddShape(_body->space_private, shape);
2013-09-13 18:07:37 +08:00
}
}
else
{
2013-09-15 20:24:25 +08:00
CCARRAY_FOREACH(_colliderBodyList, object)
2013-09-13 18:07:37 +08:00
{
ColliderBody *colliderBody = (ColliderBody *)object;
cpShape *shape = colliderBody->getShape();
2013-09-15 20:24:25 +08:00
cpSpaceRemoveShape(_body->space_private, shape);
2013-09-13 18:07:37 +08:00
}
}
2013-06-07 10:52:32 +08:00
}
2013-09-13 18:07:37 +08:00
#endif
2013-06-06 12:02:54 +08:00
}
bool ColliderDetector::getActive()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
return _active;
2013-06-06 12:02:54 +08:00
}
Array *ColliderDetector::getColliderBodyList()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
return _colliderBodyList;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
Point helpPoint;
void ColliderDetector::updateTransform(AffineTransform &t)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
if (!_active)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
return;
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
Object *object = NULL;
2013-09-15 20:24:25 +08:00
CCARRAY_FOREACH(_colliderBodyList, object)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
ColliderBody *colliderBody = (ColliderBody *)object;
ContourData *contourData = colliderBody->getContourData();
2013-09-13 18:07:37 +08:00
#if ENABLE_PHYSICS_BOX2D_DETECT
b2PolygonShape *shape = NULL;
2013-09-15 20:24:25 +08:00
if (_body != NULL)
2013-09-13 18:07:37 +08:00
{
shape = (b2PolygonShape *)colliderBody->getB2Fixture()->GetShape();
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
cpPolyShape *shape = NULL;
2013-09-15 20:24:25 +08:00
if (_body != NULL)
2013-09-13 18:07:37 +08:00
{
shape = (cpPolyShape *)colliderBody->getShape();
}
#endif
int num = contourData->vertexList.count();
ContourVertex2 **vs = (ContourVertex2 **)contourData->vertexList.data->arr;
2013-09-13 18:07:37 +08:00
for (int i = 0; i < num; i++)
{
helpPoint.setPoint( vs[i]->x, vs[i]->y);
helpPoint = PointApplyAffineTransform(helpPoint, t);
#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->verts[i] = v;
}
#endif
}
#if ENABLE_PHYSICS_CHIPMUNK_DETECT
cpConvexHull(num, shape->verts, NULL, NULL, 0);
for (int i = 0; i < num; 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
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
#if ENABLE_PHYSICS_BOX2D_DETECT
2013-06-06 12:02:54 +08:00
void ColliderDetector::setBody(b2Body *pBody)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_body = pBody;
2013-09-13 18:07:37 +08:00
Object *object = NULL;
2013-09-15 20:24:25 +08:00
CCARRAY_FOREACH(_colliderBodyList, object)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
ColliderBody *colliderBody = (ColliderBody *)object;
2013-06-07 10:52:32 +08:00
ContourData *contourData = colliderBody->getContourData();
2013-09-13 18:07:37 +08:00
const Array *array = &contourData->vertexList;
Object *object = NULL;
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
b2Vec2 *b2bv = new b2Vec2[contourData->vertexList.count()];
2013-06-07 10:52:32 +08:00
int i = 0;
CCARRAY_FOREACH(array, object)
{
ContourVertex2 *v = (ContourVertex2 *)object;
2013-09-13 18:07:37 +08:00
b2bv[i].Set(v->x / PT_RATIO, v->y / PT_RATIO);
i++;
}
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
b2PolygonShape polygon;
polygon.Set(b2bv, contourData->vertexList.count());
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
CC_SAFE_DELETE(b2bv);
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
b2FixtureDef fixtureDef;
fixtureDef.shape = &polygon;
fixtureDef.isSensor = true;
2013-09-15 20:24:25 +08:00
b2Fixture *fixture = _body->CreateFixture(&fixtureDef);
fixture->SetUserData(_bone);
2013-09-13 18:07:37 +08:00
if (colliderBody->getB2Fixture() != NULL)
{
2013-09-15 20:24:25 +08:00
_body->DestroyFixture(colliderBody->getB2Fixture());
2013-09-13 18:07:37 +08:00
}
colliderBody->setB2Fixture(fixture);
if (colliderBody->getB2Filter() == NULL)
{
b2Filter *filter = new b2Filter;
colliderBody->setB2Filter(filter);
}
else
{
fixture->SetFilterData(*colliderBody->getB2Filter());
}
}
}
b2Body *ColliderDetector::getBody()
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
return _body;
2013-09-13 18:07:37 +08:00
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
void ColliderDetector::setBody(cpBody *pBody)
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
_body = pBody;
2013-09-13 18:07:37 +08:00
Object *object = NULL;
2013-09-15 20:24:25 +08:00
CCARRAY_FOREACH(_colliderBodyList, object)
2013-09-13 18:07:37 +08:00
{
ColliderBody *colliderBody = (ColliderBody *)object;
ContourData *contourData = colliderBody->getContourData();
2013-09-13 18:07:37 +08:00
int num = contourData->vertexList.count();
ContourVertex2 **vs = (ContourVertex2 **)contourData->vertexList.data->arr;
2013-09-13 18:07:37 +08:00
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;
2013-06-07 10:52:32 +08:00
}
2013-09-13 18:07:37 +08:00
2013-09-15 20:24:25 +08:00
cpShape *shape = cpPolyShapeNew(_body, num, verts, cpvzero);
2013-09-13 18:07:37 +08:00
shape->sensor = true;
2013-09-15 20:24:25 +08:00
shape->data = _bone;
cpSpaceAddShape(_body->space_private, shape);
2013-09-13 18:07:37 +08:00
colliderBody->setShape(shape);
delete []verts;
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
cpBody *ColliderDetector::getBody()
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
return _body;
2013-09-13 18:07:37 +08:00
}
#endif
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
NS_CC_EXT_ARMATURE_END