axmol/extensions/CCArmature/physics/CCColliderDetector.cpp

207 lines
5.7 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 "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
NS_CC_EXT_BEGIN
CCColliderDetector *CCColliderDetector::create()
{
2013-06-07 10:52:32 +08:00
CCColliderDetector *pColliderDetector = new CCColliderDetector();
if (pColliderDetector && pColliderDetector->init())
{
pColliderDetector->autorelease();
return pColliderDetector;
}
CC_SAFE_DELETE(pColliderDetector);
return NULL;
2013-06-06 12:02:54 +08:00
}
CCColliderDetector *CCColliderDetector::create(CCBone *bone)
{
2013-06-07 10:52:32 +08:00
CCColliderDetector *pColliderDetector = new CCColliderDetector();
if (pColliderDetector && pColliderDetector->init(bone))
{
pColliderDetector->autorelease();
return pColliderDetector;
}
CC_SAFE_DELETE(pColliderDetector);
return NULL;
2013-06-06 12:02:54 +08:00
}
CCColliderDetector::CCColliderDetector()
2013-06-07 10:52:32 +08:00
: m_pColliderBodyList(NULL)
2013-06-06 12:02:54 +08:00
{
}
CCColliderDetector::~CCColliderDetector()
{
2013-06-07 10:52:32 +08:00
CCObject *object = NULL;
CCARRAY_FOREACH(m_pColliderBodyList, object)
{
ColliderBody *colliderBody = (ColliderBody *)object;
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
b2Body *body = colliderBody->getB2Body();
CCPhysicsWorld::sharedPhysicsWorld()->getNoGravityWorld()->DestroyBody(body);
}
2013-06-06 12:02:54 +08:00
m_pColliderBodyList->removeAllObjects();
CC_SAFE_DELETE(m_pColliderBodyList);
}
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
bool CCColliderDetector::init()
{
m_pColliderBodyList = CCArray::create();
CCAssert(m_pColliderBodyList, "create m_pColliderBodyList failed!");
m_pColliderBodyList->retain();
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
return true;
}
bool CCColliderDetector::init(CCBone *bone)
{
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-06 16:22:58 +08:00
void CCColliderDetector::addContourData(CCContourData *contourData)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
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);
2013-06-06 12:02:54 +08:00
m_pColliderBodyList->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-06 12:02:54 +08:00
void CCColliderDetector::addContourDataList(CCArray *contourDataList)
{
2013-06-07 10:52:32 +08:00
CCObject *object = NULL;
CCARRAY_FOREACH(contourDataList, object)
{
addContourData((CCContourData *)object);
}
2013-06-06 12:02:54 +08:00
}
2013-06-06 16:22:58 +08:00
void CCColliderDetector::removeContourData(CCContourData *_contourData)
2013-06-06 12:02:54 +08:00
{
m_pColliderBodyList->removeObject(_contourData);
}
void CCColliderDetector::removeAll()
{
2013-06-07 10:52:32 +08:00
m_pColliderBodyList->removeAllObjects();
2013-06-06 12:02:54 +08:00
}
void CCColliderDetector::setColliderFilter(b2Filter &filter)
{
2013-06-07 10:52:32 +08:00
CCObject *object = NULL;
CCARRAY_FOREACH(m_pColliderBodyList, object)
{
ColliderBody *colliderBody = (ColliderBody *)object;
colliderBody->getB2Body()->GetFixtureList()->SetFilterData(filter);
}
2013-06-06 12:02:54 +08:00
}
void CCColliderDetector::setActive(bool active)
{
2013-06-07 10:52:32 +08:00
CCObject *object = NULL;
CCARRAY_FOREACH(m_pColliderBodyList, object)
{
ColliderBody *colliderBody = (ColliderBody *)object;
colliderBody->getB2Body()->SetActive(active);
}
2013-06-06 12:02:54 +08:00
}
CCPoint helpPoint;
void CCColliderDetector::updateTransform(CCAffineTransform &t)
{
2013-06-07 10:52:32 +08:00
CCObject *object = NULL;
CCARRAY_FOREACH(m_pColliderBodyList, object)
{
ColliderBody *colliderBody = (ColliderBody *)object;
CCContourData *contourData = colliderBody->getContourData();
b2Body *body = colliderBody->getB2Body();
b2PolygonShape *shape = (b2PolygonShape *)body->GetFixtureList()->GetShape();
//! 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];
helpPoint.setPoint(cv->x, cv->y);
helpPoint = CCPointApplyAffineTransform(helpPoint, t);
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
NS_CC_EXT_END