2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CCColliderDetector.h"
|
|
|
|
#include "CCBone.h"
|
|
|
|
#include "CCTransformHelp.h"
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2013-11-01 14:36:44 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT
|
2013-11-27 11:39:58 +08:00
|
|
|
ColliderFilter::ColliderFilter(uint16 categoryBits, uint16 maskBits, int16 groupIndex)
|
2021-12-25 10:04:45 +08:00
|
|
|
: _categoryBits(categoryBits), _maskBits(maskBits), _groupIndex(groupIndex)
|
|
|
|
{}
|
2013-11-01 14:36:44 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ColliderFilter::updateShape(b2Fixture* fixture)
|
2013-11-01 14:36:44 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
b2Filter filter;
|
|
|
|
filter.categoryBits = _categoryBits;
|
|
|
|
filter.groupIndex = _groupIndex;
|
|
|
|
filter.maskBits = _maskBits;
|
|
|
|
|
|
|
|
fixture->SetFilterData(filter);
|
2013-11-01 14:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2013-11-27 11:39:58 +08:00
|
|
|
ColliderFilter::ColliderFilter(cpCollisionType collisionType, cpGroup group)
|
2021-12-25 10:04:45 +08:00
|
|
|
: _collisionType(collisionType), _group(group)
|
|
|
|
{}
|
|
|
|
void ColliderFilter::updateShape(cpShape* shape)
|
2013-11-01 14:36:44 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
shape->collision_type = _collisionType;
|
|
|
|
shape->group = _group;
|
2013-11-01 14:36:44 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody::ColliderBody(ContourData* contourData) : _fixture(nullptr), _contourData(contourData)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(_contourData);
|
2021-12-08 00:11:53 +08:00
|
|
|
_filter = new ColliderFilter();
|
2013-10-31 17:28:14 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
2013-10-31 17:28:14 +08:00
|
|
|
_calculatedVertexList = Array::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(_calculatedVertexList);
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody::ColliderBody(ContourData* contourData) : _shape(nullptr), _contourData(contourData)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(_contourData);
|
2021-12-25 10:04:45 +08:00
|
|
|
_filter = new ColliderFilter();
|
2013-10-31 17:28:14 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
2013-10-31 17:28:14 +08:00
|
|
|
_calculatedVertexList = Array::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(_calculatedVertexList);
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
2013-12-06 13:40:49 +08:00
|
|
|
#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody::ColliderBody(ContourData* contourData) : _contourData(contourData)
|
2013-12-06 13:40:49 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(_contourData);
|
2013-12-06 13:40:49 +08:00
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ColliderBody::~ColliderBody()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_contourData);
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2013-12-06 13:40:49 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(_filter);
|
2013-10-31 17:28:14 +08:00
|
|
|
#endif
|
2013-11-01 14:36:44 +08:00
|
|
|
}
|
2013-10-31 17:28:14 +08:00
|
|
|
|
2013-12-06 13:40:49 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
void ColliderBody::setColliderFilter(ColliderFilter* filter)
|
2013-11-01 14:36:44 +08:00
|
|
|
{
|
|
|
|
*_filter = *filter;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderFilter* ColliderBody::getColliderFilter()
|
2013-11-01 14:36:44 +08:00
|
|
|
{
|
|
|
|
return _filter;
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
2013-12-06 13:40:49 +08:00
|
|
|
#endif
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderDetector* ColliderDetector::create()
|
2013-06-06 12:02:54 +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())
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
pColliderDetector->autorelease();
|
|
|
|
return pColliderDetector;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(pColliderDetector);
|
2013-11-05 15:32:13 +08:00
|
|
|
return nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderDetector* ColliderDetector::create(Bone* bone)
|
2013-06-06 12:02:54 +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))
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
pColliderDetector->autorelease();
|
|
|
|
return pColliderDetector;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(pColliderDetector);
|
2013-11-05 15:32:13 +08:00
|
|
|
return nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderDetector::ColliderDetector() : _active(false)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-12-06 13:40:49 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
_body = nullptr;
|
2013-12-06 13:40:49 +08:00
|
|
|
_filter = nullptr;
|
|
|
|
#endif
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
ColliderDetector::~ColliderDetector()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-12-16 14:34:09 +08:00
|
|
|
_colliderBodyList.clear();
|
2013-12-06 13:40:49 +08:00
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(_filter);
|
2013-12-06 13:40:49 +08:00
|
|
|
#endif
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
bool ColliderDetector::init()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-12-16 14:34:09 +08:00
|
|
|
_colliderBodyList.clear();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-12-06 13:40:49 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2021-12-08 00:11:53 +08:00
|
|
|
_filter = new ColliderFilter();
|
2013-12-06 13:40:49 +08:00
|
|
|
#endif
|
2013-11-01 14:36:44 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +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
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ColliderDetector::addContourData(ContourData* contourData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* colliderBody = new ColliderBody(contourData);
|
2013-12-16 14:34:09 +08:00
|
|
|
_colliderBodyList.pushBack(colliderBody);
|
2013-09-13 18:07:37 +08:00
|
|
|
colliderBody->release();
|
2013-10-31 17:28:14 +08:00
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
2021-12-25 10:04:45 +08:00
|
|
|
std::vector<Vec2>& calculatedVertexList = colliderBody->_calculatedVertexList;
|
|
|
|
|
2013-12-17 19:32:16 +08:00
|
|
|
unsigned long num = contourData->vertexList.size();
|
|
|
|
for (unsigned long i = 0; i < num; i++)
|
2013-10-31 17:28:14 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
calculatedVertexList.push_back(Vec2());
|
2013-10-31 17:28:14 +08:00
|
|
|
}
|
|
|
|
#endif
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void ColliderDetector::addContourDataList(axis::Vector<ContourData*>& contourDataList)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& contourData : contourDataList)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-12-18 22:07:33 +08:00
|
|
|
this->addContourData(contourData);
|
2013-12-20 09:54:50 +08:00
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ColliderDetector::removeContourData(ContourData* contourData)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-12-18 22:07:33 +08:00
|
|
|
std::vector<ColliderBody*> eraseList;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (const auto& body : _colliderBodyList)
|
2013-12-18 22:07:33 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (body && body->getContourData() == contourData)
|
|
|
|
{
|
2013-12-18 22:07:33 +08:00
|
|
|
eraseList.push_back(body);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2013-12-20 09:54:50 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (const auto& body : eraseList)
|
2013-12-18 22:07:33 +08:00
|
|
|
{
|
|
|
|
this->_colliderBodyList.eraseObject(body);
|
2013-12-20 09:54:50 +08:00
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void ColliderDetector::removeAll()
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-12-16 14:34:09 +08:00
|
|
|
_colliderBodyList.clear();
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-15 19:08:45 +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
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& object : _colliderBodyList)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* colliderBody = (ColliderBody*)object;
|
|
|
|
b2Fixture* fixture = colliderBody->getB2Fixture();
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
_body->DestroyFixture(fixture);
|
2013-11-05 15:32:13 +08:00
|
|
|
colliderBody->setB2Fixture(nullptr);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2013-09-15 20:24:25 +08:00
|
|
|
if (_body)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
if (_active)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& object : _colliderBodyList)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* colliderBody = (ColliderBody*)object;
|
|
|
|
cpShape* shape = colliderBody->getShape();
|
|
|
|
if (shape->space_private == nullptr)
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
|
|
|
cpSpaceAddShape(_body->space_private, shape);
|
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& object : _colliderBodyList)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* colliderBody = (ColliderBody*)object;
|
|
|
|
cpShape* shape = colliderBody->getShape();
|
2013-11-05 15:32:13 +08:00
|
|
|
if (shape->space_private != nullptr)
|
2013-10-30 09:41:40 +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
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +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
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
const axis::Vector<ColliderBody*>& 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-12-06 13:40:49 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
void ColliderDetector::setColliderFilter(ColliderFilter* filter)
|
2013-11-01 14:36:44 +08:00
|
|
|
{
|
|
|
|
*_filter = *filter;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (auto& object : _colliderBodyList)
|
2013-11-01 14:36:44 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* colliderBody = (ColliderBody*)object;
|
2013-11-01 14:36:44 +08:00
|
|
|
colliderBody->setColliderFilter(filter);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# if ENABLE_PHYSICS_BOX2D_DETECT
|
2013-11-01 14:36:44 +08:00
|
|
|
if (colliderBody->getB2Fixture())
|
|
|
|
{
|
|
|
|
colliderBody->getColliderFilter()->updateShape(colliderBody->getB2Fixture());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
# elif ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2013-11-01 14:36:44 +08:00
|
|
|
if (colliderBody->getShape())
|
|
|
|
{
|
|
|
|
colliderBody->getColliderFilter()->updateShape(colliderBody->getShape());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2013-11-01 14:36:44 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderFilter* ColliderDetector::getColliderFilter()
|
2013-11-01 14:36:44 +08:00
|
|
|
{
|
|
|
|
return _filter;
|
|
|
|
}
|
2013-12-06 13:40:49 +08:00
|
|
|
#endif
|
2013-11-01 14:36:44 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 helpPoint;
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ColliderDetector::updateTransform(Mat4& 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
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& object : _colliderBodyList)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* colliderBody = (ColliderBody*)object;
|
|
|
|
ContourData* contourData = colliderBody->getContourData();
|
2013-09-13 18:07:37 +08:00
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
b2PolygonShape* shape = nullptr;
|
2013-11-05 15:32:13 +08:00
|
|
|
if (_body != nullptr)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
shape = (b2PolygonShape*)colliderBody->getB2Fixture()->GetShape();
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
cpPolyShape* shape = nullptr;
|
2013-11-05 15:32:13 +08:00
|
|
|
if (_body != nullptr)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
shape = (cpPolyShape*)colliderBody->getShape();
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned long num = contourData->vertexList.size();
|
2022-07-11 17:50:21 +08:00
|
|
|
std::vector<axis::Vec2>& vs = contourData->vertexList;
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2013-10-31 17:28:14 +08:00
|
|
|
#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
2022-07-11 17:50:21 +08:00
|
|
|
std::vector<axis::Vec2>& cvs = colliderBody->_calculatedVertexList;
|
2013-10-31 17:28:14 +08:00
|
|
|
#endif
|
|
|
|
|
2013-12-17 19:32:16 +08:00
|
|
|
for (unsigned long i = 0; i < num; i++)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
helpPoint.setPoint(vs.at(i).x, vs.at(i).y);
|
2013-12-11 03:07:15 +08:00
|
|
|
helpPoint = PointApplyTransform(helpPoint, t);
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2013-10-31 17:28:14 +08:00
|
|
|
#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
2013-12-13 19:40:38 +08:00
|
|
|
cvs.at(i).x = helpPoint.x;
|
|
|
|
cvs.at(i).y = helpPoint.y;
|
2013-10-31 17:28:14 +08:00
|
|
|
#endif
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT
|
2013-11-05 15:32:13 +08:00
|
|
|
if (shape != nullptr)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
b2Vec2& bv = shape->m_vertices[i];
|
2013-09-13 18:07:37 +08:00
|
|
|
bv.Set(helpPoint.x / PT_RATIO, helpPoint.y / PT_RATIO);
|
|
|
|
}
|
|
|
|
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2013-11-05 15:32:13 +08:00
|
|
|
if (shape != nullptr)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
cpVect v;
|
|
|
|
v.x = helpPoint.x;
|
|
|
|
v.y = helpPoint.y;
|
2013-09-13 18:07:37 +08:00
|
|
|
shape->verts[i] = v;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2013-12-18 22:36:10 +08:00
|
|
|
cpConvexHull((int)num, shape->verts, nullptr, nullptr, 0);
|
2013-12-17 19:32:16 +08:00
|
|
|
for (unsigned long i = 0; i < num; i++)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2021-12-25 10:04:45 +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
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& object : _colliderBodyList)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* colliderBody = (ColliderBody*)object;
|
|
|
|
|
|
|
|
ContourData* contourData = colliderBody->getContourData();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
b2Vec2* b2bv = new b2Vec2[contourData->vertexList.size()];
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
int i = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& v : contourData->vertexList)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-12-18 22:36:10 +08:00
|
|
|
b2bv[i].Set(v.x / PT_RATIO, v.y / PT_RATIO);
|
2013-09-13 18:07:37 +08:00
|
|
|
i++;
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
b2PolygonShape polygon;
|
2013-12-18 22:36:10 +08:00
|
|
|
polygon.Set(b2bv, (int)contourData->vertexList.size());
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(b2bv);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
b2FixtureDef fixtureDef;
|
2021-12-25 10:04:45 +08:00
|
|
|
fixtureDef.shape = &polygon;
|
2013-09-13 18:07:37 +08:00
|
|
|
fixtureDef.isSensor = true;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
b2Fixture* fixture = _body->CreateFixture(&fixtureDef);
|
2013-09-15 20:24:25 +08:00
|
|
|
fixture->SetUserData(_bone);
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2013-11-05 15:32:13 +08:00
|
|
|
if (colliderBody->getB2Fixture() != nullptr)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
_body->DestroyFixture(colliderBody->getB2Fixture());
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
colliderBody->setB2Fixture(fixture);
|
|
|
|
|
2013-11-01 14:36:44 +08:00
|
|
|
colliderBody->getColliderFilter()->updateShape(fixture);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
b2Body* ColliderDetector::getBody() const
|
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
|
2021-12-25 10:04:45 +08:00
|
|
|
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
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& object : _colliderBodyList)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* colliderBody = (ColliderBody*)object;
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ContourData* contourData = colliderBody->getContourData();
|
2013-09-13 18:07:37 +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];
|
2013-09-13 18:07:37 +08:00
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
{
|
2013-12-18 22:36:10 +08:00
|
|
|
verts[num - 1 - i].x = vs.at(i).x;
|
|
|
|
verts[num - 1 - i].y = vs.at(i).y;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
cpShape* shape = cpPolyShapeNew(_body, (int)num, verts, cpvzero);
|
2013-09-13 18:07:37 +08:00
|
|
|
|
|
|
|
shape->sensor = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
shape->data = _bone;
|
2013-11-01 14:36:44 +08:00
|
|
|
|
|
|
|
if (_active)
|
|
|
|
{
|
|
|
|
cpSpaceAddShape(_body->space_private, shape);
|
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
|
|
|
|
colliderBody->setShape(shape);
|
2013-11-01 14:36:44 +08:00
|
|
|
colliderBody->getColliderFilter()->updateShape(shape);
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
delete[] verts;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
cpBody* ColliderDetector::getBody() const
|
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
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|