123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- /****************************************************************************
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
-
- 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 "physics/CCPhysicsContact.h"
- #if CC_USE_PHYSICS
- #include "chipmunk/chipmunk.h"
- #include "physics/CCPhysicsBody.h"
- #include "physics/CCPhysicsHelper.h"
- #include "base/CCEventCustom.h"
- NS_CC_BEGIN
- const char* PHYSICSCONTACT_EVENT_NAME = "PhysicsContactEvent";
- PhysicsContact::PhysicsContact()
- : 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)
- {
-
- }
- PhysicsContact::~PhysicsContact()
- {
- CC_SAFE_DELETE(_contactData);
- CC_SAFE_DELETE(_preContactData);
- }
- PhysicsContact* PhysicsContact::construct(PhysicsShape* a, PhysicsShape* b)
- {
- PhysicsContact * contact = new (std::nothrow) 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);
-
- _shapeA = a;
- _shapeB = b;
-
- return true;
- } while(false);
-
- return false;
- }
- void PhysicsContact::generateContactData()
- {
- if (_contactInfo == nullptr)
- {
- return;
- }
-
- cpArbiter* arb = static_cast<cpArbiter*>(_contactInfo);
- CC_SAFE_DELETE(_preContactData);
- _preContactData = _contactData;
- _contactData = new (std::nothrow) PhysicsContactData();
- _contactData->count = cpArbiterGetCount(arb);
- for (int i=0; i<_contactData->count && i<PhysicsContactData::POINT_MAX; ++i)
- {
- _contactData->points[i] = PhysicsHelper::cpv2point(cpArbiterGetPointA(arb, i));
- }
-
- _contactData->normal = _contactData->count > 0 ? PhysicsHelper::cpv2point(cpArbiterGetNormal(arb)) : Vec2::ZERO;
- }
- // PhysicsContactPreSolve implementation
- PhysicsContactPreSolve::PhysicsContactPreSolve(void* contactInfo)
- : _contactInfo(contactInfo)
- {
- }
- PhysicsContactPreSolve::~PhysicsContactPreSolve()
- {
- }
- float PhysicsContactPreSolve::getRestitution() const
- {
- return cpArbiterGetRestitution(static_cast<cpArbiter*>(_contactInfo));
- }
- float PhysicsContactPreSolve::getFriction() const
- {
- return cpArbiterGetFriction(static_cast<cpArbiter*>(_contactInfo));
- }
- Vec2 PhysicsContactPreSolve::getSurfaceVelocity() const
- {
- return PhysicsHelper::cpv2point(cpArbiterGetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo)));
- }
- void PhysicsContactPreSolve::setRestitution(float restitution)
- {
- cpArbiterSetRestitution(static_cast<cpArbiter*>(_contactInfo), restitution);
- }
- void PhysicsContactPreSolve::setFriction(float friction)
- {
- cpArbiterSetFriction(static_cast<cpArbiter*>(_contactInfo), friction);
- }
- void PhysicsContactPreSolve::setSurfaceVelocity(const Vec2& velocity)
- {
- cpArbiterSetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo), PhysicsHelper::point2cpv(velocity));
- }
- void PhysicsContactPreSolve::ignore()
- {
- cpArbiterIgnore(static_cast<cpArbiter*>(_contactInfo));
- }
- // PhysicsContactPostSolve implementation
- PhysicsContactPostSolve::PhysicsContactPostSolve(void* contactInfo)
- : _contactInfo(contactInfo)
- {
-
- }
- PhysicsContactPostSolve::~PhysicsContactPostSolve()
- {
-
- }
- float PhysicsContactPostSolve::getRestitution() const
- {
- return cpArbiterGetRestitution(static_cast<cpArbiter*>(_contactInfo));
- }
- float PhysicsContactPostSolve::getFriction() const
- {
- return cpArbiterGetFriction(static_cast<cpArbiter*>(_contactInfo));
- }
- Vec2 PhysicsContactPostSolve::getSurfaceVelocity() const
- {
- return PhysicsHelper::cpv2point(cpArbiterGetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo)));
- }
- EventListenerPhysicsContact::EventListenerPhysicsContact()
- : onContactBegin(nullptr)
- , onContactPreSolve(nullptr)
- , onContactPostSolve(nullptr)
- , onContactSeparate(nullptr)
- {
- }
- bool EventListenerPhysicsContact::init()
- {
- auto func = [this](EventCustom* event) -> void
- {
- onEvent(event);
- };
-
- return EventListenerCustom::init(PHYSICSCONTACT_EVENT_NAME, func);
- }
- void EventListenerPhysicsContact::onEvent(EventCustom* event)
- {
- PhysicsContact* contact = dynamic_cast<PhysicsContact*>(event);
-
- if (contact == nullptr)
- {
- return;
- }
-
- switch (contact->getEventCode())
- {
- case PhysicsContact::EventCode::BEGIN:
- {
- bool ret = true;
-
- if (onContactBegin != nullptr
- && hitTest(contact->getShapeA(), contact->getShapeB()))
- {
- contact->generateContactData();
- ret = onContactBegin(*contact);
- }
-
- contact->setResult(ret);
- break;
- }
- case PhysicsContact::EventCode::PRESOLVE:
- {
- bool ret = true;
-
- if (onContactPreSolve != nullptr
- && hitTest(contact->getShapeA(), contact->getShapeB()))
- {
- PhysicsContactPreSolve solve(contact->_contactInfo);
- contact->generateContactData();
-
- ret = onContactPreSolve(*contact, solve);
- }
-
- contact->setResult(ret);
- break;
- }
- case PhysicsContact::EventCode::POSTSOLVE:
- {
- if (onContactPostSolve != nullptr
- && hitTest(contact->getShapeA(), contact->getShapeB()))
- {
- PhysicsContactPostSolve solve(contact->_contactInfo);
- onContactPostSolve(*contact, solve);
- }
- break;
- }
- case PhysicsContact::EventCode::SEPARATE:
- {
- if (onContactSeparate != nullptr
- && hitTest(contact->getShapeA(), contact->getShapeB()))
- {
- onContactSeparate(*contact);
- }
- break;
- }
- default:
- break;
- }
- }
- EventListenerPhysicsContact::~EventListenerPhysicsContact()
- {
-
- }
- EventListenerPhysicsContact* EventListenerPhysicsContact::create()
- {
- EventListenerPhysicsContact* obj = new (std::nothrow) EventListenerPhysicsContact();
-
- if (obj != nullptr && obj->init())
- {
- obj->autorelease();
- return obj;
- }
-
- CC_SAFE_DELETE(obj);
- return nullptr;
- }
- bool EventListenerPhysicsContact::hitTest(PhysicsShape* /*shapeA*/, PhysicsShape* /*shapeB*/)
- {
- return true;
- }
- bool EventListenerPhysicsContact::checkAvailable()
- {
- if (onContactBegin == nullptr && onContactPreSolve == nullptr
- && onContactPostSolve == nullptr && onContactSeparate == nullptr)
- {
- CCASSERT(false, "Invalid PhysicsContactListener.");
- return false;
- }
-
- return true;
- }
- EventListenerPhysicsContact* EventListenerPhysicsContact::clone()
- {
- EventListenerPhysicsContact* obj = EventListenerPhysicsContact::create();
-
- if (obj != nullptr)
- {
- obj->onContactBegin = onContactBegin;
- obj->onContactPreSolve = onContactPreSolve;
- obj->onContactPostSolve = onContactPostSolve;
- obj->onContactSeparate = onContactSeparate;
-
- return obj;
- }
-
- CC_SAFE_DELETE(obj);
- return nullptr;
- }
- EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::create(PhysicsBody* bodyA, PhysicsBody* bodyB)
- {
- EventListenerPhysicsContactWithBodies* obj = new (std::nothrow) EventListenerPhysicsContactWithBodies();
-
- if (obj != nullptr && obj->init())
- {
- obj->_a = bodyA;
- obj->_b = bodyB;
- obj->autorelease();
- return obj;
- }
-
- CC_SAFE_DELETE(obj);
- return nullptr;
- }
- EventListenerPhysicsContactWithBodies::EventListenerPhysicsContactWithBodies()
- : _a(nullptr)
- , _b(nullptr)
- {
-
- }
- EventListenerPhysicsContactWithBodies::~EventListenerPhysicsContactWithBodies()
- {
-
- }
- bool EventListenerPhysicsContactWithBodies::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
- {
- 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);
-
- if (obj != nullptr)
- {
- obj->onContactBegin = onContactBegin;
- obj->onContactPreSolve = onContactPreSolve;
- obj->onContactPostSolve = onContactPostSolve;
- obj->onContactSeparate = onContactSeparate;
-
- 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 (std::nothrow) EventListenerPhysicsContactWithShapes();
-
- if (obj != nullptr && obj->init())
- {
- obj->_a = shapeA;
- obj->_b = shapeB;
- obj->autorelease();
- return obj;
- }
-
- CC_SAFE_DELETE(obj);
- return nullptr;
- }
- bool EventListenerPhysicsContactWithShapes::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
- {
- if ((shapeA == _a && shapeB == _b)
- || (shapeA == _b && shapeB == _a))
- {
- return true;
- }
-
- return false;
- }
- EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::clone()
- {
- EventListenerPhysicsContactWithShapes* obj = EventListenerPhysicsContactWithShapes::create(_a, _b);
-
- if (obj != nullptr)
- {
- obj->onContactBegin = onContactBegin;
- obj->onContactPreSolve = onContactPreSolve;
- obj->onContactPostSolve = onContactPostSolve;
- obj->onContactSeparate = onContactSeparate;
-
- return obj;
- }
-
- CC_SAFE_DELETE(obj);
- return nullptr;
- }
- EventListenerPhysicsContactWithGroup::EventListenerPhysicsContactWithGroup()
- : _group(CP_NO_GROUP)
- {
- }
- EventListenerPhysicsContactWithGroup::~EventListenerPhysicsContactWithGroup()
- {
- }
- EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::create(int group)
- {
- EventListenerPhysicsContactWithGroup* obj = new (std::nothrow) EventListenerPhysicsContactWithGroup();
-
- if (obj != nullptr && obj->init())
- {
- obj->_group = group;
- obj->autorelease();
- return obj;
- }
-
- CC_SAFE_DELETE(obj);
- return nullptr;
- }
- bool EventListenerPhysicsContactWithGroup::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
- {
- if (shapeA->getGroup() == _group || shapeB->getGroup() == _group)
- {
- return true;
- }
-
- return false;
- }
- EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::clone()
- {
- EventListenerPhysicsContactWithGroup* obj = EventListenerPhysicsContactWithGroup::create(_group);
-
- if (obj != nullptr)
- {
- obj->onContactBegin = onContactBegin;
- obj->onContactPreSolve = onContactPreSolve;
- obj->onContactPostSolve = onContactPostSolve;
- obj->onContactSeparate = onContactSeparate;
-
- return obj;
- }
-
- CC_SAFE_DELETE(obj);
- return nullptr;
- }
- NS_CC_END
- #endif // CC_USE_PHYSICS
|