axmol/core/physics/PhysicsContact.cpp

435 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2021-12-25 10:04:45 +08:00
https://axmol.dev/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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.
****************************************************************************/
#include "physics/PhysicsContact.h"
#if defined(AX_ENABLE_PHYSICS)
2021-12-25 10:04:45 +08:00
# include "chipmunk/chipmunk.h"
2019-11-23 20:27:39 +08:00
# include "physics/PhysicsBody.h"
# include "physics/PhysicsHelper.h"
# include "base/EventCustom.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
const char* PHYSICSCONTACT_EVENT_NAME = "PhysicsContactEvent";
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)
{}
2019-11-23 20:27:39 +08:00
PhysicsContact::~PhysicsContact()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_contactData);
AX_SAFE_DELETE(_preContactData);
2019-11-23 20:27:39 +08:00
}
PhysicsContact* PhysicsContact::construct(PhysicsShape* a, PhysicsShape* b)
{
2021-12-25 10:04:45 +08:00
PhysicsContact* contact = new PhysicsContact();
if (contact->init(a, b))
2019-11-23 20:27:39 +08:00
{
return contact;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(contact);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool PhysicsContact::init(PhysicsShape* a, PhysicsShape* b)
{
do
{
2022-07-16 10:43:05 +08:00
AX_BREAK_IF(a == nullptr || b == nullptr);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_shapeA = a;
_shapeB = b;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
2021-12-25 10:04:45 +08:00
} while (false);
2019-11-23 20:27:39 +08:00
return false;
}
void PhysicsContact::generateContactData()
{
if (_contactInfo == nullptr)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
cpArbiter* arb = static_cast<cpArbiter*>(_contactInfo);
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_preContactData);
2021-12-25 10:04:45 +08:00
_preContactData = _contactData;
_contactData = new PhysicsContactData();
2019-11-23 20:27:39 +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)
2019-11-23 20:27:39 +08:00
{
2021-10-24 14:09:59 +08:00
_contactData->points[i] = PhysicsHelper::cpv2vec2(cpArbiterGetPointA(arb, i));
2019-11-23 20:27:39 +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;
2019-11-23 20:27:39 +08:00
}
// PhysicsContactPreSolve implementation
2021-12-25 10:04:45 +08:00
PhysicsContactPreSolve::PhysicsContactPreSolve(void* contactInfo) : _contactInfo(contactInfo) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsContactPreSolve::~PhysicsContactPreSolve() {}
2019-11-23 20:27:39 +08:00
float PhysicsContactPreSolve::getRestitution() const
{
return cpArbiterGetRestitution(static_cast<cpArbiter*>(_contactInfo));
}
float PhysicsContactPreSolve::getFriction() const
{
return cpArbiterGetFriction(static_cast<cpArbiter*>(_contactInfo));
}
Vec2 PhysicsContactPreSolve::getSurfaceVelocity() const
{
2021-10-24 14:09:59 +08:00
return PhysicsHelper::cpv2vec2(cpArbiterGetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo)));
2019-11-23 20:27:39 +08:00
}
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)
{
2021-10-24 14:09:59 +08:00
cpArbiterSetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo), PhysicsHelper::vec22cpv(velocity));
2019-11-23 20:27:39 +08:00
}
void PhysicsContactPreSolve::ignore()
{
cpArbiterIgnore(static_cast<cpArbiter*>(_contactInfo));
}
// PhysicsContactPostSolve implementation
2021-12-25 10:04:45 +08:00
PhysicsContactPostSolve::PhysicsContactPostSolve(void* contactInfo) : _contactInfo(contactInfo) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsContactPostSolve::~PhysicsContactPostSolve() {}
2019-11-23 20:27:39 +08:00
float PhysicsContactPostSolve::getRestitution() const
{
return cpArbiterGetRestitution(static_cast<cpArbiter*>(_contactInfo));
}
float PhysicsContactPostSolve::getFriction() const
{
return cpArbiterGetFriction(static_cast<cpArbiter*>(_contactInfo));
}
Vec2 PhysicsContactPostSolve::getSurfaceVelocity() const
{
2021-10-24 14:09:59 +08:00
return PhysicsHelper::cpv2vec2(cpArbiterGetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo)));
2019-11-23 20:27:39 +08:00
}
EventListenerPhysicsContact::EventListenerPhysicsContact()
2021-12-25 10:04:45 +08:00
: onContactBegin(nullptr), onContactPreSolve(nullptr), onContactPostSolve(nullptr), onContactSeparate(nullptr)
{}
2019-11-23 20:27:39 +08:00
bool EventListenerPhysicsContact::init()
{
2021-12-25 10:04:45 +08:00
auto func = [this](EventCustom* event) -> void { onEvent(event); };
2019-11-23 20:27:39 +08:00
return EventListenerCustom::init(PHYSICSCONTACT_EVENT_NAME, func);
}
void EventListenerPhysicsContact::onEvent(EventCustom* event)
{
PhysicsContact* contact = dynamic_cast<PhysicsContact*>(event);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (contact == nullptr)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
switch (contact->getEventCode())
{
2021-12-25 10:04:45 +08:00
case PhysicsContact::EventCode::BEGIN:
{
bool ret = true;
if (onContactBegin != nullptr && hitTest(contact->getShapeA(), contact->getShapeB()))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
contact->generateContactData();
ret = onContactBegin(*contact);
2019-11-23 20:27:39 +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()))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PhysicsContactPreSolve solve(contact->_contactInfo);
contact->generateContactData();
ret = onContactPreSolve(*contact, solve);
2019-11-23 20:27:39 +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()))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PhysicsContactPostSolve solve(contact->_contactInfo);
onContactPostSolve(*contact, solve);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
break;
}
case PhysicsContact::EventCode::SEPARATE:
{
if (onContactSeparate != nullptr && hitTest(contact->getShapeA(), contact->getShapeB()))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
onContactSeparate(*contact);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
break;
}
default:
break;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContact::~EventListenerPhysicsContact() {}
2019-11-23 20:27:39 +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())
2019-11-23 20:27:39 +08:00
{
obj->autorelease();
return obj;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(obj);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool EventListenerPhysicsContact::hitTest(PhysicsShape* /*shapeA*/, PhysicsShape* /*shapeB*/)
{
return true;
}
bool EventListenerPhysicsContact::checkAvailable()
{
2021-12-25 10:04:45 +08:00
if (onContactBegin == nullptr && onContactPreSolve == nullptr && onContactPostSolve == nullptr &&
onContactSeparate == nullptr)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(false, "Invalid PhysicsContactListener.");
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
EventListenerPhysicsContact* EventListenerPhysicsContact::clone()
{
EventListenerPhysicsContact* obj = EventListenerPhysicsContact::create();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (obj != nullptr)
{
2021-12-25 10:04:45 +08:00
obj->onContactBegin = onContactBegin;
obj->onContactPreSolve = onContactPreSolve;
2019-11-23 20:27:39 +08:00
obj->onContactPostSolve = onContactPostSolve;
2021-12-25 10:04:45 +08:00
obj->onContactSeparate = onContactSeparate;
2019-11-23 20:27:39 +08:00
return obj;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(obj);
2019-11-23 20:27:39 +08:00
return nullptr;
}
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::create(PhysicsBody* bodyA,
PhysicsBody* bodyB)
2019-11-23 20:27:39 +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())
2019-11-23 20:27:39 +08:00
{
obj->_a = bodyA;
obj->_b = bodyB;
obj->autorelease();
return obj;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(obj);
2019-11-23 20:27:39 +08:00
return nullptr;
}
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContactWithBodies::EventListenerPhysicsContactWithBodies() : _a(nullptr), _b(nullptr) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContactWithBodies::~EventListenerPhysicsContactWithBodies() {}
2019-11-23 20:27:39 +08:00
bool EventListenerPhysicsContactWithBodies::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
{
2021-12-25 10:04:45 +08:00
if ((shapeA->getBody() == _a && shapeB->getBody() == _b) || (shapeA->getBody() == _b && shapeB->getBody() == _a))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::clone()
{
EventListenerPhysicsContactWithBodies* obj = EventListenerPhysicsContactWithBodies::create(_a, _b);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (obj != nullptr)
{
2021-12-25 10:04:45 +08:00
obj->onContactBegin = onContactBegin;
obj->onContactPreSolve = onContactPreSolve;
2019-11-23 20:27:39 +08:00
obj->onContactPostSolve = onContactPostSolve;
2021-12-25 10:04:45 +08:00
obj->onContactSeparate = onContactSeparate;
2019-11-23 20:27:39 +08:00
return obj;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(obj);
2019-11-23 20:27:39 +08:00
return nullptr;
}
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContactWithShapes::EventListenerPhysicsContactWithShapes() : _a(nullptr), _b(nullptr) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContactWithShapes::~EventListenerPhysicsContactWithShapes() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::create(PhysicsShape* shapeA,
PhysicsShape* shapeB)
2019-11-23 20:27:39 +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())
2019-11-23 20:27:39 +08:00
{
obj->_a = shapeA;
obj->_b = shapeB;
obj->autorelease();
return obj;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(obj);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool EventListenerPhysicsContactWithShapes::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
{
2021-12-25 10:04:45 +08:00
if ((shapeA == _a && shapeB == _b) || (shapeA == _b && shapeB == _a))
2019-11-23 20:27:39 +08:00
{
return true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::clone()
{
EventListenerPhysicsContactWithShapes* obj = EventListenerPhysicsContactWithShapes::create(_a, _b);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (obj != nullptr)
{
2021-12-25 10:04:45 +08:00
obj->onContactBegin = onContactBegin;
obj->onContactPreSolve = onContactPreSolve;
2019-11-23 20:27:39 +08:00
obj->onContactPostSolve = onContactPostSolve;
2021-12-25 10:04:45 +08:00
obj->onContactSeparate = onContactSeparate;
2019-11-23 20:27:39 +08:00
return obj;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(obj);
2019-11-23 20:27:39 +08:00
return nullptr;
}
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContactWithGroup::EventListenerPhysicsContactWithGroup() : _group(CP_NO_GROUP) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
EventListenerPhysicsContactWithGroup::~EventListenerPhysicsContactWithGroup() {}
2019-11-23 20:27:39 +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())
2019-11-23 20:27:39 +08:00
{
obj->_group = group;
obj->autorelease();
return obj;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(obj);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool EventListenerPhysicsContactWithGroup::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
{
if (shapeA->getGroup() == _group || shapeB->getGroup() == _group)
{
return true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::clone()
{
EventListenerPhysicsContactWithGroup* obj = EventListenerPhysicsContactWithGroup::create(_group);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (obj != nullptr)
{
2021-12-25 10:04:45 +08:00
obj->onContactBegin = onContactBegin;
obj->onContactPreSolve = onContactPreSolve;
2019-11-23 20:27:39 +08:00
obj->onContactPostSolve = onContactPostSolve;
2021-12-25 10:04:45 +08:00
obj->onContactSeparate = onContactSeparate;
2019-11-23 20:27:39 +08:00
return obj;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(obj);
2019-11-23 20:27:39 +08:00
return nullptr;
}
NS_AX_END
#endif // defined(AX_ENABLE_PHYSICS)