2013-09-16 21:22:22 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "physics/CCPhysicsContact.h"
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "chipmunk/chipmunk.h"
|
2013-09-17 17:39:08 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "physics/CCPhysicsBody.h"
|
|
|
|
# include "physics/CCPhysicsHelper.h"
|
|
|
|
# include "base/CCEventCustom.h"
|
2013-11-01 14:50:06 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
const char* PHYSICSCONTACT_EVENT_NAME = "PhysicsContactEvent";
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsContact::PhysicsContact()
|
2021-12-25 10:04:45 +08:00
|
|
|
: EventCustom(PHYSICSCONTACT_EVENT_NAME)
|
|
|
|
, _world(nullptr)
|
|
|
|
, _shapeA(nullptr)
|
|
|
|
, _shapeB(nullptr)
|
|
|
|
, _eventCode(EventCode::NONE)
|
|
|
|
, _notificationEnable(true)
|
|
|
|
, _result(true)
|
|
|
|
, _data(nullptr)
|
|
|
|
, _contactInfo(nullptr)
|
|
|
|
, _contactData(nullptr)
|
|
|
|
, _preContactData(nullptr)
|
|
|
|
{}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
PhysicsContact::~PhysicsContact()
|
|
|
|
{
|
2013-10-25 10:31:22 +08:00
|
|
|
CC_SAFE_DELETE(_contactData);
|
2014-03-11 05:09:08 +08:00
|
|
|
CC_SAFE_DELETE(_preContactData);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-18 21:06:05 +08:00
|
|
|
PhysicsContact* PhysicsContact::construct(PhysicsShape* a, PhysicsShape* b)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsContact* contact = new PhysicsContact();
|
|
|
|
if (contact->init(a, b))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
return contact;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
CC_SAFE_DELETE(contact);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsContact::init(PhysicsShape* a, PhysicsShape* b)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(a == nullptr || b == nullptr);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
_shapeA = a;
|
|
|
|
_shapeB = b;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
} while (false);
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-25 10:31:22 +08:00
|
|
|
void PhysicsContact::generateContactData()
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-25 10:31:22 +08:00
|
|
|
if (_contactInfo == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
cpArbiter* arb = static_cast<cpArbiter*>(_contactInfo);
|
2014-03-11 05:09:08 +08:00
|
|
|
CC_SAFE_DELETE(_preContactData);
|
2021-12-25 10:04:45 +08:00
|
|
|
_preContactData = _contactData;
|
|
|
|
_contactData = new PhysicsContactData();
|
2013-10-25 10:31:22 +08:00
|
|
|
_contactData->count = cpArbiterGetCount(arb);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < _contactData->count && i < PhysicsContactData::POINT_MAX; ++i)
|
2013-10-25 10:31:22 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
_contactData->points[i] = PhysicsHelper::cpv2vec2(cpArbiterGetPointA(arb, i));
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-10-24 14:09:59 +08:00
|
|
|
_contactData->normal = _contactData->count > 0 ? PhysicsHelper::cpv2vec2(cpArbiterGetNormal(arb)) : Vec2::ZERO;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-10-25 10:31:22 +08:00
|
|
|
// PhysicsContactPreSolve implementation
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsContactPreSolve::PhysicsContactPreSolve(void* contactInfo) : _contactInfo(contactInfo) {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsContactPreSolve::~PhysicsContactPreSolve() {}
|
2013-11-05 15:54:33 +08:00
|
|
|
|
2014-02-25 15:58:19 +08:00
|
|
|
float PhysicsContactPreSolve::getRestitution() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
return cpArbiterGetRestitution(static_cast<cpArbiter*>(_contactInfo));
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-18 02:16:20 +08:00
|
|
|
float PhysicsContactPreSolve::getFriction() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
return cpArbiterGetFriction(static_cast<cpArbiter*>(_contactInfo));
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsContactPreSolve::getSurfaceVelocity() const
|
2013-10-25 10:31:22 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpArbiterGetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo)));
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
2014-02-25 15:58:19 +08:00
|
|
|
void PhysicsContactPreSolve::setRestitution(float restitution)
|
2013-10-25 10:31:22 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpArbiterSetRestitution(static_cast<cpArbiter*>(_contactInfo), restitution);
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsContactPreSolve::setFriction(float friction)
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpArbiterSetFriction(static_cast<cpArbiter*>(_contactInfo), friction);
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
void PhysicsContactPreSolve::setSurfaceVelocity(const Vec2& velocity)
|
2013-10-25 10:31:22 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
cpArbiterSetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo), PhysicsHelper::vec22cpv(velocity));
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 15:54:33 +08:00
|
|
|
void PhysicsContactPreSolve::ignore()
|
2013-10-25 10:31:22 +08:00
|
|
|
{
|
2013-11-05 15:54:33 +08:00
|
|
|
cpArbiterIgnore(static_cast<cpArbiter*>(_contactInfo));
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
// PhysicsContactPostSolve implementation
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsContactPostSolve::PhysicsContactPostSolve(void* contactInfo) : _contactInfo(contactInfo) {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsContactPostSolve::~PhysicsContactPostSolve() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2014-02-25 15:58:19 +08:00
|
|
|
float PhysicsContactPostSolve::getRestitution() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
return cpArbiterGetRestitution(static_cast<cpArbiter*>(_contactInfo));
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-18 02:16:20 +08:00
|
|
|
float PhysicsContactPostSolve::getFriction() const
|
2013-10-25 10:31:22 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
return cpArbiterGetFriction(static_cast<cpArbiter*>(_contactInfo));
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsContactPostSolve::getSurfaceVelocity() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpArbiterGetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo)));
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContact::EventListenerPhysicsContact()
|
2021-12-25 10:04:45 +08:00
|
|
|
: onContactBegin(nullptr), onContactPreSolve(nullptr), onContactPostSolve(nullptr), onContactSeparate(nullptr)
|
|
|
|
{}
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
bool EventListenerPhysicsContact::init()
|
2013-09-16 22:51:48 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto func = [this](EventCustom* event) -> void { onEvent(event); };
|
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
return EventListenerCustom::init(PHYSICSCONTACT_EVENT_NAME, func);
|
2013-09-16 22:51:48 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
void EventListenerPhysicsContact::onEvent(EventCustom* event)
|
2013-09-16 22:51:48 +08:00
|
|
|
{
|
2014-01-28 14:59:34 +08:00
|
|
|
PhysicsContact* contact = dynamic_cast<PhysicsContact*>(event);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-01-28 14:59:34 +08:00
|
|
|
if (contact == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-01-28 14:59:34 +08:00
|
|
|
switch (contact->getEventCode())
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case PhysicsContact::EventCode::BEGIN:
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (onContactBegin != nullptr && hitTest(contact->getShapeA(), contact->getShapeB()))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
contact->generateContactData();
|
|
|
|
ret = onContactBegin(*contact);
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
contact->setResult(ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PhysicsContact::EventCode::PRESOLVE:
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (onContactPreSolve != nullptr && hitTest(contact->getShapeA(), contact->getShapeB()))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsContactPreSolve solve(contact->_contactInfo);
|
|
|
|
contact->generateContactData();
|
|
|
|
|
|
|
|
ret = onContactPreSolve(*contact, solve);
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
contact->setResult(ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PhysicsContact::EventCode::POSTSOLVE:
|
|
|
|
{
|
|
|
|
if (onContactPostSolve != nullptr && hitTest(contact->getShapeA(), contact->getShapeB()))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsContactPostSolve solve(contact->_contactInfo);
|
|
|
|
onContactPostSolve(*contact, solve);
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PhysicsContact::EventCode::SEPARATE:
|
|
|
|
{
|
|
|
|
if (onContactSeparate != nullptr && hitTest(contact->getShapeA(), contact->getShapeB()))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
onContactSeparate(*contact);
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
2013-09-16 22:51:48 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContact::~EventListenerPhysicsContact() {}
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
EventListenerPhysicsContact* EventListenerPhysicsContact::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
EventListenerPhysicsContact* obj = new EventListenerPhysicsContact();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (obj->init())
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
bool EventListenerPhysicsContact::hitTest(PhysicsShape* /*shapeA*/, PhysicsShape* /*shapeB*/)
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
bool EventListenerPhysicsContact::checkAvailable()
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (onContactBegin == nullptr && onContactPreSolve == nullptr && onContactPostSolve == nullptr &&
|
|
|
|
onContactSeparate == nullptr)
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "Invalid PhysicsContactListener.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContact* EventListenerPhysicsContact::clone()
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContact* obj = EventListenerPhysicsContact::create();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
if (obj != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
obj->onContactBegin = onContactBegin;
|
|
|
|
obj->onContactPreSolve = onContactPreSolve;
|
2013-10-30 15:53:58 +08:00
|
|
|
obj->onContactPostSolve = onContactPostSolve;
|
2021-12-25 10:04:45 +08:00
|
|
|
obj->onContactSeparate = onContactSeparate;
|
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
return obj;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::create(PhysicsBody* bodyA,
|
|
|
|
PhysicsBody* bodyB)
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
EventListenerPhysicsContactWithBodies* obj = new EventListenerPhysicsContactWithBodies();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (obj->init())
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
|
|
|
obj->_a = bodyA;
|
|
|
|
obj->_b = bodyB;
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-10-30 15:53:58 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContactWithBodies::EventListenerPhysicsContactWithBodies() : _a(nullptr), _b(nullptr) {}
|
2013-10-30 15:53:58 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContactWithBodies::~EventListenerPhysicsContactWithBodies() {}
|
2013-10-30 15:53:58 +08:00
|
|
|
|
2013-11-18 03:10:13 +08:00
|
|
|
bool EventListenerPhysicsContactWithBodies::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((shapeA->getBody() == _a && shapeB->getBody() == _b) || (shapeA->getBody() == _b && shapeB->getBody() == _a))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::clone()
|
|
|
|
{
|
|
|
|
EventListenerPhysicsContactWithBodies* obj = EventListenerPhysicsContactWithBodies::create(_a, _b);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
if (obj != nullptr)
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
obj->onContactBegin = onContactBegin;
|
|
|
|
obj->onContactPreSolve = onContactPreSolve;
|
2013-11-01 14:50:06 +08:00
|
|
|
obj->onContactPostSolve = onContactPostSolve;
|
2021-12-25 10:04:45 +08:00
|
|
|
obj->onContactSeparate = onContactSeparate;
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
return obj;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContactWithShapes::EventListenerPhysicsContactWithShapes() : _a(nullptr), _b(nullptr) {}
|
2013-11-01 14:50:06 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContactWithShapes::~EventListenerPhysicsContactWithShapes() {}
|
2013-11-01 14:50:06 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::create(PhysicsShape* shapeA,
|
|
|
|
PhysicsShape* shapeB)
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
EventListenerPhysicsContactWithShapes* obj = new EventListenerPhysicsContactWithShapes();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (obj->init())
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
obj->_a = shapeA;
|
|
|
|
obj->_b = shapeB;
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-18 03:10:13 +08:00
|
|
|
bool EventListenerPhysicsContactWithShapes::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((shapeA == _a && shapeB == _b) || (shapeA == _b && shapeB == _a))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
return false;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::clone()
|
2013-09-16 22:51:48 +08:00
|
|
|
{
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContactWithShapes* obj = EventListenerPhysicsContactWithShapes::create(_a, _b);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
if (obj != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
obj->onContactBegin = onContactBegin;
|
|
|
|
obj->onContactPreSolve = onContactPreSolve;
|
2013-11-01 14:50:06 +08:00
|
|
|
obj->onContactPostSolve = onContactPostSolve;
|
2021-12-25 10:04:45 +08:00
|
|
|
obj->onContactSeparate = onContactSeparate;
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
return obj;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
2013-09-16 22:51:48 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContactWithGroup::EventListenerPhysicsContactWithGroup() : _group(CP_NO_GROUP) {}
|
2013-11-01 14:50:06 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerPhysicsContactWithGroup::~EventListenerPhysicsContactWithGroup() {}
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::create(int group)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
EventListenerPhysicsContactWithGroup* obj = new EventListenerPhysicsContactWithGroup();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (obj->init())
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
|
|
|
obj->_group = group;
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-18 03:10:13 +08:00
|
|
|
bool EventListenerPhysicsContactWithGroup::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
|
|
|
if (shapeA->getGroup() == _group || shapeB->getGroup() == _group)
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::clone()
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContactWithGroup* obj = EventListenerPhysicsContactWithGroup::create(_group);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
if (obj != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
obj->onContactBegin = onContactBegin;
|
|
|
|
obj->onContactPreSolve = onContactPreSolve;
|
2013-10-30 15:53:58 +08:00
|
|
|
obj->onContactPostSolve = onContactPostSolve;
|
2021-12-25 10:04:45 +08:00
|
|
|
obj->onContactSeparate = onContactSeparate;
|
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
return obj;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-30 15:53:58 +08:00
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
NS_CC_END
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // CC_USE_PHYSICS
|