axmol/extensions/cocostudio/CCColliderDetector.cpp

485 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2019-11-24 23:15:56 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2019-11-23 20:27:39 +08:00
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"
#include "CCTransformHelp.h"
2019-11-23 20:27:39 +08:00
using namespace cocos2d;
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
#if ENABLE_PHYSICS_BOX2D_DETECT
ColliderFilter::ColliderFilter(uint16 categoryBits, uint16 maskBits, int16 groupIndex)
2021-12-25 10:04:45 +08:00
: _categoryBits(categoryBits), _maskBits(maskBits), _groupIndex(groupIndex)
{}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void ColliderFilter::updateShape(b2Fixture* fixture)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
b2Filter filter;
filter.categoryBits = _categoryBits;
filter.groupIndex = _groupIndex;
filter.maskBits = _maskBits;
fixture->SetFilterData(filter);
2019-11-23 20:27:39 +08:00
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
ColliderFilter::ColliderFilter(cpCollisionType collisionType, cpGroup group)
2021-12-25 10:04:45 +08:00
: _collisionType(collisionType), _group(group)
{}
void ColliderFilter::updateShape(cpShape* shape)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
shape->collision_type = _collisionType;
shape->group = _group;
2019-11-23 20:27:39 +08:00
}
#endif
#if ENABLE_PHYSICS_BOX2D_DETECT
2021-12-25 10:04:45 +08:00
ColliderBody::ColliderBody(ContourData* contourData) : _fixture(nullptr), _contourData(contourData)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RETAIN(_contourData);
2021-12-08 00:11:53 +08:00
_filter = new ColliderFilter();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
# if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
2019-11-23 20:27:39 +08:00
_calculatedVertexList = Array::create();
CC_SAFE_RETAIN(_calculatedVertexList);
2021-12-25 10:04:45 +08:00
# endif
2019-11-23 20:27:39 +08:00
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
2021-12-25 10:04:45 +08:00
ColliderBody::ColliderBody(ContourData* contourData) : _shape(nullptr), _contourData(contourData)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RETAIN(_contourData);
2021-12-25 10:04:45 +08:00
_filter = new ColliderFilter();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
# if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
2019-11-23 20:27:39 +08:00
_calculatedVertexList = Array::create();
CC_SAFE_RETAIN(_calculatedVertexList);
2021-12-25 10:04:45 +08:00
# endif
2019-11-23 20:27:39 +08:00
}
#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
2021-12-25 10:04:45 +08:00
ColliderBody::ColliderBody(ContourData* contourData) : _contourData(contourData)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RETAIN(_contourData);
}
#endif
ColliderBody::~ColliderBody()
{
CC_SAFE_RELEASE(_contourData);
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
CC_SAFE_DELETE(_filter);
#endif
}
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
2021-12-25 10:04:45 +08:00
void ColliderBody::setColliderFilter(ColliderFilter* filter)
2019-11-23 20:27:39 +08:00
{
*_filter = *filter;
}
2021-12-25 10:04:45 +08:00
ColliderFilter* ColliderBody::getColliderFilter()
2019-11-23 20:27:39 +08:00
{
return _filter;
}
#endif
2021-12-25 10:04:45 +08:00
ColliderDetector* ColliderDetector::create()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderDetector* pColliderDetector = new ColliderDetector();
2021-12-08 00:11:53 +08:00
if (pColliderDetector->init())
2019-11-23 20:27:39 +08:00
{
pColliderDetector->autorelease();
return pColliderDetector;
}
CC_SAFE_DELETE(pColliderDetector);
return nullptr;
}
2021-12-25 10:04:45 +08:00
ColliderDetector* ColliderDetector::create(Bone* bone)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderDetector* pColliderDetector = new ColliderDetector();
2021-12-08 00:11:53 +08:00
if (pColliderDetector->init(bone))
2019-11-23 20:27:39 +08:00
{
pColliderDetector->autorelease();
return pColliderDetector;
}
CC_SAFE_DELETE(pColliderDetector);
return nullptr;
}
2021-12-25 10:04:45 +08:00
ColliderDetector::ColliderDetector() : _active(false)
2019-11-23 20:27:39 +08:00
{
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
2021-12-25 10:04:45 +08:00
_body = nullptr;
2019-11-23 20:27:39 +08:00
_filter = nullptr;
#endif
}
ColliderDetector::~ColliderDetector()
{
_colliderBodyList.clear();
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
CC_SAFE_DELETE(_filter);
#endif
}
bool ColliderDetector::init()
{
_colliderBodyList.clear();
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
2021-12-08 00:11:53 +08:00
_filter = new ColliderFilter();
2019-11-23 20:27:39 +08:00
#endif
return true;
}
2021-12-25 10:04:45 +08:00
bool ColliderDetector::init(Bone* bone)
2019-11-23 20:27:39 +08:00
{
init();
setBone(bone);
return true;
}
2021-12-25 10:04:45 +08:00
void ColliderDetector::addContourData(ContourData* contourData)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderBody* colliderBody = new ColliderBody(contourData);
2019-11-23 20:27:39 +08:00
_colliderBodyList.pushBack(colliderBody);
colliderBody->release();
#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
2021-12-25 10:04:45 +08:00
std::vector<Vec2>& calculatedVertexList = colliderBody->_calculatedVertexList;
2019-11-23 20:27:39 +08:00
unsigned long num = contourData->vertexList.size();
for (unsigned long i = 0; i < num; i++)
{
calculatedVertexList.push_back(Vec2());
}
#endif
}
2021-12-25 10:04:45 +08:00
void ColliderDetector::addContourDataList(cocos2d::Vector<ContourData*>& contourDataList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (const auto& contourData : contourDataList)
2019-11-23 20:27:39 +08:00
{
this->addContourData(contourData);
}
}
2021-12-25 10:04:45 +08:00
void ColliderDetector::removeContourData(ContourData* contourData)
2019-11-23 20:27:39 +08:00
{
std::vector<ColliderBody*> eraseList;
2021-12-25 10:04:45 +08:00
for (const auto& body : _colliderBodyList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (body && body->getContourData() == contourData)
{
2019-11-23 20:27:39 +08:00
eraseList.push_back(body);
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
for (const auto& body : eraseList)
2019-11-23 20:27:39 +08:00
{
this->_colliderBodyList.eraseObject(body);
}
}
void ColliderDetector::removeAll()
{
_colliderBodyList.clear();
}
void ColliderDetector::setActive(bool active)
{
if (_active == active)
{
return;
}
_active = active;
#if ENABLE_PHYSICS_BOX2D_DETECT
if (_body)
{
if (active)
{
setBody(_body);
}
else
{
2021-12-25 10:04:45 +08:00
for (auto& object : _colliderBodyList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderBody* colliderBody = (ColliderBody*)object;
b2Fixture* fixture = colliderBody->getB2Fixture();
2019-11-23 20:27:39 +08:00
_body->DestroyFixture(fixture);
colliderBody->setB2Fixture(nullptr);
}
}
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
if (_body)
{
if (_active)
{
2021-12-25 10:04:45 +08:00
for (auto& object : _colliderBodyList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderBody* colliderBody = (ColliderBody*)object;
cpShape* shape = colliderBody->getShape();
if (shape->space_private == nullptr)
2019-11-23 20:27:39 +08:00
{
cpSpaceAddShape(_body->space_private, shape);
}
}
}
else
{
2021-12-25 10:04:45 +08:00
for (auto& object : _colliderBodyList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderBody* colliderBody = (ColliderBody*)object;
cpShape* shape = colliderBody->getShape();
2019-11-23 20:27:39 +08:00
if (shape->space_private != nullptr)
{
cpSpaceRemoveShape(_body->space_private, shape);
}
}
}
}
#endif
}
bool ColliderDetector::getActive()
{
return _active;
}
const cocos2d::Vector<ColliderBody*>& ColliderDetector::getColliderBodyList()
{
return _colliderBodyList;
}
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
2021-12-25 10:04:45 +08:00
void ColliderDetector::setColliderFilter(ColliderFilter* filter)
2019-11-23 20:27:39 +08:00
{
*_filter = *filter;
2021-12-25 10:04:45 +08:00
for (auto& object : _colliderBodyList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderBody* colliderBody = (ColliderBody*)object;
2019-11-23 20:27:39 +08:00
colliderBody->setColliderFilter(filter);
2021-12-25 10:04:45 +08:00
# if ENABLE_PHYSICS_BOX2D_DETECT
2019-11-23 20:27:39 +08:00
if (colliderBody->getB2Fixture())
{
colliderBody->getColliderFilter()->updateShape(colliderBody->getB2Fixture());
}
2021-12-25 10:04:45 +08:00
# elif ENABLE_PHYSICS_CHIPMUNK_DETECT
2019-11-23 20:27:39 +08:00
if (colliderBody->getShape())
{
colliderBody->getColliderFilter()->updateShape(colliderBody->getShape());
}
2021-12-25 10:04:45 +08:00
# endif
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
ColliderFilter* ColliderDetector::getColliderFilter()
2019-11-23 20:27:39 +08:00
{
return _filter;
}
#endif
Vec2 helpPoint;
2021-12-25 10:04:45 +08:00
void ColliderDetector::updateTransform(Mat4& t)
2019-11-23 20:27:39 +08:00
{
if (!_active)
{
return;
}
2021-12-25 10:04:45 +08:00
for (auto& object : _colliderBodyList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderBody* colliderBody = (ColliderBody*)object;
ContourData* contourData = colliderBody->getContourData();
2019-11-23 20:27:39 +08:00
#if ENABLE_PHYSICS_BOX2D_DETECT
2021-12-25 10:04:45 +08:00
b2PolygonShape* shape = nullptr;
2019-11-23 20:27:39 +08:00
if (_body != nullptr)
{
2021-12-25 10:04:45 +08:00
shape = (b2PolygonShape*)colliderBody->getB2Fixture()->GetShape();
2019-11-23 20:27:39 +08:00
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
2021-12-25 10:04:45 +08:00
cpPolyShape* shape = nullptr;
2019-11-23 20:27:39 +08:00
if (_body != nullptr)
{
2021-12-25 10:04:45 +08:00
shape = (cpPolyShape*)colliderBody->getShape();
2019-11-23 20:27:39 +08:00
}
#endif
2021-12-25 10:04:45 +08:00
unsigned long num = contourData->vertexList.size();
std::vector<cocos2d::Vec2>& vs = contourData->vertexList;
2019-11-23 20:27:39 +08:00
#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
2021-12-25 10:04:45 +08:00
std::vector<cocos2d::Vec2>& cvs = colliderBody->_calculatedVertexList;
2019-11-23 20:27:39 +08:00
#endif
for (unsigned long i = 0; i < num; i++)
{
2021-12-25 10:04:45 +08:00
helpPoint.setPoint(vs.at(i).x, vs.at(i).y);
2019-11-23 20:27:39 +08:00
helpPoint = PointApplyTransform(helpPoint, t);
#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
cvs.at(i).x = helpPoint.x;
cvs.at(i).y = helpPoint.y;
#endif
#if ENABLE_PHYSICS_BOX2D_DETECT
if (shape != nullptr)
{
2021-12-25 10:04:45 +08:00
b2Vec2& bv = shape->m_vertices[i];
2019-11-23 20:27:39 +08:00
bv.Set(helpPoint.x / PT_RATIO, helpPoint.y / PT_RATIO);
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
if (shape != nullptr)
{
2021-12-25 10:04:45 +08:00
cpVect v;
v.x = helpPoint.x;
v.y = helpPoint.y;
2019-11-23 20:27:39 +08:00
shape->verts[i] = v;
}
#endif
}
#if ENABLE_PHYSICS_CHIPMUNK_DETECT
cpConvexHull((int)num, shape->verts, nullptr, nullptr, 0);
for (unsigned long 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
}
}
#if ENABLE_PHYSICS_BOX2D_DETECT
2021-12-25 10:04:45 +08:00
void ColliderDetector::setBody(b2Body* pBody)
2019-11-23 20:27:39 +08:00
{
_body = pBody;
2021-12-25 10:04:45 +08:00
for (auto& object : _colliderBodyList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderBody* colliderBody = (ColliderBody*)object;
ContourData* contourData = colliderBody->getContourData();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
b2Vec2* b2bv = new b2Vec2[contourData->vertexList.size()];
2019-11-23 20:27:39 +08:00
int i = 0;
2021-12-25 10:04:45 +08:00
for (auto& v : contourData->vertexList)
2019-11-23 20:27:39 +08:00
{
b2bv[i].Set(v.x / PT_RATIO, v.y / PT_RATIO);
i++;
}
b2PolygonShape polygon;
polygon.Set(b2bv, (int)contourData->vertexList.size());
CC_SAFE_DELETE(b2bv);
b2FixtureDef fixtureDef;
2021-12-25 10:04:45 +08:00
fixtureDef.shape = &polygon;
2019-11-23 20:27:39 +08:00
fixtureDef.isSensor = true;
2021-12-25 10:04:45 +08:00
b2Fixture* fixture = _body->CreateFixture(&fixtureDef);
2019-11-23 20:27:39 +08:00
fixture->SetUserData(_bone);
if (colliderBody->getB2Fixture() != nullptr)
{
_body->DestroyFixture(colliderBody->getB2Fixture());
}
colliderBody->setB2Fixture(fixture);
colliderBody->getColliderFilter()->updateShape(fixture);
}
}
2021-12-25 10:04:45 +08:00
b2Body* ColliderDetector::getBody() const
2019-11-23 20:27:39 +08:00
{
return _body;
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
2021-12-25 10:04:45 +08:00
void ColliderDetector::setBody(cpBody* pBody)
2019-11-23 20:27:39 +08:00
{
_body = pBody;
2021-12-25 10:04:45 +08:00
for (auto& object : _colliderBodyList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ColliderBody* colliderBody = (ColliderBody*)object;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
ContourData* contourData = colliderBody->getContourData();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
ssize_t num = contourData->vertexList.size();
auto vs = contourData->vertexList;
cpVect* verts = new cpVect[num];
2019-11-23 20:27:39 +08:00
for (int i = 0; i < num; i++)
{
verts[num - 1 - i].x = vs.at(i).x;
verts[num - 1 - i].y = vs.at(i).y;
}
2021-12-25 10:04:45 +08:00
cpShape* shape = cpPolyShapeNew(_body, (int)num, verts, cpvzero);
2019-11-23 20:27:39 +08:00
shape->sensor = true;
2021-12-25 10:04:45 +08:00
shape->data = _bone;
2019-11-23 20:27:39 +08:00
if (_active)
{
cpSpaceAddShape(_body->space_private, shape);
}
colliderBody->setShape(shape);
colliderBody->getColliderFilter()->updateShape(shape);
2021-12-25 10:04:45 +08:00
delete[] verts;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
cpBody* ColliderDetector::getBody() const
2019-11-23 20:27:39 +08:00
{
return _body;
}
#endif
2021-12-25 10:04:45 +08:00
} // namespace cocostudio