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.
|
2013-09-16 21:22:22 +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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "physics/CCPhysicsContact.h"
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2016-03-01 05:53:00 +08:00
|
|
|
#include "chipmunk/chipmunk.h"
|
2013-09-17 17:39:08 +08:00
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "physics/CCPhysicsBody.h"
|
|
|
|
#include "physics/CCPhysicsHelper.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#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()
|
2014-01-14 19:21:09 +08:00
|
|
|
: EventCustom(PHYSICSCONTACT_EVENT_NAME)
|
2013-11-01 14:50:06 +08:00
|
|
|
, _world(nullptr)
|
|
|
|
, _shapeA(nullptr)
|
2013-09-16 21:22:22 +08:00
|
|
|
, _shapeB(nullptr)
|
2013-11-01 14:50:06 +08:00
|
|
|
, _eventCode(EventCode::NONE)
|
2013-11-06 15:43:29 +08:00
|
|
|
, _notificationEnable(true)
|
2013-11-13 11:22:34 +08:00
|
|
|
, _result(true)
|
2013-09-16 21:22:22 +08:00
|
|
|
, _data(nullptr)
|
2013-10-25 10:31:22 +08:00
|
|
|
, _contactInfo(nullptr)
|
|
|
|
, _contactData(nullptr)
|
2014-03-11 05:09:08 +08:00
|
|
|
, _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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsContact * contact = new (std::nothrow) PhysicsContact();
|
2013-09-16 21:22:22 +08:00
|
|
|
if(contact && contact->init(a, b))
|
|
|
|
{
|
|
|
|
return contact;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(contact);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsContact::init(PhysicsShape* a, PhysicsShape* b)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(a == nullptr || b == nullptr);
|
|
|
|
|
|
|
|
_shapeA = a;
|
|
|
|
_shapeB = b;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-09-16 21:22:22 +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);
|
|
|
|
_preContactData = _contactData;
|
2014-08-28 07:31:57 +08:00
|
|
|
_contactData = new (std::nothrow) PhysicsContactData();
|
2013-10-25 10:31:22 +08:00
|
|
|
_contactData->count = cpArbiterGetCount(arb);
|
2013-11-26 18:22:52 +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-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
|
2014-03-11 05:09:08 +08:00
|
|
|
PhysicsContactPreSolve::PhysicsContactPreSolve(void* contactInfo)
|
|
|
|
: _contactInfo(contactInfo)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-05 15:54:33 +08:00
|
|
|
PhysicsContactPreSolve::~PhysicsContactPreSolve()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
2013-10-25 10:31:22 +08:00
|
|
|
PhysicsContactPostSolve::PhysicsContactPostSolve(void* contactInfo)
|
|
|
|
: _contactInfo(contactInfo)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsContactPostSolve::~PhysicsContactPostSolve()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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()
|
2013-09-16 22:51:48 +08:00
|
|
|
: onContactBegin(nullptr)
|
|
|
|
, onContactPreSolve(nullptr)
|
|
|
|
, onContactPostSolve(nullptr)
|
2015-04-25 05:02:39 +08:00
|
|
|
, onContactSeparate(nullptr)
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventListenerPhysicsContact::init()
|
2013-09-16 22:51:48 +08:00
|
|
|
{
|
2013-11-01 14:50:06 +08:00
|
|
|
auto func = [this](EventCustom* event) -> void
|
|
|
|
{
|
2013-11-04 14:36:38 +08:00
|
|
|
onEvent(event);
|
2013-11-01 14:50:06 +08:00
|
|
|
};
|
2013-09-16 22:51:48 +08:00
|
|
|
|
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);
|
2013-09-16 22:51:48 +08:00
|
|
|
|
2014-01-28 14:59:34 +08:00
|
|
|
if (contact == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (contact->getEventCode())
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
|
|
|
case PhysicsContact::EventCode::BEGIN:
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (onContactBegin != nullptr
|
2014-01-28 14:59:34 +08:00
|
|
|
&& hitTest(contact->getShapeA(), contact->getShapeB()))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2014-01-28 14:59:34 +08:00
|
|
|
contact->generateContactData();
|
2014-03-10 14:51:26 +08:00
|
|
|
ret = onContactBegin(*contact);
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
|
|
|
|
2014-01-28 14:59:34 +08:00
|
|
|
contact->setResult(ret);
|
2013-11-01 14:50:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PhysicsContact::EventCode::PRESOLVE:
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (onContactPreSolve != nullptr
|
2014-01-28 14:59:34 +08:00
|
|
|
&& hitTest(contact->getShapeA(), contact->getShapeB()))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2014-03-11 05:09:08 +08:00
|
|
|
PhysicsContactPreSolve solve(contact->_contactInfo);
|
2014-01-28 14:59:34 +08:00
|
|
|
contact->generateContactData();
|
2013-11-01 14:50:06 +08:00
|
|
|
|
2014-01-28 14:59:34 +08:00
|
|
|
ret = onContactPreSolve(*contact, solve);
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
|
|
|
|
2014-01-28 14:59:34 +08:00
|
|
|
contact->setResult(ret);
|
2013-11-01 14:50:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PhysicsContact::EventCode::POSTSOLVE:
|
|
|
|
{
|
2013-11-04 14:36:38 +08:00
|
|
|
if (onContactPostSolve != nullptr
|
2014-01-28 14:59:34 +08:00
|
|
|
&& hitTest(contact->getShapeA(), contact->getShapeB()))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2014-01-28 14:59:34 +08:00
|
|
|
PhysicsContactPostSolve solve(contact->_contactInfo);
|
|
|
|
onContactPostSolve(*contact, solve);
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-04-25 05:02:39 +08:00
|
|
|
case PhysicsContact::EventCode::SEPARATE:
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2015-04-25 05:02:39 +08:00
|
|
|
if (onContactSeparate != nullptr
|
2014-01-28 14:59:34 +08:00
|
|
|
&& hitTest(contact->getShapeA(), contact->getShapeB()))
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
2015-04-25 05:02:39 +08:00
|
|
|
onContactSeparate(*contact);
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 22:51:48 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContact::~EventListenerPhysicsContact()
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContact* EventListenerPhysicsContact::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
EventListenerPhysicsContact* obj = new (std::nothrow) EventListenerPhysicsContact();
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
if (obj != nullptr && obj->init())
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (onContactBegin == nullptr && onContactPreSolve == nullptr
|
2015-04-25 05:02:39 +08:00
|
|
|
&& onContactPostSolve == nullptr && onContactSeparate == nullptr)
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "Invalid PhysicsContactListener.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2013-10-30 15:53:58 +08:00
|
|
|
|
|
|
|
if (obj != nullptr)
|
|
|
|
{
|
|
|
|
obj->onContactBegin = onContactBegin;
|
|
|
|
obj->onContactPreSolve = onContactPreSolve;
|
|
|
|
obj->onContactPostSolve = onContactPostSolve;
|
2015-04-25 05:02:39 +08:00
|
|
|
obj->onContactSeparate = onContactSeparate;
|
2013-10-30 15:53:58 +08:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::create(PhysicsBody* bodyA, PhysicsBody* bodyB)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
EventListenerPhysicsContactWithBodies* obj = new (std::nothrow) EventListenerPhysicsContactWithBodies();
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
if (obj != nullptr && obj->init())
|
|
|
|
{
|
|
|
|
obj->_a = bodyA;
|
|
|
|
obj->_b = bodyB;
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-10-30 15:53:58 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContactWithBodies::EventListenerPhysicsContactWithBodies()
|
|
|
|
: _a(nullptr)
|
|
|
|
, _b(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-11-01 14:50:06 +08:00
|
|
|
if ((shapeA->getBody() == _a && shapeB->getBody() == _b)
|
|
|
|
|| (shapeA->getBody() == _b && shapeB->getBody() == _a))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::clone()
|
|
|
|
{
|
|
|
|
EventListenerPhysicsContactWithBodies* obj = EventListenerPhysicsContactWithBodies::create(_a, _b);
|
2013-10-30 15:53:58 +08:00
|
|
|
|
|
|
|
if (obj != nullptr)
|
2013-11-01 14:50:06 +08:00
|
|
|
{
|
|
|
|
obj->onContactBegin = onContactBegin;
|
|
|
|
obj->onContactPreSolve = onContactPreSolve;
|
|
|
|
obj->onContactPostSolve = onContactPostSolve;
|
2015-04-25 05:02:39 +08:00
|
|
|
obj->onContactSeparate = onContactSeparate;
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithShapes::EventListenerPhysicsContactWithShapes()
|
|
|
|
: _a(nullptr)
|
|
|
|
, _b(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithShapes::~EventListenerPhysicsContactWithShapes()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::create(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
EventListenerPhysicsContactWithShapes* obj = new (std::nothrow) EventListenerPhysicsContactWithShapes();
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
if (obj != nullptr && obj->init())
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
obj->_a = shapeA;
|
|
|
|
obj->_b = shapeB;
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-11-01 14:50:06 +08:00
|
|
|
if ((shapeA == _a && shapeB == _b)
|
|
|
|
|| (shapeA == _b && shapeB == _a))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-30 17:27:09 +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);
|
2013-09-16 22:51:48 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
if (obj != nullptr)
|
|
|
|
{
|
|
|
|
obj->onContactBegin = onContactBegin;
|
|
|
|
obj->onContactPreSolve = onContactPreSolve;
|
|
|
|
obj->onContactPostSolve = onContactPostSolve;
|
2015-04-25 05:02:39 +08:00
|
|
|
obj->onContactSeparate = onContactSeparate;
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
2013-09-16 22:51:48 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
EventListenerPhysicsContactWithGroup::EventListenerPhysicsContactWithGroup()
|
|
|
|
: _group(CP_NO_GROUP)
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
2013-11-01 14:50:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithGroup::~EventListenerPhysicsContactWithGroup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::create(int group)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
EventListenerPhysicsContactWithGroup* obj = new (std::nothrow) EventListenerPhysicsContactWithGroup();
|
2013-11-01 14:50:06 +08:00
|
|
|
|
|
|
|
if (obj != nullptr && obj->init())
|
|
|
|
{
|
|
|
|
obj->_group = group;
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-10-30 15:53:58 +08:00
|
|
|
|
|
|
|
if (obj != nullptr)
|
|
|
|
{
|
|
|
|
obj->onContactBegin = onContactBegin;
|
|
|
|
obj->onContactPreSolve = onContactPreSolve;
|
|
|
|
obj->onContactPostSolve = onContactPostSolve;
|
2015-04-25 05:02:39 +08:00
|
|
|
obj->onContactSeparate = onContactSeparate;
|
2013-10-30 15:53:58 +08:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
NS_CC_END
|
|
|
|
#endif // CC_USE_PHYSICS
|