2013-09-16 21:22:22 +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 "CCPhysicsContact.h"
|
|
|
|
#ifdef CC_USE_PHYSICS
|
2013-09-17 17:39:08 +08:00
|
|
|
|
|
|
|
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
|
|
|
#include "chipmunk.h"
|
|
|
|
#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D)
|
|
|
|
#include "Box2D.h"
|
|
|
|
#endif
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
#include "CCPhysicsBody.h"
|
|
|
|
|
2013-10-31 18:18:02 +08:00
|
|
|
#include "chipmunk/CCPhysicsContactInfo_chipmunk.h"
|
|
|
|
#include "box2d/CCPhysicsContactInfo_box2d.h"
|
|
|
|
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
|
|
|
|
#include "box2d/CCPhysicsHelper_box2d.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
#include "CCEventCustom.h"
|
|
|
|
|
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()
|
2013-11-01 14:50:06 +08:00
|
|
|
: Event(Event::Type::CUSTOM)
|
|
|
|
, _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-09-16 21:22:22 +08:00
|
|
|
, _info(nullptr)
|
2013-10-25 10:31:22 +08:00
|
|
|
, _notify(true)
|
|
|
|
, _begin(false)
|
2013-09-16 21:22:22 +08:00
|
|
|
, _data(nullptr)
|
2013-10-25 10:31:22 +08:00
|
|
|
, _contactInfo(nullptr)
|
|
|
|
, _contactData(nullptr)
|
2013-11-01 14:50:06 +08:00
|
|
|
, _result(true)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsContact::~PhysicsContact()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_info);
|
2013-10-25 10:31:22 +08:00
|
|
|
CC_SAFE_DELETE(_contactData);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsContact* PhysicsContact::create(PhysicsShape* a, PhysicsShape* b)
|
|
|
|
{
|
|
|
|
PhysicsContact * contact = new PhysicsContact();
|
|
|
|
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);
|
|
|
|
|
|
|
|
CC_BREAK_IF(!(_info = new PhysicsContactInfo(this)));
|
|
|
|
|
|
|
|
_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);
|
2013-10-25 10:31:22 +08:00
|
|
|
_contactData = new PhysicsContactData();
|
|
|
|
_contactData->count = cpArbiterGetCount(arb);
|
|
|
|
for (int i=0; i<_contactData->count; ++i)
|
|
|
|
{
|
|
|
|
_contactData->points[i] = PhysicsHelper::cpv2point(cpArbiterGetPoint(arb, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
_contactData->normal = _contactData->count > 0 ? PhysicsHelper::cpv2point(cpArbiterGetNormal(arb, 0)) : Point::ZERO;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-10-25 10:31:22 +08:00
|
|
|
// PhysicsContactPreSolve implementation
|
|
|
|
PhysicsContactPreSolve::PhysicsContactPreSolve(PhysicsContactData* data, void* contactInfo)
|
|
|
|
: _preContactData(data)
|
|
|
|
, _contactInfo(contactInfo)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsContactPreSolve::getElasticity() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
return static_cast<cpArbiter*>(_contactInfo)->e;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsContactPreSolve::getFriciton() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
return static_cast<cpArbiter*>(_contactInfo)->u;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
Point PhysicsContactPreSolve::getSurfaceVelocity() const
|
2013-10-25 10:31:22 +08:00
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
return PhysicsHelper::cpv2point(static_cast<cpArbiter*>(_contactInfo)->surface_vr);
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsContactPreSolve::setElasticity(float elasticity)
|
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
static_cast<cpArbiter*>(_contactInfo)->e = elasticity;
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsContactPreSolve::setFriction(float friction)
|
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
static_cast<cpArbiter*>(_contactInfo)->u = friction;
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsContactPreSolve::setSurfaceVelocity(Point surfaceVelocity)
|
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
static_cast<cpArbiter*>(_contactInfo)->surface_vr = PhysicsHelper::point2cpv(surfaceVelocity);
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsContactPreSolve::~PhysicsContactPreSolve()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_preContactData);
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsContactPostSolve::getElasticity() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
return static_cast<cpArbiter*>(_contactInfo)->e;
|
2013-10-25 10:31:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsContactPostSolve::getFriciton() const
|
2013-10-25 10:31:22 +08:00
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
return static_cast<cpArbiter*>(_contactInfo)->u;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
Point PhysicsContactPostSolve::getSurfaceVelocity() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
return PhysicsHelper::cpv2point(static_cast<cpArbiter*>(_contactInfo)->surface_vr);
|
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)
|
2013-11-01 14:50:06 +08:00
|
|
|
, onContactSeperate(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return onEvent(event);
|
|
|
|
};
|
2013-09-16 22:51:48 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
return EventListenerCustom::init(std::hash<std::string>()(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
|
|
|
{
|
2013-11-01 14:50:06 +08:00
|
|
|
PhysicsContact& contact = *(PhysicsContact*)(event->getUserData());
|
2013-09-16 22:51:48 +08:00
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
switch (contact.getEventCode())
|
|
|
|
{
|
|
|
|
case PhysicsContact::EventCode::BEGIN:
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (onContactBegin != nullptr
|
|
|
|
&& contact.getNotify()
|
|
|
|
&& test(contact.getShapeA(), contact.getShapeB()))
|
|
|
|
{
|
|
|
|
contact._begin = true;
|
|
|
|
contact.generateContactData();
|
|
|
|
|
|
|
|
// the mask has high priority than _listener->onContactBegin.
|
|
|
|
// so if the mask test is false, the two bodies won't have collision.
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = onContactBegin(event, contact);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
onContactBegin(event, contact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
contact.setResult(ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PhysicsContact::EventCode::PRESOLVE:
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (onContactPreSolve != nullptr
|
|
|
|
&& test(contact.getShapeA(), contact.getShapeB()))
|
|
|
|
{
|
|
|
|
PhysicsContactPreSolve solve(contact._begin ? nullptr : contact._contactData, contact._contactInfo);
|
|
|
|
contact._begin = false;
|
|
|
|
contact.generateContactData();
|
|
|
|
|
|
|
|
ret = onContactPreSolve(event, contact, solve);
|
|
|
|
}
|
|
|
|
|
|
|
|
contact.setResult(ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PhysicsContact::EventCode::POSTSOLVE:
|
|
|
|
{
|
|
|
|
if (onContactPreSolve != nullptr
|
|
|
|
&& test(contact.getShapeA(), contact.getShapeB()))
|
|
|
|
{
|
|
|
|
PhysicsContactPostSolve solve(contact._contactInfo);
|
|
|
|
onContactPostSolve(event, contact, solve);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PhysicsContact::EventCode::SEPERATE:
|
|
|
|
{
|
|
|
|
if (onContactSeperate != nullptr
|
|
|
|
&& test(contact.getShapeA(), contact.getShapeB()))
|
|
|
|
{
|
|
|
|
onContactSeperate(event, contact);
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
EventListenerPhysicsContact* obj = new EventListenerPhysicsContact();
|
|
|
|
|
|
|
|
if (obj != nullptr && obj->init())
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
bool EventListenerPhysicsContact::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
2013-10-30 15:53:58 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(shapeA);
|
|
|
|
CC_UNUSED_PARAM(shapeB);
|
|
|
|
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
|
2013-11-01 14:50:06 +08:00
|
|
|
&& onContactPostSolve == nullptr && onContactSeperate == 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;
|
2013-11-01 14:50:06 +08:00
|
|
|
obj->onContactSeperate = onContactSeperate;
|
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)
|
|
|
|
{
|
|
|
|
EventListenerPhysicsContactWithBodies* obj = new EventListenerPhysicsContactWithBodies();
|
|
|
|
|
|
|
|
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-01 14:50:06 +08:00
|
|
|
bool EventListenerPhysicsContactWithBodies::test(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;
|
|
|
|
obj->onContactSeperate = onContactSeperate;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithShapes::EventListenerPhysicsContactWithShapes()
|
|
|
|
: _a(nullptr)
|
|
|
|
, _b(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithShapes::~EventListenerPhysicsContactWithShapes()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::create(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
|
|
|
{
|
|
|
|
EventListenerPhysicsContactWithShapes* obj = new EventListenerPhysicsContactWithShapes();
|
|
|
|
|
|
|
|
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-01 14:50:06 +08:00
|
|
|
bool EventListenerPhysicsContactWithShapes::test(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;
|
|
|
|
obj->onContactSeperate = onContactSeperate;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
EventListenerPhysicsContactWithGroup* obj = new EventListenerPhysicsContactWithGroup();
|
|
|
|
|
|
|
|
if (obj != nullptr && obj->init())
|
|
|
|
{
|
|
|
|
obj->_group = group;
|
|
|
|
obj->autorelease();
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(obj);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventListenerPhysicsContactWithGroup::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
|
|
|
{
|
|
|
|
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;
|
2013-11-01 14:50:06 +08:00
|
|
|
obj->onContactSeperate = onContactSeperate;
|
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
|