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 "CCPhysicsWorld.h"
|
|
|
|
#include "../CCBone.h"
|
2013-06-07 10:52:32 +08:00
|
|
|
#include "Box2D/Box2D.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
namespace cocos2d { namespace extension { namespace armature {
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ColliderDetector *ColliderDetector::create()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +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
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ColliderDetector *ColliderDetector::create(Bone *bone)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +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
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ColliderDetector::ColliderDetector()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _colliderBodyList(NULL)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ColliderDetector::~ColliderDetector()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_colliderBodyList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
ColliderBody *colliderBody = static_cast<ColliderBody *>(object);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
b2Body *body = colliderBody->getB2Body();
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsWorld::sharedPhysicsWorld()->getNoGravityWorld()->DestroyBody(body);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_colliderBodyList->removeAllObjects();
|
|
|
|
CC_SAFE_DELETE(_colliderBodyList);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ColliderDetector::init()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
_colliderBodyList = Array::create();
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_colliderBodyList, "create _colliderBodyList failed!");
|
2013-06-15 14:03:30 +08:00
|
|
|
_colliderBodyList->retain();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ColliderDetector::addContourData(ContourData *contourData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-08-22 11:12:09 +08:00
|
|
|
const Array *array = contourData->vertexList;
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-08-22 11:12:09 +08:00
|
|
|
b2Vec2 *b2bv = new b2Vec2[contourData->vertexList->count()];
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
CCARRAY_FOREACH(array, object)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
ContourVertex2F *v = static_cast<ContourVertex2F *>(object);
|
2013-06-07 10:52:32 +08:00
|
|
|
b2bv[i].Set(v->x / PT_RATIO, v->y / PT_RATIO);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
b2PolygonShape polygon;
|
2013-08-22 11:12:09 +08:00
|
|
|
polygon.Set(b2bv, contourData->vertexList->count());
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
bodyDef.userData = _bone;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
b2Body *body = PhysicsWorld::sharedPhysicsWorld()->getNoGravityWorld()->CreateBody(&bodyDef);
|
2013-06-07 10:52:32 +08:00
|
|
|
body->CreateFixture(&fixtureDef);
|
|
|
|
|
|
|
|
ColliderBody *colliderBody = new ColliderBody(body, contourData);
|
2013-06-15 14:03:30 +08:00
|
|
|
_colliderBodyList->addObject(colliderBody);
|
2013-06-07 10:52:32 +08:00
|
|
|
colliderBody->release();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ColliderDetector::addContourDataList(Array *contourDataList)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-07 10:52:32 +08:00
|
|
|
CCARRAY_FOREACH(contourDataList, object)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
addContourData(static_cast<ContourData *>(object));
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ColliderDetector::removeContourData(ContourData *_contourData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_colliderBodyList->removeObject(_contourData);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ColliderDetector::removeAll()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_colliderBodyList->removeAllObjects();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ColliderDetector::setColliderFilter(b2Filter &filter)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_colliderBodyList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
ColliderBody *colliderBody = static_cast<ColliderBody *>(object);
|
2013-06-07 10:52:32 +08:00
|
|
|
colliderBody->getB2Body()->GetFixtureList()->SetFilterData(filter);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ColliderDetector::setActive(bool active)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_colliderBodyList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
ColliderBody *colliderBody = static_cast<ColliderBody *>(object);
|
2013-06-07 10:52:32 +08:00
|
|
|
colliderBody->getB2Body()->SetActive(active);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point helpPoint;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ColliderDetector::updateTransform(AffineTransform &t)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_colliderBodyList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
ColliderBody *colliderBody = static_cast<ColliderBody *>(object);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ContourData *contourData = colliderBody->getContourData();
|
2013-06-07 10:52:32 +08:00
|
|
|
b2Body *body = colliderBody->getB2Body();
|
|
|
|
|
|
|
|
b2PolygonShape *shape = (b2PolygonShape *)body->GetFixtureList()->GetShape();
|
|
|
|
|
|
|
|
//! update every vertex
|
2013-08-22 11:12:09 +08:00
|
|
|
const Array *array = contourData->vertexList;
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-07 10:52:32 +08:00
|
|
|
int i = 0;
|
|
|
|
CCARRAY_FOREACH(array, object)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
ContourVertex2F *cv = static_cast<ContourVertex2F *>(object);
|
2013-06-07 10:52:32 +08:00
|
|
|
b2Vec2 &bv = shape->m_vertices[i];
|
|
|
|
|
|
|
|
helpPoint.setPoint(cv->x, cv->y);
|
2013-06-20 14:15:53 +08:00
|
|
|
helpPoint = PointApplyAffineTransform(helpPoint, t);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
bv.Set(helpPoint.x / PT_RATIO, helpPoint.y / PT_RATIO);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
}}} // namespace cocos2d { namespace extension { namespace armature {
|